Sunday, 26 February 2017

Dumping HTTP Requests and Responses in WildFly 10

Requirement / Problem

During late nights (or early mornings) of debugging weird issues, you may need to see exactly what HTTP requests are passed into a Web Service.

Depending on your requirements you have two options available:

  • Option 1: Use the the Undertow Request Dumper to log the request and response headers
  • Option 2: Use a system property to log the full request and response messages

I'll show how to do both below:

Option 1: Undertow Request Dumper

While WildFly is shutdown, make the following change in standalone.xml:

 <subsystem xmlns="urn:jboss:domain:undertow:3.1">
    ...
    <server name="default-server">
       ...
       <host name="default-host" alias="localhost">
          ...
          <filter-ref name="request-dumper"/>
       </host>
    </server>
    ...
    <filters>
        ...
        <filter name="request-dumper" module="io.undertow.core" class-name="io.undertow.server.handlers.RequestDumpingHandler"/>
    </filters>
 </subsystem>

Now restart. WildFly should log the headers of your requests and responses nicely, as follows (This example was for a SOAP request for SOAPUI):

 08:31:17,302 INFO  [io.undertow.request.dump] (default task-1)
 ----------------------------REQUEST---------------------------
                URI=/rmoservice/SARBFormWebServiceImpl
  characterEncoding=null
      contentLength=825
        contentType=[multipart/related; type="text/xml"; start="<rootpart@soapui.org>"; boundary="----=_Part_2_1535191634.1488004277175"]
             header=Connection=Keep-Alive
             header=SOAPAction="http://tempuri.org/XXXXXX"
             header=Accept-Encoding=gzip,deflate
             header=Content-Type=multipart/related; type="text/xml"; start="<rootpart@soapui.org>"; boundary="----=_Part_2_1535191634.1488004277175"
             header=Content-Length=825
             header=MIME-Version=1.0
             header=User-Agent=Apache-HttpClient/4.1.1 (java 1.5)
             header=Host=localhost:8080
             locale=[]
             method=POST
           protocol=HTTP/1.1
        queryString=
         remoteAddr=/127.0.0.1:35646
         remoteHost=localhost
             scheme=http
               host=localhost:8080
         serverPort=8080
 --------------------------RESPONSE--------------------------
      contentLength=286
        contentType=text/xml;charset=UTF-8
             header=Connection=keep-alive
             header=X-Powered-By=Undertow/1
             header=Server=WildFly/10
             header=Content-Type=text/xml;charset=UTF-8
             header=Content-Length=286
             header=Date=Sat, 25 Feb 2017 06:31:17 GMT
             status=200
 ==============================================================

Option 2 HttpAdapter.dump

If the headers are not enough for you, because you need the entire message, you can also use the HttpAdapter.dump system property

Once again, open standalone.xml while WildFly is shut down. Add the following entry just after the <extensions> section:

 <system-properties>
    <property name="com.sun.xml.ws.transport.http.HttpAdapter.dump" value="true"/>
 </system-properties>

Restart WildFly

Now WildFly will log the entire inbound and outbound message to the log. Here's an example from my test service

 08:35:52,292 INFO  [org.apache.cxf.services.XXXX.XXXXX.XXXXX] (default task-2) Inbound Message
 ----------------------------
 ID: 2
 Address: http://localhost:8080/XXXX/XXXXX
 Encoding: UTF-8
 Http-Method: POST
 Content-Type: text/xml;charset=UTF-8
 Headers: {accept-encoding=[gzip,deflate], connection=[Keep-Alive], Content-Length=[384], content-type=[text/xml;charset=UTF-8], Host=[localhost:8080], SOAPAction=["http://tempuri.org/XXXX"], User-Agent=[Apache-HttpClient/4.1.1 (java 1.5)]}
 Payload: <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
    <soapenv:Header/>
    <soapenv:Body>
       <tem:XXXX>
          <tem:data>
             <tem:string>testing</tem:string>
          </tem:data>
       </tem:XXXX>
    </soapenv:Body>
 </soapenv:Envelope>
 --------------------------------------
 08:35:52,295 INFO  [org.apache.cxf.services.XXXX.XXXX.XXXX] (default task-2) Outbound Message
 ---------------------------
 ID: 2
 Response-Code: 200
 Encoding: UTF-8
 Content-Type: text/xml
 Headers: {}
 Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><XXXXResponse xmlns="http://tempuri.org/"><XXXXResult>YYYYYYYYYYYYYYYYYY</XXXXResult></XXXXResponse></soap:Body>  </soap:Envelope>

Summary

This is super useful if you need to trace very precisely what is being passed to the server.

Happy Coding

No comments:

Post a Comment