JAL-3982 highlight any 3D structure mappings for positions mapped via CDS/transcript...
[jalview.git] / src / jalview / rest / AbstractEndpoint.java
1 package jalview.rest;
2
3 import java.io.IOException;
4 import java.io.PrintWriter;
5
6 import javax.servlet.http.HttpServletRequest;
7 import javax.servlet.http.HttpServletResponse;
8
9 import jalview.bin.Console;
10 import jalview.gui.AlignFrame;
11 import jalview.gui.Desktop;
12 import jalview.rest.RestHandler.EndpointI;
13
14 public abstract class AbstractEndpoint implements EndpointI
15 {
16   private final String path;
17
18   private API api;
19
20   private final String name;
21
22   private final String parameters;
23
24   private final String description;
25
26   public AbstractEndpoint(API api, String path, String name,
27           String parameters, String description)
28   {
29     this.api = api;
30     this.path = path;
31     this.name = name;
32     this.parameters = parameters;
33     this.description = description;
34   }
35
36   public String getPath()
37   {
38     return this.path;
39   }
40
41   protected API getAPI()
42   {
43     return this.api;
44   }
45
46   public String getName()
47   {
48     return this.name;
49   }
50
51   public String getParameters()
52   {
53     return this.parameters;
54   }
55
56   public String getDescription()
57   {
58     return this.description;
59   }
60
61   public abstract void processEndpoint(HttpServletRequest request,
62           HttpServletResponse response);
63
64   /*
65    * Shared methods below here
66    */
67
68   protected String[] getEndpointPathParameters(HttpServletRequest request)
69   {
70     String pathInfo = request.getPathInfo();
71     int slashpos = pathInfo.indexOf('/', 1);
72     return slashpos < 1 ? null
73             : pathInfo.substring(slashpos + 1).split("/");
74   }
75
76   protected void returnError(HttpServletRequest request,
77           HttpServletResponse response, String message)
78   {
79     String okString = request.getParameter("ok");
80     boolean ok = (okString != null && okString.equalsIgnoreCase("true"));
81     /*
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 //
89     );
90
91     String endpointName = getPath();
92     Console.error(getAPI().getName() + " error: endpoint " + endpointName
93             + " failed: '" + message + "'");
94     try
95     {
96       PrintWriter writer = response.getWriter();
97       writer.write("message=Endpoint " + endpointName + ": " + message);
98     } catch (IOException e)
99     {
100       Console.info("Exception writing to REST response", e);
101     }
102   }
103
104   protected boolean checkParameters(HttpServletRequest request,
105           HttpServletResponse response, int i)
106   {
107     String[] parameters = getEndpointPathParameters(request);
108
109     // check we can run fetchsequence
110     if (parameters.length < i)
111     {
112       returnError(request, response,
113               "requires parameters:" + getParameters() + "\n" + getName()
114                       + ": " + getDescription());
115       return false;
116     }
117     return true;
118   }
119
120   public int[][] parseIntRanges(String rangesString)
121   {
122     if (rangesString.equals("*"))
123     {
124       return new int[][] { { -1 }, { -1 } };
125     }
126     String[] rangeStrings = rangesString.split(",");
127     int[][] ranges = new int[2][rangeStrings.length];
128     for (int i = 0; i < rangeStrings.length; i++)
129     {
130       String range = rangeStrings[i];
131       try
132       {
133         int hyphenpos = range.indexOf('-');
134         if (hyphenpos < 0)
135         {
136           ranges[0][i] = Integer.parseInt(range);
137           ranges[1][i] = ranges[0][i];
138         }
139         else
140         {
141           ranges[0][i] = Integer.parseInt(range.substring(0, hyphenpos));
142           ranges[1][i] = Integer.parseInt(range.substring(hyphenpos + 1));
143         }
144       } catch (NumberFormatException nfe)
145       {
146         return null;
147       }
148     }
149     return ranges;
150   }
151
152   /*
153    * Get all AlignFrames or just one if requested to work on a specific window (fromId query string parameter)
154    */
155   protected AlignFrame[] getAlignFrames(HttpServletRequest request,
156           boolean all)
157   {
158     return getAlignFrames(request, "fromId", all);
159   }
160
161   protected AlignFrame[] getAlignFrames(HttpServletRequest request,
162           String idParam, boolean all)
163   {
164     String fromIdString = request.getParameter(idParam);
165
166     if (fromIdString != null)
167     {
168       AlignFrame af = AlignFrame.getAlignFrameFromRestId(fromIdString);
169       return af == null ? null : new AlignFrame[] { af };
170     }
171     else if (all)
172     {
173       return Desktop.getAlignFrames();
174     }
175     else
176     {
177       return null;
178     }
179   }
180
181   protected AlignFrame getAlignFrameFromId(HttpServletRequest request)
182   {
183     return getAlignFrameFromId(request, "fromId");
184   }
185
186   protected AlignFrame getAlignFrameFromId(HttpServletRequest request,
187           String idParam)
188   {
189     AlignFrame[] afs = getAlignFrames(request, idParam, false);
190     return (afs == null || afs.length < 1 || afs[0] == null) ? null
191             : afs[0];
192   }
193
194   protected boolean deleteFromCache()
195   {
196     return false;
197   }
198
199 }