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 id = null;
37 private String fromId = null;
39 public AbstractEndpoint(API api, String path, String name,
40 String parameters, String description)
45 this.parameters = parameters;
46 this.description = description;
49 public String getPath()
54 protected API getAPI()
59 public String getName()
64 public String getParameters()
66 return this.parameters;
69 public String getDescription()
71 return this.description;
74 public abstract void processEndpoint(HttpServletRequest request,
75 HttpServletResponse response);
78 * Shared methods below here
81 protected String getId(HttpServletRequest request)
85 id = getQueryParameter(request, "id");
89 protected String getFromId(HttpServletRequest request)
93 fromId = getQueryParameter(request, "fromId");
97 protected Map<String, String[]> getQueryParameters(
98 HttpServletRequest request)
100 if (queryParameters != null)
101 return queryParameters;
102 queryParameters = request.getParameterMap();
103 return queryParameters;
106 protected String getQueryParameter(HttpServletRequest request,
109 String[] vals = getQueryParameters(request).get(param);
110 return (vals == null || vals.length < 1) ? null : vals[0];
113 protected String[] getEndpointPathParameters(HttpServletRequest request)
115 if (pathParameters != null)
116 return pathParameters;
117 String pathInfo = request.getPathInfo();
118 int slashpos = pathInfo.indexOf('/', 1);
122 pathParameters = pathInfo.substring(slashpos + 1).split("/");
123 return pathParameters;
126 protected Map<String, String> getOptions(HttpServletRequest request)
128 if (this.options != null)
130 Map<String, String> opts = new HashMap<>();
131 for (Entry<String, String[]> e : getQueryParameters(null).entrySet())
133 if (e.getKey().startsWith("option:"))
135 for (int i = 0; i < e.getValue().length; i++)
137 opts.put(e.getKey().substring(7), e.getValue()[i]);
142 String[] params = getEndpointPathParameters(request);
145 for (int i = 0; i < params.length; i++)
147 String param = params[i];
148 if (param.startsWith("option:"))
150 int ePos = param.indexOf("=");
153 opts.put(param.substring(7), null);
157 opts.put(param.substring(7, ePos), param.substring(ePos + 1));
166 protected void returnError(HttpServletRequest request,
167 HttpServletResponse response, String message)
169 String okString = request.getParameter("ok");
170 boolean ok = (okString != null && okString.equalsIgnoreCase("true"));
172 * Annoyingly jetty is not adding content to anything other than a few
173 * 20x status codes. Possibly it is closing the PrintWriter.
174 * Find a fix for this!
175 ****************************************************/
176 response.setStatus(ok ? HttpServletResponse.SC_OK : //
177 // HttpServletResponse.SC_BAD_REQUEST //
178 HttpServletResponse.SC_PARTIAL_CONTENT //
181 String endpointName = getPath();
182 Console.error(getAPI().getName() + " error: endpoint " + endpointName
183 + " failed: '" + message + "'");
186 PrintWriter writer = response.getWriter();
187 writer.write("message=Endpoint " + endpointName + ": " + message);
188 } catch (IOException e)
190 Console.info("Exception writing to REST response", e);
194 protected boolean checkParameters(HttpServletRequest request,
195 HttpServletResponse response, int i)
197 String[] parameters = getEndpointPathParameters(request);
199 // check we can run fetchsequence
200 if (parameters.length < i)
202 returnError(request, response,
203 "requires parameters:" + getParameters() + "\n" + getName()
204 + ": " + getDescription());
210 public int[][] parseIntRanges(String rangesString)
212 if (rangesString.equals("*"))
214 return new int[][] { { -1 }, { -1 } };
216 String[] rangeStrings = rangesString.split(",");
217 int[][] ranges = new int[2][rangeStrings.length];
218 for (int i = 0; i < rangeStrings.length; i++)
220 String range = rangeStrings[i];
223 int hyphenpos = range.indexOf('-');
226 ranges[0][i] = Integer.parseInt(range);
227 ranges[1][i] = ranges[0][i];
231 ranges[0][i] = Integer.parseInt(range.substring(0, hyphenpos));
232 ranges[1][i] = Integer.parseInt(range.substring(hyphenpos + 1));
234 } catch (NumberFormatException nfe)
243 * Get all AlignFrames or just one if requested to work on a specific window (fromId query string parameter)
245 protected AlignFrame[] getAlignFrames(HttpServletRequest request,
248 return getAlignFrames(request, "fromId", all);
251 protected AlignFrame[] getAlignFrames(HttpServletRequest request,
252 String idParam, boolean all)
254 String fromIdString = this.getQueryParameter(request, idParam);
256 if (fromIdString != null)
258 AlignFrame af = AlignFrame.getAlignFrameFromRestId(fromIdString);
259 return af == null ? null : new AlignFrame[] { af };
263 return Desktop.getAlignFrames();
271 protected AlignFrame getAlignFrameFromId(HttpServletRequest request)
273 return getAlignFrameFromId(request, "fromId");
276 protected AlignFrame getAlignFrameFromId(HttpServletRequest request,
279 AlignFrame[] afs = getAlignFrames(request, idParam, false);
280 return (afs == null || afs.length < 1) ? null : afs[0];
283 protected boolean deleteFromCache()