JAL-3210 Improvements to eclipse detection. New src tree and SwingJS updated from...
[jalview.git] / src / jalview / rest / RestHandler.java
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.rest;
22
23 import jalview.bin.ApplicationSingletonProvider;
24 import jalview.bin.ApplicationSingletonProvider.ApplicationSingletonI;
25 import jalview.httpserver.AbstractRequestHandler;
26
27 import java.io.IOException;
28 import java.io.PrintWriter;
29 import java.net.BindException;
30
31 import javax.servlet.http.HttpServletRequest;
32 import javax.servlet.http.HttpServletResponse;
33
34 /**
35  * A simple handler to process (or delegate) HTTP requests on /jalview/rest.
36  */
37 public class RestHandler extends AbstractRequestHandler
38         implements ApplicationSingletonI
39 {
40   private static final String MY_PATH = "rest";
41
42   private static final String MY_NAME = "Rest";
43
44   /**
45    * Returns the singleton instance of this class
46    * 
47    * @return
48    * @throws BindException
49    */
50   public static RestHandler getInstance() throws BindException
51   {
52     synchronized (RestHandler.class)
53     {
54       return (RestHandler) ApplicationSingletonProvider.getInstance(RestHandler.class);
55     }
56   }
57
58   /**
59    * Private constructor enforces use of singleton
60    * 
61    * @throws BindException
62    */
63   private RestHandler() throws BindException
64   {
65     setPath(MY_PATH);
66
67     /*
68      * We don't register the handler here - this is done as a special case in
69      * HttpServer initialisation; to do it here would invite an infinite loop of
70      * RestHandler/HttpServer constructor
71      */
72   }
73
74   /**
75    * Handle a jalview/rest request
76    * 
77    * @throws IOException
78    */
79   @Override
80   protected void processRequest(HttpServletRequest request,
81           HttpServletResponse response) throws IOException
82   {
83     /*
84      * Currently just echoes the request; add helper classes as required to
85      * process requests
86      */
87     final String queryString = request.getQueryString();
88     final String reply = "REST not yet implemented; received "
89             + request.getMethod() + ": " + request.getRequestURL()
90             + (queryString == null ? "" : "?" + queryString);
91     System.out.println(reply);
92
93     response.setHeader("Cache-Control", "no-cache/no-store");
94     response.setHeader("Content-type", "text/plain");
95     final PrintWriter writer = response.getWriter();
96     writer.write(reply);
97     writer.close();
98   }
99
100   /**
101    * Returns a display name for this service
102    */
103   @Override
104   public String getName()
105   {
106     return MY_NAME;
107   }
108
109 }