2cc3d8ddb4710b82720c15de445e16c20edd976c
[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.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;
44 import org.apache.http.params.BasicHttpParams;
45 import org.apache.http.params.CoreProtocolPNames;
46 import org.apache.http.params.HttpConnectionParams;
47 import org.apache.http.params.HttpParams;
48
49 /**
50  * Helpful procedures for working with services via HTTPClient
51  * 
52  * @author jimp
53  * 
54  */
55 public class HttpClientUtils
56 {
57   /**
58    * do a minimal HTTP post with URL-Encoded parameters passed in the Query
59    * string
60    * 
61    * @param postUrl
62    * @param vals
63    * @return Reader containing content, if any, or null if no entity returned.
64    * @throws IOException
65    * @throws ClientProtocolException
66    * @throws Exception
67    */
68   public static BufferedReader doHttpUrlPost(String postUrl,
69           List<NameValuePair> vals, int connectionTimeoutMs,
70           int readTimeoutMs) throws ClientProtocolException, IOException
71   {
72     // todo use HttpClient 4.3 or later and class RequestConfig
73     HttpParams params = new BasicHttpParams();
74     params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION,
75             HttpVersion.HTTP_1_1);
76     if (connectionTimeoutMs > 0)
77     {
78       HttpConnectionParams
79               .setConnectionTimeout(params, connectionTimeoutMs);
80     }
81     if (readTimeoutMs > 0)
82     {
83       HttpConnectionParams.setSoTimeout(params, readTimeoutMs);
84     }
85     HttpClient httpclient = new DefaultHttpClient(params);
86     HttpPost httppost = new HttpPost(postUrl);
87     UrlEncodedFormEntity ue = new UrlEncodedFormEntity(vals, "UTF-8");
88     httppost.setEntity(ue);
89     HttpResponse response = httpclient.execute(httppost);
90     HttpEntity resEntity = response.getEntity();
91
92     if (resEntity != null)
93     {
94       BufferedReader r = new BufferedReader(new InputStreamReader(
95               resEntity.getContent()));
96       return r;
97     }
98     else
99     {
100       return null;
101     }
102   }
103
104   public static BufferedReader doHttpMpartFilePost(String postUrl,
105           List<NameValuePair> vals, String fparm, File file, String mtype)
106           throws ClientProtocolException, IOException
107   {
108     HttpClient httpclient = new DefaultHttpClient();
109     HttpPost httppost = new HttpPost(postUrl);
110     MultipartEntity mpe = new MultipartEntity(
111             HttpMultipartMode.BROWSER_COMPATIBLE);
112     for (NameValuePair nvp : vals)
113     {
114       mpe.addPart(nvp.getName(), new StringBody(nvp.getValue()));
115     }
116
117     FileBody fb = new FileBody(file, mtype != null ? mtype
118             : "application/octet-stream");
119     mpe.addPart(fparm, fb);
120     UrlEncodedFormEntity ue = new UrlEncodedFormEntity(vals, "UTF-8");
121     httppost.setEntity(ue);
122     HttpResponse response = httpclient.execute(httppost);
123     HttpEntity resEntity = response.getEntity();
124
125     if (resEntity != null)
126     {
127       BufferedReader r = new BufferedReader(new InputStreamReader(
128               resEntity.getContent()));
129       return r;
130     }
131     else
132     {
133       return null;
134     }
135   }
136
137   public static BufferedReader doHttpMpartInputstreamPost(String postUrl,
138           List<NameValuePair> vals, String fparm, String fname,
139           InputStream is, String mtype) throws ClientProtocolException,
140           IOException
141   {
142     HttpClient httpclient = new DefaultHttpClient();
143     HttpPost httppost = new HttpPost(postUrl);
144     MultipartEntity mpe = new MultipartEntity(HttpMultipartMode.STRICT);
145     for (NameValuePair nvp : vals)
146     {
147       mpe.addPart(nvp.getName(), new StringBody(nvp.getValue()));
148     }
149
150     InputStreamBody fb = (mtype != null) ? new InputStreamBody(is, fname,
151             mtype) : new InputStreamBody(is, fname);
152     mpe.addPart(fparm, fb);
153     UrlEncodedFormEntity ue = new UrlEncodedFormEntity(vals, "UTF-8");
154     httppost.setEntity(ue);
155     HttpResponse response = httpclient.execute(httppost);
156     HttpEntity resEntity = response.getEntity();
157
158     if (resEntity != null)
159     {
160       BufferedReader r = new BufferedReader(new InputStreamReader(
161               resEntity.getContent()));
162       return r;
163     }
164     else
165     {
166       return null;
167     }
168   }
169 }