use of javax.ws.rs.core.Variant in project jersey by jersey.
the class ValidationExceptionMapper method toResponse.
@Override
public Response toResponse(final ValidationException exception) {
if (exception instanceof ConstraintViolationException) {
LOGGER.log(Level.FINER, LocalizationMessages.CONSTRAINT_VIOLATIONS_ENCOUNTERED(), exception);
final ConstraintViolationException cve = (ConstraintViolationException) exception;
final Response.ResponseBuilder response = Response.status(ValidationHelper.getResponseStatus(cve));
// Entity.
final Object property = config.getProperty(ServerProperties.BV_SEND_ERROR_IN_RESPONSE);
if (property != null && Boolean.valueOf(property.toString())) {
final List<Variant> variants = Variant.mediaTypes(MediaType.TEXT_PLAIN_TYPE, MediaType.TEXT_HTML_TYPE, MediaType.APPLICATION_XML_TYPE, MediaType.APPLICATION_JSON_TYPE).build();
final Variant variant = request.get().selectVariant(variants);
if (variant != null) {
response.type(variant.getMediaType());
} else {
// default media type which will be used only when none media type from {@value variants} is in accept
// header of original request.
// could be settable by configuration property.
response.type(MediaType.TEXT_PLAIN_TYPE);
}
response.entity(new GenericEntity<>(ValidationHelper.constraintViolationToValidationErrors(cve), new GenericType<List<ValidationError>>() {
}.getType()));
}
return response.build();
} else {
LOGGER.log(Level.WARNING, LocalizationMessages.VALIDATION_EXCEPTION_RAISED(), exception);
return Response.serverError().entity(exception.getMessage()).build();
}
}
use of javax.ws.rs.core.Variant in project jersey by jersey.
the class ContainerRequestTest method testSelectVariant.
@Test
public void testSelectVariant() {
ContainerRequest r = new ContainerRequest(URI.create("http://example.org/app"), URI.create("http://example.org/app/resource"), "GET", SECURITY_CONTEXT, new MapPropertiesDelegate());
r.header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON);
r.header(HttpHeaders.ACCEPT_LANGUAGE, "en");
List<Variant> lv = Variant.mediaTypes(MediaType.APPLICATION_XML_TYPE, MediaType.APPLICATION_JSON_TYPE).languages(Locale.ENGLISH, Locale.FRENCH).add().build();
assertEquals(r.selectVariant(lv).getMediaType(), MediaType.APPLICATION_JSON_TYPE);
assertEquals(r.selectVariant(lv).getLanguage(), Locale.ENGLISH);
}
use of javax.ws.rs.core.Variant in project jersey by jersey.
the class VariantListBuilderTest method testAddAndNoAddBeforeBuild.
@Test
public void testAddAndNoAddBeforeBuild() throws Exception {
final Variant v1 = new Variant(MediaType.TEXT_PLAIN_TYPE, Locale.ENGLISH, null);
final Variant v2 = new Variant(MediaType.TEXT_PLAIN_TYPE, Locale.FRENCH, null);
final Variant v3 = new Variant(MediaType.TEXT_HTML_TYPE, Locale.ENGLISH, null);
final Variant v4 = new Variant(MediaType.TEXT_HTML_TYPE, Locale.FRENCH, null);
List<Variant> variants;
variants = new VariantListBuilder().mediaTypes(MediaType.TEXT_PLAIN_TYPE, MediaType.TEXT_HTML_TYPE).languages(Locale.ENGLISH, Locale.FRENCH).add().build();
assertEquals(4, variants.size());
assertTrue(variants.contains(v1));
assertTrue(variants.contains(v2));
assertTrue(variants.contains(v3));
assertTrue(variants.contains(v4));
variants = new VariantListBuilder().mediaTypes(MediaType.TEXT_PLAIN_TYPE, MediaType.TEXT_HTML_TYPE).languages(Locale.ENGLISH, Locale.FRENCH).build();
assertEquals(4, variants.size());
assertTrue(variants.contains(v1));
assertTrue(variants.contains(v2));
assertTrue(variants.contains(v3));
assertTrue(variants.contains(v4));
}
use of javax.ws.rs.core.Variant in project jersey by jersey.
the class LanguageVariantResource method doGet.
@GET
public Response doGet(@Context Request r) {
List<Variant> vs = Variant.VariantListBuilder.newInstance().mediaTypes(MediaType.valueOf("application/foo")).languages(new Locale("en")).languages(new Locale("fr")).add().mediaTypes(MediaType.valueOf("application/bar")).languages(new Locale("en")).languages(new Locale("fr")).add().build();
Variant v = r.selectVariant(vs);
if (v == null) {
return Response.notAcceptable(vs).build();
} else {
return Response.ok(v.getMediaType().toString() + ", " + v.getLanguage(), v).build();
}
}
use of javax.ws.rs.core.Variant in project camel by apache.
the class DefaultCxfRsBinding method bindCamelMessageToRequestEntity.
public Entity<Object> bindCamelMessageToRequestEntity(Object body, Message camelMessage, Exchange camelExchange) throws Exception {
if (body == null) {
return null;
}
String contentType = camelMessage.getHeader(Exchange.CONTENT_TYPE, String.class);
if (contentType == null) {
contentType = MediaType.WILDCARD;
}
String contentEncoding = camelMessage.getHeader(Exchange.CONTENT_ENCODING, String.class);
return Entity.entity(body, new Variant(MediaType.valueOf(contentType), Locale.US, contentEncoding));
}
Aggregations