1
2
3
4
5
6
7
8
9
10
11 package net.kwfgrid.gwes.restfulclient;
12
13 import net.kwfgrid.gwes.exception.GWESException;
14 import org.apache.log4j.Logger;
15 import org.xmlpull.v1.XmlPullParser;
16 import org.xmlpull.v1.XmlPullParserFactory;
17 import org.xmlpull.v1.XmlPullParserException;
18
19 import java.net.HttpURLConnection;
20 import java.net.URL;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.OutputStream;
24 import java.io.ByteArrayInputStream;
25 import java.util.ArrayList;
26 import java.util.List;
27 import java.rmi.RemoteException;
28
29 import net.kwfgrid.gwes.util.StringUtils;
30
31
32
33
34
35
36
37 public class RestfulClient {
38
39 final static Logger logger = Logger.getLogger(RestfulClient.class);
40 private static final String ELEMENT_RETURN = "ns:return";
41
42
43
44
45 public static enum Method {
46 GET,
47 POST,
48 HEAD,
49 OPTIONS,
50 PUT,
51 DELETE,
52 TRACE
53 }
54
55 private static final String GET = "GET";
56 private static final String POST = "POST";
57 private static final String HEAD = "HEAD";
58 private static final String OPTIONS = "OPTIONS";
59 private static final String PUT = "PUT";
60 private static final String DELETE = "DELETE";
61 private static final String TRACE = "TRACE";
62
63
64
65
66
67
68
69 public String[] httpGET(String url) throws RemoteException {
70 try {
71 List response = request(new URL(url), Method.GET, null);
72 return (String[]) response.toArray(new String[0]);
73 } catch (IOException e) {
74 throw new RemoteException("exception during HTTP GET "+url+": " + e, e);
75 } catch (XmlPullParserException e) {
76 throw new RemoteException("exception during HTTP GET "+url+": " + e, e);
77 }
78 }
79
80
81
82
83
84
85
86 public String[][] httpGETArray(String url) throws RemoteException {
87 try {
88 List<List<String>> response = requestArray(new URL(url), Method.GET, null);
89 return StringUtils.convertListListToStringArrayArray(response);
90 } catch (IOException e) {
91 throw new RemoteException("exception during HTTP GET "+url+": " + e, e);
92 } catch (XmlPullParserException e) {
93 throw new RemoteException("exception during HTTP GET "+url+": " + e, e);
94 }
95 }
96
97
98
99
100
101
102
103 public String[] httpPUT(String url, String payload) throws RemoteException {
104 try {
105 InputStream in = null;
106 if (payload != null) {
107 in = new ByteArrayInputStream(payload.getBytes("UTF-8"));
108 }
109 List response = request(new URL(url), Method.PUT, in);
110 return (String[]) response.toArray(new String[0]);
111 } catch (IOException e) {
112 throw new RemoteException("exception during HTTP PUT "+url+": " + e, e);
113 } catch (XmlPullParserException e) {
114 throw new RemoteException("exception during HTTP PUT "+url+": " + e, e);
115 }
116 }
117
118
119
120
121
122
123
124 public String[] httpPOST(String url, String payload) throws RemoteException {
125 try {
126 InputStream in = null;
127 if (payload != null) {
128 in = new ByteArrayInputStream(payload.getBytes("UTF-8"));
129 }
130 List response = request(new URL(url), Method.POST, in);
131 return (String[]) response.toArray(new String[0]);
132 } catch (IOException e) {
133 throw new RemoteException("exception during HTTP POST "+url+": " + e, e);
134 } catch (XmlPullParserException e) {
135 throw new RemoteException("exception during HTTP POST "+url+": " + e, e);
136 }
137 }
138
139
140
141
142
143
144
145 public String[] httpDELETE(String url) throws RemoteException {
146 try {
147 List response = request(new URL(url), Method.DELETE, null);
148 return (String[]) response.toArray(new String[0]);
149 } catch (IOException e) {
150 throw new RemoteException("exception during HTTP DELETE "+url+": " + e, e);
151 } catch (XmlPullParserException e) {
152 throw new RemoteException("exception during HTTP DELETE "+url+": " + e, e);
153 }
154 }
155
156
157
158
159
160
161
162
163
164
165 public static List<String> request(URL url, Method method, InputStream payload) throws XmlPullParserException, IOException {
166
167 if (logger.isDebugEnabled()) {
168 logger.debug("trying to connect to "+url.toString()+" using method "+getMethodAsString(method)+"...");
169 }
170
171 HttpURLConnection connection = (HttpURLConnection)url.openConnection();
172 connection.setRequestMethod(getMethodAsString(method));
173
174
175
176
177
178
179 writeBodyToConnection(payload, connection);
180
181 ArrayList<String> list = new ArrayList<String>();
182 try {
183
184 connection.connect();
185 parseXmlTextToStringList(connection.getInputStream(), list);
186 } catch (IOException e) {
187 throw new RemoteException("Error message returned by server: \n"+ StringUtils.convertStreamToString(connection.getErrorStream()),e);
188 }
189
190 connection.disconnect();
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210 return list;
211 }
212
213
214
215
216
217
218
219
220
221
222 public static List<List<String>> requestArray(URL url, Method method, InputStream payload) throws XmlPullParserException, IOException {
223
224 if (logger.isDebugEnabled()) {
225 logger.debug("trying to connect to "+url.toString()+" using method "+getMethodAsString(method)+"...");
226 }
227
228 HttpURLConnection connection = (HttpURLConnection)url.openConnection();
229 connection.setRequestMethod(getMethodAsString(method));
230 writeBodyToConnection(payload, connection);
231
232 List<List<String>> listList = new ArrayList<List<String>>();
233 try {
234
235 connection.connect();
236 parseXmlTextToStringListList(connection.getInputStream(), listList);
237 } catch (IOException e) {
238 throw new RemoteException("Error message returned by server: \n"+ StringUtils.convertStreamToString(connection.getErrorStream()),e);
239 }
240
241 connection.disconnect();
242 return listList;
243 }
244
245 private static void writeBodyToConnection(InputStream payload, HttpURLConnection connection) throws IOException {
246
247 int read = 0;
248 if (payload != null) {
249 byte buffer[] = new byte[8192];
250 connection.setDoOutput(true);
251 OutputStream output = connection.getOutputStream();
252 while ((read = payload.read(buffer)) != -1) {
253 output.write(buffer, 0, read);
254 }
255 }
256 }
257
258 private static void parseXmlTextToStringList(InputStream in, List<String> list) throws XmlPullParserException, IOException {
259 XmlPullParser parser = getParser(in);
260 int event = parser.next();
261 int lastEvent;
262 while (event != XmlPullParser.END_DOCUMENT) {
263 boolean startTag = false;
264 if (event == XmlPullParser.TEXT) {
265 list.add(parser.getText());
266 }
267 lastEvent = event;
268 event = parser.next();
269
270 if (lastEvent == XmlPullParser.START_TAG && event == XmlPullParser.END_TAG) {
271 list.add("");
272 }
273 }
274 }
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296 private static void parseXmlTextToStringListList(InputStream in, List<List<String>> listList) throws XmlPullParserException, IOException {
297
298
299
300
301
302
303
304
305
306
307
308 List<String> list = null;
309
310 XmlPullParser parser = getParser(in);
311 int event = parser.next();
312 int lastEvent = -1;
313 while (event != XmlPullParser.END_DOCUMENT) {
314
315 if (event == XmlPullParser.START_TAG && parser.getName().equals(ELEMENT_RETURN)) {
316 list = new ArrayList<String>();
317 }
318 else if (event == XmlPullParser.TEXT && list != null) {
319 list.add(parser.getText());
320 }
321 else if (event == XmlPullParser.END_TAG && list != null) {
322
323 if (lastEvent == XmlPullParser.START_TAG) {
324 list.add("");
325 }
326
327 if (parser.getName().equals(ELEMENT_RETURN)) {
328 listList.add(list);
329 list = null;
330 }
331 }
332 lastEvent = event;
333 event = parser.next();
334 }
335 }
336
337
338
339
340
341
342 public static String getMethodAsString(Method method) {
343 switch (method) {
344 case DELETE:
345 return DELETE;
346 case GET:
347 return GET;
348 case HEAD:
349 return HEAD;
350 case OPTIONS:
351 return OPTIONS;
352 case POST:
353 return POST;
354 case PUT:
355 return PUT;
356 case TRACE:
357 return TRACE;
358 }
359 return null;
360 }
361
362 private static XmlPullParser getParser(InputStream inputStream) throws XmlPullParserException {
363 XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
364 XmlPullParser parser = factory.newPullParser();
365 parser.setInput(inputStream,null);
366 return parser;
367 }
368
369 }