Search in sources :

Example 1 with UnacceptableDataFormatException

use of org.infinispan.rest.operations.exceptions.UnacceptableDataFormatException in project infinispan by infinispan.

the class MediaTypeUtils method negotiateMediaType.

/**
 * Negotiates the {@link MediaType} to be used during the request execution
 *
 * @param cache the {@link AdvancedCache} associated with the request
 * @param restRequest the {@link RestRequest} with the headers
 * @return The negotiated MediaType
 * @throws UnacceptableDataFormatException if no suitable {@link MediaType} could be found.
 */
static MediaType negotiateMediaType(AdvancedCache<?, ?> cache, EncoderRegistry registry, RestRequest restRequest) throws UnacceptableDataFormatException {
    try {
        String accept = restRequest.getAcceptHeader();
        MediaType storageMedia = cache.getValueDataConversion().getStorageMediaType();
        Optional<MediaType> negotiated = MediaType.parseList(accept).filter(media -> registry.isConversionSupported(storageMedia, media)).findFirst();
        return negotiated.map(m -> {
            if (!m.matchesAll())
                return m;
            MediaType storageMediaType = cache.getValueDataConversion().getStorageMediaType();
            if (storageMediaType == null)
                return m;
            if (storageMediaType.equals(MediaType.APPLICATION_OBJECT))
                return TEXT_PLAIN;
            if (storageMediaType.match(MediaType.APPLICATION_PROTOSTREAM))
                return APPLICATION_JSON;
            return m;
        }).orElseThrow(() -> Log.REST.unsupportedDataFormat(accept));
    } catch (EncodingException e) {
        throw new UnacceptableDataFormatException();
    }
}
Also used : Arrays(java.util.Arrays) MediaType(org.infinispan.commons.dataconversion.MediaType) AdvancedCache(org.infinispan.AdvancedCache) APPLICATION_JSON(org.infinispan.commons.dataconversion.MediaType.APPLICATION_JSON) EncodingException(org.infinispan.commons.dataconversion.EncodingException) Optional(java.util.Optional) UnacceptableDataFormatException(org.infinispan.rest.operations.exceptions.UnacceptableDataFormatException) EncoderRegistry(org.infinispan.marshall.core.EncoderRegistry) Log(org.infinispan.rest.logging.Log) RestRequest(org.infinispan.rest.framework.RestRequest) TEXT_PLAIN(org.infinispan.commons.dataconversion.MediaType.TEXT_PLAIN) UnacceptableDataFormatException(org.infinispan.rest.operations.exceptions.UnacceptableDataFormatException) EncodingException(org.infinispan.commons.dataconversion.EncodingException) MediaType(org.infinispan.commons.dataconversion.MediaType)

Aggregations

Arrays (java.util.Arrays)1 Optional (java.util.Optional)1 AdvancedCache (org.infinispan.AdvancedCache)1 EncodingException (org.infinispan.commons.dataconversion.EncodingException)1 MediaType (org.infinispan.commons.dataconversion.MediaType)1 APPLICATION_JSON (org.infinispan.commons.dataconversion.MediaType.APPLICATION_JSON)1 TEXT_PLAIN (org.infinispan.commons.dataconversion.MediaType.TEXT_PLAIN)1 EncoderRegistry (org.infinispan.marshall.core.EncoderRegistry)1 RestRequest (org.infinispan.rest.framework.RestRequest)1 Log (org.infinispan.rest.logging.Log)1 UnacceptableDataFormatException (org.infinispan.rest.operations.exceptions.UnacceptableDataFormatException)1