Search in sources :

Example 1 with APPLICATION_JSON

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();
    }
}
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)

Example 2 with APPLICATION_JSON

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;
}
Also used : Transcoder(org.infinispan.commons.dataconversion.Transcoder) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) MediaType(org.infinispan.commons.dataconversion.MediaType) Hit(org.infinispan.query.remote.json.Hit) ProjectedJsonResult(org.infinispan.query.remote.json.ProjectedJsonResult) APPLICATION_JSON(org.infinispan.commons.dataconversion.MediaType.APPLICATION_JSON) JsonQueryResponse(org.infinispan.query.remote.json.JsonQueryResponse) QueryRequest(org.infinispan.query.remote.client.impl.QueryRequest) Json(org.infinispan.commons.dataconversion.internal.Json) Collectors(java.util.stream.Collectors) JsonQueryResult(org.infinispan.query.remote.json.JsonQueryResult) Hit(org.infinispan.query.remote.json.Hit) ProjectedJsonResult(org.infinispan.query.remote.json.ProjectedJsonResult) JsonQueryResult(org.infinispan.query.remote.json.JsonQueryResult) JsonQueryResponse(org.infinispan.query.remote.json.JsonQueryResponse)

Aggregations

MediaType (org.infinispan.commons.dataconversion.MediaType)2 APPLICATION_JSON (org.infinispan.commons.dataconversion.MediaType.APPLICATION_JSON)2 Arrays (java.util.Arrays)1 List (java.util.List)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1 Collectors.toList (java.util.stream.Collectors.toList)1 AdvancedCache (org.infinispan.AdvancedCache)1 EncodingException (org.infinispan.commons.dataconversion.EncodingException)1 TEXT_PLAIN (org.infinispan.commons.dataconversion.MediaType.TEXT_PLAIN)1 Transcoder (org.infinispan.commons.dataconversion.Transcoder)1 Json (org.infinispan.commons.dataconversion.internal.Json)1 EncoderRegistry (org.infinispan.marshall.core.EncoderRegistry)1 QueryRequest (org.infinispan.query.remote.client.impl.QueryRequest)1 Hit (org.infinispan.query.remote.json.Hit)1 JsonQueryResponse (org.infinispan.query.remote.json.JsonQueryResponse)1 JsonQueryResult (org.infinispan.query.remote.json.JsonQueryResult)1 ProjectedJsonResult (org.infinispan.query.remote.json.ProjectedJsonResult)1 RestRequest (org.infinispan.rest.framework.RestRequest)1 Log (org.infinispan.rest.logging.Log)1