2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
18 package jalview.ws.rest;
20 import jalview.bin.Cache;
21 import jalview.io.FileParse;
22 import jalview.io.packed.DataProvider;
23 import jalview.io.packed.ParsePackedSet;
24 import jalview.io.packed.SimpleDataProvider;
25 import jalview.io.packed.DataProvider.JvDataType;
26 import jalview.ws.io.mime.JalviewMimeContentHandler;
28 import java.io.BufferedReader;
29 import java.io.IOException;
30 import java.io.InputStreamReader;
31 import java.io.UnsupportedEncodingException;
32 import java.util.ArrayList;
33 import java.util.List;
35 import org.apache.http.HttpEntity;
36 import org.apache.http.HttpResponse;
37 import org.apache.http.client.methods.HttpRequestBase;
38 import org.apache.http.entity.mime.MultipartEntity;
39 import org.apache.james.mime4j.MimeException;
40 import org.apache.james.mime4j.parser.MimeStreamParser;
43 * data source instantiated from the response of an httpclient request.
49 public class HttpResultSet extends FileParse
52 private HttpRequestBase cachedRequest;
55 * when set, indicates that en can be recreated by repeating the HttpRequest
58 boolean repeatable = false;
61 * response that is to be parsed as jalview input data
63 private HttpEntity en = null;
66 * (sub)job that produced this result set.
68 private RestJob restJob;
70 public HttpResultSet(RestJob rj, HttpResponse con, HttpRequestBase req)
74 setDataName(rj.getJobId() + " Part " + rj.getJobnum());
81 * construct a set of dataproviders to parse a result set from this service
86 public List<DataProvider> createResultDataProviders()
88 List<DataProvider> dp = new ArrayList<DataProvider>();
89 for (JvDataType type : restJob.rsd.getResultDataTypes())
91 dp.add(new SimpleDataProvider(type, this, null));
97 * parses the results of the service output.
99 * @return the result of ParsePackedSet.getAlignment()
103 public Object[] parseResultSet() throws Exception, Error
105 List<DataProvider> dp = new ArrayList<DataProvider>();
106 Object[] results = null;
111 "Implementation Error: need to have an HttpResponse to process.");
113 jalview.io.packed.JalviewDataset ds = restJob.newJalviewDataset();
114 // Decide how we deal with content.
115 if (en instanceof MultipartEntity)
117 // Multipart messages should be properly typed, so we parse them as we go.
118 MultipartEntity mpe = (MultipartEntity) en;
120 JalviewMimeContentHandler handler = new JalviewMimeContentHandler(ds);
121 MimeStreamParser parser = new MimeStreamParser();
122 parser.setContentHandler(handler);
125 parser.parse(mpe.getContent());
126 } catch (MimeException me)
129 errormessage = "Couldn't parse message from web service.";
130 Cache.log.warn("Failed to parse MIME multipart content", me);
133 return new ParsePackedSet().getAlignment(ds,
134 handler.getJalviewDataProviders());
138 // Need to use hints from rest service description.
139 dp = createResultDataProviders();
140 ParsePackedSet pps = new ParsePackedSet();
141 return pps.getAlignment(ds, dp);
145 private void initDataSource(HttpResponse con) throws IOException
147 en = con.getEntity();
148 repeatable = en.isRepeatable();
150 if (!(en instanceof MultipartEntity))
152 // assume content is simple text stream that can be read from
153 String enc = (en.getContentEncoding() == null) ? null : en
154 .getContentEncoding().getValue();
155 if (en.getContentType() != null)
157 Cache.log.debug("Result Type: " + en.getContentType().toString());
161 Cache.log.debug("No Result Type Specified.");
163 if (enc == null || enc.length() < 1)
165 Cache.log.debug("Assuming 'Default' Result Encoding.");
169 Cache.log.debug("Result Encoded as : " + enc);
171 // attempt to identify file and construct an appropriate DataSource
172 // identifier for it.
174 // Mime-Multipart or single content type will be expected.
175 // if (enc.equals(org.apache.http.client.utils.)))
176 InputStreamReader br = null;
179 br = (enc != null) ? new InputStreamReader(en.getContent(), enc)
180 : new InputStreamReader(en.getContent());
181 } catch (UnsupportedEncodingException e)
183 Cache.log.error("Can't handle encoding '" + enc
184 + "' for response from webservice.", e);
187 errormessage = "Can't handle encoding for response from webservice";
192 dataIn = new BufferedReader(br);
199 protected void finalize() throws Throwable
202 cachedRequest = null;
209 } catch (Exception e)
219 * @return the URL that this result set read data from.
221 public String getUrl()
225 return cachedRequest.getURI().toURL().toString();
226 } catch (Exception x)