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.List;
34 import java.util.regex.Matcher;
35 import java.util.regex.Pattern;
37 import org.apache.http.entity.mime.content.ContentBody;
38 import org.apache.http.entity.mime.content.StringBody;
41 * InputType is the abstract model of each input parameter that a rest service
42 * might take. It enables the engine to validate input by providing { formatter
43 * for type, parser for type }
46 public abstract class InputType
48 private static final Pattern URL_PATTERN = Pattern
49 .compile("^([^=]+)=?'?([^']*)?'?");
60 public static List<String> toStringValues()
62 List<String> c = new ArrayList<>();
63 for (molType type : values())
65 c.add(type.toString());
75 public int max = 0; // unbounded
77 protected List<Class> inputData = new ArrayList<>();
80 * initialise the InputType with a list of jalview data classes that the
81 * RestJob needs to be able to provide to it.
85 protected InputType(Class[] types)
97 * do basic tests to ensure the job's service takes this parameter, and the
98 * job's input data can be used to generate the input data
103 public boolean validFor(RestJob restJob)
105 if (!validFor(restJob.rsd))
109 for (Class cl : inputData)
111 if (!restJob.hasDataOfType(cl))
119 public boolean validFor(RestServiceDescription restServiceDescription)
121 if (!restServiceDescription.inputParams.values().contains(this))
129 protected ContentBody utf8StringBody(String content, String type)
131 Charset utf8 = Charset.forName("UTF-8");
136 return new StringBody(
137 utf8.encode(content).asCharBuffer().toString());
141 return new StringBody(
142 utf8.encode(content).asCharBuffer().toString(), type, utf8);
144 } catch (Exception ex)
146 System.err.println("Couldn't transform string\n" + content
147 + "\nException was :");
148 ex.printStackTrace(System.err);
156 * data from which input is to be extracted and formatted
157 * @return StringBody or FileBody ready for posting
159 abstract public ContentBody formatForInput(RestJob rj)
160 throws UnsupportedEncodingException, NoValidInputDataException;
164 * @return true if no input data needs to be provided for this parameter
166 public boolean isConstant()
168 return (inputData == null || inputData.size() == 0);
172 * return a url encoded version of this parameter's value, or an empty string
173 * if the parameter has no ='value' content.
177 public abstract List<String> getURLEncodedParameter();
180 * set the property known as tok, possibly by assigning it with a given val
184 * (may be empty or null)
186 * place where parse warnings are reported
187 * @return true if property was set
189 public abstract boolean configureProperty(String tok, String val,
190 StringBuffer warnings);
193 * Get unique key for this type of parameter in a URL encoding.
195 * @return the string that prefixes an input parameter of InputType<T> type in
196 * the string returned from getURLEncodedParameter
198 public abstract String getURLtokenPrefix();
201 * parse the given token String and set InputParameter properties
205 * - urlencoded parameter string as returned from
206 * getURLEncodedParameter
208 * - place where any warning messages about bad property values are
210 * @return true if configuration succeeded, false otherwise.
212 public boolean configureFromURLtokenString(List<String> tokenstring,
213 StringBuffer warnings)
215 boolean valid = true;
216 for (String tok : tokenstring)
218 Matcher mtch = URL_PATTERN.matcher(tok);
223 if (mtch.group(1).equals("min"))
225 min = Integer.parseInt(mtch.group(2));
229 else if (mtch.group(1).equals("max"))
231 max = Integer.parseInt(mtch.group(2));
234 } catch (NumberFormatException x)
237 warnings.append("Invalid value for parameter "
238 + mtch.group(1).toLowerCase() + " '" + mtch.group(2)
239 + "' (expected an integer)\n");
242 if (!configureProperty(mtch.group(1), mtch.group(2), warnings))
244 if (warnings.length() == 0)
246 warnings.append("Failed to configure InputType :"
247 + getURLtokenPrefix() + " with property string: '"
248 + mtch.group(0) + "'\n (token is '" + mtch.group(1)
249 + "' and value is '" + mtch.group(2) + "')\n");
258 public void addBaseParams(ArrayList<String> prms)
260 // todo : check if replaceids should be a global for the service, rather
261 // than for a specific parameter.
264 prms.add("min='" + min + "'");
268 prms.add("max='" + max + "'");
272 public abstract List<OptionI> getOptions();
274 public List<OptionI> getBaseOptions()
276 ArrayList<OptionI> opts = new ArrayList<>();
277 opts.add(new IntegerParameter("min",
278 "Minimum number of data of this type", true, 1, min, 0, -1));
279 opts.add(new IntegerParameter("max",
280 "Maximum number of data of this type", false, 0, max, 0, -1));
285 * make a copy of this InputType
287 * @return may not be needed public abstract InputType copy();
291 * parse a set of configuration options
293 * @param currentSettings
294 * - modified settings originally from getOptions
295 * @throws InvalidArgumentException
296 * thrown if currentSettings contains invalid options for this type.
298 public void configureFromArgumentI(List<ArgumentI> currentSettings)
299 throws InvalidArgumentException
301 List<String> urltoks = new ArrayList<>();
303 for (ArgumentI arg : currentSettings)
305 if (arg instanceof ParameterI)
307 rg = arg.getName() + "='" + arg.getValue() + "'";
311 // TODO: revise architecture - this is counter intuitive - options with
312 // different values to their names are actually parameters
313 rg = (arg.getValue().length() > 0)
314 ? (arg.getValue().equals(arg.getName()) ? arg.getName()
315 : arg.getName() + "='" + arg.getValue() + "'")
323 StringBuffer warnings;
324 if (!configureFromURLtokenString(urltoks,
325 warnings = new StringBuffer()))
327 throw new InvalidArgumentException(warnings.toString());
331 protected OptionI createMolTypeOption(String name, String descr,
332 boolean req, molType curType, molType defType)
334 return new Option(name, descr, req,
335 defType == null ? "" : defType.toString(),
336 curType == null ? "" : curType.toString(),
337 molType.toStringValues(), null);