JAX-RS - @Priority Examples

Code Snippets JAX-RS JAVA EE 

@Priority(1)
@Provider
public class InvalidRequestFilter implements ContainerRequestFilter {

@Override
public void filter(ContainerRequestContext reqContext) throws IOException {
Response responseForInvalidRequest = validateRequest(reqContext);
if (responseForInvalidRequest != null) {
reqContext.abortWith(responseForInvalidRequest);
}
}

private Response validateRequest(ContainerRequestContext reqContext) {
UriInfo uriInfo = reqContext.getUriInfo();
String path = uriInfo.getPath();
if (path.equals("orders/history")) {
String msg = String.format("The feature is not yet supported: %s%n", path);
CacheControl cc = new CacheControl();
cc.setNoStore(true);
Response response = Response.status(Response.Status.NOT_IMPLEMENTED)
.cacheControl(cc)
.entity(msg)
.build();
return response;
}
return null;
}
}
Original Post




@Priority(2)
@Provider
public class LogFilter implements ContainerRequestFilter, ContainerResponseFilter {

@Override
public void filter(ContainerRequestContext reqContext) throws IOException {
System.out.println("-- request info --");
UriInfo uriInfo = reqContext.getUriInfo();
log(uriInfo, reqContext.getHeaders());
}

@Override
public void filter(ContainerRequestContext reqContext,
ContainerResponseContext resContext) throws IOException {
System.out.println("-- response info --");
UriInfo uriInfo = reqContext.getUriInfo();
log(uriInfo, resContext.getHeaders());
}

private void log(UriInfo uriInfo, MultivaluedMap<String, ?> headers) {
System.out.println("Path: " + uriInfo.getPath());
System.out.println("HEADERS:");
headers.entrySet().forEach(h -> System.out.println(h.getKey() + ": " + h.getValue()));
}
}
Original Post




See Also