3 import java.io.IOException;
4 import java.io.PrintWriter;
6 import javax.servlet.http.HttpServletRequest;
7 import javax.servlet.http.HttpServletResponse;
9 import jalview.bin.Console;
10 import jalview.gui.AlignFrame;
11 import jalview.gui.Desktop;
12 import jalview.rest.RestHandler.EndpointI;
14 public abstract class AbstractEndpoint implements EndpointI
16 private final String path;
20 private final String name;
22 private final String parameters;
24 private final String description;
26 public AbstractEndpoint(API api, String path, String name,
27 String parameters, String description)
32 this.parameters = parameters;
33 this.description = description;
36 public String getPath()
41 protected API getAPI()
46 public String getName()
51 public String getParameters()
53 return this.parameters;
56 public String getDescription()
58 return this.description;
61 public abstract void processEndpoint(HttpServletRequest request,
62 HttpServletResponse response);
65 * Shared methods below here
68 protected String[] getEndpointPathParameters(HttpServletRequest request)
70 String pathInfo = request.getPathInfo();
71 int slashpos = pathInfo.indexOf('/', 1);
72 return slashpos < 1 ? null
73 : pathInfo.substring(slashpos + 1).split("/");
76 protected void returnError(HttpServletRequest request,
77 HttpServletResponse response, String message)
79 String okString = request.getParameter("ok");
80 boolean ok = (okString != null && okString.equalsIgnoreCase("true"));
82 * Annoyingly jetty is not adding content to anything other than a few
83 * 20x status codes. Possibly it is closing the PrintWriter.
84 * Find a fix for this!
85 ****************************************************/
86 response.setStatus(ok ? HttpServletResponse.SC_OK : //
87 // HttpServletResponse.SC_BAD_REQUEST //
88 HttpServletResponse.SC_PARTIAL_CONTENT //
91 String endpointName = getPath();
92 Console.error(getAPI().getName() + " error: endpoint " + endpointName
93 + " failed: '" + message + "'");
96 PrintWriter writer = response.getWriter();
97 writer.write("message=Endpoint " + endpointName + ": " + message);
98 } catch (IOException e)
100 Console.info("Exception writing to REST response", e);
104 protected boolean checkParameters(HttpServletRequest request,
105 HttpServletResponse response, int i)
107 String[] parameters = getEndpointPathParameters(request);
109 // check we can run fetchsequence
110 if (parameters.length < i)
112 returnError(request, response,
113 "requires parameters:" + getParameters() + "\n" + getName()
114 + ": " + getDescription());
120 public int[][] parseIntRanges(String rangesString)
122 if (rangesString.equals("*"))
124 return new int[][] { { -1 }, { -1 } };
126 String[] rangeStrings = rangesString.split(",");
127 int[][] ranges = new int[2][rangeStrings.length];
128 for (int i = 0; i < rangeStrings.length; i++)
130 String range = rangeStrings[i];
133 int hyphenpos = range.indexOf('-');
136 ranges[0][i] = Integer.parseInt(range);
137 ranges[1][i] = ranges[0][i];
141 ranges[0][i] = Integer.parseInt(range.substring(0, hyphenpos));
142 ranges[1][i] = Integer.parseInt(range.substring(hyphenpos + 1));
144 } catch (NumberFormatException nfe)
153 * Get all AlignFrames or just one if requested to work on a specific window (fromId query string parameter)
155 protected AlignFrame[] getAlignFrames(HttpServletRequest request,
158 return getAlignFrames(request, "fromId", all);
161 protected AlignFrame[] getAlignFrames(HttpServletRequest request,
162 String idParam, boolean all)
164 String fromIdString = request.getParameter(idParam);
166 if (fromIdString != null)
168 AlignFrame af = AlignFrame.getAlignFrameFromRestId(fromIdString);
169 return af == null ? null : new AlignFrame[] { af };
173 return Desktop.getAlignFrames();
181 protected AlignFrame getAlignFrameFromId(HttpServletRequest request)
183 return getAlignFrameFromId(request, "fromId");
186 protected AlignFrame getAlignFrameFromId(HttpServletRequest request,
189 AlignFrame[] afs = getAlignFrames(request, idParam, false);
190 return (afs == null || afs.length < 1 || afs[0] == null) ? null
194 protected boolean deleteFromCache()