Example usage for io.vertx.core Future failedFuture

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

Introduction

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

Prototype

static <T> Future<T> failedFuture(String failureMessage) 

Source Link

Document

Create a failed future with the specified failure message.

Usage

From source file:fr.pjthin.vertx.client.UserDaoVertxEBProxy.java

License:Apache License

public void deleteByLogin(String login, Handler<AsyncResult<Void>> complete) {
    if (closed) {
        complete.handle(Future.failedFuture(new IllegalStateException("Proxy is closed")));
        return;/*ww  w  .  j a v  a2s  .c o m*/
    }
    JsonObject _json = new JsonObject();
    _json.put("login", login);
    DeliveryOptions _deliveryOptions = (_options != null) ? new DeliveryOptions(_options)
            : new DeliveryOptions();
    _deliveryOptions.addHeader("action", "deleteByLogin");
    _vertx.eventBus().<Void>send(_address, _json, _deliveryOptions, res -> {
        if (res.failed()) {
            complete.handle(Future.failedFuture(res.cause()));
        } else {
            complete.handle(Future.succeededFuture(res.result().body()));
        }
    });
}

From source file:io.apiman.gateway.engine.vertx.polling.fetchers.auth.KeycloakOAuth2.java

License:Apache License

@Override
public Authenticator authenticate(Vertx vertx, Map<String, String> config, MultiMap headerMap,
        Handler<AsyncResult<Void>> resultHandler) {

    OAuth2FlowType flowType = getFlowType(config.get("flowType"));
    JsonObject params = new JsonObject();
    if (config.get("username") != null) {
        params.put("username", config.get("username"));
    }/*w ww .j a  v  a2  s.co  m*/
    if (config.get("password") != null) {
        params.put("password", config.get("password"));
    }

    OAuth2Auth oauth2 = KeycloakAuth.create(vertx, flowType, mapToJson(config));

    oauth2.getToken(params, tokenResult -> {
        if (tokenResult.succeeded()) {
            log.debug("OAuth2 Keycloak exchange succeeded.");
            AccessToken token = tokenResult.result();
            headerMap.set("Authorization", "Bearer " + token.principal().getString("access_token"));
            resultHandler.handle(Future.succeededFuture());
        } else {
            log.error("Access Token Error: {0}.", tokenResult.cause().getMessage());
            resultHandler.handle(Future.failedFuture(tokenResult.cause()));
        }
    });
    return this;
}

From source file:io.apiman.gateway.engine.vertx.polling.fetchers.auth.KeycloakOAuth2Client.java

License:Apache License

@Override
public Authenticator authenticate(Vertx vertx, Map<String, String> config, MultiMap headerMap,
        AsyncResultHandler<Void> resultHandler) {

    OAuth2FlowType flowType = getFlowType(config.get("flowType"));
    JsonObject params = new JsonObject();
    if (config.get("username") != null) {
        params.put("username", config.get("username"));
    }//www . j av  a  2s.c  o m
    if (config.get("password") != null) {
        params.put("password", config.get("password"));
    }

    OAuth2Auth oauth2 = OAuth2Auth.createKeycloak(vertx, flowType, mapToJson(config));

    oauth2.getToken(params, tokenResult -> {
        if (tokenResult.succeeded()) {
            log.debug("OAuth2 Keycloak exchange succeeded.");
            AccessToken token = tokenResult.result();
            headerMap.set("Authorization", "Bearer " + token.principal().getString("access_token"));
            resultHandler.handle(Future.succeededFuture());
        } else {
            log.error("Access Token Error: {0}.", tokenResult.cause().getMessage());
            resultHandler.handle(Future.failedFuture(tokenResult.cause()));
        }
    });
    return this;
}

From source file:io.apiman.gateway.engine.vertx.polling.fetchers.auth.OAuth2.java

License:Apache License

@Override
public Authenticator authenticate(Vertx vertx, Map<String, String> config, MultiMap headerMap,
        Handler<AsyncResult<Void>> resultHandler) {
    OAuth2ClientOptions credentials = new OAuth2ClientOptions(mapToJson(config));
    if (config.get("oauthUri") != null) {
        credentials.setSite(config.get("oauthUri"));
    }/*from w w  w . j a  v a  2 s .co m*/
    if (config.get("clientId") != null) {
        credentials.setClientID(config.get("clientId"));
    }

    OAuth2FlowType flowType = getFlowType(config.get("flowType"));
    JsonObject params = new JsonObject();
    if (config.get("username") != null) {
        params.put("username", config.get("username"));
    }
    if (config.get("password") != null) {
        params.put("password", config.get("password"));
    }

    OAuth2Auth oauth2 = OAuth2Auth.create(vertx, flowType, credentials);

    oauth2.getToken(params, tokenResult -> {
        if (tokenResult.succeeded()) {
            log.debug("OAuth2 exchange succeeded.");
            AccessToken token = tokenResult.result();
            headerMap.set("Authorization", "Bearer " + token.principal().getString("access_token"));
            resultHandler.handle(Future.succeededFuture());
        } else {
            log.error("Access Token Error: {0}.", tokenResult.cause().getMessage());
            resultHandler.handle(Future.failedFuture(tokenResult.cause()));
        }
    });
    return this;
}

From source file:io.apiman.gateway.engine.vertx.polling.fetchers.auth.OAuth2Client.java

License:Apache License

@Override
public Authenticator authenticate(Vertx vertx, Map<String, String> config, MultiMap headerMap,
        AsyncResultHandler<Void> resultHandler) {
    OAuth2ClientOptions credentials = new OAuth2ClientOptions(mapToJson(config));
    if (config.get("oauthUri") != null) {
        credentials.setSite(config.get("oauthUri"));
    }//from   w  w w  .  ja v a 2  s. co  m
    if (config.get("clientId") != null) {
        credentials.setClientID(config.get("clientId"));
    }

    OAuth2FlowType flowType = getFlowType(config.get("flowType"));
    JsonObject params = new JsonObject();
    if (config.get("username") != null) {
        params.put("username", config.get("username"));
    }
    if (config.get("password") != null) {
        params.put("password", config.get("password"));
    }

    OAuth2Auth oauth2 = OAuth2Auth.create(vertx, flowType, credentials);

    oauth2.getToken(params, tokenResult -> {
        if (tokenResult.succeeded()) {
            log.debug("OAuth2 exchange succeeded.");
            AccessToken token = tokenResult.result();
            headerMap.set("Authorization", "Bearer " + token.principal().getString("access_token"));
            resultHandler.handle(Future.succeededFuture());
        } else {
            log.error("Access Token Error: {0}.", tokenResult.cause().getMessage());
            resultHandler.handle(Future.failedFuture(tokenResult.cause()));
        }
    });
    return this;
}

From source file:io.apiman.gateway.platforms.vertx3.api.auth.BasicAuth.java

License:Apache License

private static AuthProvider authenticateBasic(JsonObject apimanConfig) {
    return (authInfo, resultHandler) -> {
        String storedUsername = apimanConfig.getString("username");
        String storedPassword = apimanConfig.getString("password");

        if (storedUsername == null || storedPassword == null) {
            resultHandler.handle(Future.failedFuture("Credentials not set in configuration."));
            return;
        }/*from  w ww  .  j a va  2 s. c  om*/

        String username = authInfo.getString("username");
        String password = StringUtils.chomp(authInfo.getString("password"));

        if (storedUsername.equals(username) && storedPassword.equals(password)) {
            resultHandler.handle(Future.succeededFuture());
        } else {
            resultHandler.handle(Future.failedFuture("No such user, or password incorrect."));
        }
    };
}

From source file:io.apiman.gateway.platforms.vertx3.services.impl.IngestorToPolicyImpl.java

License:Apache License

public void fail(Throwable error) {
    log.debug(String.format("%s indicated #fail", uuid));
    resultHandler.handle(Future.failedFuture(error));
}

From source file:io.apiman.gateway.platforms.vertx3.services.IngestorToPolicyServiceVertxEBProxy.java

License:Apache License

@Override
public void head(VertxApiRequest apiRequest, Handler<AsyncResult<Boolean>> readyHandler) {
    if (closed) {
        readyHandler.handle(Future.failedFuture(new IllegalStateException("Proxy is closed")));
        return;//ww  w.  j av  a 2 s .  com
    }
    JsonObject _json = new JsonObject();
    _json.put("apiRequest", apiRequest == null ? null : apiRequest.toJson());
    DeliveryOptions _deliveryOptions = new DeliveryOptions();
    _deliveryOptions.addHeader("action", "head");
    _vertx.eventBus().<Boolean>send(_address, _json, _deliveryOptions, res -> {
        if (res.failed()) {
            readyHandler.handle(Future.failedFuture(res.cause()));
        } else {
            readyHandler.handle(Future.succeededFuture(res.result().body()));
        }
    });
}

From source file:io.apiman.gateway.platforms.vertx3.services.IngestorToPolicyServiceVertxEBProxy.java

License:Apache License

@Override
public void end(Handler<AsyncResult<Void>> resultHandler) {
    if (closed) {
        resultHandler.handle(Future.failedFuture(new IllegalStateException("Proxy is closed")));
        return;//from  w w w  .j  a v  a  2s . c  o m
    }
    closed = true;
    JsonObject _json = new JsonObject();
    DeliveryOptions _deliveryOptions = new DeliveryOptions();
    _deliveryOptions.addHeader("action", "end");
    _vertx.eventBus().<Void>send(_address, _json, _deliveryOptions, res -> {
        if (res.failed()) {
            resultHandler.handle(Future.failedFuture(res.cause()));
        } else {
            resultHandler.handle(Future.succeededFuture(res.result().body()));
        }
    });
}

From source file:io.apiman.gateway.platforms.vertx3.services.InitializeIngestorServiceVertxEBProxy.java

License:Apache License

@Override
public void createIngestor(String uuid, Handler<AsyncResult<IngestorToPolicyService>> resultHandler) {
    if (closed) {
        resultHandler.handle(Future.failedFuture(new IllegalStateException("Proxy is closed")));
        return;//from   ww  w. j ava2 s .co  m
    }
    JsonObject _json = new JsonObject();
    _json.put("uuid", uuid);
    DeliveryOptions _deliveryOptions = new DeliveryOptions();
    _deliveryOptions.addHeader("action", "createIngestor");
    _vertx.eventBus().<IngestorToPolicyService>send(_address, _json, _deliveryOptions, res -> {
        if (res.failed()) {
            resultHandler.handle(Future.failedFuture(res.cause()));
        } else {
            String addr = res.result().headers().get("proxyaddr");
            resultHandler.handle(Future
                    .succeededFuture(ProxyHelper.createProxy(IngestorToPolicyService.class, _vertx, addr)));
        }
    });
}