1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
package jalview.httpserver; |
22 |
|
|
23 |
|
import jalview.rest.RestHandler; |
24 |
|
|
25 |
|
import java.net.BindException; |
26 |
|
import java.net.URI; |
27 |
|
import java.util.Collections; |
28 |
|
import java.util.HashMap; |
29 |
|
import java.util.Map; |
30 |
|
|
31 |
|
import javax.servlet.http.HttpServletRequest; |
32 |
|
import javax.servlet.http.HttpServletResponse; |
33 |
|
|
34 |
|
import org.eclipse.jetty.server.Handler; |
35 |
|
import org.eclipse.jetty.server.Server; |
36 |
|
import org.eclipse.jetty.server.ServerConnector; |
37 |
|
import org.eclipse.jetty.server.handler.ContextHandler; |
38 |
|
import org.eclipse.jetty.server.handler.HandlerCollection; |
39 |
|
import org.eclipse.jetty.util.thread.QueuedThreadPool; |
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
@author |
50 |
|
@see |
51 |
|
|
|
|
| 0% |
Uncovered Elements: 78 (78) |
Complexity: 19 |
Complexity Density: 0.34 |
|
52 |
|
public class HttpServer |
53 |
|
{ |
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
private static final String JALVIEW_PATH = "jalview"; |
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
private static HttpServer instance; |
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
private Server server; |
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
private HandlerCollection contextHandlers; |
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
|
78 |
|
Map<Handler, ContextHandler> myHandlers = new HashMap<Handler, ContextHandler>(); |
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
|
83 |
|
private URI contextRoot; |
84 |
|
|
85 |
|
|
86 |
|
|
87 |
|
|
88 |
|
@return |
89 |
|
@throws |
90 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
91 |
0 |
public static HttpServer getInstance() throws BindException... |
92 |
|
{ |
93 |
0 |
synchronized (HttpServer.class) |
94 |
|
{ |
95 |
0 |
if (instance == null) |
96 |
|
{ |
97 |
0 |
instance = new HttpServer(); |
98 |
|
} |
99 |
0 |
return instance; |
100 |
|
} |
101 |
|
} |
102 |
|
|
103 |
|
|
104 |
|
|
105 |
|
|
106 |
|
@throws |
107 |
|
|
108 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
109 |
0 |
private HttpServer() throws BindException... |
110 |
|
{ |
111 |
0 |
startServer(); |
112 |
|
|
113 |
|
|
114 |
|
|
115 |
|
|
116 |
0 |
registerHandler(RestHandler.getInstance()); |
117 |
|
} |
118 |
|
|
119 |
|
|
120 |
|
|
121 |
|
|
122 |
|
@throws |
123 |
|
|
|
|
| 0% |
Uncovered Elements: 19 (19) |
Complexity: 4 |
Complexity Density: 0.24 |
|
124 |
0 |
private void startServer() throws BindException... |
125 |
|
{ |
126 |
0 |
try |
127 |
|
{ |
128 |
|
|
129 |
|
|
130 |
|
|
131 |
|
|
132 |
0 |
QueuedThreadPool tp = new QueuedThreadPool(4, 1); |
133 |
0 |
server = new Server(tp); |
134 |
|
|
135 |
0 |
ServerConnector connector = new ServerConnector(server, 0, 2); |
136 |
|
|
137 |
0 |
connector.setHost("localhost"); |
138 |
0 |
server.addConnector(connector); |
139 |
|
|
140 |
|
|
141 |
|
|
142 |
|
|
143 |
0 |
server.setStopAtShutdown(true); |
144 |
|
|
145 |
|
|
146 |
|
|
147 |
|
|
148 |
|
|
149 |
|
|
150 |
0 |
contextHandlers = new HandlerCollection(true); |
151 |
0 |
server.setHandler(contextHandlers); |
152 |
0 |
server.start(); |
153 |
|
|
154 |
|
|
155 |
|
|
156 |
0 |
contextRoot = server.getURI(); |
157 |
|
} catch (Exception e) |
158 |
|
{ |
159 |
0 |
jalview.bin.Console.errPrintln( |
160 |
|
"Error trying to start HttpServer: " + e.getMessage()); |
161 |
0 |
try |
162 |
|
{ |
163 |
0 |
server.stop(); |
164 |
|
} catch (Exception e1) |
165 |
|
{ |
166 |
0 |
e1.printStackTrace(); |
167 |
|
} |
168 |
|
} |
169 |
0 |
if (server == null) |
170 |
|
{ |
171 |
0 |
throw new BindException("HttpServer failed to allocate a port"); |
172 |
|
} |
173 |
|
} |
174 |
|
|
175 |
|
|
176 |
|
|
177 |
|
|
178 |
|
@return |
179 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 2 |
Complexity Density: 2 |
|
180 |
0 |
public URI getUri()... |
181 |
|
{ |
182 |
0 |
return server == null ? null : server.getURI(); |
183 |
|
} |
184 |
|
|
185 |
|
|
186 |
|
|
187 |
|
|
188 |
|
@param |
189 |
|
@param |
190 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
|
191 |
0 |
protected void dumpRequest(HttpServletRequest request,... |
192 |
|
HttpServletResponse response) |
193 |
|
{ |
194 |
0 |
for (String hdr : Collections.list(request.getHeaderNames())) |
195 |
|
{ |
196 |
0 |
for (String val : Collections.list(request.getHeaders(hdr))) |
197 |
|
{ |
198 |
0 |
jalview.bin.Console.outPrintln(hdr + ": " + val); |
199 |
|
} |
200 |
|
} |
201 |
0 |
for (String param : Collections.list(request.getParameterNames())) |
202 |
|
{ |
203 |
0 |
for (String val : request.getParameterValues(param)) |
204 |
|
{ |
205 |
0 |
jalview.bin.Console.outPrintln(param + "=" + val); |
206 |
|
} |
207 |
|
} |
208 |
|
} |
209 |
|
|
210 |
|
|
211 |
|
|
212 |
|
|
|
|
| 0% |
Uncovered Elements: 9 (9) |
Complexity: 4 |
Complexity Density: 0.8 |
|
213 |
0 |
public void stopServer()... |
214 |
|
{ |
215 |
0 |
if (server != null) |
216 |
|
{ |
217 |
0 |
if (server.isStarted()) |
218 |
|
{ |
219 |
0 |
try |
220 |
|
{ |
221 |
0 |
server.stop(); |
222 |
|
} catch (Exception e) |
223 |
|
{ |
224 |
0 |
jalview.bin.Console.errPrintln("Error stopping Http Server on " |
225 |
|
+ server.getURI() + ": " + e.getMessage()); |
226 |
|
} |
227 |
|
} |
228 |
|
} |
229 |
|
} |
230 |
|
|
231 |
|
|
232 |
|
|
233 |
|
|
234 |
|
@param |
235 |
|
@return |
236 |
|
@throws |
237 |
|
|
238 |
|
|
|
|
| 0% |
Uncovered Elements: 18 (18) |
Complexity: 3 |
Complexity Density: 0.19 |
|
239 |
0 |
public void registerHandler(AbstractRequestHandler handler)... |
240 |
|
{ |
241 |
0 |
String path = handler.getPath(); |
242 |
0 |
if (path == null) |
243 |
|
{ |
244 |
0 |
throw new IllegalStateException( |
245 |
|
"Must set handler path before registering handler"); |
246 |
|
} |
247 |
|
|
248 |
|
|
249 |
0 |
ContextHandler ch = new ContextHandler(); |
250 |
0 |
ch.setAllowNullPathInfo(true); |
251 |
0 |
ch.setContextPath("/" + JALVIEW_PATH + "/" + path); |
252 |
0 |
ch.setResourceBase("."); |
253 |
0 |
ch.setClassLoader(Thread.currentThread().getContextClassLoader()); |
254 |
0 |
ch.setHandler(handler); |
255 |
|
|
256 |
|
|
257 |
|
|
258 |
|
|
259 |
0 |
this.myHandlers.put(handler, ch); |
260 |
|
|
261 |
|
|
262 |
|
|
263 |
|
|
264 |
0 |
contextHandlers.addHandler(ch); |
265 |
0 |
try |
266 |
|
{ |
267 |
0 |
ch.start(); |
268 |
|
} catch (Exception e) |
269 |
|
{ |
270 |
0 |
jalview.bin.Console.errPrintln( |
271 |
|
"Error starting handler for " + path + ": " + e.getMessage()); |
272 |
|
} |
273 |
|
|
274 |
0 |
handler.setUri(this.contextRoot + ch.getContextPath().substring(1)); |
275 |
0 |
jalview.bin.Console.outPrintln("Jalview " + handler.getName() |
276 |
|
+ " handler started on " + handler.getUri()); |
277 |
|
} |
278 |
|
|
279 |
|
|
280 |
|
|
281 |
|
|
282 |
|
|
283 |
|
@param |
284 |
|
|
|
|
| 0% |
Uncovered Elements: 7 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
285 |
0 |
public void removeHandler(AbstractRequestHandler handler)... |
286 |
|
{ |
287 |
|
|
288 |
|
|
289 |
|
|
290 |
|
|
291 |
0 |
ContextHandler ch = myHandlers.get(handler); |
292 |
0 |
if (ch != null) |
293 |
|
{ |
294 |
0 |
contextHandlers.removeHandler(ch); |
295 |
0 |
myHandlers.remove(handler); |
296 |
0 |
jalview.bin.Console.outPrintln("Stopped Jalview " + handler.getName() |
297 |
|
+ " handler on " + handler.getUri()); |
298 |
|
} |
299 |
|
} |
300 |
|
} |