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