Search in sources :

Example 6 with ContentSource

use of org.infinispan.rest.framework.ContentSource in project infinispan by infinispan.

the class TasksResource method createScriptTask.

private CompletionStage<RestResponse> createScriptTask(RestRequest request) {
    String taskName = request.variables().get("taskName");
    EmbeddedCacheManager cacheManager = invocationHelper.getRestCacheManager().getInstance().withSubject(request.getSubject());
    ScriptingManager scriptingManager = SecurityActions.getGlobalComponentRegistry(cacheManager).getComponent(ScriptingManager.class);
    NettyRestResponse.Builder builder = new NettyRestResponse.Builder();
    ContentSource contents = request.contents();
    byte[] bytes = contents.rawContent();
    MediaType sourceType = request.contentType() == null ? APPLICATION_JAVASCRIPT : request.contentType();
    String script = StandardConversions.convertTextToObject(bytes, sourceType);
    return CompletableFuture.supplyAsync(() -> {
        Subject.doAs(request.getSubject(), (PrivilegedAction<Void>) () -> {
            scriptingManager.addScript(taskName, script);
            return null;
        });
        return builder.build();
    }, invocationHelper.getExecutor());
}
Also used : ContentSource(org.infinispan.rest.framework.ContentSource) ScriptingManager(org.infinispan.scripting.ScriptingManager) MediaType(org.infinispan.commons.dataconversion.MediaType) EmbeddedCacheManager(org.infinispan.manager.EmbeddedCacheManager) NettyRestResponse(org.infinispan.rest.NettyRestResponse)

Example 7 with ContentSource

use of org.infinispan.rest.framework.ContentSource in project infinispan by infinispan.

the class CacheResourceV2 method createOrUpdate.

private CompletableFuture<RestResponse> createOrUpdate(RestRequest request) {
    NettyRestResponse.Builder responseBuilder = new NettyRestResponse.Builder();
    List<String> template = request.parameters().get("template");
    String cacheName = request.variables().get("cacheName");
    EnumSet<AdminFlag> adminFlags = request.getAdminFlags();
    if (request.method() == PUT) {
        if (adminFlags == null) {
            adminFlags = EnumSet.of(AdminFlag.UPDATE);
        } else {
            adminFlags.add(AdminFlag.UPDATE);
        }
    }
    EmbeddedCacheManagerAdmin initialAdmin = invocationHelper.getRestCacheManager().getCacheManagerAdmin(request);
    EmbeddedCacheManagerAdmin administration = adminFlags == null ? initialAdmin : initialAdmin.withFlags(adminFlags);
    if (template != null && !template.isEmpty()) {
        if (request.method() == PUT) {
            return CompletableFuture.completedFuture(responseBuilder.status(BAD_REQUEST).build());
        }
        String templateName = template.iterator().next();
        return CompletableFuture.supplyAsync(() -> {
            administration.createCache(cacheName, templateName);
            responseBuilder.status(OK);
            return responseBuilder.build();
        }, invocationHelper.getExecutor());
    }
    ContentSource contents = request.contents();
    byte[] bytes = contents.rawContent();
    if (bytes == null || bytes.length == 0) {
        if (request.method() == PUT) {
            return CompletableFuture.completedFuture(responseBuilder.status(BAD_REQUEST).build());
        }
        return CompletableFuture.supplyAsync(() -> {
            administration.createCache(cacheName, (String) null);
            responseBuilder.status(OK);
            return responseBuilder.build();
        }, invocationHelper.getExecutor());
    }
    MediaType sourceType = request.contentType() == null ? APPLICATION_JSON : request.contentType();
    if (!sourceType.match(APPLICATION_JSON) && !sourceType.match(APPLICATION_XML) && !sourceType.match(APPLICATION_YAML)) {
        responseBuilder.status(HttpResponseStatus.UNSUPPORTED_MEDIA_TYPE);
        return CompletableFuture.completedFuture(responseBuilder.build());
    }
    return CompletableFuture.supplyAsync(() -> {
        try {
            ConfigurationBuilderHolder holder = invocationHelper.getParserRegistry().parse(new String(bytes, UTF_8), sourceType);
            ConfigurationBuilder cfgBuilder = holder.getCurrentConfigurationBuilder() != null ? holder.getCurrentConfigurationBuilder() : new ConfigurationBuilder();
            if (request.method() == PUT) {
                administration.getOrCreateCache(cacheName, cfgBuilder.build());
            } else {
                administration.createCache(cacheName, cfgBuilder.build());
            }
            responseBuilder.status(OK);
        } catch (Throwable t) {
            responseBuilder.status(BAD_REQUEST).entity(t.getMessage());
        }
        return responseBuilder.build();
    }, invocationHelper.getExecutor());
}
Also used : ConfigurationBuilder(org.infinispan.configuration.cache.ConfigurationBuilder) AdminFlag(org.infinispan.commons.api.CacheContainerAdmin.AdminFlag) ConfigurationBuilder(org.infinispan.configuration.cache.ConfigurationBuilder) NettyRestResponse(org.infinispan.rest.NettyRestResponse) ContentSource(org.infinispan.rest.framework.ContentSource) ConfigurationBuilderHolder(org.infinispan.configuration.parsing.ConfigurationBuilderHolder) MediaType(org.infinispan.commons.dataconversion.MediaType) MediaTypeUtils.negotiateMediaType(org.infinispan.rest.resources.MediaTypeUtils.negotiateMediaType) EmbeddedCacheManagerAdmin(org.infinispan.manager.EmbeddedCacheManagerAdmin)

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