1 package jalview.ws.rest;
3 import java.io.BufferedInputStream;
4 import java.io.BufferedReader;
5 import java.io.IOException;
6 import java.io.InputStreamReader;
7 import java.io.UnsupportedEncodingException;
9 import org.apache.http.HttpEntity;
10 import org.apache.http.HttpResponse;
11 import org.apache.http.client.methods.HttpRequestBase;
12 import org.apache.http.entity.mime.MultipartEntity;
13 import org.apache.james.mime4j.MimeException;
14 import org.apache.james.mime4j.parser.ContentHandler;
15 import org.apache.james.mime4j.parser.MimeStreamParser;
17 import jalview.bin.Cache;
18 import jalview.io.FileParse;
19 import jalview.io.mime.JalviewMimeContentHandler;
22 * data source instantiated from the response of an httpclient request.
28 public class HttpResultSet extends FileParse
31 private HttpRequestBase cachedRequest;
34 * when set, indicates that en can be recreated by repeating the HttpRequest
37 boolean repeatable = false;
40 * response that is to be parsed as jalview input data
42 private HttpEntity en = null;
45 * (sub)job that produced this result set.
47 private RestJob restJob;
49 public HttpResultSet(RestJob rj, HttpResponse con, HttpRequestBase req)
53 setDataName(rj.getJobId() + " Part " + rj.getJobnum());
59 private void initDataSource(HttpResponse con) throws IOException
62 repeatable = en.isRepeatable();
64 if (en instanceof MultipartEntity)
66 MultipartEntity mpe = (MultipartEntity) en;
68 jalview.io.packed.JalviewDataset ds = restJob.newJalviewDataset();
69 ContentHandler handler = new JalviewMimeContentHandler(ds);
70 MimeStreamParser parser = new MimeStreamParser();
71 parser.setContentHandler(handler);
74 parser.parse(mpe.getContent());
75 } catch (MimeException me)
78 errormessage = "Couldn't parse message from web service.";
79 Cache.log.warn("Failed to parse MIME multipart content", me);
85 // assume content is simple text stream that can be read from
86 String enc = (en.getContentEncoding()==null) ? null : en.getContentEncoding().getValue();
87 if (en.getContentType()!=null) {
88 Cache.log.info("Result Type: " + en.getContentType().toString());
90 Cache.log.info("No Result Type Specified.");
92 if (enc == null || enc.length() < 1)
94 Cache.log.debug("Assuming 'Default' Result Encoding.");
98 Cache.log.debug("Result Encoded as : "+enc);
100 // attempt to identify file and construct an appropriate DataSource
101 // identifier for it.
103 // Mime-Multipart or single content type will be expected.
104 // if (enc.equals(org.apache.http.client.utils.)))
105 InputStreamReader br = null;
108 br = (enc != null) ? new InputStreamReader(en.getContent(), enc)
109 : new InputStreamReader(en.getContent());
110 } catch (UnsupportedEncodingException e)
112 Cache.log.error("Can't handle encoding '" + enc
113 + "' for response from webservice.", e);
116 errormessage = "Can't handle encoding for response from webservice";
121 dataIn = new BufferedReader(br);
128 protected void finalize() throws Throwable
131 cachedRequest = null;
138 } catch (Exception e)