3 import java.io.BufferedReader;
4 import java.io.IOException;
5 import java.io.PrintWriter;
7 import javax.servlet.http.HttpServletRequest;
8 import javax.servlet.http.HttpServletResponse;
10 import jalview.bin.Console;
11 import jalview.gui.AlignFrame;
12 import jalview.gui.Desktop;
13 import jalview.rest.RestHandler.EndpointI;
15 public abstract class AbstractEndpoint implements EndpointI
17 private final String path;
21 private final String name;
23 private final String parameters;
25 private final String description;
27 public AbstractEndpoint(API api, String path, String name,
28 String parameters, String description)
33 this.parameters = parameters;
34 this.description = description;
37 public String getPath()
42 protected API getAPI()
47 public String getName()
52 public String getParameters()
54 return this.parameters;
57 public String getDescription()
59 return this.description;
62 public abstract void processEndpoint(HttpServletRequest request,
63 HttpServletResponse response);
66 * Shared methods below here
69 protected String[] getEndpointPathParameters(HttpServletRequest request)
71 String pathInfo = request.getPathInfo();
72 int slashpos = pathInfo.indexOf('/', 1);
73 return slashpos < 1 ? null
74 : pathInfo.substring(slashpos + 1).split("/");
77 protected void returnError(HttpServletRequest request,
78 HttpServletResponse response, String message)
80 String okString = request.getParameter("ok");
81 boolean ok = (okString != null && okString.equalsIgnoreCase("true"));
83 * Annoyingly jetty is not adding content to anything other than a few
84 * 20x status codes. Possibly it is closing the PrintWriter.
85 * Find a fix for this!
86 ****************************************************/
87 response.setStatus(ok ? HttpServletResponse.SC_OK : //
88 // HttpServletResponse.SC_BAD_REQUEST //
89 HttpServletResponse.SC_PARTIAL_CONTENT //
92 String endpointName = getPath();
93 Console.error(getAPI().getName() + " error: endpoint " + endpointName
94 + " failed: '" + message + "'");
97 PrintWriter writer = response.getWriter();
98 writer.write("message=Endpoint " + endpointName + ": " + message);
99 } catch (IOException e)
101 Console.info("Exception writing to REST response", e);
105 protected String getRequestBody(HttpServletRequest request)
108 StringBuilder sb = new StringBuilder();
109 BufferedReader reader = request.getReader();
113 while ((line = reader.readLine()) != null)
115 sb.append(line).append('\n');
121 return sb.toString();
124 protected boolean checkParameters(HttpServletRequest request,
125 HttpServletResponse response, int i)
127 String[] parameters = getEndpointPathParameters(request);
129 // check we can run fetchsequence
130 if (parameters.length < i)
132 returnError(request, response,
133 "requires parameters:" + getParameters() + "\n" + getName()
134 + ": " + getDescription());
140 public int[][] parseIntRanges(String rangesString)
142 if (rangesString.equals("*"))
144 return new int[][] { { -1 }, { -1 } };
146 String[] rangeStrings = rangesString.split(",");
147 int[][] ranges = new int[2][rangeStrings.length];
148 for (int i = 0; i < rangeStrings.length; i++)
150 String range = rangeStrings[i];
153 int hyphenpos = range.indexOf('-');
156 ranges[0][i] = Integer.parseInt(range);
157 ranges[1][i] = ranges[0][i];
161 ranges[0][i] = Integer.parseInt(range.substring(0, hyphenpos));
162 ranges[1][i] = Integer.parseInt(range.substring(hyphenpos + 1));
164 } catch (NumberFormatException nfe)
173 * Get all AlignFrames or just one if requested to work on a specific window (fromId query string parameter)
175 protected AlignFrame[] getAlignFrames(HttpServletRequest request,
178 return getAlignFrames(request, "fromId", all);
181 protected AlignFrame[] getAlignFrames(HttpServletRequest request,
182 String idParam, boolean all)
184 String fromIdString = request.getParameter(idParam);
186 if (fromIdString != null)
188 AlignFrame af = AlignFrame.getAlignFrameFromRestId(fromIdString);
189 return af == null ? null : new AlignFrame[] { af };
193 return Desktop.getAlignFrames();
201 protected AlignFrame getAlignFrameFromId(HttpServletRequest request)
203 return getAlignFrameFromId(request, "fromId");
206 protected AlignFrame getAlignFrameFromId(HttpServletRequest request,
209 AlignFrame[] afs = getAlignFrames(request, idParam, false);
210 return (afs == null || afs.length < 1 || afs[0] == null) ? null
214 protected boolean deleteFromCache()