use of org.infinispan.commons.dataconversion.MediaType 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());
}
use of org.infinispan.commons.dataconversion.MediaType in project infinispan by infinispan.
the class ServerResource method config.
private CompletionStage<RestResponse> config(RestRequest request) {
NettyRestResponse.Builder responseBuilder = new NettyRestResponse.Builder();
MediaType accept = negotiateMediaType(request, APPLICATION_JSON, APPLICATION_XML, APPLICATION_YAML);
responseBuilder.contentType(accept);
StringBuilderWriter sw = new StringBuilderWriter();
try (ConfigurationWriter w = ConfigurationWriter.to(sw).withType(accept).prettyPrint(false).build()) {
invocationHelper.getServer().serializeConfiguration(w);
}
responseBuilder.entity(sw.toString());
return CompletableFuture.completedFuture(responseBuilder.build());
}
use of org.infinispan.commons.dataconversion.MediaType in project infinispan by infinispan.
the class BaseCacheResource method getCacheValue.
CompletionStage<RestResponse> getCacheValue(RestRequest request) throws RestResponseException {
String cacheName = request.variables().get("cacheName");
MediaType keyContentType = request.keyContentType();
AdvancedCache<?, ?> cache = invocationHelper.getRestCacheManager().getCache(cacheName, request);
MediaType requestedMediaType = negotiateMediaType(cache, invocationHelper.getEncoderRegistry(), request);
Object key = getKey(request);
String cacheControl = request.getCacheControlHeader();
boolean returnBody = request.method() == GET;
RestCacheManager<Object> restCacheManager = invocationHelper.getRestCacheManager();
return restCacheManager.getInternalEntry(cacheName, key, keyContentType, requestedMediaType, request).thenApply(entry -> {
NettyRestResponse.Builder responseBuilder = new NettyRestResponse.Builder();
responseBuilder.status(HttpResponseStatus.NOT_FOUND);
if (entry instanceof InternalCacheEntry) {
InternalCacheEntry<Object, Object> ice = (InternalCacheEntry<Object, Object>) entry;
Long lastMod = CacheOperationsHelper.lastModified(ice);
Date expires = ice.canExpire() ? new Date(ice.getExpiryTime()) : null;
OptionalInt minFreshSeconds = CacheOperationsHelper.minFresh(cacheControl);
if (CacheOperationsHelper.entryFreshEnough(expires, minFreshSeconds)) {
Metadata meta = ice.getMetadata();
String etag = calcETAG(ice.getValue());
String ifNoneMatch = request.getEtagIfNoneMatchHeader();
String ifMatch = request.getEtagIfMatchHeader();
String ifUnmodifiedSince = request.getIfUnmodifiedSinceHeader();
String ifModifiedSince = request.getIfModifiedSinceHeader();
if (ifNoneMatch != null && ifNoneMatch.equals(etag)) {
return responseBuilder.status(HttpResponseStatus.NOT_MODIFIED).build();
}
if (ifMatch != null && !ifMatch.equals(etag)) {
return responseBuilder.status(HttpResponseStatus.PRECONDITION_FAILED).build();
}
if (DateUtils.ifUnmodifiedIsBeforeModificationDate(ifUnmodifiedSince, lastMod)) {
return responseBuilder.status(HttpResponseStatus.PRECONDITION_FAILED).build();
}
if (DateUtils.isNotModifiedSince(ifModifiedSince, lastMod)) {
return responseBuilder.status(HttpResponseStatus.NOT_MODIFIED).build();
}
Object value = ice.getValue();
MediaType configuredMediaType = restCacheManager.getValueConfiguredFormat(cacheName, request);
writeValue(value, requestedMediaType, configuredMediaType, responseBuilder, returnBody);
responseBuilder.status(HttpResponseStatus.OK).lastModified(lastMod).eTag(etag).cacheControl(CacheOperationsHelper.calcCacheControl(expires)).expires(expires).timeToLive(meta.lifespan()).maxIdle(meta.maxIdle()).created(ice.getCreated()).lastUsed(ice.getLastUsed());
List<String> extended = request.parameters().get(EXTENDED_HEADER.getValue());
RestServerConfiguration restServerConfiguration = invocationHelper.getConfiguration();
if (extended != null && extended.size() > 0 && CacheOperationsHelper.supportsExtendedHeaders(restServerConfiguration, extended.iterator().next())) {
responseBuilder.clusterPrimaryOwner(restCacheManager.getPrimaryOwner(cacheName, key, request)).clusterBackupOwners(restCacheManager.getBackupOwners(cacheName, key, request)).clusterNodeName(restCacheManager.getNodeName()).clusterServerAddress(restCacheManager.getServerAddress());
}
}
}
return responseBuilder.build();
});
}
use of org.infinispan.commons.dataconversion.MediaType in project infinispan by infinispan.
the class ServerTaskEngine method invokeTask.
private <T> CompletableFuture<T> invokeTask(TaskContext context, ServerTaskWrapper<T> task) {
ServerTaskRunner runner;
switch(task.getExecutionMode()) {
case ONE_NODE:
runner = localRunner;
break;
default:
runner = distributedRunner;
break;
}
launderParameters(context);
MediaType requestMediaType = context.getCache().map(c -> c.getAdvancedCache().getValueDataConversion().getRequestMediaType()).orElse(MediaType.MATCH_ALL);
context.getCache().ifPresent(c -> context.cache(c.getAdvancedCache().withMediaType(APPLICATION_OBJECT, APPLICATION_OBJECT)));
return runner.execute(task.getName(), context).thenApply(r -> (T) scriptConversions.convertToRequestType(r, APPLICATION_OBJECT, requestMediaType));
}
Aggregations