Subversion Repository Public Repository

litesoft

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://unlicense.org
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
            }
        }
    }
}

Commits for litesoft/trunk/Java/ImageServlet/src/org/litesoft/servlet/imageservlet/ImageServlet.java

Diff revisions: vs.
Revision Author Commited Message
947 Diff Diff GeorgeS picture GeorgeS Fri 06 Jun, 2014 23:36:56 +0000

Correct Spelling of package!

939 Diff Diff GeorgeS picture GeorgeS Mon 02 Jun, 2014 21:30:31 +0000

Extracting commonfoundation

806 Diff Diff GeorgeS picture GeorgeS Thu 16 Aug, 2012 03:48:22 +0000
77 GeorgeS picture GeorgeS Wed 27 Oct, 2010 22:50:45 +0000

Image Servlet