2db03668db47d4c4f737b41b4d30cbd4954edc00
[jalview.git] / src / jalview / rest / AbstractEndpoint.java
1 package jalview.rest;
2
3 import java.io.BufferedReader;
4 import java.io.IOException;
5 import java.io.PrintWriter;
6
7 import javax.servlet.http.HttpServletRequest;
8 import javax.servlet.http.HttpServletResponse;
9
10 import jalview.bin.Cache;
11 import jalview.rest.RestHandler.EndpointI;
12
13 public abstract class AbstractEndpoint implements EndpointI
14 {
15   private final String path;
16
17   private API api;
18
19   private final String name;
20
21   private final String parameters;
22
23   private final String description;
24
25   public AbstractEndpoint(API api, String path, String name,
26           String parameters, String description)
27   {
28     this.api = api;
29     this.path = path;
30     this.name = name;
31     this.parameters = parameters;
32     this.description = description;
33   }
34
35   public String getPath()
36   {
37     return this.path;
38   }
39
40   protected API getAPI()
41   {
42     return this.api;
43   }
44
45   public String getName()
46   {
47     return this.name;
48   }
49
50   public String getParameters()
51   {
52     return this.parameters;
53   }
54
55   public String getDescription()
56   {
57     return this.description;
58   }
59
60   public abstract void processEndpoint(HttpServletRequest request,
61           HttpServletResponse response);
62
63   /*
64    * Shared methods below here
65    */
66
67   protected String[] getEndpointPathParameters(HttpServletRequest request)
68   {
69     String pathInfo = request.getPathInfo();
70     int slashpos = pathInfo.indexOf('/', 1);
71     return slashpos < 1 ? null
72             : pathInfo.substring(slashpos + 1).split("/");
73   }
74
75   protected void returnError(HttpServletRequest request,
76           HttpServletResponse response, String message)
77   {
78     response.setStatus(500); // set this to something better
79     String endpointName = getPath();
80     Cache.error(getAPI().getName() + " error: endpoint " + endpointName
81             + " failed: '" + message + "'");
82     try
83     {
84       PrintWriter writer = response.getWriter();
85       writer.write("Endpoint " + endpointName + ": " + message);
86       writer.close();
87     } catch (IOException e)
88     {
89       Cache.debug(e);
90     }
91   }
92
93   protected String getRequestBody(HttpServletRequest request)
94           throws IOException
95   {
96     StringBuilder sb = new StringBuilder();
97     BufferedReader reader = request.getReader();
98     try
99     {
100       String line;
101       while ((line = reader.readLine()) != null)
102       {
103         sb.append(line).append('\n');
104       }
105     } finally
106     {
107       reader.close();
108     }
109     return sb.toString();
110   }
111
112   protected boolean checkParameters(HttpServletRequest request,
113           HttpServletResponse response, int i)
114   {
115     String[] parameters = getEndpointPathParameters(request);
116
117     // check we can run fetchsequence
118     if (parameters.length < i)
119     {
120       returnError(request, response,
121               "requires parameters:" + getParameters() + "\n" + getName()
122                       + ": " + getDescription());
123       return false;
124     }
125     return true;
126   }
127
128 }