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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
19 package jalview.ws.rest;
21 import jalview.ws.params.ArgumentI;
22 import jalview.ws.params.InvalidArgumentException;
23 import jalview.ws.params.OptionI;
24 import jalview.ws.params.ParameterI;
25 import jalview.ws.params.simple.IntegerParameter;
26 import jalview.ws.params.simple.Option;
28 import java.io.UnsupportedEncodingException;
29 import java.nio.charset.Charset;
30 import java.util.ArrayList;
31 import java.util.Collection;
32 import java.util.List;
33 import java.util.regex.Matcher;
34 import java.util.regex.Pattern;
36 import org.apache.http.entity.mime.content.ContentBody;
37 import org.apache.http.entity.mime.content.StringBody;
40 * InputType is the abstract model of each input parameter that a rest service
41 * might take. It enables the engine to validate input by providing { formatter
42 * for type, parser for type }
45 public abstract class InputType
56 public static Collection<String> toStringValues()
58 Collection<String> c = new ArrayList<String>();
59 for (molType type : values())
61 c.add(type.toString());
71 public int max = 0; // unbounded
73 protected ArrayList<Class> inputData = new ArrayList<Class>();
76 * initialise the InputType with a list of jalview data classes that the
77 * RestJob needs to be able to provide to it.
81 protected InputType(Class[] types)
93 * do basic tests to ensure the job's service takes this parameter, and the
94 * job's input data can be used to generate the input data
99 public boolean validFor(RestJob restJob)
101 if (!validFor(restJob.rsd))
103 for (Class cl : inputData)
105 if (!restJob.hasDataOfType(cl))
113 public boolean validFor(RestServiceDescription restServiceDescription)
115 if (!restServiceDescription.inputParams.values().contains(this))
121 protected ContentBody utf8StringBody(String content, String type)
123 Charset utf8 = Charset.forName("UTF-8");
128 return new StringBody(utf8.encode(content).asCharBuffer()
133 return new StringBody(utf8.encode(content).asCharBuffer()
134 .toString(), type, utf8);
136 } catch (Exception ex)
138 System.err.println("Couldn't transform string\n" + content
139 + "\nException was :");
140 ex.printStackTrace(System.err);
148 * data from which input is to be extracted and formatted
149 * @return StringBody or FileBody ready for posting
151 abstract public ContentBody formatForInput(RestJob rj)
152 throws UnsupportedEncodingException, NoValidInputDataException;
156 * @return true if no input data needs to be provided for this parameter
158 public boolean isConstant()
160 return (inputData == null || inputData.size() == 0);
164 * return a url encoded version of this parameter's value, or an empty string
165 * if the parameter has no ='value' content.
169 public abstract List<String> getURLEncodedParameter();
172 * set the property known as tok, possibly by assigning it with a given val
176 * (may be empty or null)
178 * place where parse warnings are reported
179 * @return true if property was set
181 public abstract boolean configureProperty(String tok, String val,
182 StringBuffer warnings);
185 * Get unique key for this type of parameter in a URL encoding.
187 * @return the string that prefixes an input parameter of InputType<T> type in
188 * the string returned from getURLEncodedParameter
190 public abstract String getURLtokenPrefix();
193 * parse the given token String and set InputParameter properties
197 * - urlencoded parameter string as returned from
198 * getURLEncodedParameter
200 * - place where any warning messages about bad property values are
202 * @return true if configuration succeeded, false otherwise.
204 public boolean configureFromURLtokenString(List<String> tokenstring,
205 StringBuffer warnings)
207 boolean valid = true;
208 for (String tok : tokenstring)
210 Matcher mtch = Pattern.compile("^([^=]+)=?'?([^']*)?'?").matcher(tok);
215 if (mtch.group(1).equals("min"))
217 min = Integer.parseInt(mtch.group(2));
221 else if (mtch.group(1).equals("max"))
223 max = Integer.parseInt(mtch.group(2));
226 } catch (NumberFormatException x)
229 warnings.append("Invalid value for parameter "
230 + mtch.group(1).toLowerCase() + " '" + mtch.group(2)
231 + "' (expected an integer)\n");
234 if (!configureProperty(mtch.group(1), mtch.group(2), warnings))
236 if (warnings.length() == 0)
238 warnings.append("Failed to configure InputType :"
239 + getURLtokenPrefix() + " with property string: '"
240 + mtch.group(0) + "'\n (token is '" + mtch.group(1)
241 + "' and value is '" + mtch.group(2) + "')\n");
250 public void addBaseParams(ArrayList<String> prms)
252 // todo : check if replaceids should be a global for the service, rather
253 // than for a specific parameter.
256 prms.add("min='" + min + "'");
260 prms.add("max='" + max + "'");
264 public abstract List<OptionI> getOptions();
266 public List<OptionI> getBaseOptions()
268 ArrayList<OptionI> opts = new ArrayList<OptionI>();
269 opts.add(new IntegerParameter("min",
270 "Minimum number of data of this type", true, 1, min, 0, -1));
271 opts.add(new IntegerParameter("max",
272 "Maximum number of data of this type", false, 0, max, 0, -1));
277 * make a copy of this InputType
279 * @return may not be needed public abstract InputType copy();
283 * parse a set of configuration options
285 * @param currentSettings
286 * - modified settings originally from getOptions
287 * @throws InvalidArgumentException
288 * thrown if currentSettings contains invalid options for this type.
290 public void configureFromArgumentI(List<ArgumentI> currentSettings)
291 throws InvalidArgumentException
293 ArrayList<String> urltoks = new ArrayList<String>();
295 for (ArgumentI arg : currentSettings)
297 if (arg instanceof ParameterI)
299 rg = arg.getName() + "='" + arg.getValue() + "'";
303 // TODO: revise architecture - this is counter intuitive - options with
304 // different values to their names are actually parameters
305 rg = (arg.getValue().length() > 0) ? (arg.getValue().equals(
306 arg.getName()) ? arg.getName() : arg.getName() + "='"
307 + arg.getValue() + "'") : arg.getName();
314 StringBuffer warnings;
315 if (!configureFromURLtokenString(urltoks, warnings = new StringBuffer()))
317 throw new InvalidArgumentException(warnings.toString());
321 protected OptionI createMolTypeOption(String name, String descr,
322 boolean req, molType curType, molType defType)
324 return new Option(name, descr, req, defType == null ? ""
325 : defType.toString(),
326 curType == null ? "" : curType.toString(),
327 molType.toStringValues(), null);