
org.cloudfoundry.reactor.util._DefaultConnectionContext Maven / Gradle / Ivy
The newest version!
/*
* 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.reactor.util;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.cloudfoundry.Nullable;
import org.immutables.value.Value;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;
import reactor.core.publisher.Mono;
import reactor.io.netty.config.HttpClientOptions;
import reactor.io.netty.http.HttpClient;
import java.time.Duration;
import java.util.Map;
import java.util.Optional;
import static reactor.io.netty.common.NettyHandlerNames.SslHandler;
@Value.Immutable
abstract class _DefaultConnectionContext implements ConnectionContext {
private static final int DEFAULT_PORT = 443;
private static final int RECEIVE_BUFFER_SIZE = 10 * 1024 * 1024;
private static final int SEND_BUFFER_SIZE = 10 * 1024 * 1024;
private static final int UNDEFINED_PORT = -1;
public abstract AuthorizationProvider getAuthorizationProvider();
@Value.Default
public String getClientId() {
return "cf";
}
@Value.Default
public String getClientSecret() {
return "";
}
@Value.Derived
public HttpClient getHttpClient() {
return HttpClient.create(HttpClientOptions.create()
.sslSupport()
.sndbuf(SEND_BUFFER_SIZE)
.rcvbuf(RECEIVE_BUFFER_SIZE)
.pipelineConfigurer(pipeline -> getProxyContext().getHttpProxyHandler().ifPresent(handler -> pipeline.addBefore(SslHandler, null, handler)))
.sslConfigurer(ssl -> getSslCertificateTruster().ifPresent(trustManager -> ssl.trustManager(new StaticTrustManagerFactory(trustManager)))));
}
@Value.Default
public ObjectMapper getObjectMapper() {
return new ObjectMapper();
}
@Value.Derived
public Mono getRoot() {
Integer port = getPort();
UriComponentsBuilder builder = UriComponentsBuilder.newInstance().scheme("https").host(getHost());
if (port != null) {
builder.port(port);
}
UriComponents components = normalize(builder);
trust(components, getSslCertificateTruster());
return Mono.just(components.toUriString());
}
@Override
public Mono getRoot(String key) {
return getInfo()
.map(info -> normalize(UriComponentsBuilder.fromUriString(info.get(key))))
.doOnSuccess(components -> trust(components, getSslCertificateTruster()))
.map(UriComponents::toUriString)
.cache();
}
abstract String getHost();
@SuppressWarnings("unchecked")
@Value.Derived
Mono
© 2015 - 2025 Weber Informatics LLC | Privacy Policy