2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
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
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
23 import java.io.BufferedReader;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.io.InputStreamReader;
28 import java.util.List;
30 import org.apache.http.HttpEntity;
31 import org.apache.http.HttpResponse;
32 import org.apache.http.NameValuePair;
33 import org.apache.http.client.ClientProtocolException;
34 import org.apache.http.client.HttpClient;
35 import org.apache.http.client.entity.UrlEncodedFormEntity;
36 import org.apache.http.client.methods.HttpGet;
37 import org.apache.http.client.methods.HttpPost;
38 import org.apache.http.entity.mime.HttpMultipartMode;
39 import org.apache.http.entity.mime.MultipartEntity;
40 import org.apache.http.entity.mime.content.FileBody;
41 import org.apache.http.entity.mime.content.InputStreamBody;
42 import org.apache.http.entity.mime.content.StringBody;
43 import org.apache.http.impl.client.DefaultHttpClient;
46 * Helpful procedures for working with services via HTTPClient
51 public class HttpClientUtils
54 * do a minimal HTTP post with URL-Encoded parameters passed in the Query
59 * @return Reader containing content, if any, or null if no entity returned.
61 * @throws ClientProtocolException
64 public static BufferedReader doHttpUrlPost(String postUrl,
65 List<NameValuePair> vals) throws ClientProtocolException,
68 HttpClient httpclient = new DefaultHttpClient();
69 HttpPost httppost = new HttpPost(postUrl);
70 UrlEncodedFormEntity ue = new UrlEncodedFormEntity(vals, "UTF-8");
71 httppost.setEntity(ue);
72 HttpResponse response = httpclient.execute(httppost);
73 HttpEntity resEntity = response.getEntity();
75 if (resEntity != null)
77 BufferedReader r = new BufferedReader(new InputStreamReader(
78 resEntity.getContent()));
87 public static String doHttpUrlGet(String getUrl /* , HttpParams params */)
88 throws ClientProtocolException, IOException
90 HttpClient httpclient = new DefaultHttpClient();
91 HttpGet httpget = new HttpGet(getUrl);
92 String responseString = "";
93 HttpResponse response = httpclient.execute(httpget);
94 HttpEntity resEntity = response.getEntity();
96 if (resEntity != null)
98 BufferedReader r = new BufferedReader(new InputStreamReader(
99 resEntity.getContent()));
102 StringBuilder responseBuilder = new StringBuilder();
103 while ((aux = r.readLine()) != null)
105 responseBuilder.append(aux);
107 responseString = responseBuilder.toString();
109 return responseString;
112 public static BufferedReader doHttpMpartFilePost(String postUrl,
113 List<NameValuePair> vals, String fparm, File file, String mtype)
114 throws ClientProtocolException, IOException
116 HttpClient httpclient = new DefaultHttpClient();
117 HttpPost httppost = new HttpPost(postUrl);
118 MultipartEntity mpe = new MultipartEntity(
119 HttpMultipartMode.BROWSER_COMPATIBLE);
120 for (NameValuePair nvp : vals)
122 mpe.addPart(nvp.getName(), new StringBody(nvp.getValue()));
125 FileBody fb = new FileBody(file, mtype != null ? mtype
126 : "application/octet-stream");
127 mpe.addPart(fparm, fb);
128 UrlEncodedFormEntity ue = new UrlEncodedFormEntity(vals, "UTF-8");
129 httppost.setEntity(ue);
130 HttpResponse response = httpclient.execute(httppost);
131 HttpEntity resEntity = response.getEntity();
133 if (resEntity != null)
135 BufferedReader r = new BufferedReader(new InputStreamReader(
136 resEntity.getContent()));
145 public static BufferedReader doHttpMpartInputstreamPost(String postUrl,
146 List<NameValuePair> vals, String fparm, String fname,
147 InputStream is, String mtype) throws ClientProtocolException,
150 HttpClient httpclient = new DefaultHttpClient();
151 HttpPost httppost = new HttpPost(postUrl);
152 MultipartEntity mpe = new MultipartEntity(HttpMultipartMode.STRICT);
153 for (NameValuePair nvp : vals)
155 mpe.addPart(nvp.getName(), new StringBody(nvp.getValue()));
158 InputStreamBody fb = (mtype != null) ? new InputStreamBody(is, fname,
159 mtype) : new InputStreamBody(is, fname);
160 mpe.addPart(fparm, fb);
161 UrlEncodedFormEntity ue = new UrlEncodedFormEntity(vals, "UTF-8");
162 httppost.setEntity(ue);
163 HttpResponse response = httpclient.execute(httppost);
164 HttpEntity resEntity = response.getEntity();
166 if (resEntity != null)
168 BufferedReader r = new BufferedReader(new InputStreamReader(
169 resEntity.getContent()));