@Override public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException { System.out.println("MyResponseFilter.postFilter() enter"); responseContext.setEntity( responseContext.getEntity() + ":" + getClass().getSimpleName(), null, MediaType.TEXT_PLAIN_TYPE); System.out.println("MyResponseFilter.postFilter() exit"); } }
@Override public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException { System.out.println("WebApplicationExceptionFilter.postFilter() enter"); if (responseContext.hasEntity() && responseContext.getEntity().equals("Response Exception")) { throw new WebApplicationException(Response.Status.OK); } System.out.println("WebApplicationExceptionFilter.postFilter() exit"); } }
@Override public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException { if (responseContext.hasEntity()) { Exception e = (Exception) responseContext.getEntity(); if (exceptionClass.isAssignableFrom(e.getClass()) || (e.getCause() != null && exceptionClass.isAssignableFrom(e.getCause().getClass()))) { meter.mark(); } responseContext.setEntity(null); responseContext.getHeaders().add("X-Exceptions-Thrown", e.toString() + " : " + meter.getCount()); } } }
@Override public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException { if (responseContext.hasEntity()) { responseContext.setEntity("" + responseContext.getEntity() + "NAM_MATCH_OUT", null, MediaType.TEXT_PLAIN_TYPE); } } }
@Override public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException { if (responseContext.hasEntity()) { responseContext.setEntity("" + responseContext.getEntity() + "PRE_MATCH_OUT", null, MediaType.TEXT_PLAIN_TYPE); } } }
@Override public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException { if (responseContext.hasEntity()) { responseContext.setEntity("" + responseContext.getEntity() + "DYN_MATCH_OUT", null, MediaType.TEXT_PLAIN_TYPE); } } }
@Override @SuppressWarnings("unchecked") public void filter(ContainerRequestContext request, ContainerResponseContext response) { final Object entity = response.getEntity(); if (entity != null && !uriInfo.getMatchedResources().isEmpty()) { Class<?> entityClass = entity.getClass(); HeaderProcessor lhp = new HeaderProcessor(entityClass); lhp.processLinkHeaders(entity, uriInfo, rmc, response.getHeaders()); FieldProcessor lp = new FieldProcessor(entityClass); lp.processLinks(entity, uriInfo, rmc, rlcc); } } }
@Override public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException { if (responseContext.getEntity() != null) { return; // let WriterInterceptor handle this } MultivaluedMap<String, Object> headers = responseContext.getHeaders(); List<DKIMSignature> list = getHeaders(headers); for (DKIMSignature dosetaSignature : list) { try { sign(null, headers, null, dosetaSignature); } catch (Exception e) { throw new RuntimeException(e); } } }
@Override public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException { ResteasyDeployment deployment = ResteasyContext.getContextData(ResteasyDeployment.class); if (deployment != null) { Boolean disable = (Boolean) deployment.getProperty(ResteasyContextParameters.RESTEASY_DISABLE_HTML_SANITIZER); if (disable != null && disable) { return; } } if (HttpResponseCodes.SC_BAD_REQUEST == responseContext.getStatus()) { Object entity = responseContext.getEntity(); if (entity != null && entity instanceof String) { ArrayList<Object> contentTypes = (ArrayList<Object>)responseContext.getHeaders().get("Content-Type"); if (contentTypes != null && containsHtmlText(contentTypes)) { String escapedMsg = escapeXml((String) entity); responseContext.setEntity(escapedMsg); } } } }
public class ApplicationResponseFilter implements ContainerResponseFilter { @Override public void filter(final ContainerRequestContext request, final ContainerResponseContext response) throws IOException { // your code response.getEntity(); } }
public class PrintFilter implements ContainerResponseFilter, ContainerRequestFilter { @Override public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException { System.out.println("Response body: " + responseContext.getEntity()); } @Override public void filter(ContainerRequestContext requestContext) throws IOException { String string = IOUtils.toString(requestContext.getEntityStream()); System.out.println("request body: " + string); } }
public void processResponse(ContainerRequestContext requestContext, ContainerResponseContext responseContext, ResourceInfo resourceInfo) { Object responseData = responseContext.getEntity(); logger.info("response:\n",JsonUtils.toJson(responseData)); }
@Override public void filter(ContainerRequestContext containerRequestContext, ContainerResponseContext containerResponseContext) throws IOException { if (containerResponseContext.getStatus() == Response.Status.OK.getStatusCode()) { Object o = containerResponseContext.getEntity(); if (o == null) { containerResponseContext.setStatus(Response.Status.NO_CONTENT.getStatusCode()); } } } }
@Override public long getLength() { int res = ctx.getLength(); if (res < 0) { if (ctx.hasEntity()) { res = ctx.getEntity().toString().length(); } } return res; }
@Provider public class LoggingResponseFilter implements ContainerResponseFilter { private static final Logger logger = LoggerFactory.getLogger(LoggingResponseFilter.class); public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException { String method = requestContext.getMethod(); logger.debug("Requesting " + method + " for path " + requestContext.getUriInfo().getPath()); Object entity = responseContext.getEntity(); if (entity != null) { logger.debug("Response " + new ObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(entity)); } }
@Override public void filter(ContainerRequestContext req, ContainerResponseContext resp) throws IOException { if (resp.hasEntity() && (resp.getEntity() instanceof Status)) { Status status = (Status) resp.getEntity(); int code = (Status.State.UP == status.getState()) ? 200 : 503; resp.setStatus(code); resp.setEntity(status.toJson()); resp.getHeaders().putSingle("Content-Type", MediaType.APPLICATION_JSON); } }
@Override public void filter(ContainerRequestContext reqCtx, ContainerResponseContext respCtx) throws IOException { MediaType contentType = respCtx.getMediaType(); if (contentType != null && "multipart".equals(contentType.getType())) { List<Object> parts = getAttachmentParts(respCtx.getEntity()); respCtx.setEntity(parts); } }
@Override public void filter(ContainerRequestContext reqCtx, ContainerResponseContext respCtx) throws IOException { if (Boolean.TRUE == reqCtx.getProperty(OPEN_API_PROPERTY)) { final Object entity = respCtx.getEntity(); // Right before 1.5.18, the entity was always a String but became a model object // (io.swagger.models.Swagger) after. For now, let us serialize it to JSON manually. String swaggerJson = entity instanceof String ? (String)entity : Json.pretty(entity); String openApiJson = SwaggerToOpenApiConversionUtils.getOpenApiFromSwaggerJson( createMessageContext(), swaggerJson, openApiConfig); respCtx.setEntity(openApiJson); } }
@Override public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException { responseContext.getHeaders().add("Custom", "custom"); if (!"Postmatch filter error".equals(responseContext.getEntity())) { Book book = (Book)responseContext.getEntity(); responseContext.setEntity(new Book(book.getName(), 1 + book.getId()), null, null); } }
@Override public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException { Book book = (Book)responseContext.getEntity(); responseContext.setEntity(new Book(book.getName(), book.getId() + supplement)); } }