mime types and handler
[jalview.git] / src / jalview / ws / rest / HttpResultSet.java
1 package jalview.ws.rest;
2
3 import jalview.bin.Cache;
4 import jalview.io.FileParse;
5 import jalview.ws.io.mime.JalviewMimeContentHandler;
6
7 import java.io.BufferedReader;
8 import java.io.IOException;
9 import java.io.InputStreamReader;
10 import java.io.UnsupportedEncodingException;
11
12 import org.apache.http.HttpEntity;
13 import org.apache.http.HttpResponse;
14 import org.apache.http.client.methods.HttpRequestBase;
15 import org.apache.http.entity.mime.MultipartEntity;
16 import org.apache.james.mime4j.MimeException;
17 import org.apache.james.mime4j.parser.ContentHandler;
18 import org.apache.james.mime4j.parser.MimeStreamParser;
19
20 /**
21  * data source instantiated from the response of an httpclient request.
22  * 
23  * @author JimP
24  * 
25  */
26
27 public class HttpResultSet extends FileParse
28 {
29
30   private HttpRequestBase cachedRequest;
31
32   /**
33    * when set, indicates that en can be recreated by repeating the HttpRequest
34    * in cachedRequest
35    */
36   boolean repeatable = false;
37
38   /**
39    * response that is to be parsed as jalview input data
40    */
41   private HttpEntity en = null;
42
43   /**
44    * (sub)job that produced this result set.
45    */
46   private RestJob restJob;
47
48   public HttpResultSet(RestJob rj, HttpResponse con, HttpRequestBase req)
49           throws IOException
50   {
51     super();
52     setDataName(rj.getJobId() + " Part " + rj.getJobnum());
53     restJob = rj;
54     cachedRequest = req;
55     initDataSource(con);
56   }
57
58   private void initDataSource(HttpResponse con) throws IOException
59   {
60     en = con.getEntity();
61     repeatable = en.isRepeatable();
62
63     if (en instanceof MultipartEntity)
64     {
65       MultipartEntity mpe = (MultipartEntity) en;
66       // multipart
67       jalview.io.packed.JalviewDataset ds = restJob.newJalviewDataset();
68       ContentHandler handler = new JalviewMimeContentHandler(ds);
69       MimeStreamParser parser = new MimeStreamParser();
70       parser.setContentHandler(handler);
71       try
72       {
73         parser.parse(mpe.getContent());
74       } catch (MimeException me)
75       {
76         error = true;
77         errormessage = "Couldn't parse message from web service.";
78         Cache.log.warn("Failed to parse MIME multipart content", me);
79         en.consumeContent();
80       }
81     }
82     else
83     {
84       // assume content is simple text stream that can be read from
85       String enc = (en.getContentEncoding()==null) ? null : en.getContentEncoding().getValue();
86       if (en.getContentType()!=null) {
87         Cache.log.info("Result Type: " + en.getContentType().toString());
88       } else {
89         Cache.log.info("No Result Type Specified.");
90       }
91       if (enc == null || enc.length() < 1)
92       {
93         Cache.log.debug("Assuming 'Default' Result Encoding.");
94       }
95       else
96       {
97         Cache.log.debug("Result Encoded as : "+enc);
98       }
99       // attempt to identify file and construct an appropriate DataSource
100       // identifier for it.
101       // try to parse
102       // Mime-Multipart or single content type will be expected.
103       // if (enc.equals(org.apache.http.client.utils.)))
104       InputStreamReader br = null;
105       try
106       {
107         br = (enc != null) ? new InputStreamReader(en.getContent(), enc)
108                 : new InputStreamReader(en.getContent());
109       } catch (UnsupportedEncodingException e)
110       {
111         Cache.log.error("Can't handle encoding '" + enc
112                 + "' for response from webservice.", e);
113         en.consumeContent();
114         error = true;
115         errormessage = "Can't handle encoding for response from webservice";
116         return;
117       }
118       if (br != null)
119       {
120         dataIn = new BufferedReader(br);
121         error=false;
122       }
123     }
124   }
125
126   @Override
127   protected void finalize() throws Throwable
128   {
129     dataIn = null;
130     cachedRequest = null;
131     try
132     {
133       if (en != null)
134       {
135         en.consumeContent();
136       }
137     } catch (Exception e)
138     {
139     } catch (Error ex)
140     {
141     }
142     super.finalize();
143   }
144
145 }