use of org.infinispan.commons.dataconversion.EncodingException 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();
}
}
Aggregations