|
@@ -7,61 +7,49 @@ |
7 |
7 |
|
import javax.servlet.http.*; |
8 |
8 |
|
import java.io.*; |
9 |
9 |
|
|
10 |
|
- |
public class DeviceDesktopTestServlet extends HttpServlet |
11 |
|
- |
{ |
|
10 |
+ |
public class DeviceDesktopTestServlet extends HttpServlet { |
12 |
11 |
|
private final IndexHtmlGenerator mIndexHtmlGenerator; |
13 |
12 |
|
|
14 |
13 |
|
private ServletContext mServletContext; |
15 |
14 |
|
|
16 |
|
- |
public DeviceDesktopTestServlet( IndexHtmlGenerator pIndexHtmlGenerator ) |
17 |
|
- |
{ |
|
15 |
+ |
public DeviceDesktopTestServlet( IndexHtmlGenerator pIndexHtmlGenerator ) { |
18 |
16 |
|
mIndexHtmlGenerator = pIndexHtmlGenerator; |
19 |
17 |
|
} |
20 |
18 |
|
|
21 |
19 |
|
@Override |
22 |
20 |
|
public void init( ServletConfig config ) |
23 |
|
- |
throws ServletException |
24 |
|
- |
{ |
|
21 |
+ |
throws ServletException { |
25 |
22 |
|
super.init( config ); |
26 |
23 |
|
mServletContext = config.getServletContext(); |
27 |
24 |
|
} |
28 |
25 |
|
|
29 |
26 |
|
@Override |
30 |
27 |
|
protected void doGet( HttpServletRequest pRequest, HttpServletResponse pResponse ) |
31 |
|
- |
throws ServletException, IOException |
32 |
|
- |
{ |
|
28 |
+ |
throws ServletException, IOException { |
33 |
29 |
|
String zPath = pRequest.getPathInfo(); |
34 |
|
- |
if ( zPath == null ) |
35 |
|
- |
{ |
|
30 |
+ |
if ( zPath == null ) { |
36 |
31 |
|
zPath = "/"; |
37 |
32 |
|
} |
38 |
|
- |
if ( zPath.endsWith( "/" ) ) |
39 |
|
- |
{ |
|
33 |
+ |
if ( zPath.endsWith( "/" ) ) { |
40 |
34 |
|
zPath += "index.html"; |
41 |
35 |
|
} |
42 |
|
- |
if ( zPath.endsWith( "/index.html" ) ) |
43 |
|
- |
{ |
|
36 |
+ |
if ( zPath.endsWith( "/index.html" ) ) { |
44 |
37 |
|
String zAction = pRequest.getParameter( "action" ); |
45 |
|
- |
if ( "Restart".equals( zAction ) ) |
46 |
|
- |
{ |
|
38 |
+ |
if ( "Restart".equals( zAction ) ) { |
47 |
39 |
|
Shutdown.Instance.get().request( 0 ); |
48 |
|
- |
} |
49 |
|
- |
else if ( "ShutDown".equals( zAction ) ) |
50 |
|
- |
{ |
|
40 |
+ |
} else if ( "ShutDown".equals( zAction ) ) { |
51 |
41 |
|
Shutdown.Instance.get().request( 1 ); |
52 |
42 |
|
} |
53 |
43 |
|
mIndexHtmlGenerator.respond( zPath, pRequest, pResponse ); |
54 |
44 |
|
return; |
55 |
45 |
|
} |
56 |
46 |
|
File zFile = new File( "." + zPath ); |
57 |
|
- |
if ( !zFile.isFile() ) |
58 |
|
- |
{ |
|
47 |
+ |
if ( !zFile.isFile() ) { |
59 |
48 |
|
System.out.println( "Can't find: " + zFile.getAbsolutePath() ); |
60 |
49 |
|
pResponse.sendError( HttpServletResponse.SC_NOT_FOUND ); |
61 |
50 |
|
return; |
62 |
51 |
|
} |
63 |
|
- |
if ( !zFile.canRead() ) |
64 |
|
- |
{ |
|
52 |
+ |
if ( !zFile.canRead() ) { |
65 |
53 |
|
System.out.println( "Can't read: " + zFile.getAbsolutePath() ); |
66 |
54 |
|
pResponse.sendError( HttpServletResponse.SC_FORBIDDEN ); |
67 |
55 |
|
return; |
|
@@ -71,8 +59,7 @@ |
71 |
59 |
|
|
72 |
60 |
|
// Get the MIME type |
73 |
61 |
|
String mimeType = mServletContext.getMimeType( zFile.getName().toLowerCase() ); |
74 |
|
- |
if ( null == mimeType ) |
75 |
|
- |
{ |
|
62 |
+ |
if ( null == mimeType ) { |
76 |
63 |
|
System.err.println( "Could not get MIME type of " + zFile.getName() ); |
77 |
64 |
|
mimeType = "application/octet-stream"; |
78 |
65 |
|
} |
|
@@ -84,50 +71,40 @@ |
84 |
71 |
|
|
85 |
72 |
|
// Open the file and output streams |
86 |
73 |
|
InputStream in = new FileInputStream( zFile ); |
87 |
|
- |
try |
88 |
|
- |
{ |
|
74 |
+ |
try { |
89 |
75 |
|
OutputStream out = pResponse.getOutputStream(); |
90 |
|
- |
try |
91 |
|
- |
{ |
|
76 |
+ |
try { |
92 |
77 |
|
copyStream( in, out ); |
93 |
78 |
|
|
94 |
79 |
|
Closeable zCloseable = out; |
95 |
80 |
|
out = null; |
96 |
81 |
|
zCloseable.close(); |
97 |
82 |
|
} |
98 |
|
- |
finally |
99 |
|
- |
{ |
|
83 |
+ |
finally { |
100 |
84 |
|
closeQuietly( out ); |
101 |
85 |
|
} |
102 |
86 |
|
} |
103 |
|
- |
finally |
104 |
|
- |
{ |
|
87 |
+ |
finally { |
105 |
88 |
|
closeQuietly( in ); |
106 |
89 |
|
} |
107 |
90 |
|
} |
108 |
91 |
|
|
109 |
|
- |
private static void closeQuietly( Closeable pClosable ) |
110 |
|
- |
{ |
111 |
|
- |
if ( pClosable != null ) |
112 |
|
- |
{ |
113 |
|
- |
try |
114 |
|
- |
{ |
|
92 |
+ |
private static void closeQuietly( Closeable pClosable ) { |
|
93 |
+ |
if ( pClosable != null ) { |
|
94 |
+ |
try { |
115 |
95 |
|
pClosable.close(); |
116 |
96 |
|
} |
117 |
|
- |
catch ( IOException e ) |
118 |
|
- |
{ |
|
97 |
+ |
catch ( IOException e ) { |
119 |
98 |
|
// Whatever |
120 |
99 |
|
} |
121 |
100 |
|
} |
122 |
101 |
|
} |
123 |
102 |
|
|
124 |
103 |
|
private static void copyStream( InputStream pIn, OutputStream pOut ) |
125 |
|
- |
throws IOException |
126 |
|
- |
{ |
|
104 |
+ |
throws IOException { |
127 |
105 |
|
// Copy the contents of the file to the output stream |
128 |
106 |
|
byte[] buf = new byte[1024]; |
129 |
|
- |
for ( int count; (count = pIn.read( buf )) > 0; ) |
130 |
|
- |
{ |
|
107 |
+ |
for ( int count; (count = pIn.read( buf )) > 0; ) { |
131 |
108 |
|
pOut.write( buf, 0, count ); |
132 |
109 |
|
} |
133 |
110 |
|
} |