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