I need to record a complete HTTP request and response in a JAX-WS WebService call. For a request, I need the request headers and body, as well as the response, response headers and body.
After some research, I found that I can get this information with the property:
-Dcom.sun.xml.ws.transport.http.client.HttpTransportPipe.dump=true
and show the information that I need, but it dumps it to the console, and I need to save it in the database with the internal request identifier.
I tried to implement a handler:
public class LoggingHandler implements SOAPHandler<SOAPMessageContext> { @Override public boolean handleMessage(SOAPMessageContext context) { Boolean outbound = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY); if (outbound) { System.out.println("SOAP outbound!!!!!"); Map<String, List<String>> responseHeaders = (Map<String, List<String>>) context .get(SOAPMessageContext.HTTP_RESPONSE_HEADERS); try { String headers = getHeaders(responseHeaders); System.out.println(headers); String body = getBody(context.getMessage()); System.out.println(body); } catch (Exception ex) { // TODO: What do I have to do in this case? } } else { System.out.println("SOAP inbound!!!!!"); Map<String, List<String>> requestHeaders = (Map<String, List<String>>) context .get(SOAPMessageContext.HTTP_REQUEST_HEADERS); try { String headers = getHeaders(requestHeaders); System.out.println(headers); String body = getBody(context.getMessage()); System.out.println(body); } catch (Exception ex) { // TODO: What do I have to do in this case? } } return true; } private String getBody(SOAPMessage message) throws SOAPException, IOException { OutputStream stream = new ByteArrayOutputStream(); message.writeTo(stream); return stream.toString(); } public String getFullHttpRequest(HttpServletRequest request) throws IOException { InputStream in = request.getInputStream(); String encoding = request.getCharacterEncoding(); encoding = encoding == null ? "UTF-8" : encoding; String body = IOUtils.toString(in, encoding); return body; } private String getHeaders(Map<String, List<String>> headers) throws IOException { StringBuffer result = new StringBuffer(); if (headers != null) { for (Entry<String, List<String>> header : headers.entrySet()) { if (header.getValue().isEmpty()) { // I don't think this is legal, but let just dump it, // as the point of the dump is to uncover problems. result.append(header.getValue()); } else { for (String value : header.getValue()) { result.append(header.getKey() + ": " + value); } } result.append("\n"); } } return result.toString(); } }
but in this case I can get the headers and body of the HTTP request, but in the response I get only the body, the HTTP response headers are always empty.
Any idea on how to do this? The goal is to be able to store the full HTTP request and response in the database.
Thanks!!
source share