2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 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
58 public static Collection<String> toStringValues()
60 Collection<String> c = new ArrayList<String>();
61 for (molType type : values())
63 c.add(type.toString());
73 public int max = 0; // unbounded
75 protected ArrayList<Class> inputData = new ArrayList<Class>();
78 * initialise the InputType with a list of jalview data classes that the
79 * RestJob needs to be able to provide to it.
83 protected InputType(Class[] types)
95 * do basic tests to ensure the job's service takes this parameter, and the
96 * job's input data can be used to generate the input data
101 public boolean validFor(RestJob restJob)
103 if (!validFor(restJob.rsd))
105 for (Class cl : inputData)
107 if (!restJob.hasDataOfType(cl))
115 public boolean validFor(RestServiceDescription restServiceDescription)
117 if (!restServiceDescription.inputParams.values().contains(this))
123 protected ContentBody utf8StringBody(String content, String type)
125 Charset utf8 = Charset.forName("UTF-8");
130 return new StringBody(utf8.encode(content).asCharBuffer()
135 return new StringBody(utf8.encode(content).asCharBuffer()
136 .toString(), type, utf8);
138 } catch (Exception ex)
140 System.err.println("Couldn't transform string\n" + content
141 + "\nException was :");
142 ex.printStackTrace(System.err);
150 * data from which input is to be extracted and formatted
151 * @return StringBody or FileBody ready for posting
153 abstract public ContentBody formatForInput(RestJob rj)
154 throws UnsupportedEncodingException, NoValidInputDataException;
158 * @return true if no input data needs to be provided for this parameter
160 public boolean isConstant()
162 return (inputData == null || inputData.size() == 0);
166 * return a url encoded version of this parameter's value, or an empty string
167 * if the parameter has no ='value' content.
171 public abstract List<String> getURLEncodedParameter();
174 * set the property known as tok, possibly by assigning it with a given val
178 * (may be empty or null)
180 * place where parse warnings are reported
181 * @return true if property was set
183 public abstract boolean configureProperty(String tok, String val,
184 StringBuffer warnings);
187 * Get unique key for this type of parameter in a URL encoding.
189 * @return the string that prefixes an input parameter of InputType<T> type in
190 * the string returned from getURLEncodedParameter
192 public abstract String getURLtokenPrefix();
195 * parse the given token String and set InputParameter properties
199 * - urlencoded parameter string as returned from
200 * getURLEncodedParameter
202 * - place where any warning messages about bad property values are
204 * @return true if configuration succeeded, false otherwise.
206 public boolean configureFromURLtokenString(List<String> tokenstring,
207 StringBuffer warnings)
209 boolean valid = true;
210 for (String tok : tokenstring)
212 Matcher mtch = Pattern.compile("^([^=]+)=?'?([^']*)?'?").matcher(tok);
217 if (mtch.group(1).equals("min"))
219 min = Integer.parseInt(mtch.group(2));
223 else if (mtch.group(1).equals("max"))
225 max = Integer.parseInt(mtch.group(2));
228 } catch (NumberFormatException x)
231 warnings.append("Invalid value for parameter "
232 + mtch.group(1).toLowerCase() + " '" + mtch.group(2)
233 + "' (expected an integer)\n");
236 if (!configureProperty(mtch.group(1), mtch.group(2), warnings))
238 if (warnings.length() == 0)
240 warnings.append("Failed to configure InputType :"
241 + getURLtokenPrefix() + " with property string: '"
242 + mtch.group(0) + "'\n (token is '" + mtch.group(1)
243 + "' and value is '" + mtch.group(2) + "')\n");
252 public void addBaseParams(ArrayList<String> prms)
254 // todo : check if replaceids should be a global for the service, rather
255 // than for a specific parameter.
258 prms.add("min='" + min + "'");
262 prms.add("max='" + max + "'");
266 public abstract List<OptionI> getOptions();
268 public List<OptionI> getBaseOptions()
270 ArrayList<OptionI> opts = new ArrayList<OptionI>();
271 opts.add(new IntegerParameter("min",
272 "Minimum number of data of this type", true, 1, min, 0, -1));
273 opts.add(new IntegerParameter("max",
274 "Maximum number of data of this type", false, 0, max, 0, -1));
279 * make a copy of this InputType
281 * @return may not be needed public abstract InputType copy();
285 * parse a set of configuration options
287 * @param currentSettings
288 * - modified settings originally from getOptions
289 * @throws InvalidArgumentException
290 * thrown if currentSettings contains invalid options for this type.
292 public void configureFromArgumentI(List<ArgumentI> currentSettings)
293 throws InvalidArgumentException
295 ArrayList<String> urltoks = new ArrayList<String>();
297 for (ArgumentI arg : currentSettings)
299 if (arg instanceof ParameterI)
301 rg = arg.getName() + "='" + arg.getValue() + "'";
305 // TODO: revise architecture - this is counter intuitive - options with
306 // different values to their names are actually parameters
307 rg = (arg.getValue().length() > 0) ? (arg.getValue().equals(
308 arg.getName()) ? arg.getName() : arg.getName() + "='"
309 + arg.getValue() + "'") : arg.getName();
316 StringBuffer warnings;
317 if (!configureFromURLtokenString(urltoks, warnings = new StringBuffer()))
319 throw new InvalidArgumentException(warnings.toString());
323 protected OptionI createMolTypeOption(String name, String descr,
324 boolean req, molType curType, molType defType)
326 return new Option(name, descr, req, defType == null ? ""
327 : defType.toString(),
328 curType == null ? "" : curType.toString(),
329 molType.toStringValues(), null);