Subversion Repository Public Repository

litesoft

Diff Revisions 947 vs 948 for /trunk/Java/Bookmarklet/src/org/litesoft/servlet/bookmarklet/BookmarkletServlet.java

Diff revisions: vs.
  @@ -1,15 +1,14 @@
1 1 // This Source Code is in the Public Domain per: http://unlicense.org
2 2 package org.litesoft.servlet.bookmarklet;
3 3
4 - import java.io.*;
5 - import javax.servlet.*;
6 - import javax.servlet.http.*;
7 -
8 4 import org.litesoft.commonfoundation.typeutils.*;
9 5 import org.litesoft.configuration.*;
10 6
11 - public class BookmarkletServlet extends HttpServlet
12 - {
7 + import javax.servlet.*;
8 + import javax.servlet.http.*;
9 + import java.io.*;
10 +
11 + public class BookmarkletServlet extends HttpServlet {
13 12 private static final long serialVersionUID = 1L;
14 13
15 14 private static final String IMAGES_BASE_PATH = "ImagesBasePath";
  @@ -18,61 +17,48 @@
18 17
19 18 @Override
20 19 public void init()
21 - throws ServletException
22 - {
20 + throws ServletException {
23 21 super.init();
24 - if ( !Configuration.isInstantiated() )
25 - {
22 + if ( !Configuration.isInstantiated() ) {
26 23 new ServerConfiguration( new MapConfigDataAccessorFactory( "No Config File - Testing?", IMAGES_BASE_PATH, "/TestImages" ) );
27 24 }
28 25 String zImagesBasePath = Configuration.getStringRequired( IMAGES_BASE_PATH );
29 26 File zDir = new File( zImagesBasePath );
30 - if ( !zDir.exists() )
31 - {
32 - if ( !zDir.mkdir() )
33 - {
27 + if ( !zDir.exists() ) {
28 + if ( !zDir.mkdir() ) {
34 29 throw new ServletException( "Unable to create Image Base Path (" + zImagesBasePath + ") Directory!" );
35 30 }
36 31 }
37 - if ( !zDir.isDirectory() )
38 - {
32 + if ( !zDir.isDirectory() ) {
39 33 throw new ServletException( "Image Base Path (" + zImagesBasePath + "), not a Directory!" );
40 34 }
41 - if ( !zDir.canWrite() )
42 - {
35 + if ( !zDir.canWrite() ) {
43 36 throw new ServletException( "Image Base Path (" + zImagesBasePath + ") Directory NOT writable!" );
44 37 }
45 38 mImagesBasePath = zDir;
46 39 }
47 40
48 41 private String normalize( String pPathInfo )
49 - throws FileNotFoundException
50 - {
51 - if ( pPathInfo != null )
52 - {
42 + throws FileNotFoundException {
43 + if ( pPathInfo != null ) {
53 44 // switch any separators and insure that we start w/ a '/'
54 45 pPathInfo = "/" + Strings.noSpaces( pPathInfo.replace( '\\', '/' ) );
55 46 // change all "//"s to '/'
56 - for ( int at; -1 != (at = pPathInfo.indexOf( "//" )); )
57 - {
47 + for ( int at; -1 != (at = pPathInfo.indexOf( "//" )); ) {
58 48 pPathInfo = pPathInfo.substring( 0, at + 1 ) + pPathInfo.substring( at + 2 );
59 49 }
60 50 // change all "/./"s to '/'
61 - for ( int at; -1 != (at = pPathInfo.indexOf( "/./" )); )
62 - {
51 + for ( int at; -1 != (at = pPathInfo.indexOf( "/./" )); ) {
63 52 pPathInfo = pPathInfo.substring( 0, at + 1 ) + pPathInfo.substring( at + 3 );
64 53 }
65 54 if ( !pPathInfo.contains( "/../" ) ) // No Up Dirs!
66 55 {
67 - while ( pPathInfo.startsWith( "/" ) )
68 - {
56 + while ( pPathInfo.startsWith( "/" ) ) {
69 57 pPathInfo = pPathInfo.substring( 1 );
70 58 }
71 59 File zFile = new File( mImagesBasePath, pPathInfo );
72 - if ( zFile.isFile() )
73 - {
74 - if ( zFile.canRead() )
75 - {
60 + if ( zFile.isFile() ) {
61 + if ( zFile.canRead() ) {
76 62 return zFile.getAbsolutePath();
77 63 }
78 64 throw new FileNotFoundException( "Can't Read: " + pPathInfo + " -> " + zFile.getAbsolutePath() );
  @@ -84,8 +70,7 @@
84 70
85 71 // This method is called by the servlet container to process a GET request.
86 72 public void doGet( HttpServletRequest pReq, HttpServletResponse pResp )
87 - throws IOException
88 - {
73 + throws IOException {
89 74 // http://localhost:8080/Images/Fred.jpg -> /Fred.jpg
90 75
91 76 String filename = normalize( pReq.getPathInfo() );
  @@ -94,8 +79,7 @@
94 79
95 80 // Get the MIME type of the image
96 81 String mimeType = sc.getMimeType( filename.toLowerCase() );
97 - if ( mimeType == null )
98 - {
82 + if ( mimeType == null ) {
99 83 sc.log( "Could not get MIME type of " + filename );
100 84 pResp.setStatus( HttpServletResponse.SC_INTERNAL_SERVER_ERROR );
101 85 return;
  @@ -110,45 +94,35 @@
110 94
111 95 // Open the file and output streams
112 96 InputStream in = new FileInputStream( file );
113 - try
114 - {
97 + try {
115 98 OutputStream out = pResp.getOutputStream();
116 99
117 - try
118 - {
100 + try {
119 101 // Copy the contents of the file to the output stream
120 102 byte[] buf = new byte[1024];
121 - for ( int count; (count = in.read( buf )) > 0; )
122 - {
103 + for ( int count; (count = in.read( buf )) > 0; ) {
123 104 out.write( buf, 0, count );
124 105 }
125 106 Closeable zCloseable = out;
126 107 out = null;
127 108 zCloseable.close();
128 109 }
129 - finally
130 - {
131 - if ( out != null )
132 - {
133 - try
134 - {
110 + finally {
111 + if ( out != null ) {
112 + try {
135 113 out.close();
136 114 }
137 - catch ( IOException e )
138 - {
115 + catch ( IOException e ) {
139 116 // Whatever
140 117 }
141 118 }
142 119 }
143 120 }
144 - finally
145 - {
146 - try
147 - {
121 + finally {
122 + try {
148 123 in.close();
149 124 }
150 - catch ( IOException e )
151 - {
125 + catch ( IOException e ) {
152 126 // Whatever
153 127 }
154 128 }