Sunday, August 20, 2017

handleMessage method of SOAPHandler not getting invoked,rather getHeaders get invoked

Leave a Comment

I am new to the SOAP world.

I have coverted the wsdl file to java class using maven plugin

Below is the pom.xml configuration.

<plugin>             <groupId>org.apache.cxf</groupId>             <artifactId>cxf-codegen-plugin</artifactId>             <version>3.1.12</version>             <executions>                 <execution>                     <id>generate-sources</id>                     <phase>generate-sources</phase>                     <configuration>                         <sourceRoot>${project.basedir}/src/main/java</sourceRoot>                         <wsdlOptions>                             <wsdlOption>                                 <wsdl>${project.basedir}/src/main/resources/EIAproxy.wsdl</wsdl>                                 <wsdlLocation>classpath:EIAproxy.wsdl</wsdlLocation>                             </wsdlOption>                         </wsdlOptions>                     </configuration>                     <goals>                         <goal>wsdl2java</goal>                     </goals>                 </execution>             </executions>         </plugin> 

below is the class files

Interface definition

@WebService(targetNamespace = "http://schema.concierge.com", name = "EaiEnvelopeSoap") @XmlSeeAlso({com.concierge.schema.envelope.ObjectFactory.class, ObjectFactory.class}) @SOAPBinding(parameterStyle = SOAPBinding.ParameterStyle.BARE) public interface EaiEnvelopeSoap {  @WebResult(name = "clientRequestResponse", targetNamespace = "http://schema.concierge.com", partName = "parameters") @WebMethod(action = "http://www.openuri.org/clientRequest")   public ClientRequestResponse clientRequest(     @WebParam(partName = "parameters", name = "clientRequest", targetNamespace = "http://schema.concierge.com")     ClientRequest parameters ); } 

Here is class file that extends service

    @WebServiceClient(name = "EaiEnvelope",                       wsdlLocation = "classpath:EIAproxy.wsdl",                      targetNamespace = "http://schema.concierge.com")      public class EaiEnvelope extends Service {         public final static URL WSDL_LOCATION;         public final static QName SERVICE = new QName("http://schema.concierge.com", "EaiEnvelope");        public final static QName EaiEnvelopeSoap = new QName("http://schema.concierge.com", "EaiEnvelopeSoap");        static {            URL url = EaiEnvelope.class.getClassLoader().getResource("EIAproxy.wsdl");            if (url == null) {                java.util.logging.Logger.getLogger(EaiEnvelope.class.getName())                    .log(java.util.logging.Level.INFO,                          "Can not initialize the default wsdl from {0}", "classpath:EIAproxy.wsdl");            }                   WSDL_LOCATION = url;           }         public EaiEnvelope(URL wsdlLocation) {            super(wsdlLocation, SERVICE);        }         public EaiEnvelope(URL wsdlLocation, QName serviceName) {            super(wsdlLocation, serviceName);        }         public EaiEnvelope() {            super(WSDL_LOCATION, SERVICE);        }         public EaiEnvelope(WebServiceFeature ... features) {            super(WSDL_LOCATION, SERVICE, features);        }         public EaiEnvelope(URL wsdlLocation, WebServiceFeature ... features) {            super(wsdlLocation, SERVICE, features);        }         public EaiEnvelope(URL wsdlLocation, QName serviceName, WebServiceFeature ... features) {            super(wsdlLocation, serviceName, features);        }                /**         *         * @return         *     returns EaiEnvelopeSoap         */        @WebEndpoint(name = "EaiEnvelopeSoap")        public EaiEnvelopeSoap getEaiEnvelopeSoap() {            return super.getPort(EaiEnvelopeSoap, EaiEnvelopeSoap.class);        }         /**         *          * @param features         *     A list of {@link javax.xml.ws.WebServiceFeature} to configure on the proxy.  Supported features not in the <code>features</code> parameter will have their default values.         * @return         *     returns EaiEnvelopeSoap         */        @WebEndpoint(name = "EaiEnvelopeSoap")        public EaiEnvelopeSoap getEaiEnvelopeSoap(WebServiceFeature... features) {            return super.getPort(EaiEnvelopeSoap, EaiEnvelopeSoap.class, features);        }      }        

My SoapHandler file is

       import java.io.ByteArrayOutputStream;        import java.io.IOException;        import java.io.OutputStream;        import java.io.StringWriter;        import java.util.Set;         import javax.xml.namespace.QName;        import javax.xml.soap.SOAPBody;        import javax.xml.soap.SOAPElement;        import javax.xml.soap.SOAPEnvelope;        import javax.xml.soap.SOAPHeader;        import javax.xml.soap.SOAPMessage;        import javax.xml.ws.handler.MessageContext;        import javax.xml.ws.handler.soap.SOAPHandler;        import javax.xml.ws.handler.soap.SOAPMessageContext;         import com.xxx.fdp.common.LoggerManager;        import com.xxx.fdp.constants.LoggerConstantEnum;        import com.xxx.fdp.property.config.AbilityConfig;         public class HeaderHandler implements SOAPHandler<SOAPMessageContext> {             /** The logger manager. */            LoggerManager loggerManager = new LoggerManager();             @Override            public boolean handleMessage(SOAPMessageContext smc) {                 Boolean outboundProperty = (Boolean) smc.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);                loggerManager.info(LoggerConstantEnum.AbilityDailySyncUp, "Entered in handleMessage with outBoundProperty : " + outboundProperty);                if (outboundProperty.booleanValue()) {                     SOAPMessage message = smc.getMessage();                    loggerManager.info(LoggerConstantEnum.AbilityDailySyncUp, " Message : " + message);                     try {                        message.writeTo(System.out);                        SOAPEnvelope envelope = smc.getMessage().getSOAPPart().getEnvelope();                        envelope.addNamespaceDeclaration("com", "http://schema.concierge.com");                        message.getMimeHeaders().setHeader("Content-Type", "application/soap+xml; charset=utf-8");                        SOAPHeader header = envelope.addHeader();                         SOAPElement authentication = header.addChildElement("authentication", "auth", "http://schemas.eia.org/middleware/AuthInfo");                         SOAPElement username = authentication.addChildElement("user", "auth");                        username.addTextNode(AbilityConfig.getInstance().getSoapUser());                         SOAPElement password = authentication.addChildElement("password", "auth");                        password.addTextNode(AbilityConfig.getInstance().getSoapPassword());                         SOAPElement authType = authentication.addChildElement("type", "auth");                        authType.addTextNode(AbilityConfig.getInstance().getSoapAuthType());                        SOAPBody body = envelope.getBody();                         // Print out the outbound SOAP message to System.out                        message.saveChanges();                         loggerManager.info(LoggerConstantEnum.AbilityDailySyncUp, "Request Format : " + envelope.getBody() + ",Header : " + envelope.getHeader());                        ByteArrayOutputStream out = new ByteArrayOutputStream();                        message.writeTo(out);                        String strMsg = new String(out.toByteArray());                        loggerManager.info(LoggerConstantEnum.AbilityDailySyncUp, "Request Format Message: " + strMsg);                        StringWriter writer = new StringWriter();                        message.writeTo(new StringOutputStream(writer));                        // message.writeTo(System.out);                        System.out.println("");                     } catch (Exception e) {                        e.printStackTrace();                    }                 } else {                    try {                         // This handler does nothing with the response from the Web                        // Service so                        // we just print out the SOAP message.                        SOAPMessage message = smc.getMessage();                        loggerManager.info(LoggerConstantEnum.AbilityDailySyncUp, "Response Message : " + message);                        message.writeTo(System.out);                        System.out.println("");                     } catch (Exception ex) {                        ex.printStackTrace();                    }                }                loggerManager.info(LoggerConstantEnum.AbilityDailySyncUp, "Exit in handleMessage with outBoundProperty : " + outboundProperty);                return outboundProperty;             }             private static class StringOutputStream extends OutputStream {                 private StringWriter writer;                 public StringOutputStream(StringWriter writer) {                    this.writer = writer;                }                 @Override                public void write(int b) throws IOException {                    writer.write(b);                }            }             @Override            public boolean handleFault(SOAPMessageContext context) {                loggerManager.info(LoggerConstantEnum.AbilityDailySyncUp, "Entered in handleFault ");                return false;            }             @Override            public void close(MessageContext context) {                loggerManager.info(LoggerConstantEnum.AbilityDailySyncUp, "Entered in close ");            }             @Override            public Set<QName> getHeaders() {                // TODO Auto-generated method stub                return null;            }         } 

My handlerResolver file is

     import java.util.ArrayList;      import java.util.List;       import javax.xml.ws.handler.Handler;      import javax.xml.ws.handler.HandlerResolver;      import javax.xml.ws.handler.PortInfo;       import com.xxx.fdp.common.LoggerManager;      import com.xxx.fdp.constants.LoggerConstantEnum;        public class HeaderHandlerResolver implements HandlerResolver {           LoggerManager loggerManager = new LoggerManager();           @SuppressWarnings("rawtypes")          @Override          public List<Handler> getHandlerChain(PortInfo portInfo) {              loggerManager.info(LoggerConstantEnum.AbilityDailySyncUp,"Entered in getHandlerChain");              List<Handler> handlerChain = new ArrayList<Handler>();              loggerManager.info(LoggerConstantEnum.AbilityDailySyncUp,"Created handlerChanin object");              HeaderHandler headerHandler = new HeaderHandler();              loggerManager.info(LoggerConstantEnum.AbilityDailySyncUp,"Created HeaderHandler object");               handlerChain.add(headerHandler);              loggerManager.info(LoggerConstantEnum.AbilityDailySyncUp,"returned handlerChain");               return handlerChain;          }       } 

Method which is used to call service

        public void processRequest(EaiEnvelope envelope,AbilitySyncUpData abilityObject) {          loggerManager.info(LoggerConstantEnum.AbilityDailySyncUp, "| processing ability async up starts with TranactionId : "+abilityObject.getTransactionId());          try {              com.concierge.schema.EaiEnvelope service=new com.concierge.schema.EaiEnvelope();              HeaderHandlerResolver handlerResolver = new HeaderHandlerResolver();              service.setHandlerResolver(handlerResolver);              EaiEnvelopeSoap port=service.getEaiEnvelopeSoap();              ClientRequest request=new ClientRequest();              request.setEaiEnvelope(envelope);              loggerManager.info(LoggerConstantEnum.AbilityDailySyncUp,"Sending request to web service with TranactionId : "+abilityObject.getTransactionId());              ClientRequestResponse response=port.clientRequest(request);              loggerManager.info(LoggerConstantEnum.AbilityDailySyncUp,"Response received "+response+" After sending request to web service with TranactionId : "+abilityObject.getTransactionId());          } catch (Exception e) {              loggerManager.error(LoggerConstantEnum.AbilityDailySyncUp, "|  Exception occured  :  " + e.fillInStackTrace(), e);              writeCsvFile(abilityObject);          }          loggerManager.info(LoggerConstantEnum.AbilityDailySyncUp, "| processRequest method ends here with TranactionId : "+abilityObject.getTransactionId()); 

I am not able to get call in soapHandler's handleMessage method and call is going on getHeadersmethod when the service methodClientRequestResponse response=port.clientRequest(request);` is called.

I have reffered from several answers that are present on stackoverflow:

SoapHandler not called after WS operation is executed

https://stackoverflow.com/a/12712728/1569443

https://stackoverflow.com/a/14523921/1569443

http://www.javadb.com/using-a-message-handler-to-alter-the-soap-header-in-a-web-service-client/

https://soa2world.blogspot.com/2009/05/direct-web-service-client-using-java.html

I am still not able to call handleMessage method of handler as no logs gets printed. Call is made in handlerResolver class but not in HeaderHandler class.

How can I resolve this issue?

0 Answers

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment