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;
12 import org.apache.http.entity.mime.content.ContentBody;
13 import org.apache.http.entity.mime.content.StringBody;
15 import sun.io.CharacterEncoding;
16 import sun.misc.CharacterEncoder;
19 * InputType is the abstract model of each input parameter that a rest service might take.
20 * It enables the engine to validate input by providing
21 * { formatter for type, parser for type }
24 public abstract class InputType {
29 public enum molType { NUC, PROT, MIX}
32 public int max=0; // unbounded
33 protected ArrayList<Class> inputData=new ArrayList<Class>();
35 * initialise the InputType with a list of jalview data classes that the RestJob needs to be able to provide to it.
38 protected InputType(Class[] types)
48 * 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
52 public boolean validFor(RestJob restJob)
54 if (!validFor(restJob.rsd))
56 for (Class cl:inputData)
58 if (!restJob.hasDataOfType(cl))
66 public boolean validFor(RestServiceDescription restServiceDescription)
68 if (!restServiceDescription.inputParams.values().contains(this))
73 protected ContentBody utf8StringBody(String content, String type)
75 Charset utf8 = Charset.forName("UTF-8");
78 return new StringBody(utf8.encode(content).asCharBuffer().toString());
80 return new StringBody(utf8.encode(content).asCharBuffer().toString(), type, utf8);
82 } catch (Exception ex)
84 System.err.println("Couldn't transform string\n"+content+"\nException was :");
85 ex.printStackTrace(System.err);
91 * @param rj data from which input is to be extracted and formatted
92 * @return StringBody or FileBody ready for posting
94 abstract public ContentBody formatForInput(RestJob rj) throws UnsupportedEncodingException,NoValidInputDataException;
97 * @return true if no input data needs to be provided for this parameter
99 public boolean isConstant()
101 return (inputData==null || inputData.size()==0);