Package org.apache.wink.common.internal.http

Examples of org.apache.wink.common.internal.http.AcceptCharset$ValuedCharset


                } else {
                    acceptable.add(qCharset.charset);
                }
            }
        }
        return new AcceptCharset(value, acceptable, banned, anyAllowed, vCharsets);
    }
View Full Code Here


            encodings = AcceptEncoding.valueOf(acceptEncodings);
        }

        List<String> acceptableCharsets =
            msgContext.getHttpHeaders().getRequestHeader(HttpHeaders.ACCEPT_CHARSET);
        AcceptCharset charsets = null;
        if (acceptableCharsets != null) {
            StringBuilder acceptCharsetsTemp = new StringBuilder();
            acceptCharsetsTemp.append(acceptableCharsets.get(0));
            for (int c = 1; c < acceptableCharsets.size(); ++c) {
                acceptCharsetsTemp.append(",");
                acceptCharsetsTemp.append(acceptableCharsets.get(c));
            }
            String acceptCharsets = acceptCharsetsTemp.toString();
            charsets = AcceptCharset.valueOf(acceptCharsets);
        }

        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);
            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 {}",
                                 vMediaType,
                                 mt);
                    if (mt.isCompatible(vMediaType)) {
                        Map<String, String> params = mt.getParameters();
                        String q = params.get("q");
                        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 {}",
                                               mt,
                                               qAsDouble);
                                    break;
                                }
                                acceptQFactor = qAsDouble;
                            } catch (NumberFormatException e) {
                                logger
                                    .debug("NumberFormatException during MediaType q-factor evaluation: {}",
                                           e);
                            }
                        } else {
                            acceptQFactor = 1.0d;
                        }

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

            if (bestVariant != null) {
                if (acceptQFactor < bestVariant.acceptMediaTypeQFactor) {
                    logger
                        .debug("Best variant's media type {} q-factor {} is greater than current variant {} q-factor {}",
                               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);
                if (languages.getBannedLanguages().contains(vLocale)) {
                    logger.debug("Variant locale {} was in unacceptable languages", vLocale);
                    continue;
                }
                for (AcceptLanguage.ValuedLocale locale : languages.getValuedLocales()) {
                    logger
                        .debug("Checking against Accept-Language locale {} with quality factor {}",
                               locale.locale,
                               locale.qValue);
                    if (locale.isWildcard() || vLocale.equals(locale.locale)) {
                        logger.debug("Locale is compatible {}", locale.locale);
                        isCompatible = true;
                        acceptLanguageQFactor = locale.qValue;
                        break;
                    }
                }
                if (!isCompatible) {
                    logger.debug("Variant locale is not compatible {}", vLocale);
                    continue;
                }
            }

            if (bestVariant != null) {
                if (acceptLanguageQFactor < bestVariant.acceptLanguageQFactor) {
                    logger
                        .debug("Best variant's language {} q-factor {} is greater than current variant {} q-factor {}",
                               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);
                if (charsets.getBannedCharsets().contains(vCharset)) {
                    logger.debug("Variant charset {} was in unacceptable charsets", vCharset);
                    continue;
                }
                for (AcceptCharset.ValuedCharset charset : charsets.getValuedCharsets()) {
                    logger
                        .debug("Checking against Accept-Charset charset {} with quality factor {}",
                               charset.charset,
                               charset.qValue);
                    if (charset.isWildcard() || vCharset.equalsIgnoreCase(charset.charset)) {
View Full Code Here

            encodings = AcceptEncoding.valueOf(acceptEncodings);
        }

        List<String> acceptableCharsets =
            msgContext.getHttpHeaders().getRequestHeader(HttpHeaders.ACCEPT_CHARSET);
        AcceptCharset charsets = null;
        if (acceptableCharsets != null) {
            StringBuilder acceptCharsetsTemp = new StringBuilder();
            acceptCharsetsTemp.append(acceptableCharsets.get(0));
            for (int c = 1; c < acceptableCharsets.size(); ++c) {
                acceptCharsetsTemp.append(","); //$NON-NLS-1$
                acceptCharsetsTemp.append(acceptableCharsets.get(c));
            }
            String acceptCharsets = acceptCharsetsTemp.toString();
            charsets = AcceptCharset.valueOf(acceptCharsets);
        }

        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)) {
View Full Code Here

        logger.debug("acceptCharsets combined value is {}", acceptCharsets);
        String cached = preferredCharsets.get(acceptCharsets);
        if (cached != null) {
            return cached;
        }
        AcceptCharset charsets = AcceptCharset.valueOf(acceptCharsets);

        if (charsets.isAnyCharsetAllowed()) {
            preferredCharsets.put(acceptCharsets, DEFAULT_CHARSET);
            return DEFAULT_CHARSET;
        }

        List<String> orderedCharsets = charsets.getAcceptableCharsets();
        logger.debug("orderedCharsets is {}", orderedCharsets);
        if (!orderedCharsets.isEmpty()) {
            for (int c = 0; c < orderedCharsets.size(); ++c) {
                String charset = orderedCharsets.get(c);
                try {
View Full Code Here

        }
    }

    @Test
    public void testFromStringNull() {
        AcceptCharset charset = acceptCharsetDelegate.fromString(null);
        assertTrue(charset.isAnyCharsetAllowed());
        assertEquals(Collections.EMPTY_LIST, charset.getAcceptableCharsets());
        assertEquals(Collections.EMPTY_LIST, charset.getBannedCharsets());
    }
View Full Code Here

        assertEquals(Collections.EMPTY_LIST, charset.getBannedCharsets());
    }

    @Test
    public void testFromStringEmpty() {
        AcceptCharset charset = acceptCharsetDelegate.fromString("");
        assertFalse(charset.isAnyCharsetAllowed());
        assertEquals(Collections.singletonList("ISO-8859-1"), charset.getAcceptableCharsets());
        assertEquals(Collections.EMPTY_LIST, charset.getBannedCharsets());
    }
View Full Code Here

        assertEquals(Collections.EMPTY_LIST, charset.getBannedCharsets());
    }

    @Test
    public void testFromStringUTF8Only() {
        AcceptCharset charset = acceptCharsetDelegate.fromString("utf-8");
        assertFalse(charset.isAnyCharsetAllowed());
        // see HTTP Accept-Char set
        assertEquals(Arrays.asList(new String[] {"ISO-8859-1", "utf-8"}), charset
            .getAcceptableCharsets());
        assertEquals(Collections.EMPTY_LIST, charset.getBannedCharsets());
    }
View Full Code Here

        assertEquals(Collections.EMPTY_LIST, charset.getBannedCharsets());
    }

    @Test
    public void testFromStringUTF8AndWildcard() {
        AcceptCharset charset = acceptCharsetDelegate.fromString("utf-8,*");
        assertTrue(charset.isAnyCharsetAllowed());
        assertEquals(Arrays.asList(new String[] {"utf-8"}), charset.getAcceptableCharsets());
        // note that any charset allowed means ISO-8859-1 is part of wildcard,
        // see HTTP spec
        assertEquals(Collections.EMPTY_LIST, charset.getBannedCharsets());
    }
View Full Code Here

        assertEquals(Collections.EMPTY_LIST, charset.getBannedCharsets());
    }

    @Test
    public void testFromStringUTF8WithQuality() {
        AcceptCharset charset = acceptCharsetDelegate.fromString("utf-8;q=0.8");
        assertFalse(charset.isAnyCharsetAllowed());
        assertEquals(Arrays.asList(new String[] {"ISO-8859-1", "utf-8"}), charset
            .getAcceptableCharsets());
        assertEquals(Arrays.asList(new AcceptCharset.ValuedCharset[] {
            new AcceptCharset.ValuedCharset(1.0, "ISO-8859-1"),
            new AcceptCharset.ValuedCharset(0.8, "utf-8")}), charset.getValuedCharsets());
        assertEquals(Collections.EMPTY_LIST, charset.getBannedCharsets());
    }
View Full Code Here

        assertEquals(Collections.EMPTY_LIST, charset.getBannedCharsets());
    }

    @Test
    public void testFromStringUTF8WithQualityAndWildcardWithQuality() {
        AcceptCharset charset = acceptCharsetDelegate.fromString("utf-8;q=0.8, *;q=0.4");
        assertTrue(charset.isAnyCharsetAllowed());
        assertEquals(Arrays.asList(new String[] {"utf-8"}), charset.getAcceptableCharsets());
        assertEquals(Arrays.asList(new AcceptCharset.ValuedCharset[] {
            new AcceptCharset.ValuedCharset(0.8, "utf-8"),
            new AcceptCharset.ValuedCharset(0.4, null)}), charset.getValuedCharsets());
        // note that any charset allowed means ISO-8859-1 is part of wildcard,
        // see HTTP spec
        assertEquals(Collections.EMPTY_LIST, charset.getBannedCharsets());
    }
View Full Code Here

TOP

Related Classes of org.apache.wink.common.internal.http.AcceptCharset$ValuedCharset

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.