|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| package org.jconfig.handler; |
|
8 |
| |
|
9 |
| import java.io.File; |
|
10 |
| import java.io.FileInputStream; |
|
11 |
| import java.io.IOException; |
|
12 |
| import java.io.InputStream; |
|
13 |
| import java.io.OutputStream; |
|
14 |
| import java.net.ServerSocket; |
|
15 |
| import java.net.Socket; |
|
16 |
| |
|
17 |
| import org.jconfig.utils.ConfigurationUtils; |
|
18 |
| |
|
19 |
| |
|
20 |
| |
|
21 |
| |
|
22 |
| |
|
23 |
| |
|
24 |
| |
|
25 |
| public class SimpleWebServer extends Thread { |
|
26 |
| |
|
27 |
| private boolean active = false; |
|
28 |
| private ServerSocket socket; |
|
29 |
| |
|
30 |
1
| public SimpleWebServer() {
|
|
31 |
1
| try {
|
|
32 |
1
| socket = new ServerSocket(8181);
|
|
33 |
1
| active = true;
|
|
34 |
1
| setDaemon(true);
|
|
35 |
| |
|
36 |
1
| start();
|
|
37 |
| } |
|
38 |
| catch (Exception e) { |
|
39 |
0
| e.printStackTrace();
|
|
40 |
| } |
|
41 |
| } |
|
42 |
| |
|
43 |
1
| public void run() {
|
|
44 |
2
| while ( active ) {
|
|
45 |
2
| try {
|
|
46 |
2
| Socket clientSocket = socket.accept();
|
|
47 |
1
| clientSocket.setTcpNoDelay(true);
|
|
48 |
1
| handleConnection(clientSocket);
|
|
49 |
| } |
|
50 |
| catch (Exception ie) { |
|
51 |
0
| ie.printStackTrace();
|
|
52 |
| } |
|
53 |
| } |
|
54 |
| } |
|
55 |
| |
|
56 |
1
| public void shutdown() {
|
|
57 |
1
| active = false;
|
|
58 |
| } |
|
59 |
| |
|
60 |
1
| private void handleConnection(Socket clientSocket) {
|
|
61 |
1
| try {
|
|
62 |
| |
|
63 |
1
| OutputStream os = clientSocket.getOutputStream();
|
|
64 |
1
| InputStream input = clientSocket.getInputStream();
|
|
65 |
1
| String fileName = "config.xml";
|
|
66 |
| |
|
67 |
1
| StringBuffer request = new StringBuffer(2048);
|
|
68 |
1
| int i;
|
|
69 |
1
| byte[] buffer = new byte[2048];
|
|
70 |
1
| try {
|
|
71 |
1
| i = input.read(buffer);
|
|
72 |
| } |
|
73 |
| catch (IOException e) { |
|
74 |
0
| e.printStackTrace();
|
|
75 |
0
| i = -1;
|
|
76 |
| } |
|
77 |
1
| for (int j=0; j<i; j++) {
|
|
78 |
160
| request.append((char) buffer[j]);
|
|
79 |
| } |
|
80 |
1
| fileName = parseUri(request.toString());
|
|
81 |
1
| sendResponse(os,fileName);
|
|
82 |
| |
|
83 |
1
| os.close();
|
|
84 |
1
| input.close();
|
|
85 |
1
| clientSocket.close();
|
|
86 |
| |
|
87 |
| } catch (Exception e) { |
|
88 |
0
| e.printStackTrace();
|
|
89 |
| } |
|
90 |
| } |
|
91 |
| |
|
92 |
1
| private void sendResponse(OutputStream output,String fileName) throws IOException {
|
|
93 |
1
| byte[] bytes = new byte[2048];
|
|
94 |
1
| FileInputStream fis = null;
|
|
95 |
1
| try {
|
|
96 |
1
| File file = ConfigurationUtils.getFileFromInputStream(fileName);
|
|
97 |
1
| if (file.exists()) {
|
|
98 |
1
| fis = new FileInputStream(file);
|
|
99 |
1
| int ch = fis.read(bytes, 0, 2048);
|
|
100 |
1
| while (ch!=-1) {
|
|
101 |
1
| output.write(bytes, 0, ch);
|
|
102 |
1
| ch = fis.read(bytes, 0, 2048);
|
|
103 |
| } |
|
104 |
| } |
|
105 |
| else { |
|
106 |
| |
|
107 |
0
| String errorMessage = "HTTP/1.1 404 File Not Found\r\n" +
|
|
108 |
| "Content-Type: text/html\r\n" + |
|
109 |
| "Content-Length: 23\r\n" + |
|
110 |
| "\r\n" + |
|
111 |
| "<h1>File Not Found</h1>"; |
|
112 |
0
| output.write(errorMessage.getBytes());
|
|
113 |
| } |
|
114 |
| } |
|
115 |
| catch (Exception e) { |
|
116 |
0
| e.printStackTrace();
|
|
117 |
| } |
|
118 |
| finally { |
|
119 |
1
| if (fis!=null)
|
|
120 |
1
| fis.close();
|
|
121 |
| } |
|
122 |
| } |
|
123 |
| |
|
124 |
1
| private String parseUri(String requestString) {
|
|
125 |
1
| int index1, index2;
|
|
126 |
1
| index1 = requestString.indexOf(' ');
|
|
127 |
1
| if (index1 != -1) {
|
|
128 |
1
| index2 = requestString.indexOf(' ', index1 + 1);
|
|
129 |
1
| if (index2 > index1)
|
|
130 |
1
| return requestString.substring(index1 + 2, index2);
|
|
131 |
| } |
|
132 |
0
| return null;
|
|
133 |
| } |
|
134 |
| } |