update author list in license for (JAL-826)
[jalview.git] / src / jalview / ws / rest / HttpResultSet.java
1 /*******************************************************************************
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3  * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
4  *
5  * This file is part of Jalview.
6  *
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.
10  *
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.
15  *
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  *******************************************************************************/
18 package jalview.ws.rest;
19
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;
27
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;
34
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.ContentHandler;
41 import org.apache.james.mime4j.parser.MimeStreamParser;
42
43 /**
44  * data source instantiated from the response of an httpclient request.
45  * 
46  * @author JimP
47  * 
48  */
49
50 public class HttpResultSet extends FileParse
51 {
52
53   private HttpRequestBase cachedRequest;
54
55   /**
56    * when set, indicates that en can be recreated by repeating the HttpRequest
57    * in cachedRequest
58    */
59   boolean repeatable = false;
60
61   /**
62    * response that is to be parsed as jalview input data
63    */
64   private HttpEntity en = null;
65
66   /**
67    * (sub)job that produced this result set.
68    */
69   private RestJob restJob;
70
71   public HttpResultSet(RestJob rj, HttpResponse con, HttpRequestBase req)
72           throws IOException
73   {
74     super();
75     setDataName(rj.getJobId() + " Part " + rj.getJobnum());
76     restJob = rj;
77     cachedRequest = req;
78     initDataSource(con);
79   }
80
81   /**
82    * construct a set of dataproviders to parse a result set from this service
83    * 
84    * @param resSet
85    * @return
86    */
87   public List<DataProvider> createResultDataProviders()
88   {
89     List<DataProvider> dp = new ArrayList<DataProvider>();
90     for (JvDataType type : restJob.rsd.getResultDataTypes())
91     {
92       dp.add(new SimpleDataProvider(type, this, null));
93     }
94     return dp;
95   }
96
97   /**
98    * parses the results of the service output.
99    * @return the result of ParsePackedSet.getAlignment()
100    * @throws Exception
101    * @throws Error
102    */
103   public Object[] parseResultSet() throws Exception, Error
104   {
105     List<DataProvider> dp = new ArrayList<DataProvider>();
106     Object[] results = null;
107
108     if (en == null)
109     {
110       throw new Error(
111               "Implementation Error: need to have an HttpResponse to process.");
112     }
113     jalview.io.packed.JalviewDataset ds = restJob.newJalviewDataset();
114     // Decide how we deal with content.
115     if (en instanceof MultipartEntity)
116     {
117       // Multipart messages should be properly typed, so we parse them as we go.
118       MultipartEntity mpe = (MultipartEntity) en;
119       // multipart
120       JalviewMimeContentHandler handler = new JalviewMimeContentHandler(ds);
121       MimeStreamParser parser = new MimeStreamParser();
122       parser.setContentHandler(handler);
123       try
124       {
125         parser.parse(mpe.getContent());
126       } catch (MimeException me)
127       {
128         error = true;
129         errormessage = "Couldn't parse message from web service.";
130         Cache.log.warn("Failed to parse MIME multipart content", me);
131         en.consumeContent();
132       }
133       return new ParsePackedSet().getAlignment(ds,
134               handler.getJalviewDataProviders());
135     }
136     else
137     {
138       // Need to use hints from rest service description.
139       dp = createResultDataProviders();
140       ParsePackedSet pps = new ParsePackedSet();
141       return pps.getAlignment(ds, dp);
142     }
143   }
144
145   private void initDataSource(HttpResponse con) throws IOException
146   {
147     en = con.getEntity();
148     repeatable = en.isRepeatable();
149
150     if (!(en instanceof MultipartEntity))
151     {
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)
156       {
157         Cache.log.debug("Result Type: " + en.getContentType().toString());
158       }
159       else
160       {
161         Cache.log.debug("No Result Type Specified.");
162       }
163       if (enc == null || enc.length() < 1)
164       {
165         Cache.log.debug("Assuming 'Default' Result Encoding.");
166       }
167       else
168       {
169         Cache.log.debug("Result Encoded as : " + enc);
170       }
171       // attempt to identify file and construct an appropriate DataSource
172       // identifier for it.
173       // try to parse
174       // Mime-Multipart or single content type will be expected.
175       // if (enc.equals(org.apache.http.client.utils.)))
176       InputStreamReader br = null;
177       try
178       {
179         br = (enc != null) ? new InputStreamReader(en.getContent(), enc)
180                 : new InputStreamReader(en.getContent());
181       } catch (UnsupportedEncodingException e)
182       {
183         Cache.log.error("Can't handle encoding '" + enc
184                 + "' for response from webservice.", e);
185         en.consumeContent();
186         error = true;
187         errormessage = "Can't handle encoding for response from webservice";
188         return;
189       }
190       if (br != null)
191       {
192         dataIn = new BufferedReader(br);
193         error = false;
194       }
195     }
196   }
197
198   @Override
199   protected void finalize() throws Throwable
200   {
201     dataIn = null;
202     cachedRequest = null;
203     try
204     {
205       if (en != null)
206       {
207         en.consumeContent();
208       }
209     } catch (Exception e)
210     {
211     } catch (Error ex)
212     {
213     }
214     super.finalize();
215   }
216
217   /**
218    * 
219    * @return the URL that this result set read data from.
220    */
221   public String getUrl()
222   {
223     try {
224       return cachedRequest.getURI().toURL().toString();
225     }  catch (Exception x)
226     {
227       x.printStackTrace();
228       return null;
229     }
230   }
231
232 }