WebLogic 12c中未触发Apache CXF拦截器?

时间:2014-09-24 09:39:52

标签: apache cxf interceptor weblogic12c

拜托,我被困多日,总是出现同样的错误。我正在测试helloWorld apache cxf示例,其中我添加了一个拦截器以从HTTP头获取信息。

我正在使用IDEA Intellij 11.0.2进行开发,当我在Intellij中测试它时它运行得非常好但是当我在WebLogic 12c中部署工件时,我总是有一个空指针异常。

我在个人计算机(Windows 7 Professional)中的本地WebLogic 12c以及UNIX服务器AIX 7.1中的WebLogic 12c中遇到同样的错误。

我使用JDK 1.7.0和Apache CXF 2.7.12。

当我在IDEA Intellij中调试代码时,我看到拦截器构造函数被执行,而handleMessage也被执行,所以一切正常。但是在WebLogic中进行调试时,会执行构造函数,但永远不会执行handleMessage。

我的问题在于这行代码:

    Message message = PhaseInterceptorChain.getCurrentMessage(); 

变量"消息"我在IDEA Intellij中执行/调试时正确填充,但在WebLogic中执行/ debug时它总是为NULL。无论如何,我认为问题是在我写的之前发生的," handleMessage"永远不会在WebLogic中调用,但在IDEA Intellij中调用。

我在IDEA Intellij和WebLogic中添加一些调试截图,向您展示我的意思。我换了一行:

    Message message = PhaseInterceptorChain.getCurrentMessage(); 

对于这3行,因为我看到我有相同的结果,但在调试时会有更多的东西显示:

    Bus bus = BusFactory.getDefaultBus(); 
    PhaseInterceptorChain chain = new PhaseInterceptorChain(bus.getExtension(PhaseManager.class).getInPhases()); 
    Message message = chain.getCurrentMessage(); 

这是我在Intellij中调试时得到的(一切正常):

- >对不起,我发布图片直到获得至少10个声望。无论如何,这里我展示了拦截器是以编程方式添加的,并且在运行时,拦截器中的构造函数和handleMessage被执行,所以一切正常。 " bus"," chain"和"消息"正确填充。

这是我在WebLogic中调试时得到的结果(不按预期工作):

- >对不起,我发布图片直到获得至少10个声望。无论如何,在这里我展示了拦截器是以编程方式添加的,在运行时,拦截器中的构造函数被执行但拦截器中的handleMessage永远不会被执行所以最后我有一个错误,因为" bus"," chain&#34 ;填充,但"消息"是空的。

最后,一些代码:

1)HelloWorldImpl.java

package example; 

import org.apache.cxf.Bus; 
import org.apache.cxf.BusFactory; 

import javax.jws.WebMethod; 
import javax.jws.WebService; 

import org.apache.cxf.message.Message; 
import org.apache.cxf.phase.PhaseInterceptorChain; 
import org.apache.cxf.phase.PhaseManager; 

@WebService() 
public class HelloWorldImpl implements HelloWorld { 

    public HelloWorldImpl() { 
        Bus bus = BusFactory.getDefaultBus(); 
        UserCredentialInterceptor myInterceptor = new UserCredentialInterceptor(); 
        bus.getInInterceptors().add(myInterceptor); 
    } 

    @WebMethod 
    public String sayHelloWorldFrom(String from) { 
        //Message message = PhaseInterceptorChain.getCurrentMessage(); 
        Bus bus = BusFactory.getDefaultBus(); 
        PhaseInterceptorChain chain = new PhaseInterceptorChain(bus.getExtension(PhaseManager.class).getInPhases()); 
        Message message = chain.getCurrentMessage(); 
        if (message==null) { 
            System.out.println("HELLOWORLD ERROR"); 
        } else { 
            System.out.println("HELLOWORLD OK"); 
        } 

        String result = "Hello, world, from " + from; 
        System.out.println(result); 
        return result; 
    } 
} 

2)UserCredentialInterceptor.java

package example; 

import org.apache.cxf.binding.soap.SoapMessage; 
import org.apache.cxf.binding.soap.interceptor.AbstractSoapInterceptor; 
import org.apache.cxf.binding.soap.saaj.SAAJInInterceptor; 
import org.apache.cxf.interceptor.Fault; 
import org.apache.cxf.phase.Phase; 
import org.apache.cxf.transport.http.AbstractHTTPDestination; 
import org.w3c.dom.NodeList; 
import javax.servlet.http.HttpServletRequest; 
import javax.xml.soap.SOAPException; 
import javax.xml.soap.SOAPHeader; 
import javax.xml.soap.SOAPMessage; 
import java.util.Enumeration; 
import javax.servlet.http.Cookie; 

public class UserCredentialInterceptor extends AbstractSoapInterceptor { 
    private SAAJInInterceptor saajIn = new SAAJInInterceptor(); 

    public UserCredentialInterceptor() { 
        super(Phase.PRE_PROTOCOL); 
        getAfter().add(SAAJInInterceptor.class.getName()); 
    } 

    public void handleMessage(SoapMessage message) throws Fault { 
        SOAPMessage doc = message.getContent(SOAPMessage.class); 
        if (doc == null) { 
            saajIn.handleMessage(message); 
            doc = message.getContent(SOAPMessage.class); 
        } 
        SOAPHeader headerr = null; 
        try { 
            headerr = doc.getSOAPHeader(); 
        } catch (SOAPException e) { 
            e.printStackTrace(); 
        } 
        if (headerr != null) { 
            NodeList nodes = headerr.getElementsByTagNameNS("http://asjava.com/types", "Username"); 
            if (nodes != null && nodes.item(0) != null) { 
                String user = nodes.item(0).getTextContent(); 
            } 
        } 
        //if you want to read more http header messages, just use get method to obtain from  HttpServletRequest. 
        HttpServletRequest request = (HttpServletRequest) message.get(AbstractHTTPDestination.HTTP_REQUEST); 
        if(null!=request){ 
            //Read http header to get client IP adress 
            String addr = request.getRemoteAddr(); 
            //Read http header to get HeaderNames 
            Enumeration enums = request.getHeaderNames(); 
            //Read http header to get cookie/ 
            Cookie[] cookies = request.getCookies(); 
        } 
    } 
} 

3)application.xml

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE application PUBLIC 
        "-//Sun Microsystems, Inc.//DTD J2EE Application 1.3//EN" 
        "http://java.sun.com/dtd/application_1_3.dtd"><application>
    <display-name>test_interceptors</display-name>
    <module>
        <web>
            <web-uri>/</web-uri>
            <context-root>/</context-root>
        </web>
    </module>
</application>

4)weblogic-application.xml

<?xml version="1.0" encoding="UTF-8"?> 
<wls:weblogic-application xmlns:wls="http://www.bea.com/ns/weblogic/weblogic-application" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/javaee_5.xsd http://www.bea.com/ns/weblogic/weblogic-application http://www.bea.com/ns/weblogic/weblogic-application/1.0/weblogic-application.xsd">
    <wls:application-param>
        <wls:param-name>webapp.encoding.default</wls:param-name>
        <wls:param-value>UTF-8</wls:param-value>
    </wls:application-param>
    <wls:prefer-application-packages>
        <wls:package-name>javax.wsdl.*</wls:package-name>
        <wls:package-name>javax.ws.rs.*</wls:package-name>
    </wls:prefer-application-packages>
</wls:weblogic-application>

5)web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                  http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
           version="2.5">

    <description>cxf</description>

    <display-name>cxf</display-name>
    <servlet>
        <description>Apache CXF Endpoint</description>
        <display-name>cxf</display-name>
        <servlet-name>cxf</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>cxf</servlet-name>
        <url-pattern>/services/*</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>60</session-timeout>
    </session-config>
</web-app>

6)weblogic.xml

<weblogic-web-app 
        xmlns="http://www.bea.com/ns/weblogic/weblogic-web-app"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.bea.com/ns/weblogic/weblogic-web-app/1.0/weblogic-web-app.xsd">

    <container-descriptor>
        <prefer-web-inf-classes>true</prefer-web-inf-classes>
    </container-descriptor>

</weblogic-web-app>

7)cxf-servlet.xml

<?xml version="1.0" encoding="UTF-8"?> 

<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:cxf="http://cxf.apache.org/core"
      xmlns:jaxws="http://cxf.apache.org/jaxws"
      xmlns:soap="http://cxf.apache.org/bindings/soap"
      xsi:schemaLocation=" 
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
</beans>

拜托,非常感谢任何帮助。

感谢 菲利克斯梅卡德。

1 个答案:

答案 0 :(得分:0)

您的cxf-servlet.xml根本没有定义任何bean。因此,我猜测Weblogic JAX-WS实现正在触发和部署服务,甚至没有使用CXF。在服务中执行Thread.dumpStack以查看正在执行的实现。我愿意打赌那里没有任何org.apache.cxf。