PDA

View Full Version : CommonsHttpInvokerRequestExecutor ignoring HTTP status code



james_a_woods
Sep 26th, 2005, 09:27 PM
I am writing an application using the spring HTTP remote invocation framework in conjunction with the Commons HttpClient.

I found I was having great difficulty diagnosising problems that I was having as the CommonsHttpInvokerRequestExecutor.executePostMetho d(...) method ignores the HTTP status code. I was then just getting a corrupted stream error on the read operation later on in the process.

I have worked around this in a rather simplistic manner by subclassing the CommonsHttpInvokerRequestExecutor and implementing:



protected void executePostMethod(HttpInvokerClientConfiguration pConfig, HttpClient pHttpClient, PostMethod postMethod) throws IOException {
try {
super.executePostMethod(pConfig, pHttpClient, postMethod);

} catch (IOException e) {
HttpClient httpClient = getHttpClient();

if (httpClient.getHostConfiguration().isProxySet() ) {
throw new HttpRemoteException("Error communicating with proxy server", e);
} else {
throw new HttpRemoteException("Error communicating with remote server", e);
}
}

if (postMethod.getStatusCode() >= 300) {
throw new HttpRemoteException("HTTP error communicating with remote server: ",
postMethod.getStatusCode(), postMethod.getStatusText());
}
}


Though I'm sure a more sophisticated approach would be required for general consumption, I though that this would be worth posting as a suggestion.

Cheers
James

Juergen Hoeller
Oct 8th, 2005, 05:30 PM
Thanks for the suggestion: I've added "validateResponse" template methods to both SimpleHttpInvokerRequestExecutor and CommonsHttpInvokerRequestExecutor, getting called before the response body is parsed. The default implementations reject any HTTP status code beyond 2xx, which can be customized in subclasses.

This will be part of the upcoming Spring 1.2.6.

Juergen