Search in sources :

Example 1 with Variant

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();
    }
}
Also used : Response(javax.ws.rs.core.Response) Variant(javax.ws.rs.core.Variant) ConstraintViolationException(javax.validation.ConstraintViolationException) List(java.util.List)

Example 2 with Variant

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);
}
Also used : Variant(javax.ws.rs.core.Variant) MapPropertiesDelegate(org.glassfish.jersey.internal.MapPropertiesDelegate) Test(org.junit.Test)

Example 3 with Variant

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));
}
Also used : Variant(javax.ws.rs.core.Variant) Test(org.junit.Test)

Example 4 with Variant

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();
    }
}
Also used : Locale(java.util.Locale) Variant(javax.ws.rs.core.Variant) GET(javax.ws.rs.GET)

Example 5 with Variant

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));
}
Also used : Variant(javax.ws.rs.core.Variant)

Aggregations

Variant (javax.ws.rs.core.Variant)41 Locale (java.util.Locale)25 Test (org.junit.Test)21 MediaType (javax.ws.rs.core.MediaType)16 Response (javax.ws.rs.core.Response)9 ArrayList (java.util.ArrayList)6 LinkedList (java.util.LinkedList)5 GET (javax.ws.rs.GET)2 Invocation (javax.ws.rs.client.Invocation)2 ResteasyClient (org.jboss.resteasy.client.jaxrs.ResteasyClient)2 ResteasyClientBuilder (org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder)2 ResteasyWebTarget (org.jboss.resteasy.client.jaxrs.ResteasyWebTarget)2 NotConnectedRestException (org.ow2.proactive_grid_cloud_portal.scheduler.exception.NotConnectedRestException)2 BaseEncoding (com.google.common.io.BaseEncoding)1 FeedException (com.rometools.rome.io.FeedException)1 SyndFeedOutput (com.rometools.rome.io.SyndFeedOutput)1 WireFeedOutput (com.rometools.rome.io.WireFeedOutput)1 FileInputStream (java.io.FileInputStream)1 PushbackInputStream (java.io.PushbackInputStream)1 List (java.util.List)1