2

I have the regrettable task of trying to pass slashes in url parameters(legacy app).

I've read this answer on how to solve this in 2.0+ : JAX-RS @PathParam How to pass a String with slashes, hyphens & equals too

However the following solution will not work in 1.9: @Path("{id}/{emailAddress : (.+)?}")

Test path: 5/emailpart1/part2@example.org

This returns a 404 saying it cannot find a route to match.

FYI, this is getting sent over URL-encoded, but our container(Tomcat) automatically decodes this before it gets handled by Jersey.

Edit: Turns out the entire request was getting blocked by Apache's AllowEncodedSlashes directive. Relevant: Need to allow encoded slashes on Apache

Community
  • 1
  • 1
stan
  • 4,885
  • 5
  • 49
  • 72

1 Answers1

2

Per comment to post test (note: using standalone grizzly container, not tomcat)

@Path("/email")
public class EmailResource {

    @GET
    @Path("{id}/{emailAddress : (.+)?}")
    public Response getEmail(@PathParam("id") String id,
                             @PathParam("emailAddress") String email) {
        StringBuilder builder = new StringBuilder();
        builder.append("id: ").append(id).append("\n");
        builder.append("email: ").append(email).append("\n");

        return Response.ok(builder.toString()).build();
    }
}

Standalone server

import com.sun.jersey.api.container.grizzly2.GrizzlyServerFactory;
import com.sun.jersey.api.core.PackagesResourceConfig;
import com.sun.jersey.api.core.ResourceConfig;
import org.glassfish.grizzly.http.server.HttpServer;

import javax.ws.rs.core.UriBuilder;
import java.io.IOException;
import java.net.URI;

public class Main {

    private static int getPort(int defaultPort) {
        //grab port from environment, otherwise fall back to default port 9998
        String httpPort = System.getProperty("jersey.test.port");
        if (null != httpPort) {
            try {
                return Integer.parseInt(httpPort);
            } catch (NumberFormatException e) {
            }
        }
        return defaultPort;
    }

    private static URI getBaseURI() {
        return UriBuilder.fromUri("http://localhost/api").port(getPort(8080)).build();
    }

    public static final URI BASE_URI = getBaseURI();

    protected static HttpServer startServer() throws IOException {
        ResourceConfig resourceConfig 
                 = new PackagesResourceConfig("jersey1.stackoverflow.standalone");
        System.out.println("Starting grizzly2...");
        return GrizzlyServerFactory.createHttpServer(BASE_URI, resourceConfig);
    }

    public static void main(String[] args) throws IOException {
        // Grizzly 2 initialization
        HttpServer httpServer = startServer();
        System.out.println(String.format("Jersey app started with WADL available at "
                + "%sapplication.wadl\nHit enter to stop it...",
                BASE_URI));
        System.in.read();
        httpServer.stop();
    }    
}

Note: the resource class is in jersey1.stackoverflow.standalone. To run the server, just run the Main class. The resource class will be autodiscovered based on new PackagesResourceConfig("jersey1.stackoverflow.standalone")

Using this dependency

<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-grizzly2</artifactId>
    <version>${jersey-version}</version>       <!-- 1.9 -->
</dependency>

Maven archytype can be retreived from

Group id: com.sun.jersey.archetypes
Artifact id: jersey-quickstart-grizzly2
Version: 1.9

Curl

curl -v http://localhost:8080/api/email/5/emailpart1/part2@example.org

Result:

id: 5
email: emailpart1/part2@example.org
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • Wow this example works for me, but not the piece I have running with Tomcat. I checked the request and it comes in with the %2F, but it just gives me "The requested URL /../id/email was not found on this server" Not sure where else to look. – stan Dec 18 '14 at 17:02
  • 1
    Turns out Apache was blocking it: http://stackoverflow.com/questions/4390436/need-to-allow-encoded-slashes-on-apache – stan Dec 19 '14 at 16:16