users@jersey.java.net

Re: [Jersey] XSLT and Jersey

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Mon, 19 Jan 2009 11:53:56 +0100

Hi Steve,

Grrr... we have hit the really annoying problem with Servlet and
parameter processing that breaks layers. A call to:

   request.getParameter(...)

in the servlet filter consumes the request entity if it is of the
media type "application/x-www-form-urlencoded". There is no
differentiation between query parameters and form parameters.

I can suggest a few things:

   1) you switch over to using Jersey response filters; and/or

   2) Parse the query parameters using Jersey query processing (i am
assuming that "_xslt" is a query parameter).

   3) we try and fix this bug in Jersey.



You can support 1 by declaring a response filter as follows in the
web.xml:

        <init-param>
            <param-
name>com.sun.jersey.spi.container.ContainerResponseFilters</param-name>
            <!-- ; separate list of classes -->
            <param-value>com.foo.ResponseFilter</param-value>
        </init-param>

The class com.foo.ResponseFilter implements ContainerResponseFilter
[1]. You can obtain the query parameters (or form parameter) from
ContainerRequest passed into the ContainerResponse.filter method.



You can support 2 by using:

   UriComponent.decodeQuery [2]

For example

   MultivaluedMap<String, String> qps =
UriComponent.decodeQuery(request.getQueryString(), true);
   String type = qps.getFirst("_xslt");



For 3 I am not sure how to currently fix this in Jersey. The problem
is we cannot determine how the bytes that is the request entity of the
media type "application/x-www-form-urlencoded" is processed by the
resource class. There are degenerate cases where by the bytes of the
request entity can never be re-produced exactly because there are
query and form parameters with the same name.

Paul.

[1] https://jersey.dev.java.net/source/browse/*checkout*/jersey/tags/jersey-1.0.1/api/jersey/com/sun/jersey/spi/container/ContainerResponseFilter.html
[2] https://jersey.dev.java.net/source/browse/*checkout*/jersey/tags/jersey-1.0.1/api/jersey/com/sun/jersey/api/uri/UriComponent.html
#decodeQuery(java.lang.String,%20boolean)

On Jan 19, 2009, at 6:24 AM, Steve Mactaggart wrote:

> Thanks for the pointers and I have now used a servlet filter to
> solve this issue and it works great. My only problem now is that
> when this filter is enabled Jersey doesn't seem to pull in the POST
> content.
>
> My filter is attached below, it seems that no matter what Jersey is
> not getting the post information.
> If I disable this filter it work, but with it enabled the content
> received is and empty string.
>
> My Jersey method looks like:
>
> @POST
> @Path("/create")
> public String createPOST(String content) {
> log.info("createPOST " + uriInfo.getPath() + ", content = " +
> content);
> try
> {
> return renderResponse(invokeCreate(content));
> } catch (Throwable e) {
> log.error(e.getMessage(), e);
> XMLErrors errs2 = new XMLErrors();
> errs2.add(new XMLError("general", e.getMessage()));
> return renderResponse(errs2);
> }
> }
>
>
> Any ideas?
>
> My filter is:
> public void doFilter(ServletRequest request, ServletResponse response,
> FilterChain chain) throws IOException, ServletException {
>
> // String contentType;
> String styleSheet;
>
> String type = request.getParameter("_xslt");
> String element = ((HttpServletRequest)request).getRequestURI();
>
> if (element.startsWith("/")) {
> element = element.substring(1);
> }
>
> if (element.indexOf('/') > -1) {
> element = element.substring(0, element.indexOf('/'));
> }
>
> Reader styleSheetStream = XSLTManager.getStylesheet(element, type);
>
> if (styleSheetStream == null) {
> chain.doFilter(request, response);
> }
> else {
> // do the request, get back the content and do the XSLT transform
> // this is removed for brevity, and should not be called during my
> post actions as I don't have an "xslt" query parameter
> }
>
> On Thu, Jan 8, 2009 at 7:56 AM, Steve Mactaggart <steve_at_whitesquaresoft.com
> > wrote:
> Sorry about the double posting, I was not receiving users@ emails
> and so re-registered. I did a search on the list even and assumed
> the post didn't make it due to my registration status.
>
> thanks for the pointers, and I'll look into the use of filters.
>
> Steve
>
>
> On Wed, Jan 7, 2009 at 11:10 PM, Paul Sandoz <Paul.Sandoz_at_sun.com>
> wrote:
> Hi Steve,
>
> I replied to you email yesterday:
>
> http://markmail.org/search/?q=list
> %3Anet.java.dev.jersey.users#query:list%3Anet.java.dev.jersey.users
> +page:1+mid:sk7sb73cfuexkbiv+state:results
>
> Paul.
>
>
> On Jan 7, 2009, at 1:06 PM, Steve Mactaggart wrote:
>
> Hi all,
>
> I am loving the ease of use of Jersey to expose simple XML web
> services, I have a quick question about being able to do server side
> transforms on the data using XSLT.
>
> We want to have a standard document format for accessing some of our
> data, but also the ability to pass a flag to the web service that
> allows us to turn on XSLT or some sort of processing that will
> modify the XML output.
>
> We don't want to have multiple data models, as they are close to the
> same, we just would like to do some post (jaxb) processing on the
> XML to make minor changes.
>
> Is there any way to do this inside the Jersey framework?
>
> Thank,
> Steve
>
>
>
> --
> This message has been scanned for viruses and
> dangerous content by MailScanner, and is
> believed to be clean.
>
>
>