@Override public void filter(final ContainerRequestContext request, final ContainerResponseContext response) throws IOException { final MediaType type = response.getMediaType(); if (type != null && !type.getParameters().containsKey(MediaType.CHARSET_PARAMETER)) { final MediaType typeWithCharset = type.withCharset(UTF_8); response.getHeaders().putSingle(HttpHeaders.CONTENT_TYPE, typeWithCharset); } } }
@Override public void filter(final ContainerRequestContext requestContext, final ContainerResponseContext responseContext) throws IOException { if (!logger.isLoggable(level)) { return; } final Object requestId = requestContext.getProperty(LOGGING_ID_PROPERTY); final long id = requestId != null ? (Long) requestId : _id.incrementAndGet(); final StringBuilder b = new StringBuilder(); printResponseLine(b, "Server responded with a response", id, responseContext.getStatus()); printPrefixedHeaders(b, id, RESPONSE_PREFIX, responseContext.getStringHeaders()); if (responseContext.hasEntity() && printEntity(verbosity, responseContext.getMediaType())) { final OutputStream stream = new LoggingStream(b, responseContext.getEntityStream()); responseContext.setEntityStream(stream); requestContext.setProperty(ENTITY_LOGGER_PROPERTY, stream); // not calling log(b) here - it will be called by the interceptor } else { log(b); } } }
@Override public void filter(final ContainerRequestContext requestContext, final ContainerResponseContext responseContext) throws IOException { if (!logger.isLoggable(level)) { return; } final Object requestId = requestContext.getProperty(LOGGING_ID_PROPERTY); final long id = requestId != null ? (Long) requestId : _id.incrementAndGet(); final StringBuilder b = new StringBuilder(); printResponseLine(b, "Server responded with a response", id, responseContext.getStatus()); printPrefixedHeaders(b, id, RESPONSE_PREFIX, responseContext.getStringHeaders()); if (responseContext.hasEntity() && printEntity(verbosity, responseContext.getMediaType())) { final OutputStream stream = new LoggingStream(b, responseContext.getEntityStream()); responseContext.setEntityStream(stream); requestContext.setProperty(ENTITY_LOGGER_PROPERTY, stream); // not calling log(b) here - it will be called by the interceptor } else { log(b); } } }
import javax.ws.rs.container.ContainerRequestContext; import javax.ws.rs.container.ContainerResponseContext; import javax.ws.rs.container.ContainerResponseFilter; import javax.ws.rs.core.MediaType; public class CharsetResponseFilter implements ContainerResponseFilter { @Override public void filter(ContainerRequestContext request, ContainerResponseContext response) { MediaType type = response.getMediaType(); if (type != null) { String contentType = type.toString(); if (!contentType.contains("charset")) { contentType = contentType + ";charset=utf-8"; response.getHeaders().putSingle("Content-Type", contentType); } } } }
@Nullable @Override public String getContentType() { return Objects.toString(context.getMediaType(), null); }
@Override public void filter(ContainerRequestContext request, ContainerResponseContext response) { MediaType type = response.getMediaType(); if (type != null) { String contentType = type.toString(); if (!contentType.contains("charset")) { contentType = contentType + ";charset=utf-8"; response.getHeaders().putSingle("Content-Type", contentType); } } } }
@Override public Charset getCharset() { return HttpMessages.getCharset(context.getMediaType()); }
import javax.ws.rs.container.ContainerRequestContext; import javax.ws.rs.container.ContainerResponseContext; import javax.ws.rs.container.ContainerResponseFilter; import javax.ws.rs.core.MediaType; public class HeaderResponseFilter implements ContainerResponseFilter { public void filter(ContainerRequestContext request, ContainerResponseContext response) { MediaType type = response.getMediaType(); if (type != null) { String contentType = type.toString(); if (!contentType.contains("charset")) { contentType = contentType + ";charset=utf-8"; response.getHeaders().putSingle("Content-Type", contentType); } } } }
private String createLoggingResponse(ContainerResponseContext resp) { Object entity = resp.getEntity(); if (BaseModel.class.isInstance(entity)) { BaseModel model = (BaseModel) entity; ByteArrayOutputStream out = new ByteArrayOutputStream(); model.toJson(out); return new String(out.toByteArray()); } else { MediaType mediaType = resp.getMediaType(); return "[" + entity.toString() + "," + resp.getStatusInfo().getStatusCode() + "," + resp.getStatusInfo().getReasonPhrase() + "," + mediaType.toString() + "]"; } }
private static boolean isUncompressable(ContainerResponseContext response) { MediaType mediaType = MediaType.parse(response.getMediaType().toString()); return UNCOMPRESSIBLE.stream().anyMatch(mediaType::is); }
@Override public void filter(final ContainerRequestContext request, final ContainerResponseContext response) throws IOException { final MediaType type = response.getMediaType(); if (type != null && !type.getParameters().containsKey(MediaType.CHARSET_PARAMETER)) { final MediaType typeWithCharset = type.withCharset(UTF_8); response.getHeaders().putSingle(HttpHeaders.CONTENT_TYPE, typeWithCharset); } } }
@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); } }
import javax.ws.rs.container.ContainerRequestContext; import javax.ws.rs.container.ContainerResponseContext; import javax.ws.rs.container.ContainerResponseFilter; import javax.ws.rs.core.MediaType; public class CharsetResponseFilter implements ContainerResponseFilter { @Override public void filter(ContainerRequestContext request, ContainerResponseContext response) { MediaType type = response.getMediaType(); if (type != null) { if (!type.getParameters().containsKey(MediaType.CHARSET_PARAMETER)) { MediaType typeWithCharset = type.withCharset("utf-8"); response.getHeaders().putSingle("Content-Type", typeWithCharset); } } } }
@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()); } } }
public class RangeResponseFilter implements ContainerResponseFilter { private static final String RANGE = "Range"; private static final String ACCEPT_RANGES = "Accept-Ranges"; @Override public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException { if (requestContext.getHeaders().containsKey(RANGE)) { String rangeHeader = requestContext.getHeaderString(RANGE); String contentType = responseContext.getMediaType().toString(); OutputStream originOutputStream = responseContext.getEntityStream(); RangedOutputStream rangedOutputStream = new RangedOutputStream(originOutputStream, rangeHeader, contentType, responseContext.getHeaders()); responseContext.setStatus(Status.PARTIAL_CONTENT.getStatusCode()); responseContext.getHeaders().putSingle(ACCEPT_RANGES, rangedOutputStream.getAcceptRanges()); responseContext.setEntityStream(rangedOutputStream); } } }
@Override public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException { @SuppressWarnings("unchecked") ResponseObjectAwareMessageBodyWriter<Object> writer = (ResponseObjectAwareMessageBodyWriter<Object>) getMessageWriter(responseContext.getEntityClass()); if (writer != null) { Object entity = responseContext.getEntity(); Class<?> entityClass = responseContext.getEntityClass(); int status = writer.getStatusCode(entity, entityClass); responseContext.setStatus(status); if (status == Status.NO_CONTENT.getStatusCode()) { writer.writeTo(entity, entityClass, null, null, responseContext.getMediaType(), responseContext.getHeaders(), responseContext.getEntityStream()); } } }
/** * {@inheritDoc} */ @Override public void filter(final ContainerRequestContext requestContext, final ContainerResponseContext responseContext) throws IOException { final Object requestId = requestContext.getProperty(LOGGING_ID_PROPERTY); final long id = requestId != null ? (Long) requestId : _id.incrementAndGet(); final StringBuilder b = new StringBuilder(); requestContext.setProperty(LOGGER_BUFFER_PROPERTY, b); printResponseLine(b, "Server responded with a response", id, responseContext.getStatus()); if (printEntity && isSupportPrintType(responseContext.getMediaType()) && responseContext.hasEntity()) { final OutputStream stream = new LoggingStream(b, responseContext.getEntityStream()); responseContext.setEntityStream(stream); requestContext.setProperty(ENTITY_LOGGER_PROPERTY, stream); // not calling log(b) here - it will be called by the interceptor } }
@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); } }
/** * Builds the {@link HttpResponse}. * * @return the {@link HttpResponse} */ private HttpResponse buildHttpResponse() { HttpResponseImpl httpResponse = new HttpResponseImpl(); if (containerResponseContext != null) { httpResponse.setStatusCode(containerResponseContext.getStatus()); httpResponse.setHeaders(containerResponseContext.getHeaders()); httpResponse.setContentType(getMediaTypeName(containerResponseContext.getMediaType())); httpResponse.setEntity(responseEntity); } return httpResponse; }
/** * Builds the {@link HttpResponse}. * * @return the {@link HttpResponse} */ private HttpResponse buildHttpResponse() { HttpResponseImpl httpResponse = new HttpResponseImpl(); if (containerResponseContext != null) { httpResponse.setStatusCode(containerResponseContext.getStatus()); httpResponse.setHeaders(containerResponseContext.getHeaders()); httpResponse.setContentType(getMediaTypeName(containerResponseContext.getMediaType())); httpResponse.setEntity(responseEntity); } return httpResponse; }