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