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