users@jersey.java.net

Re: [Jersey] Redirect /foo/ to /foo (remove trailing slash)

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Mon, 07 Sep 2009 08:53:17 +0200

Hi,

Can you log an issue?

This is something should be fairly easy to implement in terms of
redirection, the only tricky aspect is what the user should utilize to
declare that redirection in the reverse direction should occur for a
resource (rather than being something that applies to all resources)

Paul.

On Sep 7, 2009, at 3:11 AM, Charles Brooking wrote:

> Hi all,
>
> The Redirect feature in Jersey
> (com.sun.jersey.config.feature.Redirect)
> can be used to redirect an "unslashed" URI (eg /foo) to it's slashed
> version (eg /foo/) if the @Path on a resource class ends with "/".
>
> But in my application, I wanted the opposite: to have @Path values
> ending
> without slashes and provide a redirect in case the user includes a
> slash.
> Using paths without slashes seems appropriate when using MediaType
> mappings (eg having /foo.html instead of /foo.html/ or /foo/.html).
>
> I wrote a short filter to achieve this (see below). But I wonder if
> it's
> worthwhile including this in the semantics of the existing Redirect
> feature?
>
> {{{
> package au.edu.uq.itee.eresearch.dimer.webapp.app;
>
> import java.net.URI;
>
> import javax.ws.rs.WebApplicationException;
> import javax.ws.rs.core.Response;
>
> import com.sun.jersey.spi.container.ContainerRequest;
> import com.sun.jersey.spi.container.ContainerRequestFilter;
>
> public class RemoveSlashFilter implements ContainerRequestFilter {
> private static final String pattern = "\\/.+";
>
> public ContainerRequest filter(ContainerRequest request) {
> final String uri = request.getRequestUri().getRawPath().toString();
> if (uri.matches(pattern) && uri.endsWith("/")) {
> URI unslashed = URI.create(uri.substring(0, uri.length() - 1));
> Response response =
> Response.temporaryRedirect(unslashed).build();
> throw new WebApplicationException(response);
> }
> return request;
> }
> }
> }}}
>
> Later
> Charlie
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>