litesoft
@ 939
litesoft / trunk / Java / ImageServlet / src / org / litesoft / servlet / imageservlet / ImageServlet.java
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 |
// This Source Code is in the Public Domain per: http://litesoft.org/License.txt package org.litesoft.servlet.imageservlet; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import org.litesoft.commonfoundation.typeutils.*; import org.litesoft.configuration.*; public class ImageServlet extends HttpServlet { private static final long serialVersionUID = 1L; private static final String IMAGES_BASE_PATH = "ImagesBasePath"; private File mImagesBasePath = null; @Override public void init() throws ServletException { super.init(); if ( !Configuration.isInstantiated() ) { new ServerConfiguration( new MapConfigDataAccessorFactory( "No Config File - Testing?", IMAGES_BASE_PATH, "/TestImages" ) ); } String zImagesBasePath = Configuration.getStringRequired( IMAGES_BASE_PATH ); File zDir = new File( zImagesBasePath ); if ( !zDir.exists() ) { if ( !zDir.mkdir() ) { throw new ServletException( "Unable to create Image Base Path (" + zImagesBasePath + ") Directory!" ); } } if ( !zDir.isDirectory() ) { throw new ServletException( "Image Base Path (" + zImagesBasePath + "), not a Directory!" ); } if ( !zDir.canWrite() ) { throw new ServletException( "Image Base Path (" + zImagesBasePath + ") Directory NOT writable!" ); } mImagesBasePath = zDir; } private String normalize( String pPathInfo ) throws FileNotFoundException { if ( pPathInfo != null ) { // switch any separators and insure that we start w/ a '/' pPathInfo = "/" + Strings.noSpaces( pPathInfo.replace( '\\', '/' ) ); // change all "//"s to '/' for ( int at; -1 != (at = pPathInfo.indexOf( "//" )); ) { pPathInfo = pPathInfo.substring( 0, at + 1 ) + pPathInfo.substring( at + 2 ); } // change all "/./"s to '/' for ( int at; -1 != (at = pPathInfo.indexOf( "/./" )); ) { pPathInfo = pPathInfo.substring( 0, at + 1 ) + pPathInfo.substring( at + 3 ); } if ( !pPathInfo.contains( "/../" ) ) // No Up Dirs! { while ( pPathInfo.startsWith( "/" ) ) { pPathInfo = pPathInfo.substring( 1 ); } File zFile = new File( mImagesBasePath, pPathInfo ); if ( zFile.isFile() ) { if ( zFile.canRead() ) { return zFile.getAbsolutePath(); } throw new FileNotFoundException( "Can't Read: " + pPathInfo + " -> " + zFile.getAbsolutePath() ); } } } throw new FileNotFoundException( "No Resource of: " + pPathInfo ); } // This method is called by the servlet container to process a GET request. public void doGet( HttpServletRequest pReq, HttpServletResponse pResp ) throws IOException { // http://localhost:8080/Images/Fred.jpg -> /Fred.jpg String filename = normalize( pReq.getPathInfo() ); ServletContext sc = getServletContext(); // Get the MIME type of the image String mimeType = sc.getMimeType( filename.toLowerCase() ); if ( mimeType == null ) { sc.log( "Could not get MIME type of " + filename ); pResp.setStatus( HttpServletResponse.SC_INTERNAL_SERVER_ERROR ); return; } // Set content type pResp.setContentType( mimeType ); // Set content size File file = new File( filename ); pResp.setContentLength( (int) file.length() ); // Open the file and output streams InputStream in = new FileInputStream( file ); try { OutputStream out = pResp.getOutputStream(); try { // Copy the contents of the file to the output stream byte[] buf = new byte[1024]; for ( int count; (count = in.read( buf )) > 0; ) { out.write( buf, 0, count ); } Closeable zCloseable = out; out = null; zCloseable.close(); } finally { if ( out != null ) { try { out.close(); } catch ( IOException e ) { // Whatever } } } } finally { try { in.close(); } catch ( IOException e ) { // Whatever } } } } |