Search in sources :

Example 6 with Variant

use of javax.ws.rs.core.Variant in project jersey by jersey.

the class VariantSelector method getVariantHolderList.

private static LinkedList<VariantHolder> getVariantHolderList(final List<Variant> variants) {
    final LinkedList<VariantHolder> l = new LinkedList<>();
    for (final Variant v : variants) {
        final MediaType mt = v.getMediaType();
        if (mt != null) {
            if (mt instanceof QualitySourceMediaType || mt.getParameters().containsKey(Quality.QUALITY_SOURCE_PARAMETER_NAME)) {
                final int qs = QualitySourceMediaType.getQualitySource(mt);
                l.add(new VariantHolder(v, qs));
            } else {
                l.add(new VariantHolder(v));
            }
        } else {
            l.add(new VariantHolder(v));
        }
    }
    return l;
}
Also used : Variant(javax.ws.rs.core.Variant) MediaType(javax.ws.rs.core.MediaType) LinkedList(java.util.LinkedList)

Example 7 with Variant

use of javax.ws.rs.core.Variant in project jersey by jersey.

the class ContainerRequest method selectVariant.

// JAX-RS request
@Override
public Variant selectVariant(final List<Variant> variants) throws IllegalArgumentException {
    if (variants == null || variants.isEmpty()) {
        throw new IllegalArgumentException(METHOD_PARAMETER_CANNOT_BE_NULL_OR_EMPTY);
    }
    final Ref<String> varyValueRef = Refs.emptyRef();
    final Variant variant = VariantSelector.selectVariant(this, variants, varyValueRef);
    this.varyValue = varyValueRef.get();
    return variant;
}
Also used : Variant(javax.ws.rs.core.Variant)

Example 8 with Variant

use of javax.ws.rs.core.Variant in project jersey by jersey.

the class ResponseTest method variantTest.

/*
     * Create an instance of Response using
     * Response.status(int).variant(Variant).build()
     * verify that correct status code is returned
     */
@Test
public void variantTest() {
    Response resp;
    int status = 200;
    List<String> encoding = Arrays.asList("gzip", "compress");
    List<String> lang = Arrays.asList("en-US", "en-GB", "zh-CN");
    MediaType mt = new MediaType("text", "plain");
    List<Variant> vts = Variant.VariantListBuilder.newInstance().mediaTypes(mt).languages(new Locale("en", "US"), new Locale("en", "GB"), new Locale("zh", "CN")).encodings((String[]) encoding.toArray()).add().build();
    String tmp;
    for (Variant vt : vts) {
        resp = Response.status(status).variant(vt).build();
        tmp = verifyResponse(resp, null, status, encoding, lang, null, null, null, null);
        if (tmp.endsWith("false")) {
            System.out.println("### " + tmp);
            fail();
        }
    }
}
Also used : Response(javax.ws.rs.core.Response) Locale(java.util.Locale) Variant(javax.ws.rs.core.Variant) MediaType(javax.ws.rs.core.MediaType) Test(org.junit.Test)

Example 9 with Variant

use of javax.ws.rs.core.Variant in project jersey by jersey.

the class ResponseTest method testVariants.

// Reproducer for JERSEY-1553
@Test
public void testVariants() {
    List<String> encoding = Arrays.asList("gzip", "compress");
    List<Variant> list = Variant.VariantListBuilder.newInstance().mediaTypes(MediaType.TEXT_PLAIN_TYPE).languages(new Locale("en", "US"), new Locale("en", "GB")).encodings(encoding.toArray(new String[encoding.size()])).add().build();
    final Response r1 = Response.ok().variants(list).build();
    assertNotNull(r1);
    assertNotNull(r1.getHeaderString(HttpHeaders.VARY));
    final Response r2 = Response.ok().variants(list.toArray(new Variant[list.size()])).build();
    assertNotNull(r2);
    assertNotNull(r2.getHeaderString(HttpHeaders.VARY));
}
Also used : Locale(java.util.Locale) Variant(javax.ws.rs.core.Variant) Response(javax.ws.rs.core.Response) Test(org.junit.Test)

Example 10 with Variant

use of javax.ws.rs.core.Variant in project jersey by jersey.

the class ResponseTest method OkTest5.

/*
     * Create an instance of Response using Response.ok(String, Variant).build()
     * verify that correct status code is returned
     */
@Test
public void OkTest5() {
    Response resp;
    int status = 200;
    String content = "Test Only";
    List<String> encoding = Arrays.asList("gzip", "compress");
    List<String> lang = Arrays.asList("en-US", "en-GB", "zh-CN");
    MediaType mt = new MediaType("text", "plain");
    List<Variant> vts = Variant.VariantListBuilder.newInstance().mediaTypes(mt).languages(new Locale("en", "US"), new Locale("en", "GB"), new Locale("zh", "CN")).encodings((String[]) encoding.toArray()).add().build();
    String tmp;
    for (Variant vt : vts) {
        resp = Response.ok(content, vt).build();
        tmp = verifyResponse(resp, content, status, encoding, lang, null, null, null, null);
        if (tmp.endsWith("false")) {
            System.out.println("### " + tmp);
            fail();
        }
    }
}
Also used : Response(javax.ws.rs.core.Response) Locale(java.util.Locale) Variant(javax.ws.rs.core.Variant) MediaType(javax.ws.rs.core.MediaType) Test(org.junit.Test)

Aggregations

Variant (javax.ws.rs.core.Variant)11 Test (org.junit.Test)6 Locale (java.util.Locale)5 Response (javax.ws.rs.core.Response)5 MediaType (javax.ws.rs.core.MediaType)4 LinkedList (java.util.LinkedList)1 List (java.util.List)1 ConstraintViolationException (javax.validation.ConstraintViolationException)1 GET (javax.ws.rs.GET)1 MapPropertiesDelegate (org.glassfish.jersey.internal.MapPropertiesDelegate)1