package jalview.rest; import java.io.BufferedReader; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import jalview.bin.Console; import jalview.gui.AlignFrame; import jalview.gui.Desktop; import jalview.rest.RestHandler.EndpointI; public abstract class AbstractEndpoint implements EndpointI { private final String path; private API api; private final String name; private final String parameters; private final String description; public AbstractEndpoint(API api, String path, String name, String parameters, String description) { this.api = api; this.path = path; this.name = name; this.parameters = parameters; this.description = description; } public String getPath() { return this.path; } protected API getAPI() { return this.api; } public String getName() { return this.name; } public String getParameters() { return this.parameters; } public String getDescription() { return this.description; } public abstract void processEndpoint(HttpServletRequest request, HttpServletResponse response); /* * Shared methods below here */ protected String[] getEndpointPathParameters(HttpServletRequest request) { String pathInfo = request.getPathInfo(); int slashpos = pathInfo.indexOf('/', 1); return slashpos < 1 ? null : pathInfo.substring(slashpos + 1).split("/"); } protected void returnError(HttpServletRequest request, HttpServletResponse response, String message) { String okString = request.getParameter("ok"); boolean ok = (okString != null && okString.equalsIgnoreCase("true")); /* * Annoyingly jetty is not adding content to anything other than a few * 20x status codes. Possibly it is closing the PrintWriter. * Find a fix for this! ****************************************************/ response.setStatus(ok ? HttpServletResponse.SC_OK : // // HttpServletResponse.SC_BAD_REQUEST // HttpServletResponse.SC_PARTIAL_CONTENT // ); String endpointName = getPath(); Console.error(getAPI().getName() + " error: endpoint " + endpointName + " failed: '" + message + "'"); try { PrintWriter writer = response.getWriter(); writer.write("message=Endpoint " + endpointName + ": " + message); } catch (IOException e) { Console.info("Exception writing to REST response", e); } } protected String getRequestBody(HttpServletRequest request) throws IOException { StringBuilder sb = new StringBuilder(); BufferedReader reader = request.getReader(); try { String line; while ((line = reader.readLine()) != null) { sb.append(line).append('\n'); } } finally { reader.close(); } return sb.toString(); } protected boolean checkParameters(HttpServletRequest request, HttpServletResponse response, int i) { String[] parameters = getEndpointPathParameters(request); // check we can run fetchsequence if (parameters.length < i) { returnError(request, response, "requires parameters:" + getParameters() + "\n" + getName() + ": " + getDescription()); return false; } return true; } public int[][] parseIntRanges(String rangesString) { if (rangesString.equals("*")) { return new int[][] { { -1 }, { -1 } }; } String[] rangeStrings = rangesString.split(","); int[][] ranges = new int[2][rangeStrings.length]; for (int i = 0; i < rangeStrings.length; i++) { String range = rangeStrings[i]; try { int hyphenpos = range.indexOf('-'); if (hyphenpos < 0) { ranges[0][i] = Integer.parseInt(range); ranges[1][i] = ranges[0][i]; } else { ranges[0][i] = Integer.parseInt(range.substring(0, hyphenpos)); ranges[1][i] = Integer.parseInt(range.substring(hyphenpos + 1)); } } catch (NumberFormatException nfe) { return null; } } return ranges; } /* * Get all AlignFrames or just one if requested to work on a specific window (fromId query string parameter) */ protected AlignFrame[] getAlignFrames(HttpServletRequest request, boolean all) { return getAlignFrames(request, "fromId", all); } protected AlignFrame[] getAlignFrames(HttpServletRequest request, String idParam, boolean all) { String fromIdString = request.getParameter(idParam); if (fromIdString != null) { AlignFrame af = AlignFrame.getAlignFrameFromRestId(fromIdString); return af == null ? null : new AlignFrame[] { af }; } else if (all) { return Desktop.getAlignFrames(); } else { return null; } } protected AlignFrame getAlignFrameFromId(HttpServletRequest request) { return getAlignFrameFromId(request, "fromId"); } protected AlignFrame getAlignFrameFromId(HttpServletRequest request, String idParam) { AlignFrame[] afs = getAlignFrames(request, idParam, false); return (afs == null || afs.length < 1 || afs[0] == null) ? null : afs[0]; } protected boolean deleteFromCache() { return false; } }