All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.cloudfoundry.spring.util.network.AccessTokenUriBuilder Maven / Gradle / Ivy

The newest version!
// Generated by delombok at Sat Jun 18 07:22:04 PDT 2016
/*
 * Copyright 2013-2016 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.cloudfoundry.spring.util.network;

import org.springframework.web.client.RestOperations;
import org.springframework.web.util.UriComponentsBuilder;
import java.net.URI;
import java.util.Map;
import java.util.Optional;
import static java.util.concurrent.TimeUnit.SECONDS;

final class AccessTokenUriBuilder {
    private String host;
    private Integer port;
    private RestOperations restOperations;
    private SslCertificateTruster sslCertificateTruster;

    @SuppressWarnings("unchecked")
    public URI build() {
        Map info = this.restOperations.getForObject(getInfoUri(this.host, this.port, this.sslCertificateTruster), Map.class);
        return getAccessTokenUri(info.get("token_endpoint"), this.sslCertificateTruster);
    }

    private static URI getAccessTokenUri(String tokenEndpoint, SslCertificateTruster sslCertificateTruster) {
        URI uri = UriComponentsBuilder.fromUriString(tokenEndpoint).pathSegment("oauth", "token").build().toUri();
        sslCertificateTruster.trust(uri.getHost(), uri.getPort(), 5, SECONDS);
        return uri;
    }

    private static URI getInfoUri(String host, Integer port, SslCertificateTruster sslCertificateTruster) {
        URI uri = UriComponentsBuilder.newInstance().scheme("https").host(host).port(Optional.ofNullable(port).orElse(443)).pathSegment("v2", "info").build().toUri();
        sslCertificateTruster.trust(uri.getHost(), uri.getPort(), 5, SECONDS);
        return uri;
    }

    @java.lang.SuppressWarnings("all")
    @javax.annotation.Generated("lombok")
    public AccessTokenUriBuilder host(final String host) {
        this.host = host;
        return this;
    }

    @java.lang.SuppressWarnings("all")
    @javax.annotation.Generated("lombok")
    public AccessTokenUriBuilder port(final Integer port) {
        this.port = port;
        return this;
    }

    @java.lang.SuppressWarnings("all")
    @javax.annotation.Generated("lombok")
    public AccessTokenUriBuilder restOperations(final RestOperations restOperations) {
        this.restOperations = restOperations;
        return this;
    }

    @java.lang.SuppressWarnings("all")
    @javax.annotation.Generated("lombok")
    public AccessTokenUriBuilder sslCertificateTruster(final SslCertificateTruster sslCertificateTruster) {
        this.sslCertificateTruster = sslCertificateTruster;
        return this;
    }

    @java.lang.Override
    @java.lang.SuppressWarnings("all")
    @javax.annotation.Generated("lombok")
    public java.lang.String toString() {
        return "AccessTokenUriBuilder(host=" + this.host + ", port=" + this.port + ", restOperations=" + this.restOperations + ", sslCertificateTruster=" + this.sslCertificateTruster + ")";
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy