Search in sources :

Example 1 with NettyRestRequest

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());
    }
}
Also used : Path(java.nio.file.Path) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) Json(org.infinispan.commons.dataconversion.internal.Json) IOException(java.io.IOException) BackupManager(org.infinispan.server.core.BackupManager) DiskAttribute(io.netty.handler.codec.http.multipart.DiskAttribute) InterfaceHttpPostRequestDecoder(io.netty.handler.codec.http.multipart.InterfaceHttpPostRequestDecoder) DiskFileUpload(io.netty.handler.codec.http.multipart.DiskFileUpload) DefaultHttpDataFactory(io.netty.handler.codec.http.multipart.DefaultHttpDataFactory) HttpPostMultipartRequestDecoder(io.netty.handler.codec.http.multipart.HttpPostMultipartRequestDecoder) MediaType(org.infinispan.commons.dataconversion.MediaType) NettyRestRequest(org.infinispan.rest.NettyRestRequest)

Aggregations

FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)1 DefaultHttpDataFactory (io.netty.handler.codec.http.multipart.DefaultHttpDataFactory)1 DiskAttribute (io.netty.handler.codec.http.multipart.DiskAttribute)1 DiskFileUpload (io.netty.handler.codec.http.multipart.DiskFileUpload)1 HttpPostMultipartRequestDecoder (io.netty.handler.codec.http.multipart.HttpPostMultipartRequestDecoder)1 InterfaceHttpPostRequestDecoder (io.netty.handler.codec.http.multipart.InterfaceHttpPostRequestDecoder)1 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 MediaType (org.infinispan.commons.dataconversion.MediaType)1 Json (org.infinispan.commons.dataconversion.internal.Json)1 NettyRestRequest (org.infinispan.rest.NettyRestRequest)1 BackupManager (org.infinispan.server.core.BackupManager)1