1 package jalview.ws.rest;
3 import java.io.IOException;
4 import java.io.OutputStream;
5 import java.io.OutputStreamWriter;
6 import java.io.PrintWriter;
7 import java.io.StringWriter;
8 import java.io.UnsupportedEncodingException;
9 import java.nio.charset.Charset;
10 import java.util.ArrayList;
11 import java.util.List;
12 import java.util.regex.Matcher;
13 import java.util.regex.Pattern;
15 import org.apache.http.entity.mime.content.ContentBody;
16 import org.apache.http.entity.mime.content.StringBody;
18 import sun.io.CharacterEncoding;
19 import sun.misc.CharacterEncoder;
22 * InputType is the abstract model of each input parameter that a rest service might take.
23 * It enables the engine to validate input by providing
24 * { formatter for type, parser for type }
27 public abstract class InputType {
32 public enum molType { NUC, PROT, MIX}
35 public int max=0; // unbounded
36 protected ArrayList<Class> inputData=new ArrayList<Class>();
38 * initialise the InputType with a list of jalview data classes that the RestJob needs to be able to provide to it.
41 protected InputType(Class[] types)
51 * 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
55 public boolean validFor(RestJob restJob)
57 if (!validFor(restJob.rsd))
59 for (Class cl:inputData)
61 if (!restJob.hasDataOfType(cl))
69 public boolean validFor(RestServiceDescription restServiceDescription)
71 if (!restServiceDescription.inputParams.values().contains(this))
76 protected ContentBody utf8StringBody(String content, String type)
78 Charset utf8 = Charset.forName("UTF-8");
81 return new StringBody(utf8.encode(content).asCharBuffer().toString());
83 return new StringBody(utf8.encode(content).asCharBuffer().toString(), type, utf8);
85 } catch (Exception ex)
87 System.err.println("Couldn't transform string\n"+content+"\nException was :");
88 ex.printStackTrace(System.err);
94 * @param rj data from which input is to be extracted and formatted
95 * @return StringBody or FileBody ready for posting
97 abstract public ContentBody formatForInput(RestJob rj) throws UnsupportedEncodingException,NoValidInputDataException;
100 * @return true if no input data needs to be provided for this parameter
102 public boolean isConstant()
104 return (inputData==null || inputData.size()==0);
107 * return a url encoded version of this parameter's value, or an empty string if the parameter has no ='value' content.
110 public abstract List<String> getURLEncodedParameter();
113 * set the property known as tok, possibly by assigning it with a given val
115 * @param val (may be empty or null)
116 * @param warnings place where parse warnings are reported
117 * @return true if property was set
119 public abstract boolean configureProperty(String tok, String val, StringBuffer warnings);
122 * Get unique key for this type of parameter in a URL encoding.
123 * @return the string that prefixes an input parameter of InputType<T> type in the string returned from getURLEncodedParameter
125 public abstract String getURLtokenPrefix();
127 * parse the given token String and set InputParameter properties appropriately
128 * @param tokenstring - urlencoded parameter string as returned from getURLEncodedParameter
129 * @param warnings - place where any warning messages about bad property values are written
130 * @return true if configuration succeeded, false otherwise.
132 public boolean configureFromURLtokenString(List<String> tokenstring, StringBuffer warnings) {
134 for (String tok:tokenstring)
136 Matcher mtch = Pattern.compile("^([^=]+)=?'?([^']*)?'?").matcher(tok);
139 if (mtch.group(1).equals("min"))
141 min = Integer.parseInt(mtch.group(2));
145 if (mtch.group(1).equals("max"))
147 max = Integer.parseInt(mtch.group(2));
151 catch (NumberFormatException x)
154 warnings.append("Invalid value for parameter "+mtch.group(1).toLowerCase()+" '"+mtch.group(2)+"' (expected an integer)\n");
157 valid = valid && configureProperty(mtch.group(1), mtch.group(2), warnings);
162 public void addBaseParams(ArrayList<String> prms)
164 // todo : check if replaceids should be a global for the service, rather than for a specific parameter.
166 prms.add("min='"+min+"'");
169 prms.add("min='"+max+"'");