0027057390c4fde89434f92b8afbb2340eefb4f9
[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
9 import java.io.BufferedOutputStream;
10 import java.io.ByteArrayOutputStream;
11 import java.io.File;
12 import java.io.FileOutputStream;
13 import java.io.OutputStreamWriter;
14 import java.io.PrintWriter;
15 import java.io.StringWriter;
16 import java.io.UnsupportedEncodingException;
17 import java.net.URLEncoder;
18 import java.nio.charset.Charset;
19
20 import org.apache.http.entity.mime.content.ContentBody;
21 import org.apache.http.entity.mime.content.FileBody;
22 import org.apache.http.entity.mime.content.StringBody;
23
24 /**
25  * format an alignment for input to rest service.
26  * @author JimP
27  *
28  */
29 public class Alignment extends InputType {
30   public Alignment()
31   {
32     super(new Class[] { AlignmentI.class} );
33   }
34
35   String format="FASTA";
36   molType type;
37   boolean jvsuffix=false;
38   /**
39    * input data as a file upload rather than inline content
40    */
41   public boolean writeAsFile;
42   @Override
43   public ContentBody formatForInput(RestJob rj) throws UnsupportedEncodingException, NoValidInputDataException
44   {
45     AlignmentI alignment = rj.getAlignmentForInput(token,type);
46     if (writeAsFile)
47     {
48     try {
49       File fa = File.createTempFile("jvmime", ".fa");
50       PrintWriter pw = new PrintWriter(new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream(fa)), "UTF-8"));
51       pw.append(new jalview.io.FormatAdapter().formatSequences(format, alignment, jvsuffix));
52       pw.close();
53       return new FileBody(fa, "text/plain");
54     } catch (Exception ex)
55     {
56       throw new NoValidInputDataException("Couldn't write out alignment to file.",ex);
57     }
58     } else {
59       jalview.io.FormatAdapter fa = new jalview.io.FormatAdapter();
60       fa.setNewlineString("\r\n");
61       return new StringBody((fa.formatSequences(format, alignment, jvsuffix)));
62       //,
63       //"text/plain",Charset.forName("UTF-8"));
64       // , "text/plain", Charset.forName("UTF-8"));
65     // sb.getContentTypeParameters().put("filename", "alignment.fa");
66     }
67   }
68 }