HTTP response jax-ws request and response

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!!

+6
source share
1 answer

You can also try

  -Dcom.sun.xml.ws.transport.http.HttpAdapter.dump=true 

I assume that you are providing your web service from some Java EE application server (and not from a standalone client). You cannot access the Java EE framework, for example, HttpServletRequest and HttpServletResponse outside the context of the Web / Java EE container.

You can try to access the actual servlet response object (in a web context) with

  HttpServletResponse response = (HttpServletResponse) messageContext.get(SOAPMessageContext.SERVLET_RESPONSE); //messageContext is the SOAPMessageContext List<String> responseHeaderNames = (List<String>)response.getHeaderNames(); for(String headerName : responseHeaderNames){ //Do whatever you want with it. } 

I seriously doubt that you can get all the response headers in the handler. Your question really intrigued me, and I spent a lot of time studying this part. In all the code examples that I saw, even the example on the website does not allow this functionality to be implemented, and I think the reason is simple. As at the time the handler was called, the container may not have enough final information for stamping the http header of the outgoing message. You may be able to add material, but this is doubtful.

+2
source

All Articles