more fixes and extension to allow return data types to be specified JAL-715
[jalview.git] / src / jalview / ws / rest / InputType.java
1 package jalview.ws.rest;
2
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
12 import org.apache.http.entity.mime.content.ContentBody;
13 import org.apache.http.entity.mime.content.StringBody;
14
15 import sun.io.CharacterEncoding;
16 import sun.misc.CharacterEncoder;
17
18 /***
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 }
22  *  
23  */
24 public abstract class InputType {
25   /**
26    * not used yet
27    */
28   boolean replaceids;
29   public enum molType { NUC, PROT, MIX}
30   public String token;
31   public int min=1;
32   public int max=0; // unbounded
33   protected ArrayList<Class> inputData=new ArrayList<Class>();
34   /**
35    * initialise the InputType with a list of jalview data classes that the RestJob needs to be able to provide to it. 
36    * @param types
37    */
38   protected InputType(Class[] types)
39   {
40     if(types!=null)
41       {for (Class t:types)
42     {
43       inputData.add(t);
44     }
45       }
46   }
47   /**
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 
49    * @param restJob
50    * @return
51    */
52   public boolean validFor(RestJob restJob)
53   {
54     if (!validFor(restJob.rsd))
55       return false;
56     for (Class cl:inputData)
57     {
58       if (!restJob.hasDataOfType(cl))
59       {
60         return false;
61       }
62     }
63     return true;
64   }
65   
66   public boolean validFor(RestServiceDescription restServiceDescription)
67   {
68     if (!restServiceDescription.inputParams.values().contains(this))
69       return false;
70     
71     return true;
72   }
73   protected ContentBody utf8StringBody(String content, String type)
74   {
75     Charset utf8 = Charset.forName("UTF-8");
76     try {
77     if (type==null ) {
78       return new StringBody(utf8.encode(content).asCharBuffer().toString());
79     } else {
80       return new StringBody(utf8.encode(content).asCharBuffer().toString(), type, utf8);
81     }
82     } catch (Exception ex)
83     {
84       System.err.println("Couldn't transform string\n"+content+"\nException was :");
85       ex.printStackTrace(System.err);
86     }
87     return null;
88   }
89   /**
90    * 
91    * @param rj data from which input is to be extracted and formatted
92    * @return StringBody or FileBody ready for posting
93    */
94   abstract public ContentBody formatForInput(RestJob rj) throws UnsupportedEncodingException,NoValidInputDataException;
95   /**
96    * 
97    * @return true if no input data needs to be provided for this parameter
98    */
99   public boolean isConstant()
100   {
101     return (inputData==null || inputData.size()==0);
102   }
103 }