java Filters

java Filters

Context redirect filter:

Filter must be applied at pre-match extension point and thus use annotation @PreMatching.

Filters implementing this interface must be annotated with @Provider to be discovered by the JAX-RS runtime. Container request filter instances may also be discovered and bound dynamically to particular resource methods.

Sample Code:

@Provider
@PreMatching
public class ContextFilter implements ContainerRequestFilter {
private static final String serviceContext = “/ServiceContext”;
private static Map<String, String> eventToContext = new HashMap<>();
static {

//path set based on event name
eventToContext.put(eventName, serviceContext+ “/event”);
eventToContext.put(eventName2,serviceContext + “/event2”);
}

private String getEntityBody(ContainerRequestContext requestContext) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
InputStream in = requestContext.getEntityStream();
final StringBuilder b = new StringBuilder();
try {
ReaderWriter.writeTo(in, out);
byte[] requestEntity = out.toByteArray();
if (requestEntity.length == 0) {
b.append(“”).append(“\n”);
} else {
b.append(new String(requestEntity)).append(“\n”);
}
requestContext.setEntityStream(new ByteArrayInputStream(requestEntity));
} catch (IOException ex) {
}
return b.toString();
}
@Override
public void filter(ContainerRequestContext containerRequestContext) throws IOException {
System.out.println(containerRequestContext.getUriInfo().getAbsolutePath());
String eventName = containerRequestContext.getHeaderString(“X_EVENT_NAME”);
if (eventName == null){
return;
}
String context = getContextForEvent(eventName);
System.out.println(context);

UriBuilder urlBuilder = containerRequestContext.getUriInfo().getRequestUriBuilder();

URI uri = urlBuilder.replacePath( context).build();
containerRequestContext.setRequestUri(uri);
}

private String getContextForEvent(String eventName) {
return eventToContext.get(eventName);
}

}
Logging Filter :

Logs every request and response.

@Provider
public class LoggingFilter implements ContainerRequestFilter, ContainerResponseFilter {
@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
StringBuilder sb = new StringBuilder();
sb.append(“User: “).append(requestContext.getSecurityContext().getUserPrincipal() == null ? “unknown”
: requestContext.getSecurityContext().getUserPrincipal());
sb.append(” – Path: “).append(requestContext.getUriInfo().getPath());
sb.append(” – Header: “).append(requestContext.getHeaders());
sb.append(” – Entity: “).append(getEntityBody(requestContext));

// log sb

}

private String getEntityBody(ContainerRequestContext requestContext) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
InputStream in = requestContext.getEntityStream();
final StringBuilder b = new StringBuilder();
try {
ReaderWriter.writeTo(in, out);
byte[] requestEntity = out.toByteArray();
if (requestEntity.length == 0) {
b.append(“”).append(“\n”);
} else {
b.append(new String(requestEntity)).append(“\n”);
}

requestContext.setEntityStream(new ByteArrayInputStream(requestEntity));
} catch (IOException ex) {
//log exception
return b.toString();
}

@Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws
IOException {
String sb = “Header: ” + responseContext.getHeaders() +
” – Entity: ” + ObjectMapperUtils.writeValueAsString(responseContext.getEntity()) +
” Response Code: ” + responseContext.getStatus();
}
}

 

C++ C C# Health concepts Leetcode Programming Data Streucture