use of org.infinispan.rest.framework.ContentSource in project infinispan by infinispan.
the class CacheResourceV2 method addSourceConnection.
private CompletionStage<RestResponse> addSourceConnection(RestRequest request) {
final NettyRestResponse.Builder builder = new NettyRestResponse.Builder();
builder.status(NO_CONTENT);
String cacheName = request.variables().get("cacheName");
ContentSource contents = request.contents();
byte[] config = contents.rawContent();
Cache<?, ?> cache = invocationHelper.getRestCacheManager().getCache(cacheName, request);
if (cache == null) {
return notFoundResponseFuture();
}
if (config == null || config.length == 0) {
return badRequestResponseFuture("A remote-store config must be provided");
}
String storeConfig = new String(config, UTF_8);
Json read = Json.read(storeConfig);
if (!read.isObject() || read.at("remote-store") == null || read.asMap().size() != 1) {
return badRequestResponseFuture("Invalid remote-store JSON description: a single remote-store element must be provided");
}
return CompletableFuture.supplyAsync(() -> {
RollingUpgradeManager upgradeManager = cache.getAdvancedCache().getComponentRegistry().getComponent(RollingUpgradeManager.class);
try {
RemoteStoreConfiguration storeConfiguration = SerializationUtils.fromJson(read.toString());
if (!upgradeManager.isConnected(MIGRATOR_NAME)) {
upgradeManager.connectSource(MIGRATOR_NAME, storeConfiguration);
} else {
builder.status(HttpResponseStatus.NOT_MODIFIED);
}
} catch (Exception e) {
Throwable rootCause = Util.getRootCause(e);
builder.status(HttpResponseStatus.INTERNAL_SERVER_ERROR).entity(rootCause.getMessage());
}
return builder.build();
}, invocationHelper.getExecutor());
}
use of org.infinispan.rest.framework.ContentSource in project infinispan by infinispan.
the class ContainerResource method convertToJson.
private CompletionStage<RestResponse> convertToJson(RestRequest restRequest) {
NettyRestResponse.Builder responseBuilder = checkCacheManager(restRequest);
if (responseBuilder.getHttpStatus() == NOT_FOUND)
return completedFuture(responseBuilder.build());
ContentSource contents = restRequest.contents();
ConfigurationBuilderHolder builderHolder = parserRegistry.parse(new String(contents.rawContent(), UTF_8));
Map.Entry<String, ConfigurationBuilder> entry = builderHolder.getNamedConfigurationBuilders().entrySet().iterator().next();
StringBuilderWriter sw = new StringBuilderWriter();
try (ConfigurationWriter w = ConfigurationWriter.to(sw).withType(APPLICATION_JSON).build()) {
invocationHelper.getParserRegistry().serialize(w, entry.getKey(), entry.getValue().build());
}
responseBuilder.contentType(APPLICATION_JSON).entity(sw.toString());
return completedFuture(responseBuilder.build());
}
use of org.infinispan.rest.framework.ContentSource 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.framework.ContentSource 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);
});
}
use of org.infinispan.rest.framework.ContentSource in project infinispan by infinispan.
the class CacheResourceQueryAction method getQueryFromJSON.
private JsonQueryRequest getQueryFromJSON(RestRequest restRequest) throws IOException {
ContentSource contents = restRequest.contents();
byte[] byteContent = contents.rawContent();
if (byteContent == null || byteContent.length == 0)
throw new IOException();
return JsonQueryRequest.fromJson(new String(byteContent, StandardCharsets.UTF_8));
}
Aggregations