2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.ws.rest;
23 import jalview.ws.params.ArgumentI;
24 import jalview.ws.params.InvalidArgumentException;
25 import jalview.ws.params.OptionI;
26 import jalview.ws.params.ParameterI;
27 import jalview.ws.params.simple.IntegerParameter;
28 import jalview.ws.params.simple.Option;
30 import java.io.UnsupportedEncodingException;
31 import java.nio.charset.Charset;
32 import java.util.ArrayList;
33 import java.util.Collection;
34 import java.util.List;
35 import java.util.regex.Matcher;
36 import java.util.regex.Pattern;
38 import org.apache.http.entity.mime.content.ContentBody;
39 import org.apache.http.entity.mime.content.StringBody;
42 * InputType is the abstract model of each input parameter that a rest service
43 * might take. It enables the engine to validate input by providing { formatter
44 * for type, parser for type }
47 public abstract class InputType
49 private static final Pattern URL_PATTERN = Pattern
50 .compile("^([^=]+)=?'?([^']*)?'?");
61 public static Collection<String> toStringValues()
63 Collection<String> c = new ArrayList<String>();
64 for (molType type : values())
66 c.add(type.toString());
76 public int max = 0; // unbounded
78 protected ArrayList<Class> inputData = new ArrayList<Class>();
81 * initialise the InputType with a list of jalview data classes that the
82 * RestJob needs to be able to provide to it.
86 protected InputType(Class[] types)
98 * do basic tests to ensure the job's service takes this parameter, and the
99 * job's input data can be used to generate the input data
104 public boolean validFor(RestJob restJob)
106 if (!validFor(restJob.rsd))
108 for (Class cl : inputData)
110 if (!restJob.hasDataOfType(cl))
118 public boolean validFor(RestServiceDescription restServiceDescription)
120 if (!restServiceDescription.inputParams.values().contains(this))
126 protected ContentBody utf8StringBody(String content, String type)
128 Charset utf8 = Charset.forName("UTF-8");
133 return new StringBody(
134 utf8.encode(content).asCharBuffer().toString());
138 return new StringBody(
139 utf8.encode(content).asCharBuffer().toString(), type, utf8);
141 } catch (Exception ex)
143 System.err.println("Couldn't transform string\n" + content
144 + "\nException was :");
145 ex.printStackTrace(System.err);
153 * data from which input is to be extracted and formatted
154 * @return StringBody or FileBody ready for posting
156 abstract public ContentBody formatForInput(RestJob rj)
157 throws UnsupportedEncodingException, NoValidInputDataException;
161 * @return true if no input data needs to be provided for this parameter
163 public boolean isConstant()
165 return (inputData == null || inputData.size() == 0);
169 * return a url encoded version of this parameter's value, or an empty string
170 * if the parameter has no ='value' content.
174 public abstract List<String> getURLEncodedParameter();
177 * set the property known as tok, possibly by assigning it with a given val
181 * (may be empty or null)
183 * place where parse warnings are reported
184 * @return true if property was set
186 public abstract boolean configureProperty(String tok, String val,
187 StringBuffer warnings);
190 * Get unique key for this type of parameter in a URL encoding.
192 * @return the string that prefixes an input parameter of InputType<T> type in
193 * the string returned from getURLEncodedParameter
195 public abstract String getURLtokenPrefix();
198 * parse the given token String and set InputParameter properties
202 * - urlencoded parameter string as returned from
203 * getURLEncodedParameter
205 * - place where any warning messages about bad property values are
207 * @return true if configuration succeeded, false otherwise.
209 public boolean configureFromURLtokenString(List<String> tokenstring,
210 StringBuffer warnings)
212 boolean valid = true;
213 for (String tok : tokenstring)
215 Matcher mtch = URL_PATTERN.matcher(tok);
220 if (mtch.group(1).equals("min"))
222 min = Integer.parseInt(mtch.group(2));
226 else if (mtch.group(1).equals("max"))
228 max = Integer.parseInt(mtch.group(2));
231 } catch (NumberFormatException x)
234 warnings.append("Invalid value for parameter "
235 + mtch.group(1).toLowerCase() + " '" + mtch.group(2)
236 + "' (expected an integer)\n");
239 if (!configureProperty(mtch.group(1), mtch.group(2), warnings))
241 if (warnings.length() == 0)
243 warnings.append("Failed to configure InputType :"
244 + getURLtokenPrefix() + " with property string: '"
245 + mtch.group(0) + "'\n (token is '" + mtch.group(1)
246 + "' and value is '" + mtch.group(2) + "')\n");
255 public void addBaseParams(ArrayList<String> prms)
257 // todo : check if replaceids should be a global for the service, rather
258 // than for a specific parameter.
261 prms.add("min='" + min + "'");
265 prms.add("max='" + max + "'");
269 public abstract List<OptionI> getOptions();
271 public List<OptionI> getBaseOptions()
273 ArrayList<OptionI> opts = new ArrayList<OptionI>();
274 opts.add(new IntegerParameter("min",
275 "Minimum number of data of this type", true, 1, min, 0, -1));
276 opts.add(new IntegerParameter("max",
277 "Maximum number of data of this type", false, 0, max, 0, -1));
282 * make a copy of this InputType
284 * @return may not be needed public abstract InputType copy();
288 * parse a set of configuration options
290 * @param currentSettings
291 * - modified settings originally from getOptions
292 * @throws InvalidArgumentException
293 * thrown if currentSettings contains invalid options for this type.
295 public void configureFromArgumentI(List<ArgumentI> currentSettings)
296 throws InvalidArgumentException
298 ArrayList<String> urltoks = new ArrayList<String>();
300 for (ArgumentI arg : currentSettings)
302 if (arg instanceof ParameterI)
304 rg = arg.getName() + "='" + arg.getValue() + "'";
308 // TODO: revise architecture - this is counter intuitive - options with
309 // different values to their names are actually parameters
310 rg = (arg.getValue().length() > 0)
311 ? (arg.getValue().equals(arg.getName()) ? arg.getName()
312 : arg.getName() + "='" + arg.getValue() + "'")
320 StringBuffer warnings;
321 if (!configureFromURLtokenString(urltoks,
322 warnings = new StringBuffer()))
324 throw new InvalidArgumentException(warnings.toString());
328 protected OptionI createMolTypeOption(String name, String descr,
329 boolean req, molType curType, molType defType)
331 return new Option(name, descr, req,
332 defType == null ? "" : defType.toString(),
333 curType == null ? "" : curType.toString(),
334 molType.toStringValues(), null);