litesoft
@ 958
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
// This Source Code is in the Public Domain per: http://unlicense.org package org.litesoft.servlets.filetransfer; import org.apache.commons.fileupload.*; import org.apache.commons.fileupload.servlet.*; import org.apache.commons.fileupload.util.*; import org.litesoft.commonfoundation.base.*; import org.litesoft.commonfoundation.typeutils.*; import org.litesoft.filesinksource.*; import org.litesoft.logger.*; import org.litesoft.server.file.*; import org.litesoft.util.*; import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.util.*; public abstract class AbstractFileTransferServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected static Logger LOGGER = LoggerFactory.getLogger( AbstractFileTransferServlet.class ); /** * Return True for all the Content Types that we want the Browser to simply show! * <p/> * Otherwise, the Browser will be told to "ask" what to do with it! */ protected boolean isDirectViewContent( String pContentType ) { return pContentType.startsWith( "image/" ) || pContentType.startsWith( "text/" ); } /** * Return False for any Content Types that we are Not aware of what we want the browser to do with it! * <p/> * Will only be called if isDirectViewContent( pContentType ) return false */ protected boolean isKnownContentType( String pContentType ) { return pContentType.startsWith( "application/" ); } @Override public void init() throws ServletException { FileSinkAndSourceFactory.setInstance( initialize() ); } abstract protected FileSinkAndSourceFactory initialize() throws ServletException; /** * Download uses GET. */ @Override public void doGet( HttpServletRequest pReq, final HttpServletResponse pResp ) throws ServletException, IOException { String zPathInfo = pReq.getPathInfo(); String zOpaqueHandle = ConstrainTo.significantOrNull( cleanFileName( zPathInfo ) ); if ( zOpaqueHandle == null ) { pResp.sendError( HttpServletResponse.SC_NOT_FOUND, "Null or zero length OpaqueHandle" ); return; } FileSource zFileSource = FileSinkAndSourceFactory.getInstance().createFileSource( zOpaqueHandle ); FileSourceInfo zFileInfo = zFileSource.get(); String zFileName = zFileInfo.getName(); String zContentType = ConstrainTo.notNull( getServletContext().getMimeType( zFileName.toLowerCase() ) ); pResp.setContentType( zContentType ); long zSize = zFileInfo.getSize(); if ( zSize < Integer.MAX_VALUE ) { pResp.setContentLength( (int) zSize ); } if ( !isDirectViewContent( zContentType ) ) { pResp.setHeader( "Content-Disposition", "attachment;filename=" + zFileName ); // Only if want OS to ask disposition if ( !isKnownContentType( zContentType ) ) { LOGGER.info.log( "zName=", zFileName, " zContentType=", zContentType ); } } // Caching Control pResp.addHeader( "cache-control", "max-age=10" ); // We'd like to add these, but IE7 has a specific bug when downloading Microsoft Office files. See: // http://support.microsoft.com/kb/317208/ // pResp.addHeader( "cache-control", "no-store" ); // pResp.addHeader( "Pragma", "no-cache" ); Utils.copy( zFileInfo.getInputStream(), new OutputStreamFactory() // closes InputStream { @Override public OutputStream createOutputStream() throws IOException { return pResp.getOutputStream(); } } ); } /** * Upload uses POST. */ @Override public void doPost( HttpServletRequest pReq, HttpServletResponse pResp ) throws ServletException, IOException { PrintWriter zPrintWriter = pResp.getWriter(); pResp.setContentType( "application/xml" ); try { insureCharacterEncoding( pReq ); // Check that we have a file upload request if ( ServletFileUpload.isMultipartContent( pReq ) ) { ServletFileUpload upload = new ServletFileUpload(); Map<String, String> zResults = new HashMap<String, String>(); // Parse the request FileItemIterator iter = upload.getItemIterator( pReq ); while ( iter.hasNext() ) { FileItemStream item = iter.next(); String name = item.getFieldName(); if ( !item.isFormField() ) { String zFileName = item.getName(); LOGGER.debug.log( "Upload file name before clean: ", zFileName ); zFileName = justFileName( zFileName ); FileSink zSink = FileSinkAndSourceFactory.getInstance().createFileSink( zFileName ); String zOpaqueHandle = zSink.put( item.openStream() ); // closes InputStream zResults.put( zFileName, zOpaqueHandle ); } else { String zStreamAsString = Streams.asString( item.openStream() ); // closes InputStream LOGGER.warn.log( "Form field ", name, " with value ", zStreamAsString, " detected." ); } } List<String> zKeys = new ArrayList<String>( zResults.keySet() ); Collections.sort( zKeys ); zPrintWriter.println( "<success>" ); for ( String zKey : zKeys ) { String zValue = zResults.get( zKey ); zPrintWriter.println( " <file name=\"" + zKey + "\" handle=\"" + zValue + "\" />" ); } zPrintWriter.println( "</success>" ); zPrintWriter.flush(); } } catch ( Exception e ) { LOGGER.warn.log( e ); zPrintWriter.println( "<fail>" ); String zFailOut = Throwables.printStackTraceToString( e ); zFailOut = Strings.replace( zFailOut, "<", "<" ); zFailOut = Strings.replace( zFailOut, ">", ">" ); zPrintWriter.println( zFailOut ); zPrintWriter.println( "</fail>" ); zPrintWriter.flush(); } } private String justFileName( String pFileName ) { pFileName = "/" + cleanFileName( pFileName ); return pFileName.substring( pFileName.lastIndexOf( '/' ) + 1 ); } private String cleanFileName( String pFileName ) { pFileName = ConstrainTo.notNull( pFileName ).trim().replace( '\\', '/' ); if ( (pFileName.length() >= 2) && Character.isLetter( pFileName.charAt( 0 ) ) && (':' == pFileName.charAt( 1 )) ) { pFileName = pFileName.substring( 2 ).trim(); } while ( pFileName.startsWith( "/" ) ) { pFileName = pFileName.substring( 1 ).trim(); } return pFileName; } private void insureCharacterEncoding( HttpServletRequest pReq ) throws UnsupportedEncodingException { String zEnc = pReq.getCharacterEncoding(); if ( zEnc != null ) { LOGGER.debug.log( zEnc, " = pReq.getCharacterEncoding();" ); } else { LOGGER.debug.log( "pReq.setCharacterEncoding( \"", FileUtils.UTF_8, "\" );" ); pReq.setCharacterEncoding( FileUtils.UTF_8 ); } } /** * Obtain information on this servlet. * * @return String describing this servlet. */ @Override public String getServletInfo() { return "File transfer servlet -- used to receive and send files"; } } |
Commits for litesoft/trunk/Java/core/Server/src/org/litesoft/servlets/filetransfer/AbstractFileTransferServlet.java
Revision | Author | Commited | Message |
---|---|---|---|
958 Diff | GeorgeS | Mon 14 Jul, 2014 15:29:35 +0000 | Embrace OSS code bases. |
950 Diff | GeorgeS | Thu 19 Jun, 2014 17:57:04 +0000 | New Lines |
948 Diff | GeorgeS | Sat 07 Jun, 2014 23:42:39 +0000 | Jusefuls Formatter Updated to New Code Format |
947 Diff | GeorgeS | Fri 06 Jun, 2014 23:36:56 +0000 | Correct Spelling of package! |
939 Diff | GeorgeS | Mon 02 Jun, 2014 21:30:31 +0000 | Extracting commonfoundation |
822 Diff | GeorgeS | Sun 19 Aug, 2012 01:03:51 +0000 | |
803 Diff | GeorgeS | Wed 15 Aug, 2012 04:08:34 +0000 | |
802 Diff | GeorgeS | Wed 15 Aug, 2012 04:04:47 +0000 | |
801 Diff | GeorgeS | Wed 15 Aug, 2012 03:59:02 +0000 | |
151 | GeorgeS | Thu 17 Mar, 2011 04:16:22 +0000 |