Clover icon

Coverage Report

  1. Project Clover database Wed Dec 3 2025 17:03:17 GMT
  2. Package jalview.httpserver

File HttpServer.java

 

Coverage histogram

../../img/srcFileCovDistChart0.png
60% of files have more coverage

Code metrics

12
54
8
1
295
146
18
0.33
6.75
8
2.25

Classes

Class Line # Actions
HttpServer 54 54 18
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /*
2    * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3    * Copyright (C) $$Year-Rel$$ The Jalview Authors
4    *
5    * This file is part of Jalview.
6    *
7    * Jalview is free software: you can redistribute it and/or
8    * modify it under the terms of the GNU General Public License
9    * as published by the Free Software Foundation, either version 3
10    * of the License, or (at your option) any later version.
11    *
12    * Jalview is distributed in the hope that it will be useful, but
13    * WITHOUT ANY WARRANTY; without even the implied warranty
14    * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15    * PURPOSE. See the GNU General Public License for more details.
16    *
17    * You should have received a copy of the GNU General Public License
18    * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19    * The Jalview Authors are detailed in the 'AUTHORS' file.
20    */
21    package jalview.httpserver;
22   
23    import jalview.bin.ApplicationSingletonProvider;
24    import jalview.bin.ApplicationSingletonProvider.ApplicationSingletonI;
25    import jalview.rest.RestHandler;
26   
27    import java.net.BindException;
28    import java.net.URI;
29    import java.util.Collections;
30    import java.util.HashMap;
31    import java.util.Map;
32   
33    import javax.servlet.http.HttpServletRequest;
34    import javax.servlet.http.HttpServletResponse;
35   
36    import org.eclipse.jetty.server.Handler;
37    import org.eclipse.jetty.server.Server;
38    import org.eclipse.jetty.server.ServerConnector;
39    import org.eclipse.jetty.server.handler.ContextHandler;
40    import org.eclipse.jetty.server.handler.HandlerCollection;
41    import org.eclipse.jetty.util.thread.QueuedThreadPool;
42   
43    /**
44    * An HttpServer built on Jetty. To use it
45    * <ul>
46    * <li>call getInstance() to create and start the server</li>
47    * <li>call registerHandler to add a handler for a path (below /jalview)</li>
48    * <li>when finished, call removedHandler</li>
49    * </ul>
50    *
51    * @author gmcarstairs
52    * @see http://eclipse.org/jetty/documentation/current/embedding-jetty.html
53    */
 
54    public class HttpServer implements ApplicationSingletonI
55    {
56    /**
57    * Returns the singleton instance of this class.
58    *
59    * @return
60    * @throws BindException
61    */
 
62  0 toggle public static HttpServer getInstance() throws BindException
63    {
64  0 synchronized (HttpServer.class)
65    {
66  0 return ApplicationSingletonProvider.getInstance(HttpServer.class);
67    }
68    }
69   
70    /**
71    * Private constructor to enforce use of singleton
72    *
73    * @throws BindException
74    * if no free port can be assigned
75    */
 
76  0 toggle private HttpServer() throws BindException
77    {
78  0 startServer();
79   
80    /*
81    * Provides a REST server by default; add more programmatically as required
82    */
83  0 registerHandler(RestHandler.getInstance());
84    }
85   
86   
87    /*
88    * 'context root' - actually just prefixed to the path for each handler for
89    * now - see registerHandler
90    */
91    private static final String JALVIEW_PATH = "jalview";
92   
93    /*
94    * The Http server
95    */
96    private Server server;
97   
98    /*
99    * Registered handlers for context paths
100    */
101    private HandlerCollection contextHandlers;
102   
103    /*
104    * Lookup of ContextHandler by its wrapped handler
105    */
106    Map<Handler, ContextHandler> myHandlers = new HashMap<Handler, ContextHandler>();
107   
108    /*
109    * The context root for the server
110    */
111    private URI contextRoot;
112   
113   
114    /**
115    * Start the http server
116    *
117    * @throws BindException
118    */
 
119  0 toggle private void startServer() throws BindException
120    {
121  0 try
122    {
123    /*
124    * Create a server with a small number of threads; jetty will allocate a
125    * free port
126    */
127  0 QueuedThreadPool tp = new QueuedThreadPool(4, 1); // max, min
128  0 server = new Server(tp);
129    // 2 selector threads to handle incoming connections
130  0 ServerConnector connector = new ServerConnector(server, 0, 2);
131    // restrict to localhost
132  0 connector.setHost("localhost");
133  0 server.addConnector(connector);
134   
135    /*
136    * HttpServer shuts down with Jalview process
137    */
138  0 server.setStopAtShutdown(true);
139   
140    /*
141    * Create a mutable set of handlers (can add handlers while the server is
142    * running). Using vanilla handlers here rather than servlets
143    */
144    // TODO how to properly configure context root "/jalview"
145  0 contextHandlers = new HandlerCollection(true);
146  0 server.setHandler(contextHandlers);
147  0 server.start();
148    // jalview.bin.Console.outPrintln(String.format(
149    // "HttpServer started with %d threads", server.getThreadPool()
150    // .getThreads()));
151  0 contextRoot = server.getURI();
152    } catch (Exception e)
153    {
154  0 jalview.bin.Console.errPrintln(
155    "Error trying to start HttpServer: " + e.getMessage());
156  0 try
157    {
158  0 server.stop();
159    } catch (Exception e1)
160    {
161  0 e1.printStackTrace();
162    }
163    }
164  0 if (server == null)
165    {
166  0 throw new BindException("HttpServer failed to allocate a port");
167    }
168    }
169   
170    /**
171    * Returns the URI on which we are listening
172    *
173    * @return
174    */
 
175  0 toggle public URI getUri()
176    {
177  0 return server == null ? null : server.getURI();
178    }
179   
180    /**
181    * For debug - write HTTP request details to stdout
182    *
183    * @param request
184    * @param response
185    */
 
186  0 toggle protected void dumpRequest(HttpServletRequest request,
187    HttpServletResponse response)
188    {
189  0 for (String hdr : Collections.list(request.getHeaderNames()))
190    {
191  0 for (String val : Collections.list(request.getHeaders(hdr)))
192    {
193  0 jalview.bin.Console.outPrintln(hdr + ": " + val);
194    }
195    }
196  0 for (String param : Collections.list(request.getParameterNames()))
197    {
198  0 for (String val : request.getParameterValues(param))
199    {
200  0 jalview.bin.Console.outPrintln(param + "=" + val);
201    }
202    }
203    }
204   
205    /**
206    * Stop the Http server.
207    */
 
208  0 toggle public void stopServer()
209    {
210  0 if (server != null)
211    {
212  0 if (server.isStarted())
213    {
214  0 try
215    {
216  0 server.stop();
217    } catch (Exception e)
218    {
219  0 jalview.bin.Console.errPrintln("Error stopping Http Server on "
220    + server.getURI() + ": " + e.getMessage());
221    }
222    }
223    }
224    }
225   
226    /**
227    * Register a handler for the given path and set its URI
228    *
229    * @param handler
230    * @return
231    * @throws IllegalStateException
232    * if handler path has not been set
233    */
 
234  0 toggle public void registerHandler(AbstractRequestHandler handler)
235    {
236  0 String path = handler.getPath();
237  0 if (path == null)
238    {
239  0 throw new IllegalStateException(
240    "Must set handler path before registering handler");
241    }
242   
243    // http://stackoverflow.com/questions/20043097/jetty-9-embedded-adding-handlers-during-runtime
244  0 ContextHandler ch = new ContextHandler();
245  0 ch.setAllowNullPathInfo(true);
246  0 ch.setContextPath("/" + JALVIEW_PATH + "/" + path);
247  0 ch.setResourceBase(".");
248  0 ch.setClassLoader(Thread.currentThread().getContextClassLoader());
249  0 ch.setHandler(handler);
250   
251    /*
252    * Remember the association so we can remove it later
253    */
254  0 this.myHandlers.put(handler, ch);
255   
256    /*
257    * A handler added to a running server must be started explicitly
258    */
259  0 contextHandlers.addHandler(ch);
260  0 try
261    {
262  0 ch.start();
263    } catch (Exception e)
264    {
265  0 jalview.bin.Console.errPrintln(
266    "Error starting handler for " + path + ": " + e.getMessage());
267    }
268   
269  0 handler.setUri(this.contextRoot + ch.getContextPath().substring(1));
270  0 jalview.bin.Console.outPrintln("Jalview " + handler.getName()
271    + " handler started on " + handler.getUri());
272    }
273   
274    /**
275    * Removes the handler from the server; more precisely, remove the
276    * ContextHandler wrapping the specified handler
277    *
278    * @param handler
279    */
 
280  0 toggle public void removeHandler(AbstractRequestHandler handler)
281    {
282    /*
283    * Have to use this cached lookup table since there is no method
284    * ContextHandler.getHandler()
285    */
286  0 ContextHandler ch = myHandlers.get(handler);
287  0 if (ch != null)
288    {
289  0 contextHandlers.removeHandler(ch);
290  0 myHandlers.remove(handler);
291  0 jalview.bin.Console.outPrintln("Stopped Jalview " + handler.getName()
292    + " handler on " + handler.getUri());
293    }
294    }
295    }