initial implementation of Rest client framework (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 /** Service Input info { a sequence of [ Sequence Id vector (min,max,moltype, separator,regex,colrange(start-end)), Sequence(format-bare or alignment, moltype, min, max, separator)), Alignment(format, moltype),
19  */ 
20 public abstract class InputType {
21   /**
22    * not used yet
23    */
24   boolean replaceids;
25   public enum molType { NUC, PROT, MIX}
26   public String token;
27   public int min=1;
28   public int max=0; // unbounded
29   protected ArrayList<Class> inputData=new ArrayList<Class>();
30   /**
31    * initialise the InputType with a list of jalview data classes that the RestJob needs to be able to provide to it. 
32    * @param types
33    */
34   protected InputType(Class[] types)
35   {
36     if(types!=null)
37       {for (Class t:types)
38     {
39       inputData.add(t);
40     }
41       }
42   }
43   /**
44    * 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 
45    * @param restJob
46    * @return
47    */
48   public boolean validFor(RestJob restJob)
49   {
50     if (!validFor(restJob.rsd))
51       return false;
52     for (Class cl:inputData)
53     {
54       if (!restJob.hasDataOfType(cl))
55       {
56         return false;
57       }
58     }
59     return true;
60   }
61   
62   public boolean validFor(RestServiceDescription restServiceDescription)
63   {
64     if (!restServiceDescription.inputParams.values().contains(this))
65       return false;
66     
67     return true;
68   }
69   protected ContentBody utf8StringBody(String content, String type)
70   {
71     Charset utf8 = Charset.forName("UTF-8");
72     try {
73     if (type==null ) {
74       return new StringBody(utf8.encode(content).asCharBuffer().toString());
75     } else {
76       return new StringBody(utf8.encode(content).asCharBuffer().toString(), type, utf8);
77     }
78     } catch (Exception ex)
79     {
80       System.err.println("Couldn't transform string\n"+content+"\nException was :");
81       ex.printStackTrace(System.err);
82     }
83     return null;
84   }
85   /**
86    * 
87    * @param rj data from which input is to be extracted and formatted
88    * @return StringBody or FileBody ready for posting
89    */
90   abstract public ContentBody formatForInput(RestJob rj) throws UnsupportedEncodingException,NoValidInputDataException;
91   /**
92    * 
93    * @return true if no input data needs to be provided for this parameter
94    */
95   public boolean isConstant()
96   {
97     return (inputData==null || inputData.size()==0);
98   }
99 }