1

I am wondering if it is possible to dispatch a request from a servlet to a Jersey (JAX-RS implementation) resource class. I am trying to do it but it doesn't seem to work and according to my logging, the jersey resource is never reached.

Code examples are below. Is what I'm trying to do impossible for some reason?

Please note, the Jersey resource works correctly when I access it directly in a web browser via the address bar.

Also please note that 'RequestDispatcher.forward()' works as expected. It is just 'include' that doesn't.

The servlet

//The Jersey dispatcher url-filter is set to '/api/*'
String servletID = "/api/items";
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(servletID);
dispatcher.include(request, response);    

The Jersey resource

@GET @Path("/items")
@Produces ({MediaType.TEXT_XML})
public JAXBElement<Items> getItems(@PathParam("project") String project) throws       IOException, JAXBException {

    log.debug("reached getItems");

    //Omitted code that returns 'Items' object wrapped in JAXBElement

}

Relevant parts of web.xml

<servlet>
    <servlet-name>jerseyDispatcher</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>uk.co.web.api.resource</param-value>
    </init-param>
</servlet>

<servlet-mapping>
    <servlet-name>jerseyDispatcher</servlet-name>
    <url-pattern>/api/*</url-pattern>
</servlet-mapping>
Paul
  • 113
  • 2
  • 10

2 Answers2

0

It is possible you forward the request.

HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
RequestDispatcher requestDispatcher = null;
requestDispatcher = httpServletRequest.getRequestDispatcher("/items");
dispatcher.forward(request, response);
return;

However note if you receive a GET request and try to forward to a POST resource, It will throw a 405 error.

Edit:

Let me understand what you are trying to achieve, if you need write content to response output stream you could use jersey Resource Filter.

 public class YourResourceFilter implements ResourceFilter
    {
    public ContainerRequestFilter getRequestFilter()
        {
            return new ContainerRequestFilter()
            {
                @Override
                public ContainerRequest filter(ContainerRequest containerRequest)
                {
//Pre- editing the request
                return containerRequest;
                }
            };
        }

    @Override
        public ContainerResponseFilter getResponseFilter()
        {
            return new ContainerResponseFilter()
            {
                @Override
                public ContainerResponse filter(ContainerRequest containerRequest, ContainerResponse containerResponse)
                {
// after the request has been completed by your jersey resource
                    return containerResponse;
                }
            };
        }

    }
Gerard
  • 71
  • 2
  • 6
  • I cannot forward the request as unfortunately the servlet needs to add more content to the outputstream afterwards – Paul Jan 16 '14 at 09:13
  • Sorry I don't think you understand the problem. I want to dispatch a request that is first handled by javax.servlet.http.HttpServlet to a Jersey resource method. – Paul Jan 16 '14 at 13:02
  • I tried what you just posted with 'include' got the problem Sorry, I was just suggesting a different approach to your need 'to add more content to the output stream afterwards'. – Gerard Jan 16 '14 at 16:42
  • Thanks for your help, Gerard =) Unfortunately I cannot change how this works but I appreciate the suggestion! – Paul Jan 16 '14 at 16:57
0

I got it to work, sort of (Jersey 2.13) by configuring Jersey as a filter, and not a servlet, in web.xml. Then, you can tell the container to apply the filter to included requests too:

<filter-mapping>
    <filter-name>Jersey Web Application</filter-name>
    <url-pattern>/api/*</url-pattern>
    <dispatcher>FORWARD</dispatcher>
    <dispatcher>INCLUDE</dispatcher>
    <dispatcher>REQUEST</dispatcher>
</filter-mapping>

RequestDispatcher.include will then work for request handled by Jersey, too. There's a caveat, though. Jersey calls response.getOutputStream, so all output must be performed through said output stream - this rules out JSP pages, that use response.getWriter instead. So, unless you figure out how to work around the problem, forget about including a Jersey resource in a JSP page or, vice versa, including the result of evaluating a JSP as part of a REST response.

Alessio Stalla
  • 1,022
  • 6
  • 22
  • Can you please provide a link, where it says how to include the `` tags? I cant make it work, when including it in `web.xml`, eclipse says `Invalid content...` – ulrich Apr 10 '15 at 07:49
  • See e.g. (http://sqltech.cl/doc/oas10gR31/web.1013/b28959/filters.htm#BCFIEDGB); `` was added in Servlet 2.4, make sure you're declaring the correct web.xml prologue. – Alessio Stalla Apr 10 '15 at 13:12