Search in sources :

Example 1 with NoDataFoundException

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

the class ProtobufResource method createOrReplace.

private CompletionStage<RestResponse> createOrReplace(RestRequest request, boolean create) {
    String schemaName = checkMandatorySchemaName(request);
    ContentSource contents = request.contents();
    if (contents == null)
        throw new NoDataFoundException("Schema data not sent in the request");
    AdvancedCache<Object, Object> cache = invocationHelper.getRestCacheManager().getCache(ProtobufMetadataManager.PROTOBUF_METADATA_CACHE_NAME, request);
    NettyRestResponse.Builder builder = new NettyRestResponse.Builder();
    CompletableFuture<Object> putSchema;
    if (create) {
        putSchema = cache.putIfAbsentAsync(schemaName, contents.asString()).thenApply(result -> {
            if (result == null) {
                builder.status(HttpResponseStatus.CREATED);
            } else {
                builder.status(HttpResponseStatus.CONFLICT);
            }
            return result;
        });
    } else {
        putSchema = cache.putAsync(schemaName, contents.asString()).thenApply(result -> builder.status(HttpResponseStatus.OK));
    }
    return putSchema.thenCompose(r -> {
        if (isOkOrCreated(builder)) {
            return cache.getAsync(schemaName + ProtobufMetadataManagerConstants.ERRORS_KEY_SUFFIX);
        } else {
            return CompletableFutures.completedNull();
        }
    }).thenApply(validationError -> {
        if (isOkOrCreated(builder)) {
            ProtoSchema protoSchema = new ProtoSchema();
            protoSchema.name = schemaName;
            if (validationError != null) {
                protoSchema.error = createErrorContent(schemaName, (String) validationError);
            }
            addEntityAsJson(protoSchema, builder);
        }
        return builder.build();
    });
}
Also used : PUT(org.infinispan.rest.framework.Method.PUT) JsonSerialization(org.infinispan.commons.dataconversion.internal.JsonSerialization) InternalCacheEntry(org.infinispan.container.entries.InternalCacheEntry) ResourceHandler(org.infinispan.rest.framework.ResourceHandler) GET(org.infinispan.rest.framework.Method.GET) NoDataFoundException(org.infinispan.rest.operations.exceptions.NoDataFoundException) CompletableFuture(java.util.concurrent.CompletableFuture) POST(org.infinispan.rest.framework.Method.POST) Function(java.util.function.Function) MediaType(org.infinispan.commons.dataconversion.MediaType) RestCacheManager(org.infinispan.rest.cachemanager.RestCacheManager) AdvancedCache(org.infinispan.AdvancedCache) CompletableFutures(org.infinispan.util.concurrent.CompletableFutures) DELETE(org.infinispan.rest.framework.Method.DELETE) ResourceUtil.asJsonResponse(org.infinispan.rest.resources.ResourceUtil.asJsonResponse) RestRequest(org.infinispan.rest.framework.RestRequest) NoKeyException(org.infinispan.rest.operations.exceptions.NoKeyException) Invocations(org.infinispan.rest.framework.impl.Invocations) Flowable(io.reactivex.rxjava3.core.Flowable) NettyRestResponse(org.infinispan.rest.NettyRestResponse) AuthorizationPermission(org.infinispan.security.AuthorizationPermission) ContentSource(org.infinispan.rest.framework.ContentSource) ProtobufMetadataManager(org.infinispan.query.remote.ProtobufMetadataManager) InvocationHelper(org.infinispan.rest.InvocationHelper) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) Json(org.infinispan.commons.dataconversion.internal.Json) Collectors(java.util.stream.Collectors) ProtobufMetadataManagerConstants(org.infinispan.query.remote.client.ProtobufMetadataManagerConstants) CompletionStage(java.util.concurrent.CompletionStage) ResourceUtil.addEntityAsJson(org.infinispan.rest.resources.ResourceUtil.addEntityAsJson) AuditContext(org.infinispan.security.AuditContext) RestResponse(org.infinispan.rest.framework.RestResponse) Comparator(java.util.Comparator) ContentSource(org.infinispan.rest.framework.ContentSource) NoDataFoundException(org.infinispan.rest.operations.exceptions.NoDataFoundException) NettyRestResponse(org.infinispan.rest.NettyRestResponse)

Example 2 with NoDataFoundException

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

the class BaseCacheResource method putValueToCache.

CompletionStage<RestResponse> putValueToCache(RestRequest request) {
    String cacheName = request.variables().get("cacheName");
    MediaType contentType = request.contentType();
    MediaType keyContentType = request.keyContentType();
    RestCacheManager<Object> restCacheManager = invocationHelper.getRestCacheManager();
    AdvancedCache<Object, Object> cache = restCacheManager.getCache(cacheName, keyContentType, contentType, request);
    Object key = getKey(request);
    NettyRestResponse.Builder responseBuilder = new NettyRestResponse.Builder().status(HttpResponseStatus.NO_CONTENT);
    ContentSource contents = request.contents();
    if (contents == null)
        throw new NoDataFoundException();
    Long ttl = request.getTimeToLiveSecondsHeader();
    Long idle = request.getMaxIdleTimeSecondsHeader();
    byte[] data = request.contents().rawContent();
    return restCacheManager.getPrivilegedInternalEntry(cache, key, true).thenCompose(entry -> {
        if (request.method() == POST && entry != null) {
            return CompletableFuture.completedFuture(responseBuilder.status(HttpResponseStatus.CONFLICT).entity("An entry already exists").build());
        }
        if (entry instanceof InternalCacheEntry) {
            InternalCacheEntry ice = (InternalCacheEntry) entry;
            String etagNoneMatch = request.getEtagIfNoneMatchHeader();
            if (etagNoneMatch != null) {
                String etag = calcETAG(ice.getValue());
                if (etagNoneMatch.equals(etag)) {
                    // client's and our ETAG match. Nothing to do, an entry is cached on the client side...
                    responseBuilder.status(HttpResponseStatus.NOT_MODIFIED);
                    return CompletableFuture.completedFuture(responseBuilder.build());
                }
            }
        }
        return putInCache(responseBuilder, cache, key, data, ttl, idle);
    });
}
Also used : ContentSource(org.infinispan.rest.framework.ContentSource) InternalCacheEntry(org.infinispan.container.entries.InternalCacheEntry) MediaType(org.infinispan.commons.dataconversion.MediaType) MediaTypeUtils.negotiateMediaType(org.infinispan.rest.resources.MediaTypeUtils.negotiateMediaType) NettyRestResponse(org.infinispan.rest.NettyRestResponse) NoDataFoundException(org.infinispan.rest.operations.exceptions.NoDataFoundException)

Aggregations

MediaType (org.infinispan.commons.dataconversion.MediaType)2 InternalCacheEntry (org.infinispan.container.entries.InternalCacheEntry)2 NettyRestResponse (org.infinispan.rest.NettyRestResponse)2 ContentSource (org.infinispan.rest.framework.ContentSource)2 NoDataFoundException (org.infinispan.rest.operations.exceptions.NoDataFoundException)2 HttpResponseStatus (io.netty.handler.codec.http.HttpResponseStatus)1 Flowable (io.reactivex.rxjava3.core.Flowable)1 Comparator (java.util.Comparator)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 CompletionStage (java.util.concurrent.CompletionStage)1 Function (java.util.function.Function)1 Collectors (java.util.stream.Collectors)1 AdvancedCache (org.infinispan.AdvancedCache)1 Json (org.infinispan.commons.dataconversion.internal.Json)1 JsonSerialization (org.infinispan.commons.dataconversion.internal.JsonSerialization)1 ProtobufMetadataManager (org.infinispan.query.remote.ProtobufMetadataManager)1 ProtobufMetadataManagerConstants (org.infinispan.query.remote.client.ProtobufMetadataManagerConstants)1 InvocationHelper (org.infinispan.rest.InvocationHelper)1 RestCacheManager (org.infinispan.rest.cachemanager.RestCacheManager)1 DELETE (org.infinispan.rest.framework.Method.DELETE)1