Subversion Repository Public Repository

litesoft

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

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