use of org.infinispan.commons.api.CacheContainerAdmin.AdminFlag 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());
}
Aggregations