2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
3 * Copyright (C) 2014 The Jalview Authors
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/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
19 package jalview.ws.rest;
21 import jalview.bin.Cache;
22 import jalview.io.FileParse;
23 import jalview.io.packed.DataProvider;
24 import jalview.io.packed.ParsePackedSet;
25 import jalview.io.packed.SimpleDataProvider;
26 import jalview.io.packed.DataProvider.JvDataType;
27 import jalview.ws.io.mime.JalviewMimeContentHandler;
29 import java.io.BufferedReader;
30 import java.io.IOException;
31 import java.io.InputStreamReader;
32 import java.io.UnsupportedEncodingException;
33 import java.util.ArrayList;
34 import java.util.List;
36 import org.apache.http.HttpEntity;
37 import org.apache.http.HttpResponse;
38 import org.apache.http.client.methods.HttpRequestBase;
39 import org.apache.http.entity.mime.MultipartEntity;
40 import org.apache.james.mime4j.MimeException;
41 import org.apache.james.mime4j.parser.MimeStreamParser;
44 * data source instantiated from the response of an httpclient request.
50 public class HttpResultSet extends FileParse
53 private HttpRequestBase cachedRequest;
56 * when set, indicates that en can be recreated by repeating the HttpRequest
59 boolean repeatable = false;
62 * response that is to be parsed as jalview input data
64 private HttpEntity en = null;
67 * (sub)job that produced this result set.
69 private RestJob restJob;
71 public HttpResultSet(RestJob rj, HttpResponse con, HttpRequestBase req)
75 setDataName(rj.getJobId() + " Part " + rj.getJobnum());
82 * construct a set of dataproviders to parse a result set from this service
87 public List<DataProvider> createResultDataProviders()
89 List<DataProvider> dp = new ArrayList<DataProvider>();
90 for (JvDataType type : restJob.rsd.getResultDataTypes())
92 dp.add(new SimpleDataProvider(type, this, null));
98 * parses the results of the service output.
100 * @return the result of ParsePackedSet.getAlignment()
104 public Object[] parseResultSet() throws Exception, Error
106 List<DataProvider> dp = new ArrayList<DataProvider>();
107 Object[] results = null;
112 "Implementation Error: need to have an HttpResponse to process.");
114 jalview.io.packed.JalviewDataset ds = restJob.newJalviewDataset();
115 // Decide how we deal with content.
116 if (en instanceof MultipartEntity)
118 // Multipart messages should be properly typed, so we parse them as we go.
119 MultipartEntity mpe = (MultipartEntity) en;
121 JalviewMimeContentHandler handler = new JalviewMimeContentHandler(ds);
122 MimeStreamParser parser = new MimeStreamParser();
123 parser.setContentHandler(handler);
126 parser.parse(mpe.getContent());
127 } catch (MimeException me)
130 errormessage = "Couldn't parse message from web service.";
131 Cache.log.warn("Failed to parse MIME multipart content", me);
134 return new ParsePackedSet().getAlignment(ds,
135 handler.getJalviewDataProviders());
139 // Need to use hints from rest service description.
140 dp = createResultDataProviders();
141 ParsePackedSet pps = new ParsePackedSet();
142 return pps.getAlignment(ds, dp);
146 private void initDataSource(HttpResponse con) throws IOException
148 en = con.getEntity();
149 repeatable = en.isRepeatable();
151 if (!(en instanceof MultipartEntity))
153 // assume content is simple text stream that can be read from
154 String enc = (en.getContentEncoding() == null) ? null : en
155 .getContentEncoding().getValue();
156 if (en.getContentType() != null)
158 Cache.log.debug("Result Type: " + en.getContentType().toString());
162 Cache.log.debug("No Result Type Specified.");
164 if (enc == null || enc.length() < 1)
166 Cache.log.debug("Assuming 'Default' Result Encoding.");
170 Cache.log.debug("Result Encoded as : " + enc);
172 // attempt to identify file and construct an appropriate DataSource
173 // identifier for it.
175 // Mime-Multipart or single content type will be expected.
176 // if (enc.equals(org.apache.http.client.utils.)))
177 InputStreamReader br = null;
180 br = (enc != null) ? new InputStreamReader(en.getContent(), enc)
181 : new InputStreamReader(en.getContent());
182 } catch (UnsupportedEncodingException e)
184 Cache.log.error("Can't handle encoding '" + enc
185 + "' for response from webservice.", e);
188 errormessage = "Can't handle encoding for response from webservice";
193 dataIn = new BufferedReader(br);
200 protected void finalize() throws Throwable
203 cachedRequest = null;
210 } catch (Exception e)
220 * @return the URL that this result set read data from.
222 public String getUrl()
226 return cachedRequest.getURI().toURL().toString();
227 } catch (Exception x)