2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
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/>.
20 import java.io.BufferedReader;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.InputStreamReader;
25 import java.util.List;
27 import org.apache.http.HttpEntity;
28 import org.apache.http.HttpResponse;
29 import org.apache.http.NameValuePair;
30 import org.apache.http.client.ClientProtocolException;
31 import org.apache.http.client.HttpClient;
32 import org.apache.http.client.entity.UrlEncodedFormEntity;
33 import org.apache.http.client.methods.HttpPost;
34 import org.apache.http.entity.mime.HttpMultipartMode;
35 import org.apache.http.entity.mime.MultipartEntity;
36 import org.apache.http.entity.mime.content.FileBody;
37 import org.apache.http.entity.mime.content.InputStreamBody;
38 import org.apache.http.entity.mime.content.StringBody;
39 import org.apache.http.impl.client.DefaultHttpClient;
42 * Helpful procedures for working with services via HTTPClient
46 public class HttpClientUtils
49 * do a minimal HTTP post with URL-Encoded parameters passed in the Query
54 * @return Reader containing content, if any, or null if no entity returned.
56 * @throws ClientProtocolException
59 public static BufferedReader doHttpUrlPost(String postUrl,
60 List<NameValuePair> vals) throws ClientProtocolException,
63 HttpClient httpclient = new DefaultHttpClient();
64 HttpPost httppost = new HttpPost(postUrl);
65 UrlEncodedFormEntity ue = new UrlEncodedFormEntity(vals, "UTF-8");
66 httppost.setEntity(ue);
67 HttpResponse response = httpclient.execute(httppost);
68 HttpEntity resEntity = response.getEntity();
70 if (resEntity != null)
72 BufferedReader r = new BufferedReader(new InputStreamReader(
73 resEntity.getContent()));
82 public static BufferedReader doHttpMpartFilePost(String postUrl,
83 List<NameValuePair> vals, String fparm,File file, String mtype) throws ClientProtocolException,
86 HttpClient httpclient = new DefaultHttpClient();
87 HttpPost httppost = new HttpPost(postUrl);
88 MultipartEntity mpe = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
89 for (NameValuePair nvp:vals)
91 mpe.addPart(nvp.getName(), new StringBody(nvp.getValue()));
94 FileBody fb = new FileBody(file, mtype!=null ? mtype : "application/octet-stream");
95 mpe.addPart(fparm, fb);
96 UrlEncodedFormEntity ue = new UrlEncodedFormEntity(vals, "UTF-8");
97 httppost.setEntity(ue);
98 HttpResponse response = httpclient.execute(httppost);
99 HttpEntity resEntity = response.getEntity();
101 if (resEntity != null)
103 BufferedReader r = new BufferedReader(new InputStreamReader(
104 resEntity.getContent()));
112 public static BufferedReader doHttpMpartInputstreamPost(String postUrl,
113 List<NameValuePair> vals, String fparm,String fname, InputStream is, String mtype) throws ClientProtocolException,
116 HttpClient httpclient = new DefaultHttpClient();
117 HttpPost httppost = new HttpPost(postUrl);
118 MultipartEntity mpe = new MultipartEntity(HttpMultipartMode.STRICT);
119 for (NameValuePair nvp:vals)
121 mpe.addPart(nvp.getName(), new StringBody(nvp.getValue()));
124 InputStreamBody fb = (mtype!=null) ? new InputStreamBody(is, fname, mtype) : new InputStreamBody(is, fname);
125 mpe.addPart(fparm, fb);
126 UrlEncodedFormEntity ue = new UrlEncodedFormEntity(vals, "UTF-8");
127 httppost.setEntity(ue);
128 HttpResponse response = httpclient.execute(httppost);
129 HttpEntity resEntity = response.getEntity();
131 if (resEntity != null)
133 BufferedReader r = new BufferedReader(new InputStreamReader(
134 resEntity.getContent()));