1 package jalview.ws.rest;
3 import jalview.bin.Cache;
4 import jalview.io.FileParse;
5 import jalview.ws.io.mime.JalviewMimeContentHandler;
7 import java.io.BufferedReader;
8 import java.io.IOException;
9 import java.io.InputStreamReader;
10 import java.io.UnsupportedEncodingException;
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;
21 * data source instantiated from the response of an httpclient request.
27 public class HttpResultSet extends FileParse
30 private HttpRequestBase cachedRequest;
33 * when set, indicates that en can be recreated by repeating the HttpRequest
36 boolean repeatable = false;
39 * response that is to be parsed as jalview input data
41 private HttpEntity en = null;
44 * (sub)job that produced this result set.
46 private RestJob restJob;
48 public HttpResultSet(RestJob rj, HttpResponse con, HttpRequestBase req)
52 setDataName(rj.getJobId() + " Part " + rj.getJobnum());
58 private void initDataSource(HttpResponse con) throws IOException
61 repeatable = en.isRepeatable();
63 if (en instanceof MultipartEntity)
65 MultipartEntity mpe = (MultipartEntity) en;
67 jalview.io.packed.JalviewDataset ds = restJob.newJalviewDataset();
68 ContentHandler handler = new JalviewMimeContentHandler(ds);
69 MimeStreamParser parser = new MimeStreamParser();
70 parser.setContentHandler(handler);
73 parser.parse(mpe.getContent());
74 } catch (MimeException me)
77 errormessage = "Couldn't parse message from web service.";
78 Cache.log.warn("Failed to parse MIME multipart content", me);
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());
89 Cache.log.info("No Result Type Specified.");
91 if (enc == null || enc.length() < 1)
93 Cache.log.debug("Assuming 'Default' Result Encoding.");
97 Cache.log.debug("Result Encoded as : "+enc);
99 // attempt to identify file and construct an appropriate DataSource
100 // identifier for it.
102 // Mime-Multipart or single content type will be expected.
103 // if (enc.equals(org.apache.http.client.utils.)))
104 InputStreamReader br = null;
107 br = (enc != null) ? new InputStreamReader(en.getContent(), enc)
108 : new InputStreamReader(en.getContent());
109 } catch (UnsupportedEncodingException e)
111 Cache.log.error("Can't handle encoding '" + enc
112 + "' for response from webservice.", e);
115 errormessage = "Can't handle encoding for response from webservice";
120 dataIn = new BufferedReader(br);
127 protected void finalize() throws Throwable
130 cachedRequest = null;
137 } catch (Exception e)