8f97226946d39d44c8e2209be1add733cd3ac339
[jalview.git] / src / jalview / ws / HttpClientUtils.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
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
10  * of the License, or (at your option) any later version.
11  *  
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.
16  * 
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.
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  * Helpful procedures for working with services via HTTPClient
52  * 
53  * @author jimp
54  * 
55  */
56 public class HttpClientUtils
57 {
58   /**
59    * do a minimal HTTP post with URL-Encoded parameters passed in the Query
60    * string
61    * 
62    * @param postUrl
63    * @param vals
64    * @return Reader containing content, if any, or null if no entity returned.
65    * @throws IOException
66    * @throws ClientProtocolException
67    * @throws Exception
68    */
69   public static BufferedReader doHttpUrlPost(String postUrl,
70           List<NameValuePair> vals, int connectionTimeoutMs,
71           int readTimeoutMs) throws ClientProtocolException, IOException
72   {
73     // todo use HttpClient 4.3 or later and class RequestConfig
74     HttpParams params = new BasicHttpParams();
75     params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION,
76             HttpVersion.HTTP_1_1);
77     if (connectionTimeoutMs > 0)
78     {
79       HttpConnectionParams.setConnectionTimeout(params,
80               connectionTimeoutMs);
81     }
82     if (readTimeoutMs > 0)
83     {
84       HttpConnectionParams.setSoTimeout(params, readTimeoutMs);
85     }
86     HttpClient httpclient = new DefaultHttpClient(params);
87     HttpPost httppost = new HttpPost(postUrl);
88     UrlEncodedFormEntity ue = new UrlEncodedFormEntity(vals, "UTF-8");
89     httppost.setEntity(ue);
90     HttpResponse response = httpclient.execute(httppost);
91     HttpEntity resEntity = response.getEntity();
92
93     if (resEntity != null)
94     {
95       BufferedReader r = new BufferedReader(
96               new InputStreamReader(resEntity.getContent()));
97       return r;
98     }
99     else
100     {
101       return null;
102     }
103   }
104
105   public static BufferedReader doHttpMpartFilePost(String postUrl,
106           List<NameValuePair> vals, String fparm, File file, String mtype)
107           throws ClientProtocolException, IOException
108   {
109     HttpClient httpclient = new DefaultHttpClient();
110     HttpPost httppost = new HttpPost(postUrl);
111     MultipartEntity mpe = new MultipartEntity(
112             HttpMultipartMode.BROWSER_COMPATIBLE);
113     for (NameValuePair nvp : vals)
114     {
115       mpe.addPart(nvp.getName(), new StringBody(nvp.getValue()));
116     }
117
118     FileBody fb = new FileBody(file,
119             mtype != null ? mtype : "application/octet-stream");
120     mpe.addPart(fparm, fb);
121     UrlEncodedFormEntity ue = new UrlEncodedFormEntity(vals, "UTF-8");
122     httppost.setEntity(ue);
123     HttpResponse response = httpclient.execute(httppost);
124     HttpEntity resEntity = response.getEntity();
125
126     if (resEntity != null)
127     {
128       BufferedReader r = new BufferedReader(
129               new InputStreamReader(resEntity.getContent()));
130       return r;
131     }
132     else
133     {
134       return null;
135     }
136   }
137
138   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     HttpClient httpclient = new DefaultHttpClient();
144     HttpPost httppost = new HttpPost(postUrl);
145     MultipartEntity mpe = new MultipartEntity(HttpMultipartMode.STRICT);
146     for (NameValuePair nvp : vals)
147     {
148       mpe.addPart(nvp.getName(), new StringBody(nvp.getValue()));
149     }
150
151     InputStreamBody fb = (mtype != null)
152             ? new InputStreamBody(is, fname, mtype)
153             : new InputStreamBody(is, fname);
154     mpe.addPart(fparm, fb);
155     UrlEncodedFormEntity ue = new UrlEncodedFormEntity(vals, "UTF-8");
156     httppost.setEntity(ue);
157     HttpResponse response = httpclient.execute(httppost);
158     HttpEntity resEntity = response.getEntity();
159
160     if (resEntity != null)
161     {
162       BufferedReader r = new BufferedReader(
163               new InputStreamReader(resEntity.getContent()));
164       return r;
165     }
166     else
167     {
168       return null;
169     }
170   }
171
172   /**
173    * do an HTTP GET with URL-Encoded parameters passed in the Query string
174    * 
175    * @param url
176    * @param vals
177    * @return Reader containing content, if any, or null if no entity returned.
178    * @throws IOException
179    * @throws ClientProtocolException
180    * @throws Exception
181    */
182   public static BufferedReader doHttpGet(String url,
183           List<NameValuePair> vals, int connectionTimeoutMs,
184           int readTimeoutMs) throws ClientProtocolException, IOException
185   {
186     // todo use HttpClient 4.3 or later and class RequestConfig
187     HttpParams params = new BasicHttpParams();
188     params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION,
189             HttpVersion.HTTP_1_1);
190     if (connectionTimeoutMs > 0)
191     {
192       HttpConnectionParams.setConnectionTimeout(params,
193               connectionTimeoutMs);
194     }
195     if (readTimeoutMs > 0)
196     {
197       HttpConnectionParams.setSoTimeout(params, readTimeoutMs);
198     }
199     boolean first = true;
200     for (NameValuePair param : vals)
201     {
202       if (first)
203       {
204         url += "?";
205       }
206       else
207       {
208         url += "&";
209       }
210       url += param.getName();
211       url += "=";
212       url += param.getValue();
213     }
214     HttpClient httpclient = new DefaultHttpClient(params);
215     HttpGet httpGet = new HttpGet(url);
216     // UrlEncodedFormEntity ue = new UrlEncodedFormEntity(vals, "UTF-8");
217     // httpGet.setEntity(ue);
218     HttpResponse response = httpclient.execute(httpGet);
219     HttpEntity resEntity = response.getEntity();
220   
221     if (resEntity != null)
222     {
223       BufferedReader r = new BufferedReader(
224               new InputStreamReader(resEntity.getContent()));
225       return r;
226     }
227     else
228     {
229       return null;
230     }
231   }
232 }