use of org.infinispan.rest.framework.RestResponse in project infinispan by infinispan.
the class ContainerResource method getAllCachesConfigurationTemplates.
private CompletionStage<RestResponse> getAllCachesConfigurationTemplates(RestRequest request) {
NettyRestResponse.Builder responseBuilder = checkCacheManager(request);
if (responseBuilder.getHttpStatus() == NOT_FOUND)
return completedFuture(responseBuilder.build());
EmbeddedCacheManager cacheManager = invocationHelper.getRestCacheManager().getInstance();
EmbeddedCacheManager subjectCacheManager = cacheManager.withSubject(request.getSubject());
Set<String> cacheConfigurationNames = subjectCacheManager.getCacheConfigurationNames();
List<NamedCacheConfiguration> configurations = cacheConfigurationNames.stream().filter(n -> !internalCacheRegistry.isInternalCache(n)).filter(n -> SecurityActions.getCacheConfigurationFromManager(subjectCacheManager, n).isTemplate()).distinct().map(n -> {
return getNamedCacheConfiguration(subjectCacheManager, n);
}).sorted(Comparator.comparing(c -> c.name)).collect(Collectors.toList());
return asJsonResponseFuture(Json.make(configurations), responseBuilder);
}
use of org.infinispan.rest.framework.RestResponse 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.RestResponse in project infinispan by infinispan.
the class SearchAdminResource method runIndexer.
private CompletionStage<RestResponse> runIndexer(RestRequest request, Function<Indexer, CompletionStage<Void>> op, boolean supportAsync) {
NettyRestResponse.Builder responseBuilder = new NettyRestResponse.Builder();
List<String> mode = request.parameters().get("mode");
boolean asyncParams = mode != null && !mode.isEmpty() && mode.iterator().next().equalsIgnoreCase("async");
boolean async = asyncParams && supportAsync;
AdvancedCache<?, ?> cache = lookupIndexedCache(request, responseBuilder);
int status = responseBuilder.getStatus();
if (status < OK.code() || status >= MULTIPLE_CHOICES.code()) {
return completedFuture(responseBuilder.build());
}
responseBuilder.status(NO_CONTENT);
Indexer indexer = ComponentRegistryUtils.getIndexer(cache);
if (async) {
try {
LOG.asyncMassIndexerStarted();
op.apply(indexer).whenComplete((v, e) -> {
if (e == null) {
LOG.asyncMassIndexerSuccess();
} else {
LOG.errorExecutingMassIndexer(e.getCause());
}
});
} catch (Exception e) {
responseBuilder.status(INTERNAL_SERVER_ERROR).entity("Error executing the MassIndexer " + e.getCause());
}
return CompletableFuture.completedFuture(responseBuilder.build());
}
return op.apply(indexer).exceptionally(e -> {
if (e instanceof MassIndexerAlreadyStartedException) {
responseBuilder.status(BAD_REQUEST).entity("MassIndexer already started");
} else {
responseBuilder.status(INTERNAL_SERVER_ERROR).entity("Error executing the MassIndexer " + e.getCause());
}
return null;
}).thenApply(v -> responseBuilder.build());
}
use of org.infinispan.rest.framework.RestResponse in project infinispan by infinispan.
the class XSiteResource method statusOperation.
private <T> CompletionStage<RestResponse> statusOperation(RestRequest request, Function<XSiteAdminOperations, T> op) {
NettyRestResponse.Builder responseBuilder = new NettyRestResponse.Builder();
Optional<XSiteAdminOperations> xsiteAdmin = getXSiteAdmin(request, responseBuilder);
return xsiteAdmin.<CompletionStage<RestResponse>>map(ops -> supplyAsync(() -> {
T result = Security.doAs(request.getSubject(), (PrivilegedAction<T>) () -> op.apply(ops));
return addEntityAsJson(Json.make(result), responseBuilder).build();
}, invocationHelper.getExecutor())).orElseGet(() -> completedFuture(responseBuilder.build()));
}
use of org.infinispan.rest.framework.RestResponse in project infinispan by infinispan.
the class SecurityResource method createRole.
private CompletionStage<RestResponse> createRole(RestRequest request) {
NettyRestResponse.Builder builder = new NettyRestResponse.Builder();
if (rolePermissionMapper == null) {
return completedFuture(new NettyRestResponse.Builder().status(CONFLICT).entity(Log.REST.rolePermissionMapperNotMutable()).build());
}
String name = request.variables().get("role");
List<String> perms = request.parameters().get("permission");
if (perms == null) {
return completedFuture(builder.status(HttpResponseStatus.BAD_REQUEST).build());
}
Set<AuthorizationPermission> permissions = perms.stream().map(p -> AuthorizationPermission.valueOf(p.toUpperCase())).collect(Collectors.toSet());
Role role = new CacheRoleImpl(name, true, permissions);
return rolePermissionMapper.addRole(role).thenCompose(ignore -> aclCacheFlush(request));
}
Aggregations