1 // Copyright (C) 2004, 2014 Christian Jaekl
3 package net.jaekl.qd.http;
5 import java.io.BufferedReader;
6 import java.io.IOException;
7 import java.io.InputStream;
8 import java.io.InputStreamReader;
9 import java.io.UnsupportedEncodingException;
10 import java.lang.reflect.InvocationTargetException;
11 import java.util.ArrayList;
13 import net.jaekl.qd.QDException;
14 import net.jaekl.qd.util.ExceptionUtils;
15 import net.jaekl.qd.xml.ParseHandler;
16 import net.jaekl.qd.xml.ParseResult;
18 import org.apache.http.HttpEntity;
19 import org.apache.http.HttpResponse;
20 import org.apache.http.NameValuePair;
21 import org.apache.http.client.HttpClient;
22 import org.apache.http.client.config.RequestConfig;
23 import org.apache.http.client.entity.UrlEncodedFormEntity;
24 import org.apache.http.client.methods.HttpPost;
25 import org.apache.http.impl.client.HttpClientBuilder;
26 import org.xml.sax.InputSource;
27 import org.xml.sax.SAXException;
28 import org.xml.sax.XMLReader;
29 import org.xml.sax.helpers.XMLReaderFactory;
31 public class RequestBroker
33 final String UTF_8 = "UTF-8";
34 final int TIMEOUT_MSEC = 5000; // Allow at most 5 seconds before we declare the server to be unresponsive
37 ArrayList<NameValuePair> m_baseParams;
39 public RequestBroker(String gatewayUrl, ArrayList<NameValuePair> baseParams)
41 m_gatewayUrl = gatewayUrl;
42 m_baseParams = new ArrayList<NameValuePair>(baseParams);
45 // NB: Caller is responsible for close()ing the returned InputStream
47 InputStream doSubmit(String method, ArrayList<NameValuePair> passedParams) throws QDException
49 ArrayList<NameValuePair> params = new ArrayList<NameValuePair>(m_baseParams);
50 params.addAll(passedParams);
53 RequestConfig.Builder requestBuilder = RequestConfig.custom();
54 requestBuilder = requestBuilder.setConnectTimeout(TIMEOUT_MSEC);
55 requestBuilder = requestBuilder.setSocketTimeout(TIMEOUT_MSEC);
56 HttpClientBuilder builder = HttpClientBuilder.create();
57 builder.setDefaultRequestConfig(requestBuilder.build());
58 HttpClient httpClient = builder.build();
59 HttpPost httpPost = new HttpPost(m_gatewayUrl + "/" + method + "/");
60 httpPost.setEntity(new UrlEncodedFormEntity(params, UTF_8));
61 HttpResponse response = httpClient.execute(httpPost);
62 HttpEntity entity = response.getEntity();
65 InputStream is = entity.getContent();
69 catch (UnsupportedEncodingException uee) {
70 // We should actually be guaranteed that this never happens,
71 // because all JVMs are required to support UTF-8
75 catch (IOException ioe) {
76 throw new QDException(ioe);
82 public String submit(String method, ArrayList<NameValuePair> passedParams) throws QDException
84 StringBuilder sb = new StringBuilder();
85 InputStream is = null;
88 is = doSubmit(method, passedParams);
89 BufferedReader br = new BufferedReader(new InputStreamReader(is));
90 String line = br.readLine();
91 while (null != line) {
92 sb.append(line).append("\n");
96 catch (IOException ioe) {
97 throw new QDException(ioe);
100 ExceptionUtils.tryClose(is);
103 return sb.toString();
106 public ParseResult submitAndParse(String method,
107 ArrayList<NameValuePair> passedParams,
108 Class<? extends ParseResult> rootParserClass)
111 return submitAndParse(method, passedParams, rootParserClass, null);
114 public ParseResult submitAndParse(String method,
115 ArrayList<NameValuePair> passedParams,
116 Class<? extends ParseResult> rootParserClass,
120 ParseResult result = null;
121 InputStream is = null;
124 if (null == rootTagName) {
125 result = (ParseResult) rootParserClass.newInstance();
128 result = (ParseResult) rootParserClass.getDeclaredConstructor(String.class).newInstance(rootTagName);
130 is = doSubmit(method, passedParams);
131 XMLReader reader = XMLReaderFactory.createXMLReader();
132 ParseHandler ph = new ParseHandler(result);
133 reader.setContentHandler(ph);
134 reader.parse(new InputSource(is));
136 catch ( InstantiationException
137 | InvocationTargetException
138 | IllegalAccessException
140 | NoSuchMethodException
144 throw new QDException(e);
147 ExceptionUtils.tryClose(is);