users@jersey.java.net

Re: [Jersey] Jersey and arbitrary length _at_PathParams

From: Martin Matula <martin.matula_at_oracle.com>
Date: Tue, 12 Oct 2010 13:36:38 +0200

Sure, you can use sub-resource locators for that. E.g. this is a
simplified example of how you could expose a directory structure:

import com.sun.jersey.api.NotFoundException;
import java.io.File;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/files")
public class FileResource {
     private File file = null;

     public FileResource() {
         this(new File("."));
     }

     public FileResource(File file) {
         this.file = file;
     }

     /** Sub-resource locator */
     @Path("{fileName}")
     public FileResource getDir(@PathParam("fileName") String file) {
         File child = new File(this.file, file);
         if (!child.exists()) {
             throw new NotFoundException();
         }
         return new FileResource(child);
     }

     @GET
     @Produces(MediaType.TEXT_PLAIN)
     public String get() {
         StringBuilder sb = new StringBuilder();
         if (file.isDirectory()) {
             sb.append("Directory: ");
         } else {
             sb.append("File: ");
         }
         sb.append(file.getAbsolutePath()).append("\n\n");
         for (String f : file.list()) {
             sb.append(f).append("\n");
         }
         return sb.toString();
     }
}

Regards,
Martin

On Oct 11, 2010, at 6:13 PM, Ronak Patel wrote:

> Hi,
>
> I've been wondering how can I deal with arbitrary length @PathParams
> using Jersey.
>
> Specifically I have urls like:
>
> http://<host>:<port>/<context>/<resource>/<some>/<resource>/<path>/
> <of>/<arbitrary>/<length>
>
> and treat the path all as one @PathParam without resorting to using
> subresources.
>
> I know this is not really REST but would it be possible to use
> regexpressions to match all subresources with one method?
>
> Thanks,
>
> Ronak Patel
>
>