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