SOAPHandler的handleMessage方法未被调用,而getHeaders被调用了

问题描述 投票:5回答:1

我是SOAP世界的新手。

我已经使用maven插件将wsdl文件隐藏为java

下面是pom.xml配置。

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

下面是课程文件

接口定义

@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
);
}

这里是扩展服务的类文件

    @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);
       }

    }       

我的SoapHandler文件为

       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;
           }

       }

我的handlerResolver文件是

     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;
         }

     }

用于致电服务的方法

        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());

我无法通过soapHandlerhandleMessage方法获得呼叫并继续进行getHeadersmethod when the service methodClientRequestResponse response = port.clientRequest(request);被调用。

我从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

我仍然无法调用处理程序的handleMessage方法,因为未打印任何日志。调用是在handlerResolver类中进行的,而不是在HeaderHandler类中进行的。

我该如何解决此问题?

java web-services maven soap soap-client
1个回答
0
投票
© www.soinside.com 2019 - 2024. All rights reserved.