Package javax.ws.rs.core

Examples of javax.ws.rs.core.Variant$VariantListBuilder


            throw new WebApplicationException();
        }
        List<Variant> variants =
            Variant.languages(Locale.ENGLISH, Locale.JAPANESE, Locale.CHINESE, Locale.GERMAN).add()
                .build();
        Variant targettedVariant = req.selectVariant(variants);
        if (targettedVariant == null) {
            return Response.status(466).build();
        }

        return Response.ok(targettedVariant.getLanguage().getLanguage()).build();
    }
View Full Code Here


    public Response evalAcceptEncodingVariant(@Context Request req) {
        if (!"GET".equals(req.getMethod())) {
            throw new WebApplicationException();
        }
        List<Variant> variants = Variant.encodings("compress", "gzip", "identity").add().build();
        Variant targettedVariant = req.selectVariant(variants);
        if (targettedVariant == null) {
            return Response.status(466).build();
        }

        return Response.ok(targettedVariant.getEncoding()).build();
    }
View Full Code Here

                .mediaTypes(MediaType.APPLICATION_JSON_TYPE,
                            MediaType.valueOf("text/*"),
                            MediaType.TEXT_XML_TYPE,
                            MediaType.TEXT_HTML_TYPE).encodings("compress", "gzip", "identity")
                .add().build();
        Variant targettedVariant = req.selectVariant(variants);
        if ("notacceptable".equals(type)) {
            List<Variant> notAcceptableVariants =
                Variant.mediaTypes(MediaType.APPLICATION_JSON_TYPE,
                                   MediaType.valueOf("text/*"),
                                   MediaType.TEXT_XML_TYPE,
                                   MediaType.TEXT_HTML_TYPE).encodings("compress",
                                                                       "gzip",
                                                                       "identity").add().build();
            return Response.notAcceptable(notAcceptableVariants).build();
        } else if ("variants".equals(type)) {
            List<Variant> okVariants =
                Variant.languages(Locale.ENGLISH, Locale.JAPANESE, Locale.CHINESE, Locale.GERMAN)
                    .encodings("compress", "gzip", "identity").add().build();
            return Response.ok().variants(okVariants).build();
        }
        if (targettedVariant == null) {
            return Response.status(466).build();
        }
        return Response.ok(targettedVariant.getMediaType().getType() + "/"
                               + targettedVariant.getMediaType().getSubtype()
                               + "-"
                               + ((targettedVariant.getLanguage() != null) ? targettedVariant
                                   .getLanguage().getLanguage() : "")
                               + "-"
                               + targettedVariant.getEncoding(),
                           new Variant(new MediaType("text", "plain"), Locale.ENGLISH, "identity"))
            .build();
    }
View Full Code Here

                .mediaTypes(MediaType.APPLICATION_JSON_TYPE,
                            MediaType.valueOf("text/*"),
                            MediaType.TEXT_XML_TYPE,
                            MediaType.TEXT_HTML_TYPE).encodings("compress", "gzip", "identity")
                .add().build();
        Variant targettedVariant = req.selectVariant(variants);
        if (targettedVariant == null) {
            return Response.status(466).build();
        }

        if ("xml".equals(targettedVariant.getMediaType().getSubtype())) {
            org.apache.wink.itest.request.Variant v =
                new org.apache.wink.itest.request.Variant();
            v.setMediatype(targettedVariant.getMediaType().getType() + "/"
                + targettedVariant.getMediaType().getSubtype());
            v.setEncoding(targettedVariant.getEncoding());
            v.setLanguage((targettedVariant.getLanguage() != null) ? targettedVariant.getLanguage()
                .getLanguage() : null);
            return Response.ok(v).build();
        }

        return Response.ok(targettedVariant.getMediaType().getType() + "/"
            + targettedVariant.getMediaType().getSubtype()
            + "-"
            + ((targettedVariant.getLanguage() != null) ? targettedVariant.getLanguage()
                .getLanguage() : "")
            + "-"
            + targettedVariant.getEncoding()).build();
    }
View Full Code Here

        r = Response.temporaryRedirect(loc).tag("tag").header("headerName", null).build();
        assertNull(r.getMetadata().getFirst("headerName"));

        r =
            Response.ok().variant(new Variant(new MediaType("application", "atom+xml"),
                                              new Locale("Hebrew"), "UTF8")).build();
        Object o = r.getMetadata().getFirst(HttpHeaders.CONTENT_TYPE);
        assertNotNull(o);

        List<Variant> l = Variant.encodings("UTF8").build();
        l.add(new Variant(null, null, "UTF8"));
        r = Response.noContent().variants(l).build();
        int i = r.getStatus();
        assertEquals(i, 204);
        assertNotNull(r.getMetadata().getFirst(HttpHeaders.VARY));
View Full Code Here

        List<Variant> variants =
            Variant.mediaTypes(MediaType.APPLICATION_JSON_TYPE,
                               MediaType.valueOf("text/*"),
                               MediaType.TEXT_XML_TYPE,
                               MediaType.TEXT_HTML_TYPE).add().build();
        Variant targettedVariant = req.selectVariant(variants);
        if (targettedVariant == null) {
            return Response.status(466).build();
        }

        if ("xml".equals(targettedVariant.getMediaType().getSubtype())) {
            org.apache.wink.itest.request.Variant v =
                new org.apache.wink.itest.request.Variant();
            v.setMediatype(targettedVariant.getMediaType().getType() + "/"
                + targettedVariant.getMediaType().getSubtype());
            v.setEncoding(targettedVariant.getEncoding());
            v.setLanguage((targettedVariant.getLanguage() != null) ? targettedVariant.getLanguage()
                .getLanguage() : null);
            return Response.ok(v).build();
        }

        return Response.ok(targettedVariant.getMediaType().getType() + "/"
            + targettedVariant.getMediaType().getSubtype()).build();
    }
View Full Code Here

        VariantQChecked bestVariant = null;
        boolean isIdentityEncodingChecked = false;

        for (Iterator<Variant> iter = variants.iterator(); iter.hasNext();) {
            double acceptQFactor = -1.0d;
            Variant v = iter.next();
            logger.debug("Variant being evaluated is: {}", v); //$NON-NLS-1$
            MediaType vMediaType = v.getMediaType();
            if (vMediaType != null && acceptableMediaTypes != null) {
                boolean isCompatible = false;
                boolean isAcceptable = true; // explicitly denied by the client
                for (MediaType mt : acceptableMediaTypes) {
                    logger.debug("Checking variant media type {} against Accept media type {}", //$NON-NLS-1$
                                 vMediaType,
                                 mt);
                    if (mt.isCompatible(vMediaType)) {
                        Map<String, String> params = mt.getParameters();
                        String q = params.get("q"); //$NON-NLS-1$
                        if (q != null) {
                            try {
                                Double qAsDouble = Double.valueOf(q);
                                if (qAsDouble.equals(0.0)) {
                                    isAcceptable = false;
                                    logger
                                        .debug("Accept Media Type: {} is NOT compatible with q-factor {}", //$NON-NLS-1$
                                               mt,
                                               qAsDouble);
                                    break;
                                }
                                acceptQFactor = qAsDouble;
                            } catch (NumberFormatException e) {
                                logger
                                    .debug("NumberFormatException during MediaType q-factor evaluation: {}", //$NON-NLS-1$
                                           e);
                            }
                        } else {
                            acceptQFactor = 1.0d;
                        }

                        isCompatible = true;
                        logger.debug("Accept Media Type: {} is compatible with q-factor {}", //$NON-NLS-1$
                                     mt,
                                     acceptQFactor);
                    }
                }
                if (!isCompatible || !isAcceptable) {
                    logger.debug("Variant {} is not compatible or not acceptable", vMediaType); //$NON-NLS-1$
                    continue;
                }
            }

            if (bestVariant != null) {
                if (acceptQFactor < bestVariant.acceptMediaTypeQFactor) {
                    logger
                        .debug("Best variant's media type {} q-factor {} is greater than current variant {} q-factor {}", //$NON-NLS-1$
                               new Object[] {bestVariant.variant,
                                   bestVariant.acceptMediaTypeQFactor, vMediaType, acceptQFactor});
                    continue;
                }
            }

            double acceptLanguageQFactor = -1.0d;
            Locale vLocale = v.getLanguage();
            if (vLocale != null && languages != null) {
                boolean isCompatible = false;
                logger.debug("Checking variant locale {}", vLocale); //$NON-NLS-1$
                if (languages.getBannedLanguages().contains(vLocale)) {
                    logger.debug("Variant locale {} was in unacceptable languages", vLocale); //$NON-NLS-1$
                    continue;
                }
                for (AcceptLanguage.ValuedLocale locale : languages.getValuedLocales()) {
                    logger
                        .debug("Checking against Accept-Language locale {} with quality factor {}", //$NON-NLS-1$
                               locale.locale,
                               locale.qValue);
                    if (locale.isWildcard() || vLocale.equals(locale.locale)) {
                        logger.debug("Locale is compatible {}", locale.locale); //$NON-NLS-1$
                        isCompatible = true;
                        acceptLanguageQFactor = locale.qValue;
                        break;
                    }
                }
                if (!isCompatible) {
                    logger.debug("Variant locale is not compatible {}", vLocale); //$NON-NLS-1$
                    continue;
                }
            }

            if (bestVariant != null) {
                if (acceptLanguageQFactor < bestVariant.acceptLanguageQFactor) {
                    logger
                        .debug("Best variant's language {} q-factor {} is greater than current variant {} q-factor {}", //$NON-NLS-1$
                               new Object[] {bestVariant.variant,
                                   bestVariant.acceptLanguageQFactor, v, acceptLanguageQFactor});
                    continue;
                }
            }

            double acceptCharsetQFactor = -1.0d;
            String vCharset = ProviderUtils.getCharsetOrNull(v.getMediaType());
            boolean hasCharSet = true;

            if (vCharset == null) {
                hasCharSet = false;
            } else if (vCharset != null && charsets != null) {
                boolean isCompatible = false;
                logger.debug("Checking variant charset: {}", vCharset); //$NON-NLS-1$
                if (charsets.getBannedCharsets().contains(vCharset)) {
                    logger.debug("Variant charset {} was in unacceptable charsets", vCharset); //$NON-NLS-1$
                    continue;
                }
                for (AcceptCharset.ValuedCharset charset : charsets.getValuedCharsets()) {
                    logger
                        .debug("Checking against Accept-Charset charset {} with quality factor {}", //$NON-NLS-1$
                               charset.charset,
                               charset.qValue);
                    if (charset.isWildcard() || vCharset.equalsIgnoreCase(charset.charset)) {
                        logger.debug("Charset is compatible with {}", charset.charset); //$NON-NLS-1$
                        isCompatible = true;
                        acceptCharsetQFactor = charset.qValue;
                        break;
                    }
                }

                if (!isCompatible) {
                    logger.debug("Variant charset is not compatible {}", vCharset); //$NON-NLS-1$
                    /*
                     * do not remove this from the acceptable list even if not
                     * compatible but set to -1.0d for now. according to HTTP
                     * spec, it is "ok" to send
                     */
                }
            }

            if (bestVariant != null) {
                if (acceptCharsetQFactor < bestVariant.acceptCharsetQFactor && hasCharSet) {
                    logger
                        .debug("Best variant's charset {} q-factor {} is greater than current variant {} q-factor {}", //$NON-NLS-1$
                               new Object[] {bestVariant.variant, bestVariant.acceptCharsetQFactor,
                                   v, acceptCharsetQFactor});
                    continue;
                }
            }

            double acceptEncodingQFactor = -1.0d;
            String vEncoding = v.getEncoding();
            if (vEncoding != null) {
                logger.debug("Checking variant encoding {}", vEncoding); //$NON-NLS-1$
                if (encodings == null) {
                    logger.debug("Accept-Encoding is null"); //$NON-NLS-1$
                    if (!v.getEncoding().equalsIgnoreCase("identity")) { //$NON-NLS-1$
                        logger
                            .debug("Variant encoding {} does not equal identity so not acceptable", //$NON-NLS-1$
                                   vEncoding);
                        // if there is no Accept Encoding, only identity is
                        // acceptable
View Full Code Here

import junit.framework.TestCase;

public class VariantListBuilderImplTest extends TestCase {

    public void testBuildMediaTypes() {
        Variant variant1 = new Variant(MediaType.APPLICATION_ATOM_XML_TYPE, null, null);
        Variant variant2 = new Variant(MediaType.APPLICATION_JSON_TYPE, null, null);

        VariantListBuilder builder = new VariantListBuilderImpl();
        List<Variant> list = builder.mediaTypes(MediaType.APPLICATION_ATOM_XML_TYPE).add().build();
        assertList(new Variant[] {variant1}, list);
View Full Code Here

                .add().build();
        assertList(new Variant[] {variant1, variant2}, list);
    }

    public void testBuildLanguages() {
        Variant variant1 = new Variant(null, Locale.ENGLISH, null);
        Variant variant2 = new Variant(null, Locale.CHINESE, null);

        VariantListBuilder builder = new VariantListBuilderImpl();
        List<Variant> list = builder.languages(Locale.ENGLISH).add().build();
        assertList(new Variant[] {variant1}, list);
View Full Code Here

        list = builder.languages(Locale.ENGLISH, Locale.CHINESE).add().build();
        assertList(new Variant[] {variant1, variant2}, list);
    }

    public void testBuildEncodings() {
        Variant variant1 = new Variant(null, null, "UTF-8");
        Variant variant2 = new Variant(null, null, "UTF-16");

        VariantListBuilder builder = new VariantListBuilderImpl();
        List<Variant> list = builder.encodings("UTF-8").add().build();
        assertList(new Variant[] {variant1}, list);
View Full Code Here

TOP

Related Classes of javax.ws.rs.core.Variant$VariantListBuilder

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.