}
URL url = createURL(request);
// Forward "InputStream", Parameters, QueryString to Servlet
HttpMethod httpMethod = null;
if (submitMethod.equals("POST")) {
httpMethod = new PostMethod();
Enumeration params = request.getParameterNames();
while (params.hasMoreElements()) {
String paramName = (String) params.nextElement();
String[] paramValues = request.getParameterValues(paramName);
for (int i = 0; i < paramValues.length; i++) {
((PostMethod) httpMethod).setParameter(paramName, paramValues[i]);
}
}
} else if (submitMethod.equals("GET")) {
httpMethod = new GetMethod();
httpMethod.setQueryString(request.getQueryString());
}
// Copy/clone Cookies
Cookie[] cookies = request.getCookies();
org.apache.commons.httpclient.Cookie[] transferedCookies = null;
if (cookies != null) {
transferedCookies = new org.apache.commons.httpclient.Cookie[cookies.length];
for (int i = 0; i < cookies.length; i++) {
boolean secure = false; // http: false, https: true
transferedCookies[i] = new org.apache.commons.httpclient.Cookie(url.getHost(),
cookies[i].getName(), cookies[i].getValue(), url.getFile(), null,
secure);
}
}
// Initialize HttpClient
HttpClient httpClient = new HttpClient();
// Set cookies
if ((transferedCookies != null) && (transferedCookies.length > 0)) {
HttpState httpState = new HttpState();
httpState.addCookies(transferedCookies);
httpClient.setState(httpState);
}
// DEBUG cookies
// Send request to servlet
httpMethod.setRequestHeader("Content-type", "text/plain");
httpMethod.setPath(url.getPath());
// FIXME
for (Enumeration e = request.getHeaderNames(); e.hasMoreElements();) {
String name = (String) e.nextElement();
httpMethod.addRequestHeader(name, request.getHeader(name));
log.debug("Header Name=" + name + "value=" + request.getHeader(name));
}
HostConfiguration hostConfiguration = new HostConfiguration();
hostConfiguration.setHost(url.getHost(), url.getPort(), url.getProtocol());
log.debug("\n----------------------------------------------------------------"
+ "\n- Starting session at URI: " + url + "\n- Host: "
+ url.getHost() + "\n- Port: " + url.getPort()
+ "\n----------------------------------------------------------------");
if (trustStore != null) {
System.setProperty("javax.net.ssl.trustStore", trustStore);
}
if (trustStorePassword != null) {
System.setProperty("javax.net.ssl.trustStorePassword", trustStorePassword);
}
int result = httpClient.executeMethod(hostConfiguration, httpMethod);
log.debug("\n----------------------------------------------------------------"
+ "\n- Result: " + result
+ "\n----------------------------------------------------------------");
// Handle GZIP Content-Encoding
InputStream is = null;
Header contentEncodingHeader = httpMethod.getResponseHeader("Content-Encoding");
if (contentEncodingHeader != null && contentEncodingHeader.getValue().equalsIgnoreCase("gzip")) {
is = new GZIPInputStream(httpMethod.getResponseBodyAsStream());
log.debug("The content type is gzip.");
} else {
is = httpMethod.getResponseBodyAsStream();
}
// Return XML
InputSource input = new InputSource(is);
parser = (SAXParser) this.manager.lookup(SAXParser.ROLE);
parser.parse(input, this.xmlConsumer);
httpMethod.releaseConnection();
} catch (SAXException e) {
throw e;
} catch (Exception e) {
getLogger().error("Generation failed: ", e);
throw new SAXException(e);