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:io.github.pflima92.plyshare.microservice.utility.NotificationServiceVertxEBProxy.java

License:Apache License

public NotificationService createNotification(Handler<AsyncResult<JsonObject>> resultHandler) {
    if (closed) {
        resultHandler.handle(Future.failedFuture(new IllegalStateException("Proxy is closed")));
        return this;
    }/*from w w w  . j  a  va  2s.  c o  m*/
    JsonObject _json = new JsonObject();
    DeliveryOptions _deliveryOptions = (_options != null) ? new DeliveryOptions(_options)
            : new DeliveryOptions();
    _deliveryOptions.addHeader("action", "createNotification");
    _vertx.eventBus().<JsonObject>send(_address, _json, _deliveryOptions, res -> {
        if (res.failed()) {
            resultHandler.handle(Future.failedFuture(res.cause()));
        } else {
            resultHandler.handle(Future.succeededFuture(res.result().body()));
        }
    });
    return this;
}

From source file:io.gravitee.am.gateway.handler.vertx.auth.handler.impl.ClientCredentialsAuthHandlerImpl.java

License:Apache License

@Override
public void parseCredentials(RoutingContext context, Handler<AsyncResult<JsonObject>> handler) {
    parseAuthorization(context, parseAuthorization -> {
        if (parseAuthorization.failed()) {
            handler.handle(Future.failedFuture(parseAuthorization.cause()));
            return;
        }/*from  w w w  .  ja va  2  s .  c  o m*/

        JsonObject clientCredentials = parseAuthorization.result();
        authProvider.authenticate(clientCredentials, authHandler -> {
            if (authHandler.failed()) {
                handler.handle(Future.failedFuture(authHandler.cause()));
                return;
            }

            context.setUser(authHandler.result());
            // continue
            handler.handle(Future.succeededFuture());
        });
    });
}

From source file:io.gravitee.am.gateway.handler.vertx.auth.handler.impl.ClientCredentialsAuthHandlerImpl.java

License:Apache License

protected final void parseAuthorization(RoutingContext context, Handler<AsyncResult<JsonObject>> handler) {
    String clientId = context.request().getParam(OAuth2Constants.CLIENT_ID);
    String clientSecret = context.request().getParam(OAuth2Constants.CLIENT_SECRET);

    if (clientId != null && clientSecret != null) {
        JsonObject clientCredentials = new JsonObject().put(USERNAME_FIELD, clientId).put(PASSWORD_FIELD,
                clientSecret);/*  w w  w  . j  ava2  s .  co m*/
        handler.handle(Future.succeededFuture(clientCredentials));
    } else {
        handler.handle(Future.failedFuture(UNAUTHORIZED));
    }
}

From source file:io.gravitee.am.gateway.handler.vertx.auth.handler.impl.OAuth2ClientAuthHandlerImpl.java

License:Apache License

@Override
public void parseCredentials(RoutingContext context, Handler<AsyncResult<JsonObject>> handler) {
    parseAuthorization(context, parseAuthorization -> {
        if (parseAuthorization.failed()) {
            handler.handle(Future.failedFuture(parseAuthorization.cause()));
            return;
        }//from w w  w . ja v  a2  s .  c  o  m

        JsonObject credentials = parseAuthorization.result();
        authProvider.authenticate(credentials, authHandler -> {
            if (authHandler.failed()) {
                handler.handle(Future.failedFuture(authHandler.cause()));
                return;
            }

            context.setUser(authHandler.result());
            // continue
            handler.handle(Future.succeededFuture());
        });
    });
}

From source file:io.gravitee.am.gateway.handler.vertx.auth.handler.impl.OAuth2ClientAuthHandlerImpl.java

License:Apache License

protected final void parseAuthorization(RoutingContext context, Handler<AsyncResult<JsonObject>> handler) {
    try {/*from   w  ww.j a  va  2s. c  om*/
        final String providerId = context.request().getParam(PROVIDER_PARAMETER);
        final String clientId = getQueryParams(
                context.session().get(RedirectAuthHandler.DEFAULT_RETURN_URL_PARAM))
                        .get(OAuth2Constants.CLIENT_ID);

        if (providerId != null) {
            identityProviderManager.get(providerId).map(authenticationProvider -> {
                if (!(authenticationProvider instanceof OAuth2AuthenticationProvider)) {
                    throw new AuthenticationServiceException("OAuth2 Provider " + providerId + "is not social");
                }
                return (OAuth2AuthenticationProvider) authenticationProvider;
            }).subscribe(authenticationProvider -> {
                try {
                    String password = context.request()
                            .getParam(authenticationProvider.configuration().getCodeParameter());
                    JsonObject clientCredentials = new JsonObject().put(USERNAME_PARAMETER, OAUTH2_IDENTIFIER)
                            .put(PASSWORD_PARAMETER, password).put(PROVIDER_PARAMETER, providerId)
                            .put(OAuth2Constants.CLIENT_ID, clientId)
                            .put(OAuth2Constants.REDIRECT_URI, buildRedirectUri(context.request()));
                    handler.handle(Future.succeededFuture(clientCredentials));
                } catch (Exception e) {
                    handler.handle(Future.failedFuture(e));
                }
            }, error -> handler.handle(Future.failedFuture(error)));
        } else {
            handler.handle(Future.failedFuture(UNAUTHORIZED));
        }
    } catch (Exception e) {
        logger.error("Failed to parseAuthorization for OAuth 2.0 provider", e);
        handler.handle(Future.failedFuture(UNAUTHORIZED));
    }
}

From source file:io.gravitee.am.gateway.handler.vertx.auth.handler.impl.RedirectAuthHandlerImpl.java

License:Apache License

@Override
public void parseCredentials(RoutingContext context, Handler<AsyncResult<JsonObject>> handler) {
    Session session = context.session();
    if (session != null) {
        try {/*w ww .  ja  v a 2s. co m*/
            // Save current request in session - we'll get redirected back here after successful login
            HttpServerRequest request = context.request();
            session.put(returnURLParam,
                    UriBuilderRequest
                            .resolveProxyRequest(new io.vertx.reactivex.core.http.HttpServerRequest(request),
                                    request.path(),
                                    request.params().entries().stream()
                                            .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)),
                                    true, false));

            // Now redirect to the login url
            String uri = UriBuilderRequest.resolveProxyRequest(
                    new io.vertx.reactivex.core.http.HttpServerRequest(request), loginRedirectURL,
                    Collections.singletonMap(OAuth2Constants.CLIENT_ID,
                            request.getParam(OAuth2Constants.CLIENT_ID)),
                    false, false);

            handler.handle(Future.failedFuture(new HttpStatusException(302, uri)));
        } catch (Exception e) {
            logger.warn("Failed to decode login redirect url", e);
            handler.handle(Future.failedFuture(new HttpStatusException(302, loginRedirectURL)));
        }
    } else {
        handler.handle(Future.failedFuture("No session - did you forget to include a SessionHandler?"));
    }
}

From source file:io.gravitee.am.gateway.handler.vertx.auth.provider.ClientAuthenticationProvider.java

License:Apache License

@Override
public void authenticate(JsonObject credentials, Handler<AsyncResult<User>> authHandler) {
    String clientId = credentials.getString(USERNAME_FIELD);
    String clientSecret = credentials.getString(PASSWORD_FIELD);

    logger.debug("Trying to authenticate a client: clientId[{}]", clientId);

    clientService.findByClientId(clientId).subscribe(client -> {
        if (client.getClientSecret().equals(clientSecret)) {
            authHandler.handle(Future.succeededFuture(new Client(client)));
        } else {/*from w w  w  .  j a va 2s  .c  om*/
            authHandler.handle(Future.failedFuture(new BadClientCredentialsException()));
        }
    }, error -> {
        logger.error("Unexpected error while looking for a client: clientId[{}]", clientId, error);
        authHandler.handle(Future.failedFuture(error));
    }, () -> authHandler.handle(Future.failedFuture(new BadClientCredentialsException())));
}

From source file:io.gravitee.am.gateway.handler.vertx.auth.provider.OAuth2ClientAuthenticationProvider.java

License:Apache License

@Override
public void authenticate(JsonObject authInfo, Handler<AsyncResult<User>> resultHandler) {
    identityProviderManager.get(authInfo.getString(PROVIDER_PARAMETER)).flatMap(authenticationProvider -> {
        String username = authInfo.getString(USERNAME_PARAMETER);
        String password = authInfo.getString(PASSWORD_PARAMETER);
        EndUserAuthentication endUserAuthentication = new EndUserAuthentication(username, password);
        endUserAuthentication.setAdditionalInformation(Collections.singletonMap(OAuth2Constants.REDIRECT_URI,
                authInfo.getString(OAuth2Constants.REDIRECT_URI)));
        return authenticationProvider.loadUserByUsername(endUserAuthentication);
    }).switchIfEmpty(Maybe.error(new BadCredentialsException())).flatMapSingle(user -> {
        // set source and client for the current authenticated end-user
        Map<String, Object> additionalInformation = user.getAdditionalInformation() == null ? new HashMap<>()
                : new HashMap<>(user.getAdditionalInformation());
        additionalInformation.put("source", authInfo.getString(PROVIDER_PARAMETER));
        additionalInformation.put(OAuth2Constants.CLIENT_ID, authInfo.getString(OAuth2Constants.CLIENT_ID));
        ((DefaultUser) user).setAdditonalInformation(additionalInformation);
        return userService.findOrCreate(user);
    }).subscribe(/*from   ww  w . ja  va  2  s  . c  o m*/
            user -> resultHandler.handle(
                    Future.succeededFuture(new io.gravitee.am.gateway.handler.vertx.auth.user.User(user))),
            error -> {
                logger.error("Unable to authenticate oauth2 provider", error);
                resultHandler.handle(Future.failedFuture(error));
            });

}

From source file:io.gravitee.am.gateway.handler.vertx.auth.provider.UserAuthenticationProvider.java

License:Apache License

@Override
public void authenticate(JsonObject authInfo, Handler<AsyncResult<User>> resultHandler) {
    String username = authInfo.getString(USERNAME_PARAMETER);
    String password = authInfo.getString(PASSWORD_PARAMETER);
    String clientId = authInfo.getString(OAuth2Constants.CLIENT_ID);

    userAuthenticationManager.authenticate(clientId, new EndUserAuthentication(username, password)).subscribe(
            user -> resultHandler.handle(
                    Future.succeededFuture(new io.gravitee.am.gateway.handler.vertx.auth.user.User(user))),
            error -> resultHandler.handle(Future.failedFuture(error)));
}

From source file:io.hijynx.ensemble.identity.PrivilegeServiceVertxEBProxy.java

License:Apache License

public PrivilegeService addPrivilege(Privilege privilege, Handler<AsyncResult<Void>> resultHandler) {
    if (closed) {
        resultHandler.handle(Future.failedFuture(new IllegalStateException("Proxy is closed")));
        return this;
    }//from  w  w  w.  jav a2 s .co  m
    JsonObject _json = new JsonObject();
    _json.put("privilege", privilege == null ? null : privilege.toJson());
    DeliveryOptions _deliveryOptions = (_options != null) ? new DeliveryOptions(_options)
            : new DeliveryOptions();
    _deliveryOptions.addHeader("action", "addPrivilege");
    _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()));
        }
    });
    return this;
}