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

Examples of org.apache.wink.common.internal.http.AcceptLanguage$ValuedLocale


        assertTrue("empty acceptable", alh.getAcceptableLanguages().isEmpty());
        assertTrue("empty banned", alh.getBannedLanguages().isEmpty());
    }

    public void testTrivialCase() {
        AcceptLanguage alh = AcceptLanguage.valueOf("en-gb-x-y,en");
        assertFalse("wildcard", alh.isAnyLanguageAllowed());
        assertEquals("locales", Arrays.asList(new Locale[] {new Locale("en", "gb", "x-y"),
            new Locale("en")}), alh.getAcceptableLanguages());
        assertTrue("empty banned", alh.getBannedLanguages().isEmpty());
    }
View Full Code Here


            new Locale("en")}), alh.getAcceptableLanguages());
        assertTrue("empty banned", alh.getBannedLanguages().isEmpty());
    }

    public void testWithoutWildcard() {
        AcceptLanguage alh = AcceptLanguage.valueOf("da, en;q=0.7,  en-gb;q=0.8");
        assertFalse("wildcard", alh.isAnyLanguageAllowed());
        assertEquals("locales", Arrays.asList(new Locale[] {new Locale("da"),
            new Locale("en", "gb"), new Locale("en")}), alh.getAcceptableLanguages());
        assertTrue("empty banned", alh.getBannedLanguages().isEmpty());
    }
View Full Code Here

            new Locale("en", "gb"), new Locale("en")}), alh.getAcceptableLanguages());
        assertTrue("empty banned", alh.getBannedLanguages().isEmpty());
    }

    public void testWithoutZero() {
        AcceptLanguage alh = AcceptLanguage.valueOf("da, en;q=0.7,  en-gb;q=0.8, jp;q=0");
        assertFalse("wildcard", alh.isAnyLanguageAllowed());
        assertEquals("locales", Arrays.asList(new Locale[] {new Locale("da"),
            new Locale("en", "gb"), new Locale("en")}), alh.getAcceptableLanguages());
        assertTrue("empty banned", alh.getBannedLanguages().isEmpty());
    }
View Full Code Here

            new Locale("en", "gb"), new Locale("en")}), alh.getAcceptableLanguages());
        assertTrue("empty banned", alh.getBannedLanguages().isEmpty());
    }

    public void testWithWildcardNoBanned() {
        AcceptLanguage alh = AcceptLanguage.valueOf("da, *;q=0.755, en;q=0.7, en-gb;q=0.8");
        assertTrue("wildcard", alh.isAnyLanguageAllowed());
        assertEquals("locales", Arrays.asList(new Locale[] {new Locale("da"),
            new Locale("en", "gb")}), alh.getAcceptableLanguages());
        assertTrue("empty banned", alh.getBannedLanguages().isEmpty());
    }
View Full Code Here

            new Locale("en", "gb")}), alh.getAcceptableLanguages());
        assertTrue("empty banned", alh.getBannedLanguages().isEmpty());
    }

    public void testWithWildcard() {
        AcceptLanguage alh =
            AcceptLanguage.valueOf("ru;q= 0, da, *;q=0.755, en;q=0.7, en-gb;q=0.8, cz;q=0");
        assertTrue("wildcard", alh.isAnyLanguageAllowed());
        assertEquals("locales", Arrays.asList(new Locale[] {new Locale("da"),
            new Locale("en", "gb")}), alh.getAcceptableLanguages());
        assertEquals("banned",
                     Arrays.asList(new Locale[] {new Locale("ru"), new Locale("cz")}),
                     alh.getBannedLanguages());
    }
View Full Code Here

                     Arrays.asList(new Locale[] {new Locale("ru"), new Locale("cz")}),
                     alh.getBannedLanguages());
    }

    public void testIllegalQ() {
        AcceptLanguage alh = AcceptLanguage.valueOf("da;q=one");
        assertFalse("wildcard", alh.isAnyLanguageAllowed());
        assertEquals("locales", Arrays.asList(new Locale[] {new Locale("da")}), alh
            .getAcceptableLanguages());
        assertTrue("empty banned", alh.getBannedLanguages().isEmpty());
    }
View Full Code Here

                    acceptLanguageTemp.append(","); //$NON-NLS-1$
                    acceptLanguageTemp.append(requestHeader.get(c));
                }
                String acceptLanguage = acceptLanguageTemp.toString();
                logger.debug("Accept-Language combined header is {}", acceptLanguage); //$NON-NLS-1$
                AcceptLanguage acceptLanguages = AcceptLanguage.valueOf(acceptLanguage);
                acceptableLanguages = acceptLanguages.getAcceptableLanguages();
            }
        }
        logger.debug("getAcceptableLanguages() returns {}", acceptableLanguages); //$NON-NLS-1$
        return acceptableLanguages;
    }
View Full Code Here

        List<MediaType> acceptableMediaTypes =
            msgContext.getHttpHeaders().getAcceptableMediaTypes();

        List<String> acceptableLanguages =
            msgContext.getHttpHeaders().getRequestHeader(HttpHeaders.ACCEPT_LANGUAGE);
        AcceptLanguage languages = null;
        if (acceptableLanguages != null) {
            StringBuilder acceptLanguageTemp = new StringBuilder();
            acceptLanguageTemp.append(acceptableLanguages.get(0));
            for (int c = 1; c < acceptableLanguages.size(); ++c) {
                acceptLanguageTemp.append(","); //$NON-NLS-1$
                acceptLanguageTemp.append(acceptableLanguages.get(c));
            }
            String acceptLanguage = acceptLanguageTemp.toString();
            languages = AcceptLanguage.valueOf(acceptLanguage);
        }

        List<String> acceptableEncodings =
            msgContext.getHttpHeaders().getRequestHeader(HttpHeaders.ACCEPT_ENCODING);
        AcceptEncoding encodings = null;
        if (acceptableEncodings != null) {
            StringBuilder acceptEncodingsTemp = new StringBuilder();
            acceptEncodingsTemp.append(acceptableEncodings.get(0));
            for (int c = 1; c < acceptableEncodings.size(); ++c) {
                acceptEncodingsTemp.append(","); //$NON-NLS-1$
                acceptEncodingsTemp.append(acceptableEncodings.get(c));
            }
            String acceptEncodings = acceptEncodingsTemp.toString();
            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)) {
View Full Code Here

                } else {
                    acceptable.add(qLocale.locale);
                }
            }
        }
        return new AcceptLanguage(value, acceptable, banned, anyAllowed, qLocales);
    }
View Full Code Here

                    acceptLanguageTemp.append(","); //$NON-NLS-1$
                    acceptLanguageTemp.append(requestHeader.get(c));
                }
                String acceptLanguage = acceptLanguageTemp.toString();
                logger.trace("Accept-Language combined header is {}", acceptLanguage); //$NON-NLS-1$
                AcceptLanguage acceptLanguages = AcceptLanguage.valueOf(acceptLanguage);
                acceptableLanguages = acceptLanguages.getAcceptableLanguages();
            }
        }
        logger.trace("getAcceptableLanguages() returns {}", acceptableLanguages); //$NON-NLS-1$
        return acceptableLanguages;
    }
View Full Code Here

TOP

Related Classes of org.apache.wink.common.internal.http.AcceptLanguage$ValuedLocale

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.