740b669c07baf63cfd7c283febef86b856fa0a29
[jalview.git] / src / jalview / ws / HttpClientUtils.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3  * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
10  *  
11  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 package jalview.ws;
19
20 import java.io.BufferedReader;
21 import java.io.File;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.InputStreamReader;
25 import java.util.List;
26
27 import org.apache.http.HttpEntity;
28 import org.apache.http.HttpResponse;
29 import org.apache.http.NameValuePair;
30 import org.apache.http.client.ClientProtocolException;
31 import org.apache.http.client.HttpClient;
32 import org.apache.http.client.entity.UrlEncodedFormEntity;
33 import org.apache.http.client.methods.HttpPost;
34 import org.apache.http.entity.mime.HttpMultipartMode;
35 import org.apache.http.entity.mime.MultipartEntity;
36 import org.apache.http.entity.mime.content.FileBody;
37 import org.apache.http.entity.mime.content.InputStreamBody;
38 import org.apache.http.entity.mime.content.StringBody;
39 import org.apache.http.impl.client.DefaultHttpClient;
40
41 /**
42  * Helpful procedures for working with services via HTTPClient
43  * @author jimp
44  *
45  */
46 public class HttpClientUtils
47 {
48   /**
49    * do a minimal HTTP post with URL-Encoded parameters passed in the Query
50    * string
51    * 
52    * @param postUrl
53    * @param vals
54    * @return Reader containing content, if any, or null if no entity returned.
55    * @throws IOException
56    * @throws ClientProtocolException
57    * @throws Exception
58    */
59   public static BufferedReader doHttpUrlPost(String postUrl,
60           List<NameValuePair> vals) throws ClientProtocolException,
61           IOException
62   {
63     HttpClient httpclient = new DefaultHttpClient();
64     HttpPost httppost = new HttpPost(postUrl);
65     UrlEncodedFormEntity ue = new UrlEncodedFormEntity(vals, "UTF-8");
66     httppost.setEntity(ue);
67     HttpResponse response = httpclient.execute(httppost);
68     HttpEntity resEntity = response.getEntity();
69
70     if (resEntity != null)
71     {
72       BufferedReader r = new BufferedReader(new InputStreamReader(
73               resEntity.getContent()));
74       return r;
75     }
76     else
77     {
78       return null;
79     }
80   }
81
82   public static BufferedReader doHttpMpartFilePost(String postUrl,
83           List<NameValuePair> vals, String fparm,File file, String mtype) throws ClientProtocolException,
84           IOException
85   {
86     HttpClient httpclient = new DefaultHttpClient();
87     HttpPost httppost = new HttpPost(postUrl);
88     MultipartEntity mpe = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
89     for (NameValuePair nvp:vals)
90     {
91       mpe.addPart(nvp.getName(), new StringBody(nvp.getValue()));
92     }
93     
94     FileBody fb = new FileBody(file, mtype!=null ? mtype : "application/octet-stream");
95     mpe.addPart(fparm, fb);
96     UrlEncodedFormEntity ue = new UrlEncodedFormEntity(vals, "UTF-8");
97     httppost.setEntity(ue);
98     HttpResponse response = httpclient.execute(httppost);
99     HttpEntity resEntity = response.getEntity();
100
101     if (resEntity != null)
102     {
103       BufferedReader r = new BufferedReader(new InputStreamReader(
104               resEntity.getContent()));
105       return r;
106     }
107     else
108     {
109       return null;
110     }
111   }
112   public static BufferedReader doHttpMpartInputstreamPost(String postUrl,
113           List<NameValuePair> vals, String fparm,String fname, InputStream is, String mtype) throws ClientProtocolException,
114           IOException
115   {
116     HttpClient httpclient = new DefaultHttpClient();
117     HttpPost httppost = new HttpPost(postUrl);
118     MultipartEntity mpe = new MultipartEntity(HttpMultipartMode.STRICT);
119     for (NameValuePair nvp:vals)
120     {
121       mpe.addPart(nvp.getName(), new StringBody(nvp.getValue()));
122     }
123     
124     InputStreamBody fb = (mtype!=null) ? new InputStreamBody(is, fname, mtype) : new InputStreamBody(is, fname);
125     mpe.addPart(fparm, fb);
126     UrlEncodedFormEntity ue = new UrlEncodedFormEntity(vals, "UTF-8");
127     httppost.setEntity(ue);
128     HttpResponse response = httpclient.execute(httppost);
129     HttpEntity resEntity = response.getEntity();
130
131     if (resEntity != null)
132     {
133       BufferedReader r = new BufferedReader(new InputStreamReader(
134               resEntity.getContent()));
135       return r;
136     }
137     else
138     {
139       return null;
140     }
141   }
142 }