Example usage for io.vertx.core.http RequestOptions RequestOptions

List of usage examples for io.vertx.core.http RequestOptions RequestOptions

Introduction

In this page you can find the example usage for io.vertx.core.http RequestOptions RequestOptions.

Prototype

public RequestOptions() 

Source Link

Document

Default constructor

Usage

From source file:examples.HTTPExamples.java

License:Open Source License

public void setSSLPerRequest(HttpClient client) {
    client.getNow(new RequestOptions().setHost("localhost").setPort(8080).setURI("/").setSsl(true),
            response -> {/*w  w  w  .ja  v  a2s .  c  o  m*/
                System.out.println("Received response with status code " + response.statusCode());
            });
}

From source file:io.gravitee.am.identityprovider.github.authentication.GithubAuthenticationProvider.java

License:Apache License

private Maybe<String> authenticate(Authentication authentication) {
    return io.reactivex.Maybe.create(emitter -> {
        // prepare body request parameters
        List<NameValuePair> urlParameters = new ArrayList<>();
        urlParameters.add(new BasicNameValuePair(CLIENT_ID, configuration.getClientId()));
        urlParameters.add(new BasicNameValuePair(CLIENT_SECRET, configuration.getClientSecret()));
        urlParameters.add(new BasicNameValuePair(REDIRECT_URI,
                (String) authentication.getAdditionalInformation().get(REDIRECT_URI)));
        urlParameters.add(new BasicNameValuePair(CODE, (String) authentication.getCredentials()));
        String bodyRequest = URLEncodedUtils.format(urlParameters);

        URI requestUri = URI.create(configuration.getAccessTokenUri());
        final int port = requestUri.getPort() != -1 ? requestUri.getPort()
                : (HTTPS_SCHEME.equals(requestUri.getScheme()) ? 443 : 80);
        boolean ssl = HTTPS_SCHEME.equalsIgnoreCase(requestUri.getScheme());

        HttpClientRequest request = client.post(new RequestOptions().setHost(requestUri.getHost()).setPort(port)
                .setSsl(ssl).setURI(requestUri.toString()), response -> {
                    if (response.statusCode() != 200) {
                        emitter.onError(new BadCredentialsException(response.statusMessage()));
                    } else {
                        response.bodyHandler(body -> {
                            Map<String, String> bodyResponse = URLEncodedUtils.format(body.toString());
                            emitter.onSuccess(bodyResponse.get("access_token"));
                        });/*  w  w  w  .  j av  a2 s.c o  m*/
                    }
                }).setChunked(true).putHeader(HttpHeaders.CONTENT_TYPE, URLEncodedUtils.CONTENT_TYPE);

        request.write(bodyRequest);
        request.end();
    });
}

From source file:io.gravitee.am.identityprovider.github.authentication.GithubAuthenticationProvider.java

License:Apache License

private Maybe<User> profile(String accessToken) {
    URI requestUri = URI.create(configuration.getUserProfileUri());
    final int port = requestUri.getPort() != -1 ? requestUri.getPort()
            : (HTTPS_SCHEME.equals(requestUri.getScheme()) ? 443 : 80);
    boolean ssl = HTTPS_SCHEME.equalsIgnoreCase(requestUri.getScheme());
    HttpClientRequest request = client/*w  w  w. java2 s. c  om*/
            .get(new RequestOptions().setHost(requestUri.getHost()).setPort(port).setSsl(ssl)
                    .setURI(requestUri.toString()))
            // https://developer.github.com/v3/#user-agent-required
            .putHeader(HttpHeaders.USER_AGENT, DEFAULT_USER_AGENT)
            .putHeader(HttpHeaders.AUTHORIZATION, "token " + accessToken);

    return request.toObservable().flatMap(httpClientResponse -> {
        if (httpClientResponse.statusCode() != 200) {
            throw new BadCredentialsException(httpClientResponse.statusMessage());
        }
        return Observable.just(httpClientResponse);
    }).flatMap(HttpClientResponse::toObservable)
            // we reduce the response chunks into a single one to have access to the full JSON object
            .reduce(Buffer.buffer(), Buffer::appendBuffer).map(buffer -> createUser(buffer.toJsonObject()))
            .toMaybe()
            // Vert.x requires the HttpClientRequest.end() method to be invoked to signaling that the request can be sent
            .doOnSubscribe(subscription -> request.end());
}

From source file:io.gravitee.am.identityprovider.oauth2.authentication.OAuth2GenericAuthenticationProvider.java

License:Apache License

private Maybe<String> authenticate(Authentication authentication) {
    return io.reactivex.Maybe.create(emitter -> {
        // prepare body request parameters
        List<NameValuePair> urlParameters = new ArrayList<>();
        urlParameters.add(new BasicNameValuePair(CLIENT_ID, configuration.getClientId()));
        urlParameters.add(new BasicNameValuePair(CLIENT_SECRET, configuration.getClientSecret()));
        urlParameters.add(new BasicNameValuePair(REDIRECT_URI,
                (String) authentication.getAdditionalInformation().get(REDIRECT_URI)));
        urlParameters.add(new BasicNameValuePair(CODE, (String) authentication.getCredentials()));
        urlParameters.add(new BasicNameValuePair(GRANT_TYPE, "authorization_code"));
        String bodyRequest = URLEncodedUtils.format(urlParameters);

        URI requestUri = URI.create(configuration.getAccessTokenUri());
        final int port = requestUri.getPort() != -1 ? requestUri.getPort()
                : (HTTPS_SCHEME.equals(requestUri.getScheme()) ? 443 : 80);
        boolean ssl = HTTPS_SCHEME.equalsIgnoreCase(requestUri.getScheme());

        HttpClientRequest request = client.post(new RequestOptions().setHost(requestUri.getHost()).setPort(port)
                .setSsl(ssl).setURI(requestUri.toString()), response -> {
                    if (response.statusCode() != 200) {
                        emitter.onError(new BadCredentialsException(response.statusMessage()));
                    } else {
                        response.bodyHandler(body -> {
                            JsonObject bodyResponse = body.toJsonObject();
                            emitter.onSuccess(bodyResponse.getString("access_token"));
                        });//from  www  .  j a v a2  s. c  om
                    }
                }).setChunked(true).putHeader(HttpHeaders.CONTENT_TYPE, URLEncodedUtils.CONTENT_TYPE);

        request.write(bodyRequest);
        request.end();
    });
}

From source file:io.gravitee.am.identityprovider.oauth2.authentication.OAuth2GenericAuthenticationProvider.java

License:Apache License

private Maybe<User> profile(String accessToken) {
    URI requestUri = URI.create(configuration.getUserProfileUri());
    final int port = requestUri.getPort() != -1 ? requestUri.getPort()
            : (HTTPS_SCHEME.equals(requestUri.getScheme()) ? 443 : 80);
    boolean ssl = HTTPS_SCHEME.equalsIgnoreCase(requestUri.getScheme());
    HttpClientRequest request = client// w w w . ja va  2s.  c  om
            .get(new RequestOptions().setHost(requestUri.getHost()).setPort(port).setSsl(ssl)
                    .setURI(requestUri.toString()))
            .putHeader(HttpHeaders.USER_AGENT, DEFAULT_USER_AGENT)
            .putHeader(HttpHeaders.AUTHORIZATION, "Bearer " + accessToken);

    return request.toObservable().flatMap(httpClientResponse -> {
        if (httpClientResponse.statusCode() != 200) {
            throw new BadCredentialsException(httpClientResponse.statusMessage());
        }
        return Observable.just(httpClientResponse);
    }).flatMap(HttpClientResponse::toObservable)
            // we reduce the response chunks into a single one to have access to the full JSON object
            .reduce(Buffer.buffer(), Buffer::appendBuffer).map(buffer -> createUser(buffer.toJsonObject()))
            .toMaybe()
            // Vert.x requires the HttpClientRequest.end() method to be invoked to signaling that the request can be sent
            .doOnSubscribe(subscription -> request.end());
}

From source file:org.apache.servicecomb.transport.rest.client.http.RestClientInvocation.java

License:Apache License

void createRequest(IpPort ipPort, String path) {
    URIEndpointObject endpoint = (URIEndpointObject) invocation.getEndpoint().getAddress();
    RequestOptions requestOptions = new RequestOptions();
    requestOptions.setHost(ipPort.getHostOrIp()).setPort(ipPort.getPort()).setSsl(endpoint.isSslEnabled())
            .setURI(path);/*from ww  w  .ja  v a  2  s . c o m*/

    HttpMethod method = getMethod();
    LOGGER.debug("Sending request by rest, method={}, qualifiedName={}, path={}, endpoint={}.", method,
            invocation.getMicroserviceQualifiedName(), path, invocation.getEndpoint().getEndpoint());
    clientRequest = httpClientWithContext.getHttpClient().request(method, requestOptions, this::handleResponse);
}

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

License:Open Source License

private RequestOptions getRequestOptions() {
    return new RequestOptions().setSsl(options.isSsl()).setHost(options.getDefaultHost())
            .setPort(options.getDefaultPort());
}