use of org.infinispan.rest.NettyRestRequest in project infinispan by infinispan.
the class BackupManagerResource method handleRestore.
static CompletionStage<RestResponse> handleRestore(String name, RestRequest request, BackupManager backupManager, TriFunction<String, Path, Json, CompletionStage<Void>> function) {
BackupManager.Status existingStatus = backupManager.getRestoreStatus(name);
if (existingStatus != BackupManager.Status.NOT_FOUND)
return responseFuture(CONFLICT);
Path path;
Json resourcesJson = Json.object();
MediaType contentType = request.contentType();
boolean uploadedBackup = contentType.match(MediaType.MULTIPART_FORM_DATA);
try {
if (uploadedBackup) {
FullHttpRequest nettyRequest = ((NettyRestRequest) request).getFullHttpRequest();
DefaultHttpDataFactory factory = new DefaultHttpDataFactory(true);
InterfaceHttpPostRequestDecoder decoder = new HttpPostMultipartRequestDecoder(factory, nettyRequest);
DiskFileUpload backup = (DiskFileUpload) decoder.getBodyHttpData("backup");
path = backup.getFile().toPath();
DiskAttribute resources = (DiskAttribute) decoder.getBodyHttpData("resources");
if (resources != null)
resourcesJson = Json.read(resources.getString());
} else if (contentType.match(MediaType.APPLICATION_JSON)) {
// Attempt to parse body as json
Json json = Json.read(request.contents().asString());
Json resources = json.at(RESOURCES_KEY);
if (resources != null)
resourcesJson = resources;
Json backupPath = json.at(LOCATION_KEY);
if (backupPath == null)
return responseFuture(BAD_REQUEST, "Required json attribute 'backup-location' not found");
path = Paths.get(backupPath.asString());
} else {
return responseFuture(UNSUPPORTED_MEDIA_TYPE);
}
function.apply(name, path, resourcesJson).whenComplete((Void, t) -> {
if (t != null) {
LOG.error(t);
}
if (uploadedBackup) {
try {
Files.delete(path);
} catch (IOException e) {
LOG.warnf(e, "Unable to delete uploaded backup file '%s'", path);
}
}
});
return responseFuture(ACCEPTED);
} catch (IOException e) {
LOG.error(e);
return responseFuture(INTERNAL_SERVER_ERROR, e.getMessage());
}
}
Aggregations