JAX-RS - UriInfo Examples

Code Snippets JAX-RS JAVA EE 

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import java.net.URI;

@Path("/")
public class MyResource {
@Context
UriInfo uriInfo;
@PathParam("num")
int pathNum;
@QueryParam("q")
String qValue;

@GET
@Path("/test{num:[1-9]{0,1}}")
public String handleRequest() {
URI uri = uriInfo.getRequestUri();
return String.format(" response from: %s%n PathNum: %s%n query paramMyResource: %s%n",
uri, pathNum, qValue);
}
}
Original Post




import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import java.net.URI;

@Path("/property/")
public class MyResource1 {
UriInfo uriInfo;
int pathNum;
String myParam;

@GET
@Path("/test{num:[1-9]{0,1}}")
public String handleRequest() {
URI uri = uriInfo.getRequestUri();
return String.format("response from: %s%nPathNum: %s%nmyParam: %s%n",
uri, pathNum, myParam);
}

@Context
public void setUriInfo(UriInfo uriInfo) {
this.uriInfo = uriInfo;
}

@PathParam("num")
public void setPathNum(int pathNum) {
this.pathNum = pathNum;
}

@QueryParam("q")
public void setMyParam(String myParam) {
this.myParam = myParam;
}
}
Original Post




@Provider
@Logged
public class LogFilter implements ContainerRequestFilter, ContainerResponseFilter {

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

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

private void log(UriInfo uriInfo, MultivaluedMap<String, ?> headers) {
System.out.println("Path: " + uriInfo.getPath());
headers.entrySet().forEach(h -> System.out.println(h.getKey() + ": " + h.getValue()));
}
}
import javax.ws.rs.NameBinding;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@NameBinding
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(value = RetentionPolicy.RUNTIME)
public @interface Logged {
}
Original Post




@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




import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import java.net.URI;

@Path("/users")
public class UserResource {
@Context
UriInfo uriInfo;

public UserResource() {
printUriInfo("In constructor");
}

@GET
public String getUsers() {
printUriInfo("In getUsers() method");
return "returning dummy user list";
}

@GET
@Path("{id}")
public String getUserById(@PathParam("id") String id) {
printUriInfo("In getUserById() method");
return "returning dummy user by id " + id;
}

private void printUriInfo(String msg) {
System.out.println("---------------");
System.out.println(msg);
System.out.println("-- UriInfo --");
if (uriInfo == null) {
System.out.println("UriInfo: " + uriInfo);
return;
}
System.out.println("absolutePath: " + uriInfo.getAbsolutePath());
System.out.println("baseUri: " + uriInfo.getBaseUri());
System.out.println("matchedResource: " + uriInfo.getMatchedResources());
System.out.println("matchedUri: " + uriInfo.getMatchedURIs());
System.out.println("path: " + uriInfo.getPath());
System.out.println("pathParameters: " + uriInfo.getPathParameters());
System.out.println("pathSegments: " + uriInfo.getPathSegments());
System.out.println("queryParameters: " + uriInfo.getQueryParameters());
System.out.println("requestUri: " + uriInfo.getRequestUri());
System.out.println("relativize test: " + uriInfo.relativize(URI.create("test")));
System.out.println("resolve test: " + uriInfo.resolve(URI.create("test")));
}
}
Original Post




See Also