|
1 |
| package org.jconfig.server; |
|
2 |
| |
|
3 |
| import java.io.File; |
|
4 |
| import java.io.FileInputStream; |
|
5 |
| import java.io.IOException; |
|
6 |
| import java.io.OutputStream; |
|
7 |
| |
|
8 |
| import org.jconfig.utils.ConfigurationUtils; |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| public class Response { |
|
19 |
| |
|
20 |
| private static final int BUFFER_SIZE = 1024; |
|
21 |
| Request request; |
|
22 |
| OutputStream output; |
|
23 |
| |
|
24 |
0
| public Response(OutputStream output) {
|
|
25 |
0
| this.output = output;
|
|
26 |
| } |
|
27 |
| |
|
28 |
0
| public void setRequest(Request request) {
|
|
29 |
0
| this.request = request;
|
|
30 |
| } |
|
31 |
| |
|
32 |
0
| public void sendStaticResource(String fileName) throws IOException {
|
|
33 |
0
| byte[] bytes = new byte[BUFFER_SIZE];
|
|
34 |
0
| FileInputStream fis = null;
|
|
35 |
0
| try {
|
|
36 |
0
| File file = new File(fileName);
|
|
37 |
0
| if (file.exists()) {
|
|
38 |
0
| fis = new FileInputStream(file);
|
|
39 |
0
| int ch = fis.read(bytes, 0, BUFFER_SIZE);
|
|
40 |
0
| while (ch!=-1) {
|
|
41 |
0
| output.write(bytes, 0, ch);
|
|
42 |
0
| ch = fis.read(bytes, 0, BUFFER_SIZE);
|
|
43 |
| } |
|
44 |
| } |
|
45 |
| else { |
|
46 |
0
| System.out.println("Cannot find file:"+fileName);
|
|
47 |
| |
|
48 |
0
| String errorMessage = "HTTP/1.1 404 File Not Found\r\n" +
|
|
49 |
| "Content-Type: text/html\r\n" + |
|
50 |
| "Content-Length: 23\r\n" + |
|
51 |
| "\r\n" + |
|
52 |
| "<h1>File Not Found</h1>"; |
|
53 |
0
| output.write(errorMessage.getBytes());
|
|
54 |
| } |
|
55 |
| } |
|
56 |
| catch (Exception e) { |
|
57 |
| |
|
58 |
0
| System.out.println(e.toString() );
|
|
59 |
| } |
|
60 |
| finally { |
|
61 |
0
| if (fis!=null)
|
|
62 |
0
| fis.close();
|
|
63 |
| } |
|
64 |
| } |
|
65 |
| |
|
66 |
0
| public void sendStaticResource() throws IOException {
|
|
67 |
0
| byte[] bytes = new byte[BUFFER_SIZE];
|
|
68 |
0
| FileInputStream fis = null;
|
|
69 |
0
| String uri = request.getUri();
|
|
70 |
0
| if ( uri.startsWith("/") ) {
|
|
71 |
0
| uri = uri.substring(1);
|
|
72 |
| } |
|
73 |
0
| uri += ".xml";
|
|
74 |
0
| try {
|
|
75 |
| |
|
76 |
0
| File file = ConfigurationUtils.getFileFromInputStream(uri);
|
|
77 |
0
| if (file.exists()) {
|
|
78 |
0
| fis = new FileInputStream(file);
|
|
79 |
0
| int ch = fis.read(bytes, 0, BUFFER_SIZE);
|
|
80 |
0
| while (ch!=-1) {
|
|
81 |
0
| output.write(bytes, 0, ch);
|
|
82 |
0
| ch = fis.read(bytes, 0, BUFFER_SIZE);
|
|
83 |
| } |
|
84 |
| } |
|
85 |
| else { |
|
86 |
| |
|
87 |
0
| String errorMessage = "HTTP/1.1 404 File Not Found\r\n" +
|
|
88 |
| "Content-Type: text/html\r\n" + |
|
89 |
| "Content-Length: 23\r\n" + |
|
90 |
| "\r\n" + |
|
91 |
| "<h1>File Not Found</h1>"; |
|
92 |
0
| output.write(errorMessage.getBytes());
|
|
93 |
| } |
|
94 |
| } |
|
95 |
| catch (Exception e) { |
|
96 |
| |
|
97 |
0
| System.out.println(e.toString() );
|
|
98 |
| } |
|
99 |
| finally { |
|
100 |
0
| if (fis!=null)
|
|
101 |
0
| fis.close();
|
|
102 |
| } |
|
103 |
| } |
|
104 |
| } |