Search in sources :

Example 1 with ContentSource

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());
}
Also used : RollingUpgradeManager(org.infinispan.upgrade.RollingUpgradeManager) ContentSource(org.infinispan.rest.framework.ContentSource) RemoteStoreConfiguration(org.infinispan.persistence.remote.configuration.RemoteStoreConfiguration) ConfigurationBuilder(org.infinispan.configuration.cache.ConfigurationBuilder) ResourceUtil.addEntityAsJson(org.infinispan.rest.resources.ResourceUtil.addEntityAsJson) Json(org.infinispan.commons.dataconversion.internal.Json) NettyRestResponse(org.infinispan.rest.NettyRestResponse) RestResponseException(org.infinispan.rest.RestResponseException)

Example 2 with ContentSource

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());
}
Also used : ConfigurationBuilder(org.infinispan.configuration.cache.ConfigurationBuilder) StringBuilderWriter(org.infinispan.commons.io.StringBuilderWriter) ContentSource(org.infinispan.rest.framework.ContentSource) ConfigurationBuilderHolder(org.infinispan.configuration.parsing.ConfigurationBuilderHolder) ConfigurationWriter(org.infinispan.commons.configuration.io.ConfigurationWriter) NettyRestResponse(org.infinispan.rest.NettyRestResponse) Map(java.util.Map) Collections.emptyMap(java.util.Collections.emptyMap)

Example 3 with ContentSource

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

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

Example 5 with ContentSource

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));
}
Also used : ContentSource(org.infinispan.rest.framework.ContentSource) IOException(java.io.IOException)

Aggregations

ContentSource (org.infinispan.rest.framework.ContentSource)7 NettyRestResponse (org.infinispan.rest.NettyRestResponse)6 MediaType (org.infinispan.commons.dataconversion.MediaType)4 ConfigurationBuilder (org.infinispan.configuration.cache.ConfigurationBuilder)3 Json (org.infinispan.commons.dataconversion.internal.Json)2 ConfigurationBuilderHolder (org.infinispan.configuration.parsing.ConfigurationBuilderHolder)2 InternalCacheEntry (org.infinispan.container.entries.InternalCacheEntry)2 NoDataFoundException (org.infinispan.rest.operations.exceptions.NoDataFoundException)2 MediaTypeUtils.negotiateMediaType (org.infinispan.rest.resources.MediaTypeUtils.negotiateMediaType)2 ResourceUtil.addEntityAsJson (org.infinispan.rest.resources.ResourceUtil.addEntityAsJson)2 HttpResponseStatus (io.netty.handler.codec.http.HttpResponseStatus)1 Flowable (io.reactivex.rxjava3.core.Flowable)1 IOException (java.io.IOException)1 Collections.emptyMap (java.util.Collections.emptyMap)1 Comparator (java.util.Comparator)1 Map (java.util.Map)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