litesoft
@ 822
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 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
// This Source Code is in the Public Domain per: http://litesoft.org/License.txt package org.litesoft.servlets.filetransfer; import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; import org.apache.commons.fileupload.*; import org.apache.commons.fileupload.servlet.*; import org.apache.commons.fileupload.util.*; import org.litesoft.core.typeutils.*; import org.litesoft.filesinksource.*; import org.litesoft.logger.*; import org.litesoft.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 = Strings.noEmpty( 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 = Strings.deNull( 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 = Strings.deNull( 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 |
---|---|---|---|
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 Diff | GeorgeS | Thu 17 Mar, 2011 04:16:22 +0000 | |
49 Diff | GeorgeS | Mon 12 Apr, 2010 02:59:10 +0000 | License Text |
24 Diff | GeorgeS | Wed 24 Feb, 2010 01:51:38 +0000 | |
2 | GeorgeS | Sun 07 Feb, 2010 12:50:58 +0000 |