Example usage for org.springframework.http MediaType SPECIFICITY_COMPARATOR

List of usage examples for org.springframework.http MediaType SPECIFICITY_COMPARATOR

Introduction

In this page you can find the example usage for org.springframework.http MediaType SPECIFICITY_COMPARATOR.

Prototype

Comparator SPECIFICITY_COMPARATOR

To view the source code for org.springframework.http MediaType SPECIFICITY_COMPARATOR.

Click Source Link

Document

Comparator used by #sortBySpecificity(List) .

Usage

From source file:org.craftercms.commons.rest.HttpMessageConvertingResponseWriter.java

/**
 * Return the more specific of the acceptable and the producible media types with the q-value of the former.
 *///from www  . j  a  v  a  2s .  c  o m
protected MediaType getMostSpecificMediaType(MediaType acceptType, MediaType produceType) {
    produceType = produceType.copyQualityValue(acceptType);

    return MediaType.SPECIFICITY_COMPARATOR.compare(acceptType, produceType) <= 0 ? acceptType : produceType;
}

From source file:org.beadle.framework.view.ReturnTypeViewResolver.java

/**
 * Return the more specific of the acceptable and the producible media types
 * with the q-value of the former.//from  ww  w. jav a  2s .  co  m
 */
private MediaType getMostSpecificMediaType(MediaType acceptType, MediaType produceType) {
    produceType = produceType.copyQualityValue(acceptType);
    return MediaType.SPECIFICITY_COMPARATOR.compare(acceptType, produceType) < 0 ? acceptType : produceType;
}

From source file:org.openrepose.filters.ratelimiting.RateLimitingHandler.java

private Optional<MediaType> getPreferredMediaType(List<String> acceptValues) {
    Optional<MediaType> preferredMediaType = Optional.of(DEFAULT_MEDIA_TYPE);
    if (!acceptValues.isEmpty()) {
        /*// ww  w. j  av a 2s  . c om
         Parses and sorts (by specificity) media types from the "Accept" header (dropping any that cannot be parsed).
         This has intentionally been left separate from the stream processing below to avoid re-parsing
         and re-sorting accept media types for every supported media type.
          */
        List<MediaType> parsedAcceptMediaTypes = acceptValues.stream().map(RateLimitingHandler::parseMediaType)
                .filter(Optional::isPresent).map(Optional::get).sorted(MediaType.SPECIFICITY_COMPARATOR)
                .collect(Collectors.toList());

        /*
         For each supported media type, the most specific acceptable media type that is compatible is found.
         If no compatible acceptable media type if found, the supported media type will not be used.
         If the quality factor of the most specific acceptable media type compatible with a supported media type is
         set to 0, the supported media type will not be used.
                
         After discerning which supported media types are acceptable, sorts the acceptable media types by quality.
         The highest quality acceptable media type is then set as the preferred media type.
         */
        preferredMediaType = SUPPORTED_MEDIA_TYPES.stream()
                .map(supportedMediaType -> parsedAcceptMediaTypes.stream()
                        .filter(supportedMediaType::isCompatibleWith).findFirst()
                        .filter(acceptMediaType -> acceptMediaType.getQualityValue() != 0.0)
                        .map(acceptMediaType -> (Map.Entry<MediaType, MediaType>) new AbstractMap.SimpleEntry(
                                supportedMediaType, acceptMediaType)))
                .filter(Optional::isPresent).map(Optional::get)
                .sorted(Map.Entry.comparingByValue(MediaType.QUALITY_VALUE_COMPARATOR)).map(Map.Entry::getKey)
                .findFirst();
    }
    return preferredMediaType;
}

From source file:org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerAdapter.java

private MediaType getMostSpecificMediaType(MediaType acceptType, MediaType produceType) {
    produceType = produceType.copyQualityValue(acceptType);
    return MediaType.SPECIFICITY_COMPARATOR.compare(acceptType, produceType) <= 0 ? acceptType : produceType;
}

From source file:org.springframework.web.reactive.result.condition.AbstractMediaTypeExpression.java

@Override
public int compareTo(AbstractMediaTypeExpression other) {
    return MediaType.SPECIFICITY_COMPARATOR.compare(this.getMediaType(), other.getMediaType());
}

From source file:org.springframework.web.reactive.result.HandlerResultHandlerSupport.java

private MediaType selectMoreSpecificMediaType(MediaType acceptable, MediaType producible) {
    producible = producible.copyQualityValue(acceptable);
    Comparator<MediaType> comparator = MediaType.SPECIFICITY_COMPARATOR;
    return (comparator.compare(acceptable, producible) <= 0 ? acceptable : producible);
}