JAL-715 - allow rest service attributes to be exported and imported as a | separated...
[jalview.git] / src / jalview / ws / rest / params / Alignment.java
1 package jalview.ws.rest.params;
2
3 import jalview.datamodel.AlignmentI;
4 import jalview.ws.rest.InputType;
5 import jalview.ws.rest.NoValidInputDataException;
6 import jalview.ws.rest.RestJob;
7 import jalview.ws.rest.InputType.molType;
8 import jalview.ws.rest.RestServiceDescription;
9
10 import java.io.BufferedOutputStream;
11 import java.io.ByteArrayOutputStream;
12 import java.io.File;
13 import java.io.FileOutputStream;
14 import java.io.OutputStreamWriter;
15 import java.io.PrintWriter;
16 import java.io.StringWriter;
17 import java.io.UnsupportedEncodingException;
18 import java.net.URLEncoder;
19 import java.nio.charset.Charset;
20 import java.util.ArrayList;
21 import java.util.List;
22
23 import org.apache.http.entity.mime.content.ContentBody;
24 import org.apache.http.entity.mime.content.FileBody;
25 import org.apache.http.entity.mime.content.StringBody;
26
27 /**
28  * format an alignment for input to rest service.
29  * 
30  * @author JimP
31  * 
32  */
33 public class Alignment extends InputType
34 {
35   public Alignment()
36   {
37     super(new Class[]
38     { AlignmentI.class });
39   }
40
41   String format = "FASTA";
42
43   molType type;
44
45   boolean jvsuffix = false;
46
47   /**
48    * input data as a file upload rather than inline content
49    */
50   public boolean writeAsFile;
51
52   @Override
53   public ContentBody formatForInput(RestJob rj)
54           throws UnsupportedEncodingException, NoValidInputDataException
55   {
56     AlignmentI alignment = rj.getAlignmentForInput(token, type);
57     if (writeAsFile)
58     {
59       try
60       {
61         File fa = File.createTempFile("jvmime", ".fa");
62         PrintWriter pw = new PrintWriter(
63                 new OutputStreamWriter(new BufferedOutputStream(
64                         new FileOutputStream(fa)), "UTF-8"));
65         pw.append(new jalview.io.FormatAdapter().formatSequences(format,
66                 alignment, jvsuffix));
67         pw.close();
68         return new FileBody(fa, "text/plain");
69       } catch (Exception ex)
70       {
71         throw new NoValidInputDataException(
72                 "Couldn't write out alignment to file.", ex);
73       }
74     }
75     else
76     {
77       jalview.io.FormatAdapter fa = new jalview.io.FormatAdapter();
78       fa.setNewlineString("\r\n");
79       return new StringBody(
80               (fa.formatSequences(format, alignment, jvsuffix)));
81       // ,
82       // "text/plain",Charset.forName("UTF-8"));
83       // , "text/plain", Charset.forName("UTF-8"));
84       // sb.getContentTypeParameters().put("filename", "alignment.fa");
85     }
86   }
87
88   @Override
89   public List<String> getURLEncodedParameter()
90   {
91     ArrayList<String> prms = new ArrayList<String>();
92     prms.add("format='" + format + "'");
93     if (type != null)
94     {
95       prms.add("type='" + type.toString() + "'");
96     }
97     if (jvsuffix)
98     {
99       prms.add("jvsuffix");
100     }
101     ;
102     if (writeAsFile)
103     {
104       prms.add("writeasfile");
105     }
106     ;
107     return prms;
108   }
109
110   @Override
111   public String getURLtokenPrefix()
112   {
113     return "ALIGNMENT";
114   }
115
116   @Override
117   public boolean configureProperty(String tok, String val,
118           StringBuffer warnings)
119   {
120     if (tok.startsWith("jvsuffix"))
121     {
122       jvsuffix = true;
123       return true;
124     }
125     if (tok.startsWith("writeasfile"))
126     {
127       writeAsFile = true;
128       return true;
129     }
130
131     if (tok.startsWith("format"))
132     {
133       for (String fmt : jalview.io.FormatAdapter.WRITEABLE_FORMATS)
134       {
135         if (val.equalsIgnoreCase(fmt))
136         {
137           format = fmt;
138           return true;
139         }
140       }
141       warnings.append("Invalid alignment format '" + val
142               + "'. Must be one of (");
143       for (String fmt : jalview.io.FormatAdapter.WRITEABLE_FORMATS)
144       {
145         warnings.append(" " + fmt);
146       }
147       warnings.append(")\n");
148     }
149     if (tok.startsWith("type"))
150     {
151       try
152       {
153         type = molType.valueOf(val);
154         return true;
155       } catch (Exception x)
156       {
157         warnings.append("Invalid molecule type '" + val
158                 + "'. Must be one of (");
159         for (molType v : molType.values())
160         {
161           warnings.append(" " + v);
162         }
163         warnings.append(")\n");
164       }
165     }
166     return false;
167   }
168 }