use of org.infinispan.commons.dataconversion.MediaType.APPLICATION_JSON 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();
}
}
use of org.infinispan.commons.dataconversion.MediaType.APPLICATION_JSON in project infinispan by infinispan.
the class JsonQuerySerializer method createQueryResponse.
@Override
public JsonQueryResponse createQueryResponse(RemoteQueryResult remoteQueryResult) {
long totalResults = remoteQueryResult.getTotalResults();
String[] projections = remoteQueryResult.getProjections();
JsonQueryResponse response;
if (projections == null) {
List<Object> results = remoteQueryResult.getResults().stream().map(o -> transcoderFromStorage.transcode(o, storageMediaTye, APPLICATION_JSON)).collect(toList());
List<Hit> hits = results.stream().map(Hit::new).collect(Collectors.toList());
response = new JsonQueryResult(hits, totalResults);
} else {
response = new ProjectedJsonResult(totalResults, projections, remoteQueryResult.getResults());
}
return response;
}
Aggregations