@Provider @Consumes("text/plain") @Produces("text/plain") public static class TextPlain extends ClipboardDataProvider { @Override public void writeTo(final Object t, final Class type, final Type genericType, final Annotation[] annotations, final MediaType mediaType, final MultivaluedMap httpHeaders, final OutputStream entityStream) throws IOException, WebApplicationException { entityStream.write(t.toString().getBytes(MessageUtils.getCharset(mediaType))); } @Override public Object readFrom(final Class type, final Type genericType, final Annotation[] annotations, final MediaType mediaType, final MultivaluedMap httpHeaders, final InputStream entityStream) throws IOException, WebApplicationException { return new ClipboardData(readStringFromStream(entityStream, MessageUtils.getCharset(mediaType))); } }
@Provider @Produces(MediaType.WILDCARD) public class OptionalIntMessageBodyWriter implements MessageBodyWriter<OptionalInt> { // Jersey ignores this @Override public long getSize(OptionalInt entity, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { return -1; } @Override public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { return OptionalInt.class.isAssignableFrom(type); } @Override public void writeTo(OptionalInt entity, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException { if (!entity.isPresent()) { throw EmptyOptionalException.INSTANCE; } entityStream.write(Integer.toString(entity.getAsInt()).getBytes(StandardCharsets.US_ASCII)); } }
@Provider public class DateNotSpecifiedFilter implements ContainerRequestFilter { @Override public void filter(ContainerRequestContext requestContext) throws IOException { final String dateHeader = requestContext.getHeaderString(HttpHeaders.DATE); if (dateHeader == null) { throw new WebApplicationException(new IllegalArgumentException("Date Header was not specified"), Response.Status.BAD_REQUEST); } } }
@Provider @Produces(MediaType.WILDCARD) public class OptionalLongMessageBodyWriter implements MessageBodyWriter<OptionalLong> { // Jersey ignores this @Override public long getSize(OptionalLong entity, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { return -1; } @Override public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { return OptionalLong.class.isAssignableFrom(type); } @Override public void writeTo(OptionalLong entity, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException { if (!entity.isPresent()) { throw EmptyOptionalException.INSTANCE; } entityStream.write(Long.toString(entity.getAsLong()).getBytes(StandardCharsets.US_ASCII)); } }
/** * Created by Gun Lee(gunlee01@gmail.com) on 2017. 8. 25. */ @Provider @Slf4j public class ValidationExceptionMapper implements ExceptionMapper<javax.validation.ValidationException> { @Context UriInfo uriInfo; @Override public Response toResponse(javax.validation.ValidationException e) { final StringBuilder strBuilder = new StringBuilder("[ValidationException] "); for (ConstraintViolation<?> cv : ((ConstraintViolationException) e).getConstraintViolations()) { strBuilder.append(cv.getPropertyPath().toString() + " " + cv.getMessage()); } CommonResultView resultView = CommonResultView.fail(400, 400, strBuilder.toString(), null); return Response.status(resultView.getStatus()) .type(MediaType.APPLICATION_JSON) .entity(resultView).build(); } }
@Provider public class DateRequiredFeature implements DynamicFeature { @Override public void configure(ResourceInfo resourceInfo, FeatureContext context) { if (resourceInfo.getResourceMethod().getAnnotation(DateRequired.class) != null) { context.register(DateNotSpecifiedFilter.class); } } }
@Provider @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) private static class JacksonProvider implements ContextResolver<ObjectMapper> { private final ObjectMapper mapper; JacksonProvider(ObjectMapper mapper) { this.mapper = mapper; } @Override public ObjectMapper getContext(Class<?> type) { return mapper; } } }
@Provider @Produces(MediaType.WILDCARD) public class OptionalDoubleMessageBodyWriter implements MessageBodyWriter<OptionalDouble> { // Jersey ignores this @Override public long getSize(OptionalDouble entity, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { return -1; } @Override public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { return OptionalDouble.class.isAssignableFrom(type); } @Override public void writeTo(OptionalDouble entity, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException { if (!entity.isPresent()) { throw EmptyOptionalException.INSTANCE; } entityStream.write(Double.toString(entity.getAsDouble()).getBytes(StandardCharsets.US_ASCII)); } }
/** * Prevents Jersey from modification Request's User-Agent header with default value, * to escape the value conflict with Dropwizard */ @Provider public class JerseyIgnoreRequestUserAgentHeaderFilter implements ClientRequestFilter { @Override public void filter(ClientRequestContext requestContext) throws IOException { ((ClientRequest) requestContext).ignoreUserAgent(true); } }
/** * @author Robin Müller */ @Provider @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public class JacksonConfig implements ContextResolver<ObjectMapper> { public ObjectMapper getContext(Class<?> type) { return new ObjectMapper() .setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES) .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) .configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL, true); } }