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() 

Source Link

Document

Create a succeeded future with a null result

Usage

From source file:org.eclipse.hono.service.amqp.AmqpServiceBase.java

License:Open Source License

private Future<Void> startSecureServer() {

    if (isSecurePortEnabled()) {
        int securePort = determineSecurePort();
        final Future<Void> result = Future.future();
        final ProtonServerOptions options = createServerOptions();
        server = createProtonServer(options).connectHandler(this::onRemoteConnectionOpen).listen(securePort,
                getConfig().getBindAddress(), bindAttempt -> {
                    if (bindAttempt.succeeded()) {
                        if (getPort() == getPortDefaultValue()) {
                            LOG.info("server listens on standard secure port [{}:{}]", getBindAddress(),
                                    getPort());
                        } else {
                            LOG.warn("server listens on non-standard secure port [{}:{}], default is {}",
                                    getBindAddress(), getPort(), getPortDefaultValue());
                        }// w ww .  ja v  a 2s  .c o  m
                        result.complete();
                    } else {
                        LOG.error("cannot bind to secure port", bindAttempt.cause());
                        result.fail(bindAttempt.cause());
                    }
                });
        return result;
    } else {
        LOG.info("secure port is not enabled");
        return Future.succeededFuture();
    }
}

From source file:org.eclipse.hono.service.auth.device.HonoAuthHandler.java

License:Open Source License

@Override
public void authorize(final User user, final Handler<AsyncResult<Void>> handler) {
    final int requiredcount = authorities.size();
    if (requiredcount > 0) {
        if (user == null) {
            handler.handle(Future.failedFuture(FORBIDDEN));
            return;
        }//from w  ww  . j  a  v  a2s  . c o m

        final AtomicInteger count = new AtomicInteger();
        final AtomicBoolean sentFailure = new AtomicBoolean();

        final Handler<AsyncResult<Boolean>> authHandler = res -> {
            if (res.succeeded()) {
                if (res.result()) {
                    if (count.incrementAndGet() == requiredcount) {
                        // Has all required authorities
                        handler.handle(Future.succeededFuture());
                    }
                } else {
                    if (sentFailure.compareAndSet(false, true)) {
                        handler.handle(Future.failedFuture(FORBIDDEN));
                    }
                }
            } else {
                handler.handle(Future.failedFuture(res.cause()));
            }
        };
        for (final String authority : authorities) {
            if (!sentFailure.get()) {
                user.isAuthorized(authority, authHandler);
            }
        }
    } else {
        // No auth required
        handler.handle(Future.succeededFuture());
    }
}

From source file:org.eclipse.hono.service.http.HttpServiceBase.java

License:Open Source License

/**
 * Invoked after the http server has been started successfully.
 * <p>//from  w  ww .  j  a  va2  s.c o  m
 * May be overridden by sub-classes.
 *
 * @return A future indicating the outcome of the operation. The start up process fails if the returned future
 *         fails.
 */
protected Future<Void> onStartupSuccess() {

    return Future.succeededFuture();
}

From source file:org.eclipse.hono.service.http.HttpServiceBase.java

License:Open Source License

private Future<HttpServer> bindSecureHttpServer(final Router router) {

    if (isSecurePortEnabled()) {
        Future<HttpServer> result = Future.future();
        final String bindAddress = server == null ? getConfig().getBindAddress() : "?";
        if (server == null) {
            server = vertx.createHttpServer(getHttpServerOptions());
        }/*from   www  .  j a v  a  2  s  .c om*/
        server.requestHandler(router::accept).listen(bindAttempt -> {
            if (bindAttempt.succeeded()) {
                if (getPort() == getPortDefaultValue()) {
                    LOG.info("server listens on standard secure port [{}:{}]", bindAddress,
                            server.actualPort());
                } else {
                    LOG.warn("server listens on non-standard secure port [{}:{}], default is {}", bindAddress,
                            server.actualPort(), getPortDefaultValue());
                }
                result.complete(bindAttempt.result());
            } else {
                LOG.error("cannot bind to secure port", bindAttempt.cause());
                result.fail(bindAttempt.cause());
            }
        });
        return result;
    } else {
        return Future.succeededFuture();
    }
}

From source file:org.eclipse.hono.service.http.HttpServiceBase.java

License:Open Source License

private Future<HttpServer> bindInsecureHttpServer(final Router router) {

    if (isInsecurePortEnabled()) {
        Future<HttpServer> result = Future.future();
        final String bindAddress = insecureServer == null ? getConfig().getInsecurePortBindAddress() : "?";
        if (insecureServer == null) {
            insecureServer = vertx.createHttpServer(getInsecureHttpServerOptions());
        }//  www. j av  a2  s  .co  m
        insecureServer.requestHandler(router::accept).listen(bindAttempt -> {
            if (bindAttempt.succeeded()) {
                if (getInsecurePort() == getInsecurePortDefaultValue()) {
                    LOG.info("server listens on standard insecure port [{}:{}]", bindAddress,
                            insecureServer.actualPort());
                } else {
                    LOG.warn("server listens on non-standard insecure port [{}:{}], default is {}", bindAddress,
                            insecureServer.actualPort(), getInsecurePortDefaultValue());
                }
                result.complete(bindAttempt.result());
            } else {
                LOG.error("cannot bind to insecure port", bindAttempt.cause());
                result.fail(bindAttempt.cause());
            }
        });
        return result;
    } else {
        return Future.succeededFuture();
    }
}

From source file:org.eclipse.hono.service.http.HttpServiceBase.java

License:Open Source License

/**
 * Invoked before the http server is shut down.
 * <p>/* ww  w  . ja  v a  2 s  .com*/
 * Subclasses may override this method.
 *
 * @return A future indicating the outcome of the operation.
 */
protected Future<Void> preShutdown() {
    return Future.succeededFuture();
}

From source file:org.eclipse.hono.service.monitoring.AbstractMessageSenderConnectionEventProducer.java

License:Open Source License

private Future<?> sendNotificationEvent(final Device authenticatedDevice, final String protocolAdapter,
        final String remoteId, final String cause, final JsonObject data) {

    if (authenticatedDevice == null) {
        // we only handle authenticated devices
        return Future.succeededFuture();
    }//from  w w w.  j  a  v  a2 s.  c o m

    // get an assertion

    final Future<String> assertionFuture = this.deviceRegistryClient
            .getOrCreateRegistrationClient(authenticatedDevice.getTenantId()).compose(registrationClient -> {
                return registrationClient.assertRegistration(authenticatedDevice.getDeviceId())
                        .map(registration -> {
                            return registration.getString(RegistrationConstants.FIELD_ASSERTION);
                        });
            });

    // get a sender

    final Future<MessageSender> senderFuture = getOrCreateSender(authenticatedDevice);

    // send message with assertion and sender

    return CompositeFuture.all(assertionFuture, senderFuture).compose(f -> {
        final String deviceId = authenticatedDevice.getDeviceId();

        final JsonObject payload = new JsonObject();
        payload.put("cause", cause);
        payload.put("remote-id", remoteId);
        payload.put("source", protocolAdapter);

        if (data != null) {
            payload.put("data", data);
        }

        return senderFuture.result().send(deviceId, payload.encode().getBytes(StandardCharsets.UTF_8),
                EventConstants.EVENT_CONNECTION_NOTIFICATION_CONTENT_TYPE, assertionFuture.result(), v -> {
                });
    });
}

From source file:org.eclipse.hono.service.monitoring.LoggingConnectionEventProducer.java

License:Open Source License

@Override
public Future<?> connected(final String remoteId, final String protocolAdapter,
        final Device authenticatedDevice, final JsonObject data) {
    logger.info("   Connected - ID: {}, Protocol Adapter: {}, Device: {}, Data: {}", remoteId, protocolAdapter,
            authenticatedDevice, data);// w  ww . j  a  va  2  s .c o m
    return Future.succeededFuture();
}

From source file:org.eclipse.hono.service.monitoring.LoggingConnectionEventProducer.java

License:Open Source License

@Override
public Future<?> disconnected(final String remoteId, final String protocolAdapter,
        final Device authenticatedDevice, final JsonObject data) {
    logger.info("Disconnected - ID: {}, Protocol Adapter: {}, Device: {}, Data: {}", remoteId, protocolAdapter,
            authenticatedDevice, data);/*  ww w.ja v a2  s  .  co  m*/
    return Future.succeededFuture();
}

From source file:org.eclipse.hono.tests.CrudHttpClient.java

License:Open Source License

/**
 * Creates a resource using an HTTP POST request.
 * /* w  w  w  . j a  va 2  s  . c  o m*/
 * @param uri The URI to post to.
 * @param body The body to post (may be {@code null}).
 * @param contentType The content type to set in the request (may be {@code null}).
 * @param successPredicate A predicate on the returned HTTP status code for determining success.
 * @return A future that will succeed if the predicate evaluates to {@code true}.
 * @throws NullPointerException if URI or predicate are {@code null}.
 */
public Future<Void> create(final String uri, final Buffer body, final String contentType,
        final Predicate<Integer> successPredicate) {

    final MultiMap headers = Optional.ofNullable(contentType)
            .map(ct -> MultiMap.caseInsensitiveMultiMap().add(HttpHeaders.CONTENT_TYPE, contentType))
            .orElse(null);

    return create(uri, body, headers, successPredicate).compose(ok -> Future.succeededFuture());
}