SOAPMessageContext.getMessage() doesn't work in Protocol handler on WAS 7

11,633

Solution 1

You can intercept SOAP incoming or outgoing header using SOAP Handler. refer the handle in WS class / delegate calls using annotation and have handleRequest() overwritten in your Handler class.

Ex:

Handler.xml:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<javaee:handler-chains 
     xmlns:javaee="http://java.sun.com/xml/ns/javaee" 
     xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <javaee:handler-chain>
    <javaee:handler>
      <javaee:handler-class>com.test.SoapHeaderValidation</javaee:handler-class>
    </javaee:handler>
  </javaee:handler-chain>
</javaee:handler-chains> 

Delegete Class:

@HandlerChain(file="handler.xml")
public class RequestHandlerDelegate {

   RequestHandler _requestHandler = new RequestHandler();

    public String sendRequest(String xmlString) {
        return _requestHandler.sendRequest(xmlString);
    }

}

Handler Class:

public boolean handleMessage(SOAPMessageContext context) {

        Boolean isRequest = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
        // for response message only, true for outbound messages, false for inbound
        if (!isRequest) {
            try {
                SOAPMessage soapMsg = context.getMessage();
                SOAPEnvelope envelope= soapMsg.getSOAPPart().getEnvelope();
                SOAPHeader header = envelope.getHeader();

                if (header == null) {
                    generateSOAPErrMessage(soapMsg, "NO SOAP Header");
                }
                NodeList nl = header.getElementsByTagName("wsse:UsernameToken");
                String user = nl.item(0).getFirstChild().getFirstChild().getNodeValue();
                String password = nl.item(0).getFirstChild().getNextSibling().getFirstChild().getNodeValue();

            } catch (Excection e) {
                System.Out.println(e);
            }

Solution 2

Following line on your stack trace shows that you have a classloading issue

Caused by: java.lang.NoClassDefFoundError: com/sun/xml/messaging/saaj/soap/SOAPDocumentImpl 

It seems that your application contains some libraries that contains some classes provided by application server. The article below discusses this kind of problems and points to solutions for common cases, also including a link at the bottom for integrating third-party JAX-WS libraries, which seems to be your case (also added direct link below).

Best Practices for Integrating Open Source Software Packages with WebSphere Application Server

Enabling a third-party JAX-WS application in WebSphere Application Server V7

If you prefer the fastest way out, I may suggest to look for and remove all WebServices related libraries in your WAR or EAR, and use what ships with WebSphere instead.

Solution 3

Try this code :(If u using this method for Req\ Rsp logging, it works fine)

public boolean handleMessage(final SOAPMessageContext context) {

        try {
            final boolean isOutgoing = ((Boolean) context
                    .get(MessageContext.MESSAGE_OUTBOUND_PROPERTY)).booleanValue();

            final SOAPMessage message = context.getMessage();

            if (null != message && null != message.getSOAPBody()) {
                if (isOutgoing) {
                    String response = extractXmlMessage(message.getSOAPBody());
                    RequestResponseLogger.log("RESPONSE", response);
                } else {
                    String request = extractXmlMessage(message.getSOAPBody());
                    RequestResponseLogger.log("REQUEST", request);
                }
            }
        } catch (TransformerException transformerException) {
            if (LOGGER.isErrorEnabled()) {
                LOGGER.error("Unable to log Request Response Message ");
            }
        } catch (SOAPException e) {
            if (LOGGER.isErrorEnabled()) {
                LOGGER.error("Unable to log Request Response Message ");
            }
        }
    return true;
}
Share:
11,633
nick
Author by

nick

bad programmer or good programmer

Updated on June 05, 2022

Comments

  • nick
    nick almost 2 years

    I have created web service using JAX-WS on RAD 8 and Websphere v7. And I made handler using ProtocolHandler to manipulate SOAP message.

    at handleMessage(SOAPMessageContext) method, I called getMessage() to get SOAP message. I couldn't. here's my code

    public boolean handleMessage(SOAPMessageContext context) {
          try {
               SOAPMessage  soapMessage = context.getMessage(); // getMessage() always cause the error
               SOAPPart     soapPart    = soapMessage.getSOAPPart();
               SOAPEnvelope soapEnvelope= soapPart.getEnvelope();
    
           } catch (SOAPException e) {
               System.out.println("SOAPException");
               e.printStackTrace();
           } catch (Exception e) {
               e.printStackTrace();
           }
          return true;
       }
    

    I always got the error below. It seems this problem is related to SAAJ library. But I don't understand that IBM JDK has SAAJ implemented class as far as I know. And... the code above worked two days ago. After that, it doesn't work.

    So I deleted all the project file and create new dynamic web project to check if it work or not. It doesn't work...

    Does someone know the solution? or has same problem?

    [13. 7. 4   21:16:41:028 KST] 0000001c SystemErr     R javax.xml.ws.WebServiceException: com.sun.xml.messaging.saaj.SOAPExceptionImpl: Unable to internalize message
    [13. 7. 4   21:16:41:028 KST] 0000001c SystemErr     R  at org.apache.axis2.jaxws.ExceptionFactory.createWebServiceException(ExceptionFactory.java:175)
    [13. 7. 4   21:16:41:028 KST] 0000001c SystemErr     R  at org.apache.axis2.jaxws.ExceptionFactory.makeWebServiceException(ExceptionFactory.java:70)
    [13. 7. 4   21:16:41:029 KST] 0000001c SystemErr     R  at org.apache.axis2.jaxws.ExceptionFactory.makeWebServiceException(ExceptionFactory.java:128)
    [13. 7. 4   21:16:41:029 KST] 0000001c SystemErr     R  at org.apache.axis2.jaxws.message.impl.MessageImpl.getAsSOAPMessage(MessageImpl.java:344)
    [13. 7. 4   21:16:41:029 KST] 0000001c SystemErr     R  at org.apache.axis2.jaxws.handler.SoapMessageContext.getMessage(SoapMessageContext.java:183)
    [13. 7. 4   21:16:41:029 KST] 0000001c SystemErr     R  at com.koreanair.naora.xml.handler.RAAWPID01ServicesProtocolHandler.handleMessage(RAAWPID01ServicesProtocolHandler.java:28)
    [13. 7. 4   21:16:41:029 KST] 0000001c SystemErr     R  at com.koreanair.naora.xml.handler.RAAWPID01ServicesProtocolHandler.handleMessage(RAAWPID01ServicesProtocolHandler.java:1)
    [13. 7. 4   21:16:41:029 KST] 0000001c SystemErr     R  at org.apache.axis2.jaxws.handler.HandlerChainProcessor.callHandleMessageWithTracker(HandlerChainProcessor.java:871)
    [13. 7. 4   21:16:41:029 KST] 0000001c SystemErr     R  at org.apache.axis2.jaxws.handler.HandlerChainProcessor.handleMessage(HandlerChainProcessor.java:516)
    [13. 7. 4   21:16:41:029 KST] 0000001c SystemErr     R  at org.apache.axis2.jaxws.handler.HandlerChainProcessor.callGenericHandlers(HandlerChainProcessor.java:289)
    [13. 7. 4   21:16:41:029 KST] 0000001c SystemErr     R  at org.apache.axis2.jaxws.handler.HandlerChainProcessor.processChain(HandlerChainProcessor.java:232)
    [13. 7. 4   21:16:41:029 KST] 0000001c SystemErr     R  at org.apache.axis2.jaxws.handler.HandlerInvokerUtils.invokeInboundHandlers(HandlerInvokerUtils.java:65)
    [13. 7. 4   21:16:41:029 KST] 0000001c SystemErr     R  at org.apache.axis2.jaxws.handler.impl.HandlerInvokerImpl.invokeInboundHandlers(HandlerInvokerImpl.java:37)
    [13. 7. 4   21:16:41:029 KST] 0000001c SystemErr     R  at org.apache.axis2.jaxws.server.EndpointController.inboundHeaderAndHandlerProcessing(EndpointController.java:348)
    [13. 7. 4   21:16:41:030 KST] 0000001c SystemErr     R  at org.apache.axis2.jaxws.server.EndpointController.handleRequest(EndpointController.java:260)
    [13. 7. 4   21:16:41:030 KST] 0000001c SystemErr     R  at org.apache.axis2.jaxws.server.EndpointController.invoke(EndpointController.java:103)
    [13. 7. 4   21:16:41:030 KST] 0000001c SystemErr     R  at org.apache.axis2.jaxws.server.JAXWSMessageReceiver.receive(JAXWSMessageReceiver.java:161)
    [13. 7. 4   21:16:41:030 KST] 0000001c SystemErr     R  at org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:189)
    [13. 7. 4   21:16:41:030 KST] 0000001c SystemErr     R  at org.apache.axis2.transport.http.HTTPTransportUtils.processHTTPPostRequest(HTTPTransportUtils.java:275)
    [13. 7. 4   21:16:41:030 KST] 0000001c SystemErr     R  at com.ibm.ws.websvcs.transport.http.WASAxis2Servlet.doPost(WASAxis2Servlet.java:1442)
    [13. 7. 4   21:16:41:030 KST] 0000001c SystemErr     R  at javax.servlet.http.HttpServlet.service(HttpServlet.java:738)
    [13. 7. 4   21:16:41:030 KST] 0000001c SystemErr     R  at javax.servlet.http.HttpServlet.service(HttpServlet.java:831)
    [13. 7. 4   21:16:41:030 KST] 0000001c SystemErr     R  at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1661)
    [13. 7. 4   21:16:41:030 KST] 0000001c SystemErr     R  at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:944)
    [13. 7. 4   21:16:41:030 KST] 0000001c SystemErr     R  at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:507)
    [13. 7. 4   21:16:41:030 KST] 0000001c SystemErr     R  at com.ibm.ws.webcontainer.servlet.ServletWrapperImpl.handleRequest(ServletWrapperImpl.java:181)
    [13. 7. 4   21:16:41:031 KST] 0000001c SystemErr     R  at com.ibm.ws.webcontainer.webapp.WebApp.handleRequest(WebApp.java:3954)
    [13. 7. 4   21:16:41:031 KST] 0000001c SystemErr     R  at com.ibm.ws.webcontainer.webapp.WebGroup.handleRequest(WebGroup.java:276)
    [13. 7. 4   21:16:41:031 KST] 0000001c SystemErr     R  at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:945)
    [13. 7. 4   21:16:41:031 KST] 0000001c SystemErr     R  at com.ibm.ws.webcontainer.WSWebContainer.handleRequest(WSWebContainer.java:1592)
    [13. 7. 4   21:16:41:031 KST] 0000001c SystemErr     R  at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:191)
    [13. 7. 4   21:16:41:031 KST] 0000001c SystemErr     R  at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:453)
    [13. 7. 4   21:16:41:031 KST] 0000001c SystemErr     R  at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewRequest(HttpInboundLink.java:515)
    [13. 7. 4   21:16:41:031 KST] 0000001c SystemErr     R  at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.processRequest(HttpInboundLink.java:306)
    [13. 7. 4   21:16:41:031 KST] 0000001c SystemErr     R  at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.ready(HttpInboundLink.java:277)
    [13. 7. 4   21:16:41:031 KST] 0000001c SystemErr     R  at com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.sendToDiscriminators(NewConnectionInitialReadCallback.java:214)
    [13. 7. 4   21:16:41:031 KST] 0000001c SystemErr     R  at com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.complete(NewConnectionInitialReadCallback.java:113)
    [13. 7. 4   21:16:41:056 KST] 0000001c SystemErr     R  at com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioReadCompletionListener.java:175)
    [13. 7. 4   21:16:41:056 KST] 0000001c SystemErr     R  at com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:217)
    [13. 7. 4   21:16:41:056 KST] 0000001c SystemErr     R  at com.ibm.io.async.AsyncChannelFuture.fireCompletionActions(AsyncChannelFuture.java:161)
    [13. 7. 4   21:16:41:056 KST] 0000001c SystemErr     R  at com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:138)
    [13. 7. 4   21:16:41:056 KST] 0000001c SystemErr     R  at com.ibm.io.async.ResultHandler.complete(ResultHandler.java:204)
    [13. 7. 4   21:16:41:057 KST] 0000001c SystemErr     R  at com.ibm.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:775)
    [13. 7. 4   21:16:41:057 KST] 0000001c SystemErr     R  at com.ibm.io.async.ResultHandler$2.run(ResultHandler.java:905)
    [13. 7. 4   21:16:41:057 KST] 0000001c SystemErr     R  at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1656)
    [13. 7. 4   21:16:41:057 KST] 0000001c SystemErr     R Caused by: com.sun.xml.messaging.saaj.SOAPExceptionImpl: Unable to internalize message
    [13. 7. 4   21:16:41:057 KST] 0000001c SystemErr     R  at com.sun.xml.messaging.saaj.soap.MessageImpl.init(MessageImpl.java:536)
    [13. 7. 4   21:16:41:057 KST] 0000001c SystemErr     R  at com.sun.xml.messaging.saaj.soap.MessageImpl.<init>(MessageImpl.java:316)
    [13. 7. 4   21:16:41:057 KST] 0000001c SystemErr     R  at com.sun.xml.messaging.saaj.soap.ver1_1.SOAPMessageFactory1_1Impl.createMessage(SOAPMessageFactory1_1Impl.java:74)
    [13. 7. 4   21:16:41:057 KST] 0000001c SystemErr     R  at org.apache.axis2.jaxws.message.impl.MessageImpl.getAsSOAPMessage(MessageImpl.java:285)
    [13. 7. 4   21:16:41:057 KST] 0000001c SystemErr     R  ... 41 more
    [13. 7. 4   21:16:41:057 KST] 0000001c SystemErr     R Caused by: java.lang.NoClassDefFoundError: com/sun/xml/messaging/saaj/soap/SOAPDocumentImpl
    [13. 7. 4   21:16:41:057 KST] 0000001c SystemErr     R  at com.sun.xml.messaging.saaj.soap.SOAPPartImpl.<init>(SOAPPartImpl.java:106)
    [13. 7. 4   21:16:41:057 KST] 0000001c SystemErr     R  at com.sun.xml.messaging.saaj.soap.ver1_1.Message1_1Impl.getSOAPPart(Message1_1Impl.java:90)
    [13. 7. 4   21:16:41:058 KST] 0000001c SystemErr     R  at com.sun.xml.messaging.saaj.soap.MessageImpl.initCharsetProperty(MessageImpl.java:1441)
    [13. 7. 4   21:16:41:058 KST] 0000001c SystemErr     R  at com.sun.xml.messaging.saaj.soap.MessageImpl.init(MessageImpl.java:406)
    [13. 7. 4   21:16:41:058 KST] 0000001c SystemErr     R  ... 44 more