1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
package jalview.ws; |
22 |
|
|
23 |
|
import java.io.BufferedReader; |
24 |
|
import java.io.File; |
25 |
|
import java.io.IOException; |
26 |
|
import java.io.InputStream; |
27 |
|
import java.io.InputStreamReader; |
28 |
|
import java.util.List; |
29 |
|
|
30 |
|
import org.apache.http.HttpEntity; |
31 |
|
import org.apache.http.HttpResponse; |
32 |
|
import org.apache.http.HttpVersion; |
33 |
|
import org.apache.http.NameValuePair; |
34 |
|
import org.apache.http.client.ClientProtocolException; |
35 |
|
import org.apache.http.client.HttpClient; |
36 |
|
import org.apache.http.client.entity.UrlEncodedFormEntity; |
37 |
|
import org.apache.http.client.methods.HttpGet; |
38 |
|
import org.apache.http.client.methods.HttpPost; |
39 |
|
import org.apache.http.entity.mime.HttpMultipartMode; |
40 |
|
import org.apache.http.entity.mime.MultipartEntity; |
41 |
|
import org.apache.http.entity.mime.content.FileBody; |
42 |
|
import org.apache.http.entity.mime.content.InputStreamBody; |
43 |
|
import org.apache.http.entity.mime.content.StringBody; |
44 |
|
import org.apache.http.impl.client.DefaultHttpClient; |
45 |
|
import org.apache.http.params.BasicHttpParams; |
46 |
|
import org.apache.http.params.CoreProtocolPNames; |
47 |
|
import org.apache.http.params.HttpConnectionParams; |
48 |
|
import org.apache.http.params.HttpParams; |
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
@author |
54 |
|
|
55 |
|
|
|
|
| 0% |
Uncovered Elements: 94 (94) |
Complexity: 15 |
Complexity Density: 0.22 |
|
56 |
|
public class HttpClientUtils |
57 |
|
{ |
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
@param |
63 |
|
@param |
64 |
|
@return |
65 |
|
@throws |
66 |
|
@throws |
67 |
|
@throws |
68 |
|
|
|
|
| 0% |
Uncovered Elements: 22 (22) |
Complexity: 4 |
Complexity Density: 0.25 |
|
69 |
0 |
public static BufferedReader doHttpUrlPost(String postUrl,... |
70 |
|
List<NameValuePair> vals, int connectionTimeoutMs, |
71 |
|
int readTimeoutMs) throws ClientProtocolException, IOException |
72 |
|
{ |
73 |
|
|
74 |
0 |
HttpParams params = new BasicHttpParams(); |
75 |
0 |
params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, |
76 |
|
HttpVersion.HTTP_1_1); |
77 |
0 |
if (connectionTimeoutMs > 0) |
78 |
|
{ |
79 |
0 |
HttpConnectionParams.setConnectionTimeout(params, |
80 |
|
connectionTimeoutMs); |
81 |
|
} |
82 |
0 |
if (readTimeoutMs > 0) |
83 |
|
{ |
84 |
0 |
HttpConnectionParams.setSoTimeout(params, readTimeoutMs); |
85 |
|
} |
86 |
0 |
HttpClient httpclient = new DefaultHttpClient(params); |
87 |
0 |
HttpPost httppost = new HttpPost(postUrl); |
88 |
0 |
UrlEncodedFormEntity ue = new UrlEncodedFormEntity(vals, "UTF-8"); |
89 |
0 |
httppost.setEntity(ue); |
90 |
0 |
HttpResponse response = httpclient.execute(httppost); |
91 |
0 |
HttpEntity resEntity = response.getEntity(); |
92 |
|
|
93 |
0 |
if (resEntity != null) |
94 |
|
{ |
95 |
0 |
BufferedReader r = new BufferedReader( |
96 |
|
new InputStreamReader(resEntity.getContent())); |
97 |
0 |
return r; |
98 |
|
} |
99 |
|
else |
100 |
|
{ |
101 |
0 |
return null; |
102 |
|
} |
103 |
|
} |
104 |
|
|
|
|
| 0% |
Uncovered Elements: 19 (19) |
Complexity: 3 |
Complexity Density: 0.2 |
|
105 |
0 |
public static BufferedReader doHttpMpartFilePost(String postUrl,... |
106 |
|
List<NameValuePair> vals, String fparm, File file, String mtype) |
107 |
|
throws ClientProtocolException, IOException |
108 |
|
{ |
109 |
0 |
HttpClient httpclient = new DefaultHttpClient(); |
110 |
0 |
HttpPost httppost = new HttpPost(postUrl); |
111 |
0 |
MultipartEntity mpe = new MultipartEntity( |
112 |
|
HttpMultipartMode.BROWSER_COMPATIBLE); |
113 |
0 |
for (NameValuePair nvp : vals) |
114 |
|
{ |
115 |
0 |
mpe.addPart(nvp.getName(), new StringBody(nvp.getValue())); |
116 |
|
} |
117 |
|
|
118 |
0 |
FileBody fb = new FileBody(file, |
119 |
0 |
mtype != null ? mtype : "application/octet-stream"); |
120 |
0 |
mpe.addPart(fparm, fb); |
121 |
0 |
UrlEncodedFormEntity ue = new UrlEncodedFormEntity(vals, "UTF-8"); |
122 |
0 |
httppost.setEntity(ue); |
123 |
0 |
HttpResponse response = httpclient.execute(httppost); |
124 |
0 |
HttpEntity resEntity = response.getEntity(); |
125 |
|
|
126 |
0 |
if (resEntity != null) |
127 |
|
{ |
128 |
0 |
BufferedReader r = new BufferedReader( |
129 |
|
new InputStreamReader(resEntity.getContent())); |
130 |
0 |
return r; |
131 |
|
} |
132 |
|
else |
133 |
|
{ |
134 |
0 |
return null; |
135 |
|
} |
136 |
|
} |
137 |
|
|
|
|
| 0% |
Uncovered Elements: 19 (19) |
Complexity: 3 |
Complexity Density: 0.2 |
|
138 |
0 |
public static BufferedReader doHttpMpartInputstreamPost(String postUrl,... |
139 |
|
List<NameValuePair> vals, String fparm, String fname, |
140 |
|
InputStream is, String mtype) |
141 |
|
throws ClientProtocolException, IOException |
142 |
|
{ |
143 |
0 |
HttpClient httpclient = new DefaultHttpClient(); |
144 |
0 |
HttpPost httppost = new HttpPost(postUrl); |
145 |
0 |
MultipartEntity mpe = new MultipartEntity(HttpMultipartMode.STRICT); |
146 |
0 |
for (NameValuePair nvp : vals) |
147 |
|
{ |
148 |
0 |
mpe.addPart(nvp.getName(), new StringBody(nvp.getValue())); |
149 |
|
} |
150 |
|
|
151 |
0 |
InputStreamBody fb = (mtype != null) |
152 |
|
? new InputStreamBody(is, fname, mtype) |
153 |
|
: new InputStreamBody(is, fname); |
154 |
0 |
mpe.addPart(fparm, fb); |
155 |
0 |
UrlEncodedFormEntity ue = new UrlEncodedFormEntity(vals, "UTF-8"); |
156 |
0 |
httppost.setEntity(ue); |
157 |
0 |
HttpResponse response = httpclient.execute(httppost); |
158 |
0 |
HttpEntity resEntity = response.getEntity(); |
159 |
|
|
160 |
0 |
if (resEntity != null) |
161 |
|
{ |
162 |
0 |
BufferedReader r = new BufferedReader( |
163 |
|
new InputStreamReader(resEntity.getContent())); |
164 |
0 |
return r; |
165 |
|
} |
166 |
|
else |
167 |
|
{ |
168 |
0 |
return null; |
169 |
|
} |
170 |
|
} |
171 |
|
|
172 |
|
|
173 |
|
|
174 |
|
|
175 |
|
@param |
176 |
|
@param |
177 |
|
@return |
178 |
|
@throws |
179 |
|
@throws |
180 |
|
@throws |
181 |
|
|
|
|
| 0% |
Uncovered Elements: 30 (30) |
Complexity: 5 |
Complexity Density: 0.23 |
|
182 |
0 |
public static BufferedReader doHttpGet(String url,... |
183 |
|
List<NameValuePair> vals, int connectionTimeoutMs, |
184 |
|
int readTimeoutMs) throws ClientProtocolException, IOException |
185 |
|
{ |
186 |
|
|
187 |
0 |
HttpParams params = new BasicHttpParams(); |
188 |
0 |
params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, |
189 |
|
HttpVersion.HTTP_1_1); |
190 |
0 |
if (connectionTimeoutMs > 0) |
191 |
|
{ |
192 |
0 |
HttpConnectionParams.setConnectionTimeout(params, |
193 |
|
connectionTimeoutMs); |
194 |
|
} |
195 |
0 |
if (readTimeoutMs > 0) |
196 |
|
{ |
197 |
0 |
HttpConnectionParams.setSoTimeout(params, readTimeoutMs); |
198 |
|
} |
199 |
0 |
boolean first = true; |
200 |
0 |
for (NameValuePair param : vals) |
201 |
|
{ |
202 |
0 |
if (first) |
203 |
|
{ |
204 |
0 |
url += "?"; |
205 |
|
} |
206 |
|
else |
207 |
|
{ |
208 |
0 |
url += "&"; |
209 |
|
} |
210 |
0 |
url += param.getName(); |
211 |
0 |
url += "="; |
212 |
0 |
url += param.getValue(); |
213 |
|
} |
214 |
0 |
HttpClient httpclient = new DefaultHttpClient(params); |
215 |
0 |
HttpGet httpGet = new HttpGet(url); |
216 |
|
|
217 |
|
|
218 |
0 |
HttpResponse response = httpclient.execute(httpGet); |
219 |
0 |
HttpEntity resEntity = response.getEntity(); |
220 |
|
|
221 |
0 |
if (resEntity != null) |
222 |
|
{ |
223 |
0 |
BufferedReader r = new BufferedReader( |
224 |
|
new InputStreamReader(resEntity.getContent())); |
225 |
0 |
return r; |
226 |
|
} |
227 |
|
else |
228 |
|
{ |
229 |
0 |
return null; |
230 |
|
} |
231 |
|
} |
232 |
|
} |