JAL-715 - allow rest service attributes to be exported and imported as a | separated...
[jalview.git] / src / jalview / ws / rest / params / SeqVector.java
1 package jalview.ws.rest.params;
2
3 import jalview.datamodel.AlignmentI;
4 import jalview.datamodel.SequenceI;
5 import jalview.ws.rest.InputType;
6 import jalview.ws.rest.NoValidInputDataException;
7 import jalview.ws.rest.RestJob;
8 import jalview.ws.rest.RestServiceDescription;
9 import jalview.ws.rest.InputType.molType;
10
11 import java.io.UnsupportedEncodingException;
12 import java.util.ArrayList;
13 import java.util.List;
14
15 import org.apache.http.entity.mime.content.ContentBody;
16 import org.apache.http.entity.mime.content.StringBody;
17
18 /**
19  * input a list of sequences separated by some separator 
20  * @author JimP
21  *
22  */
23 public class SeqVector extends InputType {
24   String sep;
25   molType type;
26   public SeqVector()
27   {
28     super(new Class[] { AlignmentI.class} );
29   }
30
31   @Override
32   public ContentBody formatForInput(RestJob rj) throws UnsupportedEncodingException, NoValidInputDataException
33   {
34     StringBuffer idvector = new StringBuffer();
35     boolean list=false;
36     for (SequenceI seq:rj.getSequencesForInput(token, type))
37     {
38       if (list)
39       {
40         idvector.append(sep);
41       }
42       idvector.append(seq.getSequence());
43     }
44     return new StringBody(idvector.toString());
45   }
46   @Override
47   public List<String> getURLEncodedParameter()
48   {
49     ArrayList<String> prms = new ArrayList<String>();
50     super.addBaseParams(prms);
51     prms.add("sep='"+ sep+"'");
52     prms.add("type='"+type+"'");
53     return prms;
54   }
55
56   @Override
57   public String getURLtokenPrefix()
58   {
59     return "SEQS";
60   }
61
62   @Override
63   public boolean configureProperty(String tok, String val,
64           StringBuffer warnings)
65   {
66
67     if (tok.startsWith("sep"))
68     {
69       sep=val;
70       return true;
71     }
72     if (tok.startsWith("type"))
73     {
74       try {
75         type=molType.valueOf(val);
76         return true;
77       } catch (Exception x)
78       {
79         warnings.append("Invalid molecule type '"+val+"'. Must be one of (");
80         for (molType v:molType.values())
81         {
82           warnings.append(" "+v);
83         }
84         warnings.append(")\n");
85       }
86     }
87     return false;
88   }
89
90 }