11

I am using a JAX-RS interface with XMLHttpRequest (XHR). Due to the XHR preflight, XHR send always OPTIONS before calling the real resource.

Now I have dozens of methods and I need the OPTIONS for every resoruce. Is there any way to do this automatically? I dont want to write dozens of methods like:

@OPTIONS
@Path("/{id}")
@PermitAll
public Response optionsById() {
    return Response.status(Response.Status.NO_CONTENT).build();
}

@OPTIONS
@Path("/{id}/data")
@PermitAll
public Response optionsByData() {
    return Response.status(Response.Status.NO_CONTENT).build();
}
Dennis
  • 4,011
  • 7
  • 36
  • 50
  • I guess you're mixing two parts of the spec: "1. Call a method annotated with a request method designator for OPTIONS or, if none present, 2. Generate an automatic response using the metadata provided by the JAX-RS annotations on the matching class and its methods." -> How can I generate an automatic response using the metadata? – Dennis Dec 06 '13 at 12:16
  • 1
    Now I got it, that they are generated automatically from the metadata. As far I can see, it works well! Except I can't add CORS header to it... any ideas? – Dennis Dec 09 '13 at 15:07
  • 1
    Fixed with property `ResourceConfig.PROPERTY_CONTAINER_RESPONSE_FILTERS` – Dennis Dec 09 '13 at 15:20

3 Answers3

7

UPDATE 09/12/2013: THIS DOES NOT WORK. Using this all @GET/@DELETE/@POST/@PUT are not working any more.

Finally I solved my problem. I created a super class OptionsResource, from which all resources inherit. This resoruce contains:

// Match root-resources
@OPTIONS
@PermitAll
public Response options() {
    return Response.status(Response.Status.NO_CONTENT).build();
}

// Match sub-resources
@OPTIONS
@Path("{path:.*}")
@PermitAll
public Response optionsAll(@PathParam("path") String path) {
    return Response.status(Response.Status.NO_CONTENT).build();
}

An example:

@Path("/test")
public class TestResource extends OptionsResource {

    @GET
    @Produces("text/plain;charset=UTF-8")
    public Response index() {
        return Response.status(Status.OK).entity("works").build();
    }

}

This matches:

Dennis
  • 4,011
  • 7
  • 36
  • 50
  • Hi Dennis, I am facing the same issue as you had. I am newbie to Jersey. How to set the following property in my project >> ResourceConfig.PROPERTY_CONTAINER_RESPONSE_FILTERS – Inaccessible Jul 20 '15 at 06:05
1

Quite a late reply, but a much nicer solution is to use a filter that catches all the OPTIONS call before path matching. In Kotlin, it will look like this:

@Provider @PreMatching
class OptionsFilter: ContainerRequestFilter {
    override fun filter(requestContext: ContainerRequestContext) {
        if (requestContext.method == "OPTIONS") {
            requestContext.abortWith(Response.status(Response.Status.NO_CONTENT).build())
        }
    }
}
gmariotti
  • 479
  • 4
  • 14
1

The java version:

@Provider
@PreMatching
public class OptionFilter implements ContainerRequestFilter {
    @Override
    public void filter(ContainerRequestContext requestContext) throws IOException {
        if (requestContext.getMethod().contentEquals("OPTIONS")) {
        
requestContext.abortWith(Response.status(Response.Status.NO_CONTENT).build());
        }
    }
}
Heero Yui
  • 150
  • 1
  • 8