Example usage for io.vertx.core Future succeededFuture

List of usage examples for io.vertx.core Future succeededFuture

Introduction

In this page you can find the example usage for io.vertx.core Future succeededFuture.

Prototype

static <T> Future<T> succeededFuture(T result) 

Source Link

Document

Created a succeeded future with the specified result.

Usage

From source file:org.entcore.directory.services.impl.DefaultUserBookService.java

License:Open Source License

@Override
public void getAvatar(String userId, Optional<String> size, String defaultAvatarDirty,
        HttpServerRequest request) {/*from   w ww . j  a va2 s  . com*/
    String fileIdSized = avatarFileNameFromUserId(userId, size);
    sendAvatar(request, fileIdSized)// try with size
            .compose(success -> {// try without size
                if (success) {
                    return Future.succeededFuture(true);
                } else {
                    if (size.isPresent()) {// try without size
                        String fileIdUnsized = avatarFileNameFromUserId(userId, Optional.empty());
                        return sendAvatar(request, fileIdUnsized);
                    } else {// without size already tried
                        return Future.succeededFuture(false);
                    }
                }
            }).compose(success -> {// try default
                if (success) {
                    return Future.succeededFuture(true);
                } else {
                    String fidIdDefault = FileUtils.stripExtension(defaultAvatarDirty);
                    return sendAvatar(request, fidIdDefault);
                }
            }).setHandler(res -> {
                if (res.failed() || !res.result()) {// could not found any img
                    Renders.notFound(request);
                }
            });
}

From source file:org.etourdot.vertx.marklogic.impl.MarkLogicAdminImpl.java

License:Open Source License

void deleteRESTAppServer(RestApiOptions restApiOption, Handler<AsyncResult<String>> resultHandler) {
    requireNonNull(restApiOption, "restApiOption cannot be null");
    requireNonNull(resultHandler, "resultHandler cannot be null");

    requireNonNull(restApiOption.getRetrieveInstance(), "rettrieve-instance cannot be null");

    String removeServerString = REST_APIS + "/" + restApiOption.getRetrieveInstance();
    MarkLogicRequest marklogicRequest = restService.newMarklogicRequest();
    if (restApiOption.isRemoveContent()) {
        marklogicRequest.addParam(INCLUDE, CONTENT);
    }/*w  w  w  .  j a  va  2  s  .c  o  m*/
    if (restApiOption.isRemoveModules()) {
        marklogicRequest.addParam(INCLUDE, MODULES);
    }
    marklogicRequest.delete(removeServerString).withBody(restApiOption.toJson()).execute(response -> {
        if (HttpResponseStatus.ACCEPTED.code() == response.statusCode()) {
            // Todo: is it a convienient return ?
            resultHandler.handle(Future.succeededFuture("deleted"));
        } else {
            resultHandler.handle(Future.failedFuture(response.statusMessage()));
        }
    });
}

From source file:org.etourdot.vertx.marklogic.impl.MarkLogicAdminImpl.java

License:Open Source License

void getRESTAppServerConfig(RestApiOptions restApiOption, Handler<AsyncResult<JsonObject>> resultHandler) {
    requireNonNull(restApiOption, "restApiOption cannot be null");
    requireNonNull(resultHandler, "resultHandler cannot be null");

    String getServerString = REST_APIS;
    if (restApiOption.hasRetrieveInstance()) {
        getServerString += "/" + restApiOption.getRetrieveInstance();
    }//w w  w . j  a  v a 2s. c  om

    MarkLogicRequest marklogicRequest = restService.newMarklogicRequest();
    if (restApiOption.hasRetrieveDatabase()) {
        marklogicRequest.addParam(DATABASE, restApiOption.getRetrieveDatabase());
    }
    marklogicRequest.get(getServerString).execute(response -> {
        if (HttpResponseStatus.OK.code() == response.statusCode()) {
            response.contentHandler(
                    buffer -> resultHandler.handle(Future.succeededFuture(buffer.toJsonObject())));
        } else {
            resultHandler.handle(Future.failedFuture(response.statusMessage()));
        }
    });
}

From source file:org.etourdot.vertx.marklogic.impl.MarkLogicConfigImpl.java

License:Open Source License

void saveTransformation(Transformation transformation, Handler<AsyncResult<String>> resultHandler) {
    requireNonNull(transformation, "config cannot be null");
    requireNonNull(resultHandler, "resultHandler cannot be null");

    MarkLogicRequest marklogicRequest = restService.newMarklogicRequest();
    marklogicRequest.put(TRANSFORMS_URL + "/" + transformation.getName());
    if (transformation.hasTitle()) {
        marklogicRequest.addParam(TITLE, transformation.getTitle());
    }//w w w. j av  a 2 s  . co m
    if (transformation.hasProvider()) {
        marklogicRequest.addParam(PROVIDER, transformation.getProvider());
    }
    if (transformation.hasVersion()) {
        marklogicRequest.addParam(VERSION, transformation.getVersion());
    }
    if (transformation.hasDescription()) {
        marklogicRequest.addParam(DESCRIPTION, transformation.getDescription());
    }
    marklogicRequest.addParam(FORMAT, transformation.getFormat());
    final String contentType = ("xslt".equals(transformation.getFormat())) ? "application/xslt+xml"
            : ("xquery".equals(transformation.getFormat())) ? "application/xquery"
                    : "application/vnd.io.vertx.ext.marklogic-javascript";
    final Buffer buffer = fileSystem.readFileBlocking(transformation.getSource());
    marklogicRequest.withBody(buffer.toString(), contentType).execute(response -> {
        if (HttpResponseStatus.NO_CONTENT.code() == response.statusCode()) {
            // Todo: better return ?
            resultHandler.handle(Future.succeededFuture("ok"));
        }
    });
}

From source file:org.etourdot.vertx.marklogic.impl.MarkLogicDocumentImpl.java

License:Open Source License

void exists(String docUri, Handler<AsyncResult<JsonObject>> resultHandler) {
    requireNonNull(docUri, "docUri cannot be null");
    requireNonNull(resultHandler, "resultHandler cannot be null");

    MarkLogicRequest marklogicRequest = restService.newMarklogicRequest();
    marklogicRequest.head(DOCUMENTS_URL).addParam(MarkLogicConstants.URI, docUri).execute(response -> {
        if (HttpResponseStatus.OK.code() == response.statusCode()) {
            Document document = Document.create();
            document.uri(docUri);/* ww w .  j  av a  2 s. c o m*/
            Format format = response.getFormat();
            if (format != null) {
                document.format(format.getFormat());
            }
            resultHandler.handle(Future.succeededFuture(document.toJson()));
        } else {
            resultHandler.handle(Future.succeededFuture());
        }
    });
}

From source file:org.etourdot.vertx.marklogic.impl.MarkLogicDocumentImpl.java

License:Open Source License

void delete(String docId, Handler<AsyncResult<String>> resultHandler) {
    requireNonNull(docId, "docId cannot be null");
    requireNonNull(resultHandler, "resultHandler cannot be null");

    MarkLogicRequest marklogicRequest = restService.newMarklogicRequest();
    marklogicRequest.delete(DOCUMENTS_URL).addParam(MarkLogicConstants.URI, docId).execute(response -> {
        if (HttpResponseStatus.NO_CONTENT.code() == response.statusCode()) {
            resultHandler.handle(Future.succeededFuture(docId));
        }//from   w  w  w  .  ja v  a  2s  . c  o m
    });
}

From source file:org.etourdot.vertx.marklogic.impl.MarkLogicDocumentImpl.java

License:Open Source License

void readDocument(String docId, List<String> categories, Handler<AsyncResult<JsonObject>> resultHandler) {
    JsonArray docIds = new JsonArray(Collections.singletonList(docId));
    readDocuments(docIds, categories, (result) -> {
        if (result.failed()) {
            resultHandler.handle(Future.failedFuture(result.cause()));
        } else {//  www. ja  va 2  s  .  c o m
            resultHandler.handle(Future.succeededFuture(result.result().getJsonObject(0)));
        }
    });
}

From source file:org.etourdot.vertx.marklogic.impl.MarkLogicDocumentImpl.java

License:Open Source License

void readDocuments(JsonArray docIds, List<String> categories, Handler<AsyncResult<JsonArray>> resultHandler) {
    MultiPartRequest marklogicRequest = restService.newMultipartRequest();
    marklogicRequest.get(DOCUMENTS_URL).addParam(FORMAT, "json").addParams(CATEGORY, categories);
    docIds.stream().forEach(docId -> marklogicRequest.addParam(MarkLogicConstants.URI, (String) docId));
    marklogicRequest.execute(response -> {
        SearchResponse searchResponse = new SearchResponse(response);
        searchResponse.endHandler(marklogicResponse -> {
            if (HttpResponseStatus.OK.code() == marklogicResponse.statusCode()) {
                JsonArray documents = new JsonArray();
                for (Document document : ((SearchResponse) marklogicResponse).getDocuments()) {
                    documents.add(document.toJson());
                }//  ww w .  j  av a 2s.  co  m
                resultHandler.handle(Future.succeededFuture(documents));
            }
            if (HttpResponseStatus.NOT_FOUND.code() == marklogicResponse.statusCode()) {
                resultHandler.handle(
                        Future.failedFuture(new ResourceNotFoundException(marklogicResponse.statusMessage())));
            }
            resultHandler.handle(Future.succeededFuture());
        });
        searchResponse.process();
    });
}

From source file:org.etourdot.vertx.marklogic.impl.MarkLogicDocumentImpl.java

License:Open Source License

void saveDocuments(Documents documents, TransformOptions transformOptions,
        Handler<AsyncResult<JsonArray>> resultHandler) {
    logger.debug("Start saveDocuments " + this);
    MultiPartRequest marklogicRequest = restService.newMultipartRequest();
    marklogicRequest.post(DOCUMENTS_URL);
    documents.getDocumentsList().stream().forEach(marklogicRequest::addDocument);
    if (transformOptions != null) {
        marklogicRequest.addParam(TRANSFORM, transformOptions.getName());
        if (transformOptions.hasParameters()) {
            for (Map.Entry<String, Object> param : transformOptions.getParameters().getMap().entrySet()) {
                marklogicRequest.addParam("trans:" + param.getKey(), (String) param.getValue());
            }/*  w ww  .  ja  v a 2  s  .  c  o m*/
        }
    }
    logger.debug("saveDocuments request:" + marklogicRequest);
    marklogicRequest.execute(response -> {
        BulkResponse bulkResponse = new BulkResponse(response);
        bulkResponse.endHandler(marklogicResponse -> {
            logger.debug("saveDocuments request endhandler:" + marklogicRequest);
            if (HttpResponseStatus.OK.code() == marklogicResponse.statusCode()) {
                JsonArray resultDocs = new JsonArray();
                for (Document document : ((BulkResponse) marklogicResponse).getDocuments()) {
                    resultDocs.add(document.getUri());
                }
                resultHandler.handle(Future.succeededFuture(resultDocs));
            }
        });
        bulkResponse.process();
    });
}

From source file:org.etourdot.vertx.marklogic.impl.MarkLogicDocumentImpl.java

License:Open Source License

private void save(Document document, TransformOptions transformOptions,
        Handler<AsyncResult<JsonArray>> resultHandler) {
    final Format format = Format.valueOf(document.getFormat());
    MarkLogicRequest marklogicRequest = restService.newMarklogicRequest();
    if (document.hasUri()) {
        marklogicRequest.put(DOCUMENTS_URL).addParam(MarkLogicConstants.URI, document.getUri())
                .withResponseHandler(response -> {
                    if (HttpResponseStatus.CREATED.code() == response.statusCode()) {
                        String uri = response.getDocumentUri();
                        if (uri != null) {
                            resultHandler.handle(Future.succeededFuture(new JsonArray().add(uri)));
                        }// w w w.j  a  va 2  s. c o m
                    } else if (HttpResponseStatus.NO_CONTENT.code() == response.statusCode()) {
                        resultHandler.handle(Future.succeededFuture(new JsonArray().add(document.getUri())));
                    }
                });
    } else if (document.isNewDocument()) {
        marklogicRequest.post(DOCUMENTS_URL).addParam(EXTENSION, document.getExtension())
                .addParam(DIRECTORY, document.getDirectory()).withResponseHandler(response -> {
                    if (HttpResponseStatus.CREATED.code() == response.statusCode()) {
                        String uri = response.getDocumentUri();
                        if (uri != null) {
                            resultHandler.handle(Future.succeededFuture(new JsonArray().add(uri)));
                        }
                    }
                });
    }
    if (document.hasQuality()) {
        marklogicRequest.addParam(QUALITY, Integer.toString(document.getQuality()));
    }
    if (document.hasProperties()) {
        for (Map.Entry<String, Object> property : document.getProperties().getMap().entrySet()) {
            marklogicRequest.addParam("prop:" + property.getKey(), (String) property.getValue());
        }
    }
    if (document.hasPermissions()) {
        document.getPermissions().stream().forEach(perm -> {
            Permission permission = new Permission((JsonObject) perm);
            permission.getCapabilities().stream().forEach(capability -> marklogicRequest
                    .addParam("perm:" + permission.getRoleName(), (String) capability));
        });
    }
    marklogicRequest.addParam(CATEGORY, CONTENT);
    if (document.hasCollections()) {
        document.getCollections().stream()
                .forEach(coll -> marklogicRequest.addParam(COLLECTION, (String) coll));
    }
    marklogicRequest.addParam(FORMAT, format.getFormat());
    if (Format.JSON == format) {
        marklogicRequest.withBody(((JsonObject) document.getContent()));
    } else if (Format.XML == format) {
        marklogicRequest.withBody((String) document.getContent(), Format.XML);
    } else if (Format.TEXT == format) {
        marklogicRequest.withBody((String) document.getContent(), Format.TEXT);
    } else if (Format.BINARY == format) {
        marklogicRequest.withBody((String) document.getContent(), Format.TEXT);
    }
    if (transformOptions != null) {
        marklogicRequest.addParam(TRANSFORM, transformOptions.getName());
        if (transformOptions.hasParameters()) {
            for (Map.Entry<String, Object> param : transformOptions.getParameters().getMap().entrySet()) {
                marklogicRequest.addParam("trans:" + param.getKey(), (String) param.getValue());
            }
        }
    }
    marklogicRequest.execute();
}