|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| package org.jconfig.server; |
|
8 |
| |
|
9 |
| import java.io.File; |
|
10 |
| import java.io.InputStream; |
|
11 |
| import java.io.OutputStream; |
|
12 |
| import java.net.Socket; |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| public class ConfigProtocolHandler implements ProtocolHandler { |
|
18 |
| |
|
19 |
0
| public ConfigProtocolHandler() {
|
|
20 |
| } |
|
21 |
| |
|
22 |
0
| public void execute(Socket socket) {
|
|
23 |
0
| try {
|
|
24 |
0
| InputStream input = null;
|
|
25 |
0
| OutputStream output = null;
|
|
26 |
0
| input = socket.getInputStream();
|
|
27 |
0
| output = socket.getOutputStream();
|
|
28 |
| |
|
29 |
| |
|
30 |
0
| Request request = new Request(input);
|
|
31 |
0
| request.parse(socket);
|
|
32 |
| |
|
33 |
0
| Response response = new Response(output);
|
|
34 |
0
| response.setRequest(request);
|
|
35 |
0
| response.sendStaticResource();
|
|
36 |
| |
|
37 |
| |
|
38 |
0
| socket.close();
|
|
39 |
| } |
|
40 |
| catch (Exception e) { |
|
41 |
0
| e.printStackTrace();
|
|
42 |
| } |
|
43 |
| } |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| |
|
54 |
0
| private String getFileName(Request request,ServerContext serverContext) {
|
|
55 |
0
| String uri = request.getUri();
|
|
56 |
0
| String ip_adr = request.getHostAddress();
|
|
57 |
0
| if ( uri.startsWith("/") ) {
|
|
58 |
0
| uri = uri.substring(1);
|
|
59 |
| } |
|
60 |
0
| uri += ".xml";
|
|
61 |
0
| String tmp = serverContext.getDocumentRoot()+ip_adr+"_"+uri;
|
|
62 |
0
| if ( checkFile(tmp)) {
|
|
63 |
0
| return tmp;
|
|
64 |
| } |
|
65 |
0
| ip_adr = ip_adr.substring(0,ip_adr.lastIndexOf("."));
|
|
66 |
0
| tmp = serverContext.getDocumentRoot()+ip_adr+"_"+uri;
|
|
67 |
0
| if ( checkFile(tmp)) {
|
|
68 |
0
| return tmp;
|
|
69 |
| } |
|
70 |
0
| ip_adr = ip_adr.substring(0,ip_adr.lastIndexOf("."));
|
|
71 |
0
| tmp = serverContext.getDocumentRoot()+ip_adr+"_"+uri;
|
|
72 |
0
| if ( checkFile(tmp)) {
|
|
73 |
0
| return tmp;
|
|
74 |
| } |
|
75 |
0
| ip_adr = ip_adr.substring(0,ip_adr.lastIndexOf("."));
|
|
76 |
0
| tmp = serverContext.getDocumentRoot()+ip_adr+"_"+uri;
|
|
77 |
0
| if ( checkFile(tmp)) {
|
|
78 |
0
| return tmp;
|
|
79 |
| } |
|
80 |
0
| tmp = serverContext.getDocumentRoot()+uri;
|
|
81 |
0
| if ( checkFile(tmp)) {
|
|
82 |
0
| return tmp;
|
|
83 |
| } |
|
84 |
0
| uri = serverContext.getDocumentRoot()+uri;
|
|
85 |
0
| return uri;
|
|
86 |
| } |
|
87 |
| |
|
88 |
0
| private boolean checkFile(String name) {
|
|
89 |
0
| File file = new File(name);
|
|
90 |
0
| return file.exists();
|
|
91 |
| } |
|
92 |
| |
|
93 |
0
| public void execute(Socket socket, ServerContext serverContext) {
|
|
94 |
0
| try {
|
|
95 |
0
| InputStream input = null;
|
|
96 |
0
| OutputStream output = null;
|
|
97 |
0
| input = socket.getInputStream();
|
|
98 |
0
| output = socket.getOutputStream();
|
|
99 |
| |
|
100 |
| |
|
101 |
0
| Request request = new Request(input);
|
|
102 |
0
| request.parse(socket);
|
|
103 |
| |
|
104 |
0
| Response response = new Response(output);
|
|
105 |
0
| response.setRequest(request);
|
|
106 |
0
| String filename = getFileName(request,serverContext);
|
|
107 |
0
| response.sendStaticResource(filename);
|
|
108 |
| |
|
109 |
| |
|
110 |
0
| socket.close();
|
|
111 |
| } |
|
112 |
| catch (Exception e) { |
|
113 |
0
| e.printStackTrace();
|
|
114 |
| } |
|
115 |
| } |
|
116 |
| |
|
117 |
| } |