3 import java.io.IOException;
4 import java.io.PrintWriter;
5 import java.util.HashMap;
7 import java.util.Map.Entry;
9 import javax.servlet.http.HttpServletRequest;
10 import javax.servlet.http.HttpServletResponse;
12 import jalview.bin.Console;
13 import jalview.gui.AlignFrame;
14 import jalview.gui.Desktop;
15 import jalview.rest.RestHandler.EndpointI;
17 public abstract class AbstractEndpoint implements EndpointI
19 private final String path;
23 private final String name;
25 private final String parameters;
27 private final String description;
29 private String[] pathParameters = null;
31 private Map<String, String[]> queryParameters = null;
33 private Map<String, String> options = null;
35 private String requestUrl = null;
37 private String id = null;
39 private String fromId = null;
41 public AbstractEndpoint(API api, String path, String name,
42 String parameters, String description)
47 this.parameters = parameters;
48 this.description = description;
51 public String getPath()
56 protected API getAPI()
61 public String getName()
66 public String getParameters()
68 return this.parameters;
71 public String getDescription()
73 return this.description;
76 public abstract void processEndpoint(HttpServletRequest request,
77 HttpServletResponse response);
80 * Shared methods below here
83 protected String getId(HttpServletRequest request)
87 id = getQueryParameter(request, "id");
91 protected String getFromId(HttpServletRequest request)
95 fromId = getQueryParameter(request, "fromId");
99 protected Map<String, String[]> getQueryParameters(
100 HttpServletRequest request)
102 if (queryParameters != null)
103 return queryParameters;
104 queryParameters = request.getParameterMap();
105 return queryParameters;
108 protected String getQueryParameter(HttpServletRequest request,
111 String[] vals = getQueryParameters(request).get(param);
112 return (vals == null || vals.length < 1) ? null : vals[0];
115 protected String[] getEndpointPathParameters(HttpServletRequest request)
117 if (pathParameters != null)
118 return pathParameters;
119 String pathInfo = request.getPathInfo();
120 int slashpos = pathInfo.indexOf('/', 1);
124 pathParameters = pathInfo.substring(slashpos + 1).split("/");
125 return pathParameters;
128 protected Map<String, String> getOptions(HttpServletRequest request)
130 if (this.options != null)
132 Map<String, String> opts = new HashMap<>();
133 for (Entry<String, String[]> e : getQueryParameters(null).entrySet())
135 if (e.getKey().startsWith("option:"))
137 for (int i = 0; i < e.getValue().length; i++)
139 opts.put(e.getKey().substring(7), e.getValue()[i]);
144 String[] params = getEndpointPathParameters(request);
147 for (int i = 0; i < params.length; i++)
149 String param = params[i];
150 if (param.startsWith("option:"))
152 int ePos = param.indexOf("=");
155 opts.put(param.substring(7), null);
159 opts.put(param.substring(7, ePos), param.substring(ePos + 1));
168 protected String getRequestUrl(HttpServletRequest request)
170 if (requestUrl != null)
174 StringBuilder sb = new StringBuilder(
175 request.getRequestURL().toString());
176 String query = request.getQueryString();
177 if (query != null || query.length() > 0)
179 sb.append("?").append(query);
181 requestUrl = sb.toString();
185 Console.debug("AbstractEndpoint: request is null");
190 protected void returnError(HttpServletRequest request,
191 HttpServletResponse response, String message)
193 String okString = request.getParameter("ok");
194 boolean ok = (okString != null && okString.equalsIgnoreCase("true"));
196 * Annoyingly jetty is not adding content to anything other than a few
197 * 20x status codes. Possibly it is closing the PrintWriter.
198 * Find a fix for this!
199 ****************************************************/
200 response.setStatus(ok ? HttpServletResponse.SC_OK : //
201 // HttpServletResponse.SC_BAD_REQUEST //
202 HttpServletResponse.SC_PARTIAL_CONTENT //
205 String endpointName = getPath();
206 Console.error(getAPI().getName() + " error: endpoint " + endpointName
207 + " failed: '" + message + "'");
210 PrintWriter writer = response.getWriter();
211 writer.write("message=Endpoint " + endpointName + ": " + message);
212 } catch (IOException e)
214 Console.info("Exception writing to REST response", e);
218 protected boolean checkParameters(HttpServletRequest request,
219 HttpServletResponse response, int i)
221 String[] parameters = getEndpointPathParameters(request);
223 // check we can run fetchsequence
224 if (parameters.length < i)
226 returnError(request, response,
227 "requires parameters:" + getParameters() + "\n" + getName()
228 + ": " + getDescription());
234 public int[][] parseIntRanges(String rangesString)
236 if (rangesString.equals("*"))
238 return new int[][] { { -1 }, { -1 } };
240 String[] rangeStrings = rangesString.split(",");
241 int[][] ranges = new int[2][rangeStrings.length];
242 for (int i = 0; i < rangeStrings.length; i++)
244 String range = rangeStrings[i];
247 int hyphenpos = range.indexOf('-');
250 ranges[0][i] = Integer.parseInt(range);
251 ranges[1][i] = ranges[0][i];
255 ranges[0][i] = Integer.parseInt(range.substring(0, hyphenpos));
256 ranges[1][i] = Integer.parseInt(range.substring(hyphenpos + 1));
258 } catch (NumberFormatException nfe)
267 * Get all AlignFrames or just one if requested to work on a specific window (fromId query string parameter)
269 protected AlignFrame[] getAlignFrames(HttpServletRequest request,
272 return getAlignFrames(request, "fromId", all);
275 protected AlignFrame[] getAlignFrames(HttpServletRequest request,
276 String idParam, boolean all)
278 String fromIdString = this.getQueryParameter(request, idParam);
280 if (fromIdString != null)
282 AlignFrame af = AlignFrame.getAlignFrameFromRestId(fromIdString);
283 return af == null ? null : new AlignFrame[] { af };
287 return Desktop.getAlignFrames();
295 protected AlignFrame getAlignFrameUsingId(HttpServletRequest request)
297 return getAlignFrameUsingFromId(request, getId(request));
300 protected AlignFrame getAlignFrameUsingFromId(HttpServletRequest request)
302 return getAlignFrameUsingFromId(request, "fromId");
305 protected AlignFrame getAlignFrameUsingFromId(HttpServletRequest request,
308 AlignFrame[] afs = getAlignFrames(request, idParam, false);
309 return (afs == null || afs.length < 1) ? null : afs[0];
312 protected boolean deleteFromCache()