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

Examples of org.apache.wink.common.internal.http.AcceptEncoding$ValuedEncoding


                } else {
                    acceptable.add(qEncoding.encoding);
                }
            }
        }
        return new AcceptEncoding(value, acceptable, banned, anyAllowed, vEncodings);
    }
View Full Code Here


            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(",");
                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(",");
                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)) {
                        logger.debug("Charset is compatible with {}", charset.charset);
                        isCompatible = true;
                        acceptCharsetQFactor = charset.qValue;
                        break;
                    }
                }

                if (!isCompatible) {
                    logger.debug("Variant charset is not compatible {}", vCharset);
                    /*
                     * 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 {}",
                               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);
                if (encodings == null || encodings.isAnyEncodingAllowed()) {
                    logger.debug("Accept-Encoding is null or wildcard");
                    if (!v.getEncoding().equalsIgnoreCase("identity")) {
                        logger
                            .debug("Variant encoding {} does not equal identity so not acceptable",
                                   vEncoding);
                        // if there is no Accept Encoding, only identity is
                        // acceptable
                        // mark that identity encoding was checked so that the
                        // Vary header has Accept-Encoding added appropriately
                        isIdentityEncodingChecked = true;
                        continue;
                    }
                } else {
                    boolean isAcceptable = true;
                    for (String encoding : encodings.getBannedEncodings()) {
                        logger.debug("Checking against not acceptable encoding: {}", encoding);
                        if (encoding.equalsIgnoreCase(vEncoding)) {
                            logger.debug("Encoding was not acceptable: {}", vEncoding);
                            isAcceptable = false;
                            break;
                        }
                    }
                    if (!isAcceptable) {
                        continue;
                    }

                    boolean isCompatible = false;
                    for (AcceptEncoding.ValuedEncoding encoding : encodings.getValuedEncodings()) {
                        logger.debug("Checking against acceptable encoding: {}", encoding.encoding);
                        if (encoding.isWildcard() || encoding.encoding.equalsIgnoreCase(vEncoding)) {
                            isCompatible = true;
                            acceptEncodingQFactor = encoding.qValue;
                            logger.debug("Encoding {} was acceptable with q-factor {}",
View Full Code Here

                } else {
                    acceptable.add(qEncoding.encoding);
                }
            }
        }
        return new AcceptEncoding(value, acceptable, banned, anyAllowed, vEncodings);
    }
View Full Code Here

            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)) {
                        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 || encodings.isAnyEncodingAllowed()) {
                    logger.debug("Accept-Encoding is null or wildcard"); //$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
                        // mark that identity encoding was checked so that the
                        // Vary header has Accept-Encoding added appropriately
                        isIdentityEncodingChecked = true;
                        continue;
                    }
                } else {
                    boolean isAcceptable = true;
                    for (String encoding : encodings.getBannedEncodings()) {
                        logger.debug("Checking against not acceptable encoding: {}", encoding); //$NON-NLS-1$
                        if (encoding.equalsIgnoreCase(vEncoding)) {
                            logger.debug("Encoding was not acceptable: {}", vEncoding); //$NON-NLS-1$
                            isAcceptable = false;
                            break;
                        }
                    }
                    if (!isAcceptable) {
                        continue;
                    }

                    boolean isCompatible = false;
                    for (AcceptEncoding.ValuedEncoding encoding : encodings.getValuedEncodings()) {
                        logger.debug("Checking against acceptable encoding: {}", encoding.encoding); //$NON-NLS-1$
                        if (encoding.isWildcard() || encoding.encoding.equalsIgnoreCase(vEncoding)) {
                            isCompatible = true;
                            acceptEncodingQFactor = encoding.qValue;
                            logger.debug("Encoding {} was acceptable with q-factor {}", //$NON-NLS-1$
View Full Code Here

        /*
         * wraps the servlet response if necessary
         */
        if (servletRequest instanceof HttpServletRequest && servletResponse instanceof HttpServletResponse) {
            HttpServletRequest httpServletRequest = (HttpServletRequest)servletRequest;
            final AcceptEncoding acceptEncoding = getAcceptEncodingHeader(httpServletRequest);
            logger.trace("AcceptEncoding header was {}", acceptEncoding); //$NON-NLS-1$
            if (acceptEncoding != null && (acceptEncoding.isAnyEncodingAllowed() || acceptEncoding
                .getAcceptableEncodings().size() > 0)) {
                logger.trace("AcceptEncoding header was set so wrapping HttpServletResponse"); //$NON-NLS-1$
                HttpServletResponseContentEncodingWrapperImpl wrappedServletResponse =
                    new HttpServletResponseContentEncodingWrapperImpl(
                                                                      (HttpServletResponse)servletResponse,
View Full Code Here

                    logger.debug("qEncoding is not a wildcard so adding to acceptable list");
                    acceptable.add(qEncoding.encoding);
                }
            }
        }
        return new AcceptEncoding(value, acceptable, banned, anyAllowed, vEncodings);
    }
View Full Code Here

        /*
         * wraps the servlet response if necessary
         */
        if (servletRequest instanceof HttpServletRequest && servletResponse instanceof HttpServletResponse) {
            HttpServletRequest httpServletRequest = (HttpServletRequest)servletRequest;
            final AcceptEncoding acceptEncoding = getAcceptEncodingHeader(httpServletRequest);
            logger.debug("AcceptEncoding header was {}", acceptEncoding);
            if (acceptEncoding != null && (acceptEncoding.isAnyEncodingAllowed() || acceptEncoding
                .getAcceptableEncodings().size() > 0)) {
                logger.debug("AcceptEncoding header was set so wrapping HttpServletResponse");
                HttpServletResponseContentEncodingWrapperImpl wrappedServletResponse =
                    new HttpServletResponseContentEncodingWrapperImpl(
                                                                      (HttpServletResponse)servletResponse,
View Full Code Here

            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)) {
                        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
                        // mark that identity encoding was checked so that the
                        // Vary header has Accept-Encoding added appropriately
                        isIdentityEncodingChecked = true;
                        continue;
                    }
                } else {
                    boolean isAcceptable = true;
                    for (String encoding : encodings.getBannedEncodings()) {
                        logger.debug("Checking against not acceptable encoding: {}", encoding); //$NON-NLS-1$
                        if (encoding.equalsIgnoreCase(vEncoding)) {
                            logger.debug("Encoding was not acceptable: {}", vEncoding); //$NON-NLS-1$
                            isAcceptable = false;
                            break;
                        }
                    }
                    if (!isAcceptable) {
                        continue;
                    }

                    boolean isCompatible = false;
                    for (AcceptEncoding.ValuedEncoding encoding : encodings.getValuedEncodings()) {
                        logger.debug("Checking against acceptable encoding: {}", encoding.encoding); //$NON-NLS-1$
                        if (encoding.isWildcard() || encoding.encoding.equalsIgnoreCase(vEncoding)) {
                            isCompatible = true;
                            acceptEncodingQFactor = encoding.qValue;
                            logger.debug("Encoding {} was acceptable with q-factor {}", //$NON-NLS-1$
View Full Code Here

                    logger.trace("qEncoding is not a wildcard so adding to acceptable list"); //$NON-NLS-1$
                    acceptable.add(qEncoding.encoding);
                }
            }
        }
        return new AcceptEncoding(value, acceptable, banned, anyAllowed, vEncodings);
    }
View Full Code Here

            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.trace("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.trace("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
                                        .trace("Accept Media Type: {} is NOT compatible with q-factor {}", //$NON-NLS-1$
                                               mt,
                                               qAsDouble);
                                    break;
                                }
                                acceptQFactor = qAsDouble;
                            } catch (NumberFormatException e) {
                                logger
                                    .trace("NumberFormatException during MediaType q-factor evaluation: {}", //$NON-NLS-1$
                                           e);
                            }
                        } else {
                            acceptQFactor = 1.0d;
                        }

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

            if (bestVariant != null) {
                if (acceptQFactor < bestVariant.acceptMediaTypeQFactor) {
                    logger
                        .trace("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.trace("Checking variant locale {}", vLocale); //$NON-NLS-1$
                if (languages.getBannedLanguages().contains(vLocale)) {
                    logger.trace("Variant locale {} was in unacceptable languages", vLocale); //$NON-NLS-1$
                    continue;
                }
                for (AcceptLanguage.ValuedLocale locale : languages.getValuedLocales()) {
                    logger
                        .trace("Checking against Accept-Language locale {} with quality factor {}", //$NON-NLS-1$
                               locale.locale,
                               locale.qValue);
                    if (locale.isWildcard() || vLocale.equals(locale.locale)) {
                        logger.trace("Locale is compatible {}", locale.locale); //$NON-NLS-1$
                        isCompatible = true;
                        acceptLanguageQFactor = locale.qValue;
                        break;
                    }
                }
                if (!isCompatible) {
                    logger.trace("Variant locale is not compatible {}", vLocale); //$NON-NLS-1$
                    continue;
                }
            }

            if (bestVariant != null) {
                if (acceptLanguageQFactor < bestVariant.acceptLanguageQFactor) {
                    logger
                        .trace("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.trace("Checking variant charset: {}", vCharset); //$NON-NLS-1$
                if (charsets.getBannedCharsets().contains(vCharset)) {
                    logger.trace("Variant charset {} was in unacceptable charsets", vCharset); //$NON-NLS-1$
                    continue;
                }
                for (AcceptCharset.ValuedCharset charset : charsets.getValuedCharsets()) {
                    logger
                        .trace("Checking against Accept-Charset charset {} with quality factor {}", //$NON-NLS-1$
                               charset.charset,
                               charset.qValue);
                    if (charset.isWildcard() || vCharset.equalsIgnoreCase(charset.charset)) {
                        logger.trace("Charset is compatible with {}", charset.charset); //$NON-NLS-1$
                        isCompatible = true;
                        acceptCharsetQFactor = charset.qValue;
                        break;
                    }
                }

                if (!isCompatible) {
                    logger.trace("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
                        .trace("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.trace("Checking variant encoding {}", vEncoding); //$NON-NLS-1$
                if (encodings == null) {
                    logger.trace("Accept-Encoding is null"); //$NON-NLS-1$
                    if (!v.getEncoding().equalsIgnoreCase("identity")) { //$NON-NLS-1$
                        logger
                            .trace("Variant encoding {} does not equal identity so not acceptable", //$NON-NLS-1$
                                   vEncoding);
                        // if there is no Accept Encoding, only identity is
                        // acceptable
                        // mark that identity encoding was checked so that the
                        // Vary header has Accept-Encoding added appropriately
                        isIdentityEncodingChecked = true;
                        continue;
                    }
                } else {
                    boolean isAcceptable = true;
                    for (String encoding : encodings.getBannedEncodings()) {
                        logger.trace("Checking against not acceptable encoding: {}", encoding); //$NON-NLS-1$
                        if (encoding.equalsIgnoreCase(vEncoding)) {
                            logger.trace("Encoding was not acceptable: {}", vEncoding); //$NON-NLS-1$
                            isAcceptable = false;
                            break;
                        }
                    }
                    if (!isAcceptable) {
                        continue;
                    }

                    boolean isCompatible = false;
                    for (AcceptEncoding.ValuedEncoding encoding : encodings.getValuedEncodings()) {
                        logger.trace("Checking against acceptable encoding: {}", encoding.encoding); //$NON-NLS-1$
                        if (encoding.isWildcard() || encoding.encoding.equalsIgnoreCase(vEncoding)) {
                            isCompatible = true;
                            acceptEncodingQFactor = encoding.qValue;
                            logger.trace("Encoding {} was acceptable with q-factor {}", //$NON-NLS-1$
View Full Code Here

TOP

Related Classes of org.apache.wink.common.internal.http.AcceptEncoding$ValuedEncoding

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.