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
package org.litesoft.ddt;

import org.litesoft.html.*;
import org.litesoft.jetty.Shutdown;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class DeviceDesktopTestServlet extends HttpServlet {
    private final IndexHtmlGenerator mIndexHtmlGenerator;

    private ServletContext mServletContext;

    public DeviceDesktopTestServlet( IndexHtmlGenerator pIndexHtmlGenerator ) {
        mIndexHtmlGenerator = pIndexHtmlGenerator;
    }

    @Override
    public void init( ServletConfig config )
            throws ServletException {
        super.init( config );
        mServletContext = config.getServletContext();
    }

    @Override
    protected void doGet( HttpServletRequest pRequest, HttpServletResponse pResponse )
            throws ServletException, IOException {
        String zPath = pRequest.getPathInfo();
        if ( zPath == null ) {
            zPath = "/";
        }
        if ( zPath.endsWith( "/" ) ) {
            zPath += "index.html";
        }
        if ( zPath.endsWith( "/index.html" ) ) {
            String zAction = pRequest.getParameter( "action" );
            if ( "Restart".equals( zAction ) ) {
                Shutdown.Instance.get().request( 0 );
            } else if ( "ShutDown".equals( zAction ) ) {
                Shutdown.Instance.get().request( 1 );
            }
            mIndexHtmlGenerator.respond( zPath, pRequest, pResponse );
            return;
        }
        File zFile = new File( "." + zPath );
        if ( !zFile.isFile() ) {
            System.out.println( "Can't find: " + zFile.getAbsolutePath() );
            pResponse.sendError( HttpServletResponse.SC_NOT_FOUND );
            return;
        }
        if ( !zFile.canRead() ) {
            System.out.println( "Can't read: " + zFile.getAbsolutePath() );
            pResponse.sendError( HttpServletResponse.SC_FORBIDDEN );
            return;
        }

        CacheHeaders.forever( pResponse );

        // Get the MIME type
        String mimeType = mServletContext.getMimeType( zFile.getName().toLowerCase() );
        if ( null == mimeType ) {
            System.err.println( "Could not get MIME type of " + zFile.getName() );
            mimeType = "application/octet-stream";
        }
        // Set content type
        pResponse.setContentType( mimeType );

        // Set content size
        pResponse.setContentLength( (int) zFile.length() );

        // Open the file and output streams
        InputStream in = new FileInputStream( zFile );
        try {
            OutputStream out = pResponse.getOutputStream();
            try {
                copyStream( in, out );

                Closeable zCloseable = out;
                out = null;
                zCloseable.close();
            }
            finally {
                closeQuietly( out );
            }
        }
        finally {
            closeQuietly( in );
        }
    }

    private static void closeQuietly( Closeable pClosable ) {
        if ( pClosable != null ) {
            try {
                pClosable.close();
            }
            catch ( IOException e ) {
                // Whatever
            }
        }
    }

    private static void copyStream( InputStream pIn, OutputStream pOut )
            throws IOException {
        // Copy the contents of the file to the output stream
        byte[] buf = new byte[1024];
        for ( int count; (count = pIn.read( buf )) > 0; ) {
            pOut.write( buf, 0, count );
        }
    }
}

Commits for litesoft/trunk/DeviceDesktopTest/src/org/litesoft/ddt/DeviceDesktopTestServlet.java

Diff revisions: vs.
Revision Author Commited Message
961 Diff Diff GeorgeS picture GeorgeS Fri 01 Aug, 2014 03:13:31 +0000

Externalization Work.

935 Diff Diff GeorgeS picture GeorgeS Fri 30 May, 2014 20:28:08 +0000

Reformatted.

927 Diff Diff GeorgeS picture GeorgeS Mon 17 Mar, 2014 03:58:40 +0000

Template system and index.html as a resource...

926 Diff Diff GeorgeS picture GeorgeS Fri 14 Mar, 2014 23:58:15 +0000

Switched from Cookie to Param.

924 Diff Diff GeorgeS picture GeorgeS Fri 14 Mar, 2014 22:02:56 +0000

Drop IWS

923 Diff Diff GeorgeS picture GeorgeS Fri 21 Feb, 2014 00:03:57 +0000

Multi-Port Jetty

922 Diff Diff GeorgeS picture GeorgeS Tue 18 Feb, 2014 16:18:01 +0000

Mini App ready for packaging...

920 GeorgeS picture GeorgeS Sun 16 Feb, 2014 19:06:51 +0000

DDT w/ Jetty