1 package jalview.ws.rest;
3 import jalview.ws.rest.params.SeqGroupIndexVector;
5 import java.io.IOException;
6 import java.io.OutputStream;
7 import java.io.OutputStreamWriter;
8 import java.io.PrintWriter;
9 import java.io.StringWriter;
10 import java.io.UnsupportedEncodingException;
11 import java.nio.charset.Charset;
12 import java.util.ArrayList;
13 import java.util.List;
15 import java.util.regex.Matcher;
16 import java.util.regex.Pattern;
18 import org.apache.http.entity.mime.content.ContentBody;
19 import org.apache.http.entity.mime.content.StringBody;
21 import sun.io.CharacterEncoding;
22 import sun.misc.CharacterEncoder;
25 * InputType is the abstract model of each input parameter that a rest service might take.
26 * It enables the engine to validate input by providing
27 * { formatter for type, parser for type }
30 public abstract class InputType {
35 public enum molType { NUC, PROT, MIX}
38 public int max=0; // unbounded
39 protected ArrayList<Class> inputData=new ArrayList<Class>();
41 * initialise the InputType with a list of jalview data classes that the RestJob needs to be able to provide to it.
44 protected InputType(Class[] types)
54 * do basic tests to ensure the job's service takes this parameter, and the job's input data can be used to generate the input data
58 public boolean validFor(RestJob restJob)
60 if (!validFor(restJob.rsd))
62 for (Class cl:inputData)
64 if (!restJob.hasDataOfType(cl))
72 public boolean validFor(RestServiceDescription restServiceDescription)
74 if (!restServiceDescription.inputParams.values().contains(this))
79 protected ContentBody utf8StringBody(String content, String type)
81 Charset utf8 = Charset.forName("UTF-8");
84 return new StringBody(utf8.encode(content).asCharBuffer().toString());
86 return new StringBody(utf8.encode(content).asCharBuffer().toString(), type, utf8);
88 } catch (Exception ex)
90 System.err.println("Couldn't transform string\n"+content+"\nException was :");
91 ex.printStackTrace(System.err);
97 * @param rj data from which input is to be extracted and formatted
98 * @return StringBody or FileBody ready for posting
100 abstract public ContentBody formatForInput(RestJob rj) throws UnsupportedEncodingException,NoValidInputDataException;
103 * @return true if no input data needs to be provided for this parameter
105 public boolean isConstant()
107 return (inputData==null || inputData.size()==0);
110 * return a url encoded version of this parameter's value, or an empty string if the parameter has no ='value' content.
113 public abstract List<String> getURLEncodedParameter();
116 * set the property known as tok, possibly by assigning it with a given val
118 * @param val (may be empty or null)
119 * @param warnings place where parse warnings are reported
120 * @return true if property was set
122 public abstract boolean configureProperty(String tok, String val, StringBuffer warnings);
125 * Get unique key for this type of parameter in a URL encoding.
126 * @return the string that prefixes an input parameter of InputType<T> type in the string returned from getURLEncodedParameter
128 public abstract String getURLtokenPrefix();
130 * parse the given token String and set InputParameter properties appropriately
131 * @param tokenstring - urlencoded parameter string as returned from getURLEncodedParameter
132 * @param warnings - place where any warning messages about bad property values are written
133 * @return true if configuration succeeded, false otherwise.
135 public boolean configureFromURLtokenString(List<String> tokenstring, StringBuffer warnings) {
137 for (String tok:tokenstring)
139 Matcher mtch = Pattern.compile("^([^=]+)=?'?([^']*)?'?").matcher(tok);
142 if (mtch.group(1).equals("min"))
144 min = Integer.parseInt(mtch.group(2));
148 if (mtch.group(1).equals("max"))
150 max = Integer.parseInt(mtch.group(2));
154 catch (NumberFormatException x)
157 warnings.append("Invalid value for parameter "+mtch.group(1).toLowerCase()+" '"+mtch.group(2)+"' (expected an integer)\n");
160 valid = valid && configureProperty(mtch.group(1), mtch.group(2), warnings);
165 public void addBaseParams(ArrayList<String> prms)
167 // todo : check if replaceids should be a global for the service, rather than for a specific parameter.
169 prms.add("min='"+min+"'");
172 prms.add("min='"+max+"'");