users@jersey.java.net

make jsp:include work

From: Yoryos <valotas_at_gmail.com>
Date: Thu, 26 Nov 2009 14:31:19 +0200

These days I'm trying to make jersey work right with the jsp:include. I mean
given a jsp page to be able to do a <jsp:include
page="/some/path/to/our/jaxrs" />. I managed to have a working solution
after extending the Jersey filter, and the JSPTemplateProcessor. Shouldn't
something like that be part of default imlementation of jersey. The only
thing that has to be changed is a check if the request is "imported" and if
so proceed based on the url informations that can be extracted from the
imported request uri and query srting. The above is a working implementation
of that

public class ExtendedJerseyServletContainer extends ServletContainer {
    private static final long serialVersionUID = -9034703985328011311L;
    private static final Logger logger =
Logger.getLogger(ExtendedJerseyServletContainer.class.getName());

    private static final String INCLUDE_REQUEST_URI =
"javax.servlet.include.request_uri";
    private static final String INCLUDE_QUERYSTRING =
"javax.servlet.include.query_string";
    private static final String INCLUDE_SERVLETPATH =
"javax.servlet.include.servlet_path";

    @Override
    public void doFilter(HttpServletRequest request, HttpServletResponse
response, FilterChain chain) throws IOException, ServletException {
        String includeRequestURI = (String)
request.getAttribute(INCLUDE_REQUEST_URI);
        if (includeRequestURI == null) {
            super.doFilter(request, response, chain);
            return;
        }

        String requestURI = request.getRequestURI();
        if (includeRequestURI.equalsIgnoreCase(requestURI)) {
            super.doFilter(request, response, chain);
            return;
        }

         // if we match the static content regular expression lets delegate
to the filter chain
        // to use the default container servlets & handlers
        Pattern p = getStaticContentPattern();
        if (p != null &&
p.matcher((String)request.getAttribute(INCLUDE_SERVLETPATH)).matches()) {
            chain.doFilter(request, response);
            return;
        }

        String includeQueryString = (String)
request.getAttribute(INCLUDE_QUERYSTRING);
        final UriBuilder absoluteUriBuilder =
UriBuilder.fromUri(request.getRequestURL().toString());
        final URI baseUri =
absoluteUriBuilder.replacePath(request.getContextPath()).path("/").build();
        final URI requestUri =
absoluteUriBuilder.replacePath(includeRequestURI).replaceQuery(includeQueryString).build();

        if (logger.isLoggable(Level.FINE)) logger.log(Level.FINE,
String.format("Seems like we have an included request: '%s?%s' when the
default requese was '%s?%s'", includeRequestURI, includeQueryString,
requestURI, request.getQueryString()));

        service(baseUri, requestUri, request, response);
    }
}

The problem is with the returning of Viewables as they bind by default the
model to the "it" key, so I should also create a Custom template processor
that would bind the model to a key based on the class name of the jaxrs
resource object.

It could be much easier to have Viewable be able to constructed with an
additional String parameter witch would be the key that will be used to bind
the model to the request scope. I couldn't find out how Viewable exactly
works to overcome this. But as I've seen the actually binding get placed in
the JspTemplateProcessor and more precisely to the custom RequestDispatcher
that is used. I couldn't find there any class/field/something that had to do
with Viewable. Could someone guide me on this one?

Thanks in advanced
Yoryos