1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
package jalview.ws.rest; |
22 |
|
|
23 |
|
import jalview.bin.Console; |
24 |
|
import jalview.io.FileParse; |
25 |
|
import jalview.io.packed.DataProvider; |
26 |
|
import jalview.io.packed.DataProvider.JvDataType; |
27 |
|
import jalview.io.packed.ParsePackedSet; |
28 |
|
import jalview.io.packed.SimpleDataProvider; |
29 |
|
import jalview.util.MessageManager; |
30 |
|
import jalview.ws.io.mime.JalviewMimeContentHandler; |
31 |
|
|
32 |
|
import java.io.BufferedReader; |
33 |
|
import java.io.IOException; |
34 |
|
import java.io.InputStreamReader; |
35 |
|
import java.io.UnsupportedEncodingException; |
36 |
|
import java.util.ArrayList; |
37 |
|
import java.util.List; |
38 |
|
|
39 |
|
import org.apache.http.HttpEntity; |
40 |
|
import org.apache.http.HttpResponse; |
41 |
|
import org.apache.http.client.methods.HttpRequestBase; |
42 |
|
import org.apache.http.entity.mime.MultipartEntity; |
43 |
|
import org.apache.james.mime4j.MimeException; |
44 |
|
import org.apache.james.mime4j.parser.MimeStreamParser; |
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
@author |
50 |
|
|
51 |
|
|
52 |
|
|
|
|
| 0% |
Uncovered Elements: 83 (83) |
Complexity: 21 |
Complexity Density: 0.36 |
|
53 |
|
public class HttpResultSet extends FileParse implements AutoCloseable |
54 |
|
{ |
55 |
|
|
56 |
|
private HttpRequestBase cachedRequest; |
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
boolean repeatable = false; |
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
private HttpEntity en = null; |
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
private RestJob restJob; |
73 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
74 |
0 |
public HttpResultSet(RestJob rj, HttpResponse con, HttpRequestBase req)... |
75 |
|
throws IOException |
76 |
|
{ |
77 |
0 |
super(); |
78 |
0 |
setDataName(rj.getJobId() + " Part " + rj.getJobnum()); |
79 |
0 |
restJob = rj; |
80 |
0 |
cachedRequest = req; |
81 |
0 |
initDataSource(con); |
82 |
|
} |
83 |
|
|
84 |
|
|
85 |
|
|
86 |
|
|
87 |
|
@param |
88 |
|
@return |
89 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
90 |
0 |
public List<DataProvider> createResultDataProviders()... |
91 |
|
{ |
92 |
0 |
List<DataProvider> dp = new ArrayList<>(); |
93 |
0 |
for (JvDataType type : restJob.rsd.getResultDataTypes()) |
94 |
|
{ |
95 |
0 |
dp.add(new SimpleDataProvider(type, this, null)); |
96 |
|
} |
97 |
0 |
return dp; |
98 |
|
} |
99 |
|
|
100 |
|
|
101 |
|
|
102 |
|
|
103 |
|
@return |
104 |
|
@throws |
105 |
|
@throws |
106 |
|
|
|
|
| 0% |
Uncovered Elements: 24 (24) |
Complexity: 4 |
Complexity Density: 0.2 |
|
107 |
0 |
public Object[] parseResultSet() throws Exception, Error... |
108 |
|
{ |
109 |
0 |
List<DataProvider> dp = new ArrayList<>(); |
110 |
0 |
Object[] results = null; |
111 |
|
|
112 |
0 |
if (en == null) |
113 |
|
{ |
114 |
0 |
throw new Error(MessageManager.getString( |
115 |
|
"error.implementation_error_need_to_have_httpresponse")); |
116 |
|
} |
117 |
0 |
jalview.io.packed.JalviewDataset ds = restJob.newJalviewDataset(); |
118 |
|
|
119 |
0 |
if (en instanceof MultipartEntity) |
120 |
|
{ |
121 |
|
|
122 |
0 |
MultipartEntity mpe = (MultipartEntity) en; |
123 |
|
|
124 |
0 |
JalviewMimeContentHandler handler = new JalviewMimeContentHandler(ds); |
125 |
0 |
MimeStreamParser parser = new MimeStreamParser(); |
126 |
0 |
parser.setContentHandler(handler); |
127 |
0 |
try |
128 |
|
{ |
129 |
0 |
parser.parse(mpe.getContent()); |
130 |
|
} catch (MimeException me) |
131 |
|
{ |
132 |
0 |
error = true; |
133 |
0 |
errormessage = "Couldn't parse message from web service."; |
134 |
0 |
Console.warn("Failed to parse MIME multipart content", me); |
135 |
0 |
en.consumeContent(); |
136 |
|
} |
137 |
0 |
return new ParsePackedSet().getAlignment(ds, |
138 |
|
handler.getJalviewDataProviders()); |
139 |
|
} |
140 |
|
else |
141 |
|
{ |
142 |
|
|
143 |
0 |
dp = createResultDataProviders(); |
144 |
0 |
ParsePackedSet pps = new ParsePackedSet(); |
145 |
0 |
return pps.getAlignment(ds, dp); |
146 |
|
} |
147 |
|
} |
148 |
|
|
|
|
| 0% |
Uncovered Elements: 33 (33) |
Complexity: 9 |
Complexity Density: 0.43 |
|
149 |
0 |
private void initDataSource(HttpResponse con) throws IOException... |
150 |
|
{ |
151 |
0 |
en = con.getEntity(); |
152 |
0 |
repeatable = en.isRepeatable(); |
153 |
|
|
154 |
0 |
if (!(en instanceof MultipartEntity)) |
155 |
|
{ |
156 |
|
|
157 |
0 |
String enc = (en.getContentEncoding() == null) ? null |
158 |
|
: en.getContentEncoding().getValue(); |
159 |
0 |
if (en.getContentType() != null) |
160 |
|
{ |
161 |
0 |
Console.debug("Result Type: " + en.getContentType().toString()); |
162 |
|
} |
163 |
|
else |
164 |
|
{ |
165 |
0 |
Console.debug("No Result Type Specified."); |
166 |
|
} |
167 |
0 |
if (enc == null || enc.length() < 1) |
168 |
|
{ |
169 |
0 |
Console.debug("Assuming 'Default' Result Encoding."); |
170 |
|
} |
171 |
|
else |
172 |
|
{ |
173 |
0 |
Console.debug("Result Encoded as : " + enc); |
174 |
|
} |
175 |
|
|
176 |
|
|
177 |
|
|
178 |
|
|
179 |
|
|
180 |
0 |
InputStreamReader br = null; |
181 |
0 |
try |
182 |
|
{ |
183 |
0 |
br = (enc != null) ? new InputStreamReader(en.getContent(), enc) |
184 |
|
: new InputStreamReader(en.getContent()); |
185 |
|
} catch (UnsupportedEncodingException e) |
186 |
|
{ |
187 |
0 |
Console.error("Can't handle encoding '" + enc |
188 |
|
+ "' for response from webservice.", e); |
189 |
0 |
en.consumeContent(); |
190 |
0 |
error = true; |
191 |
0 |
errormessage = "Can't handle encoding for response from webservice"; |
192 |
0 |
return; |
193 |
|
} |
194 |
0 |
if (br != null) |
195 |
|
{ |
196 |
0 |
dataIn = new BufferedReader(br); |
197 |
0 |
error = false; |
198 |
|
} |
199 |
|
} |
200 |
|
} |
201 |
|
|
|
|
| 0% |
Uncovered Elements: 7 (7) |
Complexity: 4 |
Complexity Density: 0.8 |
|
202 |
0 |
@Override... |
203 |
|
public void close() |
204 |
|
{ |
205 |
0 |
dataIn = null; |
206 |
0 |
cachedRequest = null; |
207 |
0 |
try |
208 |
|
{ |
209 |
0 |
if (en != null) |
210 |
|
{ |
211 |
0 |
en.consumeContent(); |
212 |
|
} |
213 |
|
} catch (Exception e) |
214 |
|
{ |
215 |
|
} catch (Error ex) |
216 |
|
{ |
217 |
|
} |
218 |
|
|
219 |
|
|
220 |
|
} |
221 |
|
|
222 |
|
|
223 |
|
|
224 |
|
@return |
225 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 2 |
Complexity Density: 0.5 |
|
226 |
0 |
public String getUrl()... |
227 |
|
{ |
228 |
0 |
try |
229 |
|
{ |
230 |
0 |
return cachedRequest.getURI().toURL().toString(); |
231 |
|
} catch (Exception x) |
232 |
|
{ |
233 |
0 |
x.printStackTrace(); |
234 |
0 |
return null; |
235 |
|
} |
236 |
|
} |
237 |
|
|
238 |
|
} |