users@jersey.java.net

Abstract servlet adaptor <was> Re: Using HttpServletRequest/HttpServletResponse

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Wed, 19 Sep 2007 14:56:45 +0200

Aaron Anderson wrote:
>
> > Can you log an issue? plus it would help if you can provide an example
> > of what you need so i can cater for it effectively in the abstract
> > class.
>
> Done. Issue #13
>

Now that we have property bag support perhaps an abstract adapter can
retain default support for known servlet parameters, for example [1]
shows the web.xml for the HelloWorldWebApp exmaple, and [2] shows the
web.xml for the GlassfishDB example.

Notice the init-param "webresourceclass" which points in the Java class
that implements ResourceConfig. And, for the GlassfishDB example, the
init-param "unit:WidgetPU", enables the Jersey runtime to inject an
EntityManagerFactory onto resource classes.

It seems appropriate for the abstract servlet adaptor to retain this
functionality by default but yet allow extentions to override:

    /**
     * Create the WebApplication instance.
     * This method may be overridden for servlet adaptors that need to
     * specialize creation.
     */
     protected WebApplication createWebApplication() {
         return WebApplicationFactory.createWebApplication();
     }

    /*
     * Initiate the web application.
     * This method may be overridden for servlet adaptors that need to
     * specialize initiation, for example modifying the features and
     * properties of the ResourceConfig instance or adding further
     * injectables to the WebApplication instance.
     * The overridden method must call WebApplication.initiate or call
     * super.initiate(c, app);
     */
     protected void initiate(ServletConfig servletConfig,
             ResourceConfig c, WebApplication app) {
         app.initiate(this, resourceConfig);
     }

Would that work for you?


I think it may be appropriate for the abstract servlet adapter to
support injection of HttpServletRequest, HttpServletResponse and
ServletConfig i.e. standard servlet stuff. And for the Jersey
implementation to support injection of EntityManagerFactory. That way it
is possible to avoid a dependency on the persistence API jar. Plus the
persistence stuff may require some more work.

Paul.


[1]
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
     <servlet>
         <servlet-name>Jersey Web Application</servlet-name>
 
<servlet-class>com.sun.ws.rest.impl.container.servlet.ServletAdaptor</servlet-class>
         <init-param>
             <param-name>webresourceclass</param-name>
 
<param-value>com.sun.ws.rest.samples.helloworld.resources.WebResources</param-value>
         </init-param>
         <load-on-startup>1</load-on-startup>
     </servlet>
     <servlet-mapping>
         <servlet-name>Jersey Web Application</servlet-name>
         <url-pattern>/*</url-pattern>
     </servlet-mapping>
</web-app>


[2]
<web-app version="2.5" 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">
     <servlet>
         <servlet-name>Jersey Web Application Application</servlet-name>
 
<servlet-class>com.sun.ws.rest.impl.container.servlet.ServletAdaptor</servlet-class>
         <init-param>
             <param-name>webresourceclass</param-name>
             <param-value>com.example.resources.WebResources</param-value>
         </init-param>
         <init-param>
             <param-name>unit:WidgetPU</param-name>
             <param-value>persistence/widget</param-value>
         </init-param>
         <load-on-startup>1</load-on-startup>
     </servlet>
     <servlet-mapping>
         <servlet-name>Jersey Web Application Application</servlet-name>
         <url-pattern>/resources/*</url-pattern>
     </servlet-mapping>
     <session-config>
         <session-timeout>
             30
         </session-timeout>
     </session-config>
     <welcome-file-list>
        <welcome-file>
             index.jsp
         </welcome-file>
     </welcome-file-list>
     <persistence-unit-ref>
 
<persistence-unit-ref-name>persistence/widget</persistence-unit-ref-name>
         <persistence-unit-name>WidgetPU</persistence-unit-name>
     </persistence-unit-ref>
     <resource-ref>
         <res-ref-name>UserTransaction</res-ref-name>
         <res-type>javax.transaction.UserTransaction</res-type>
         <res-auth>Container</res-auth>
     </resource-ref>
</web-app>

-- 
| ? + ? = To question
----------------\
    Paul Sandoz
         x38109
+33-4-76188109