OverrideMethodFilter implements ContainerRequestFilter { @Override public void filter(ContainerRequestContext request) { String method = request.getHeaders().getFirst(HEADER); if (Strings.isNullOrEmpty(method)) { method = request.getUriInfo().getQueryParameters().getFirst(METHOD_PARAM); } if (!Strings.isNullOrEmpty(method)) { if (request.getMethod().equalsIgnoreCase("POST")) { request.setMethod(method); } else { throw new WebApplicationException(Response.Status.BAD_REQUEST); } } }  @Override void filter(ContainerRequestContext request); static final String HEADER; static final String METHOD_PARAM; }
@Test(dataProvider = "methods") public void testRequestUnmodifiedWithNoOverride(String method) { OverrideMethodFilter filter = new OverrideMethodFilter(); ContainerRequest request = new ContainerRequest( URI.create("http: URI.create("http: method, new MockSecurityContext(), new MockPropertiesDelegate()); filter.filter(request); assertEqualsIgnoreCase(request.getMethod(), method); }
@Test public void testHeaderHasPrecedenceOverQueryParam() { OverrideMethodFilter filter = new OverrideMethodFilter(); ContainerRequest request = new ContainerRequest( URI.create("http: URI.create("http: "POST", new MockSecurityContext(), new MockPropertiesDelegate()); request.header(OverrideMethodFilter.HEADER, "DELETE"); filter.filter(request); assertEqualsIgnoreCase(request.getMethod(), "DELETE"); }