users@glassfish.java.net

Adding an interceptor to WebService

From: David Wheeler <david_at_inomial.com>
Date: Fri, 4 Jan 2013 16:25:46 +1100

Hi Folks,

I'm trying to add an interceptor to a WebService class so that SOAP calls are intercepted. I have it working but not the way I need it to work: it will work using a URL that it generates itself, but not using the url that I specify in web.xml

Glassfish: 3.1.2.2
WebService:

@WebService(name="WSNew", targetNamespace="http://ws.inomial.com/smile.2", serviceName="WSNewService", portName="WSNewPort")
@Interceptors({SessionManagerInterceptor.class})
@Stateless(name="WSNew")
public class WSNew implements WebServiceContextProvider
{

  @Resource
  private WebServiceContext context;

  @WebMethod
  @WebResult(name="result")
  public String getMe()
  {
    return "me";
  }

  @Override
  @WebMethod(exclude=true)
  public WebServiceContext getWsContext()
  {
    return context;
  }
}


important bits of web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <!-- ... -->

  <servlet>
    <servlet-name>WSNew</servlet-name>
    <servlet-class>com.inomial.smile.ws.v2.subscription.WSNew</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>WSNew</servlet-name>
    <url-pattern>/ws/new</url-pattern>
  </servlet-mapping>

    <!-- ... -->
</web-app>

Notes:
* I only added the @Stateless to get the @Interceptors annotation to be recognised, I don't intend to use this class as an EJB
* same behaviour if serviceName, portName not specified in @WebService
 
The app is deployed as a .war file. When I access the endpoint via http://localhost:8080/WSNewService/WSNew then it all works splendidly. But my web.xml should make it use http://localhost:8080/ws/new, and that's not working. It fails with this in the logs:

[#|2013-01-04T15:49:31.491+1100|INFO|glassfish3.1.2|javax.enterprise.system.container.web.com.sun.enterprise.web|_ThreadID=58;_ThreadName=Thread-4;|PWC1412: WebModule[null] ServletContext.log():PWC1409: Marking servlet WSNew as unavailable|#]

[#|2013-01-04T15:49:31.492+1100|WARNING|glassfish3.1.2|javax.enterprise.system.container.web.com.sun.enterprise.web|_ThreadID=58;_ThreadName=Thread-4;|StandardWrapperValve[WSNew]: PWC1382: Allocate exception for servlet WSNew
java.lang.ClassCastException: com.inomial.smile.ws.v2.subscription.WSNew cannot be cast to javax.servlet.Servlet
        at com.sun.enterprise.web.WebContainer.createServletInstance(WebContainer.java:729)
        at com.sun.enterprise.web.WebModule.createServletInstance(WebModule.java:1959)
        at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1272)
        at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:1079)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:189)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
        at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
        at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
        at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
        at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
        at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
        at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860)
        at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757)
        at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056)
        at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229)
        at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
        at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
        at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
        at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
        at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
        at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
        at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
        at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
        at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
        at java.lang.Thread.run(Thread.java:680)
|#]

I've tried replacing the servlet-class with com.sun.xml.ws.transport.http.servlet.WSServlet (something I saw on the internet somewhere), and that makes the exception go away, but the wsdl at that location is empty.

Is anyone able to tell me if this is likely to be a bug? Is there anything else I can try?

Cheers,

-- David