litesoft
@ 948
litesoft / trunk / Java / Bookmarklet / src / org / litesoft / servlet / bookmarklet / BookmarkletServlet.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 |
// This Source Code is in the Public Domain per: http://unlicense.org package org.litesoft.servlet.bookmarklet; import org.litesoft.commonfoundation.typeutils.*; import org.litesoft.configuration.*; import javax.servlet.*; import javax.servlet.http.*; import java.io.*; public class BookmarkletServlet 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 } } } } |
Commits for litesoft/trunk/Java/Bookmarklet/src/org/litesoft/servlet/bookmarklet/BookmarkletServlet.java
Revision | Author | Commited | Message |
---|---|---|---|
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 |
806 Diff | GeorgeS | Thu 16 Aug, 2012 03:48:22 +0000 | |
252 | GeorgeS | Thu 09 Jun, 2011 00:08:18 +0000 |