use of org.infinispan.rest.framework.RestRequest 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.RestRequest in project infinispan by infinispan.
the class MediaTypeUtils method negotiateMediaType.
/**
* Negotiates the {@link MediaType} to be used during the request execution
*
* @param cache the {@link AdvancedCache} associated with the request
* @param restRequest the {@link RestRequest} with the headers
* @return The negotiated MediaType
* @throws UnacceptableDataFormatException if no suitable {@link MediaType} could be found.
*/
static MediaType negotiateMediaType(AdvancedCache<?, ?> cache, EncoderRegistry registry, RestRequest restRequest) throws UnacceptableDataFormatException {
try {
String accept = restRequest.getAcceptHeader();
MediaType storageMedia = cache.getValueDataConversion().getStorageMediaType();
Optional<MediaType> negotiated = MediaType.parseList(accept).filter(media -> registry.isConversionSupported(storageMedia, media)).findFirst();
return negotiated.map(m -> {
if (!m.matchesAll())
return m;
MediaType storageMediaType = cache.getValueDataConversion().getStorageMediaType();
if (storageMediaType == null)
return m;
if (storageMediaType.equals(MediaType.APPLICATION_OBJECT))
return TEXT_PLAIN;
if (storageMediaType.match(MediaType.APPLICATION_PROTOSTREAM))
return APPLICATION_JSON;
return m;
}).orElseThrow(() -> Log.REST.unsupportedDataFormat(accept));
} catch (EncodingException e) {
throw new UnacceptableDataFormatException();
}
}
use of org.infinispan.rest.framework.RestRequest 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.RestRequest 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.RestRequest 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()));
}
Aggregations