@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 { 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() + "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 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 { // we have already added the necessary headers for OPTIONS requests below if ("options".equalsIgnoreCase(requestContext.getRequest().getMethod())) { if(Response.Status.Family.familyOf(responseContext.getStatus()) == Response.Status.Family.SUCCESSFUL) { return; } responseContext.setStatus(Response.Status.NO_CONTENT.getStatusCode()); responseContext.setEntity(""); } String origin = requestContext.getHeaders().getFirst("Origin"); if (origin != null && !origin.isEmpty()) { responseContext.getHeaders().add("Access-Control-Allow-Origin", origin); responseContext.getHeaders().add("Access-Control-Allow-Credentials", true); responseContext.getHeaders().add("Access-Control-Allow-Headers", "Authorization, Content-Type, X-Graylog-No-Session-Extension, X-Requested-With, X-Requested-By"); responseContext.getHeaders().add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS"); // In order to avoid redoing the preflight thingy for every request, see http://stackoverflow.com/a/12021982/1088469 responseContext.getHeaders().add("Access-Control-Max-Age", "600"); // 10 minutes seems to be the maximum allowable value } }
@Override public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException { final Response.StatusType responseStatus = responseContext.getStatusInfo(); final String requestPath = requestContext.getUriInfo().getAbsolutePath().getPath(); final List<MediaType> acceptableMediaTypes = requestContext.getAcceptableMediaTypes(); final boolean acceptsHtml = acceptableMediaTypes.stream() .anyMatch(mediaType -> mediaType.isCompatible(MediaType.TEXT_HTML_TYPE) || mediaType.isCompatible(MediaType.APPLICATION_XHTML_XML_TYPE)); final boolean isGetRequest = "get".equalsIgnoreCase(requestContext.getMethod()); if (isGetRequest && responseStatus == Response.Status.NOT_FOUND && acceptsHtml && !requestPath.startsWith("/" + HttpConfiguration.PATH_API)) { final String entity = indexHtmlGenerator.get(requestContext.getHeaders()); responseContext.setStatusInfo(Response.Status.OK); responseContext.setEntity(entity, new Annotation[0], MediaType.TEXT_HTML_TYPE); responseContext.getHeaders().putSingle("X-UA-Compatible", "IE=edge"); } } }
@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); } } } }
@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()) { 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 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 containerRequestContext, ContainerResponseContext containerResponseContext) throws IOException { if (containerResponseContext.getStatus() == Response.Status.OK.getStatusCode()) { Object o = containerResponseContext.getEntity(); JsonResponseWrapper wrapper; Class clazz = o.getClass(); if (List.class.isAssignableFrom(clazz)) { wrapper = new JsonResponseWrapper((List) o); } else if (SearchResult.class.isAssignableFrom(clazz)) { wrapper = new JsonResponseWrapper((SearchResult) o); } else { wrapper = new JsonResponseWrapper(o); } containerResponseContext.setEntity(wrapper, containerResponseContext.getEntityAnnotations(), containerResponseContext.getMediaType()); } } }
@Override public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException { if (HttpResponseCodes.SC_BAD_REQUEST == responseContext.getStatus()) { Object entity = responseContext.getEntity(); if (entity != null && entity instanceof String) { ArrayList contentTypes = (ArrayList)responseContext.getHeaders().get("Content-Type"); if (contentTypes != null && containsHtmlText(contentTypes)) { String escapedMsg = escapeXml((String) entity); responseContext.setEntity(escapedMsg); } } } }
@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 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); } }
/** * Set the entity response to a 404 JSON entity. */ private void replaceResponse(final ContainerRequestContext requestContext, final ContainerResponseContext responseContext) { final ServerError serverError = new ServerError(); if (requestContext.getUriInfo().getPathParameters().size() == 1) { // Single parameter ---> entity identifier or whatever identifying a data serverError.setCode("entity"); serverError.setMessage(requestContext.getUriInfo().getPathParameters().values().iterator().next().get(0)); } else { serverError.setCode("data"); } responseContext.setStatus(Status.NOT_FOUND.getStatusCode()); responseContext.setEntity(toEntity(serverError), responseContext.getEntityAnnotations(), MediaType.APPLICATION_JSON_TYPE); }
@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 processResponse(ContainerRequestContext requestContext, ContainerResponseContext responseContext, ResourceInfo resourceInfo) { MediaType mediaType = responseContext.getMediaType(); if (mediaType != null && MediaType.APPLICATION_JSON_TYPE.equals(mediaType)) { Object responseData = responseContext.getEntity(); WrapperResponseEntity jsonResponse; if (responseData instanceof WrapperResponseEntity) { jsonResponse = (WrapperResponseEntity) responseData; } else { jsonResponse = new WrapperResponseEntity(ResponseCode.OK); jsonResponse.setData(responseData); } responseContext.setStatus(ResponseCode.OK.getCode()); responseContext.setEntity(jsonResponse); } }
@Override public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException { Book book = (Book)responseContext.getEntity(); responseContext.setEntity(new Book(book.getName(), book.getId() + supplement)); } }