Spring WS: log HTTP Headers of SOAP request/response

Here is another quick’n’dirty snippet for printing HTTP headers of SOAP requests/responses.

You can set the interceptor when creating the WebServiceTemplate object.

Spring WS version : 2.2.3.RELEASE

// import org.springframework.ws.*;
// import org.apache.http.Header;
// ArrayUtils (apache commons-lang)
// WebServiceTemplate wsTemplate

ClientInterceptor[] interceptors = wsTemplate.getInterceptors();
        
interceptors = (ClientInterceptor[]) ArrayUtils.add(interceptors, new ClientInterceptor() {
    @Override
    public boolean handleRequest(MessageContext messageContext) throws WebServiceClientException {
        return true;
    }

    @Override
    public boolean handleResponse(MessageContext messageContext) throws WebServiceClientException {
        return true;
    }

    @Override
    public boolean handleFault(MessageContext messageContext) throws WebServiceClientException {
        return true;
    }

    @Override
    public void afterCompletion(MessageContext messageContext, Exception ex) throws WebServiceClientException {
		HttpComponentsConnection connection = (HttpComponentsConnection) TransportContextHolder.getTransportContext().getConnection();

		String requestHeaders = "";
		for(Header header : connection.getHttpPost().getAllHeaders()) {
			requestHeaders += header.getName() + ": " + header.getValue() + "; ";
		}

		String responseHeaders = "";
		for(Header header : connection.getHttpResponse().getAllHeaders()) {
			responseHeaders += header.getName() + ": " + header.getValue() + "; ";
		}

		System.out.println("Request HTTP Headers : "+requestHeaders);
		System.out.println("Response HTTP Headers : "+responseHeaders);
    }
});

wsTemplate.setInterceptors(interceptors);

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s