1 package jalview.ws.rest.params;
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;
10 import java.io.BufferedOutputStream;
11 import java.io.ByteArrayOutputStream;
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;
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;
28 * format an alignment for input to rest service.
33 public class Alignment extends InputType
38 { AlignmentI.class });
41 String format = "FASTA";
45 boolean jvsuffix = false;
48 * input data as a file upload rather than inline content
50 public boolean writeAsFile;
53 public ContentBody formatForInput(RestJob rj)
54 throws UnsupportedEncodingException, NoValidInputDataException
56 AlignmentI alignment = rj.getAlignmentForInput(token, type);
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));
68 return new FileBody(fa, "text/plain");
69 } catch (Exception ex)
71 throw new NoValidInputDataException(
72 "Couldn't write out alignment to file.", ex);
77 jalview.io.FormatAdapter fa = new jalview.io.FormatAdapter();
78 fa.setNewlineString("\r\n");
79 return new StringBody(
80 (fa.formatSequences(format, alignment, jvsuffix)));
82 // "text/plain",Charset.forName("UTF-8"));
83 // , "text/plain", Charset.forName("UTF-8"));
84 // sb.getContentTypeParameters().put("filename", "alignment.fa");
89 public List<String> getURLEncodedParameter()
91 ArrayList<String> prms = new ArrayList<String>();
92 prms.add("format='" + format + "'");
95 prms.add("type='" + type.toString() + "'");
104 prms.add("writeasfile");
111 public String getURLtokenPrefix()
117 public boolean configureProperty(String tok, String val,
118 StringBuffer warnings)
120 if (tok.startsWith("jvsuffix"))
125 if (tok.startsWith("writeasfile"))
131 if (tok.startsWith("format"))
133 for (String fmt : jalview.io.FormatAdapter.WRITEABLE_FORMATS)
135 if (val.equalsIgnoreCase(fmt))
141 warnings.append("Invalid alignment format '" + val
142 + "'. Must be one of (");
143 for (String fmt : jalview.io.FormatAdapter.WRITEABLE_FORMATS)
145 warnings.append(" " + fmt);
147 warnings.append(")\n");
149 if (tok.startsWith("type"))
153 type = molType.valueOf(val);
155 } catch (Exception x)
157 warnings.append("Invalid molecule type '" + val
158 + "'. Must be one of (");
159 for (molType v : molType.values())
161 warnings.append(" " + v);
163 warnings.append(")\n");