JAL-3851 some changes. HighlightSequenceEndpoint and SelectSequenceEndpoint
[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   public int[][] parseIntRanges(String rangesString)
129   {
130     String[] rangeStrings = rangesString.split(",");
131     int[][] ranges = new int[2][rangeStrings.length];
132     for (int i = 0; i < rangeStrings.length; i++)
133     {
134       String range = rangeStrings[i];
135       try
136       {
137         int hyphenpos = range.indexOf('-');
138         if (hyphenpos < 0)
139         {
140           ranges[0][i] = Integer.parseInt(range);
141           ranges[1][i] = ranges[0][i];
142         }
143         else
144         {
145           ranges[0][i] = Integer.parseInt(range.substring(0, hyphenpos));
146           ranges[1][i] = Integer.parseInt(range.substring(hyphenpos + 1));
147         }
148       } catch (NumberFormatException nfe)
149       {
150         return null;
151       }
152     }
153     return ranges;
154   }
155
156 }