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