|
| 1 | +package org.openstack4j.connectors.jersey2; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.net.HttpURLConnection; |
| 5 | +import java.net.InetSocketAddress; |
| 6 | +import java.net.Proxy; |
| 7 | +import java.net.Proxy.Type; |
| 8 | +import java.net.URL; |
| 9 | +import java.util.concurrent.ExecutionException; |
| 10 | +import java.util.concurrent.TimeUnit; |
| 11 | + |
| 12 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 13 | +import com.google.common.cache.CacheBuilder; |
| 14 | +import com.google.common.cache.CacheLoader; |
| 15 | +import com.google.common.cache.LoadingCache; |
| 16 | +import jakarta.ws.rs.client.Client; |
| 17 | +import jakarta.ws.rs.client.ClientBuilder; |
| 18 | +import jakarta.ws.rs.client.ClientRequestContext; |
| 19 | +import jakarta.ws.rs.client.ClientRequestFilter; |
| 20 | +import jakarta.ws.rs.ext.ContextResolver; |
| 21 | +import org.glassfish.jersey.client.ClientConfig; |
| 22 | +import org.glassfish.jersey.client.ClientProperties; |
| 23 | +import org.glassfish.jersey.client.HttpUrlConnectorProvider; |
| 24 | +import org.glassfish.jersey.client.HttpUrlConnectorProvider.ConnectionFactory; |
| 25 | +import org.glassfish.jersey.jackson.JacksonFeature; |
| 26 | +import org.openstack4j.api.exceptions.ConnectionException; |
| 27 | +import org.openstack4j.core.transport.ClientConstants; |
| 28 | +import org.openstack4j.core.transport.Config; |
| 29 | +import org.openstack4j.core.transport.ObjectMapperSingleton; |
| 30 | +import org.openstack4j.core.transport.UntrustedSSL; |
| 31 | + |
| 32 | +/** |
| 33 | + * A factory for creating a rest Client which is mapped to Jackson for JSON processing. |
| 34 | + * |
| 35 | + * @author Jeremy Unruh |
| 36 | + */ |
| 37 | +class ClientFactory { |
| 38 | + |
| 39 | + private static final CustomContextResolver RESOLVER = new CustomContextResolver(); |
| 40 | + private static LoadingCache<Config, Client> CACHE = CacheBuilder.newBuilder() |
| 41 | + .expireAfterAccess(20, TimeUnit.MINUTES) |
| 42 | + .build(new CacheLoader<Config, Client>() { |
| 43 | + @Override |
| 44 | + public Client load(Config config) throws Exception { |
| 45 | + return buildClientFromConfig(config); |
| 46 | + } |
| 47 | + }); |
| 48 | + |
| 49 | + /** |
| 50 | + * Creates or Returns a Client |
| 51 | + * |
| 52 | + * @param config the configuration to use for the given client |
| 53 | + * @return the client |
| 54 | + */ |
| 55 | + static Client create(Config config) { |
| 56 | + try { |
| 57 | + return CACHE.get(config); |
| 58 | + } catch (ExecutionException e) { |
| 59 | + throw new ConnectionException("Issue creating Jersey 2 Client: " + e.getMessage(), 0, e); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + private static Client buildClientFromConfig(Config config) { |
| 64 | + ClientConfig clientConfig = new ClientConfig(); |
| 65 | + |
| 66 | + if (config.getProxy() != null) { |
| 67 | + addProxy(clientConfig, config); |
| 68 | + } |
| 69 | + |
| 70 | + ClientBuilder cb = ClientBuilder.newBuilder() |
| 71 | + .withConfig(clientConfig) |
| 72 | + .property(ClientProperties.SUPPRESS_HTTP_COMPLIANCE_VALIDATION, "true") |
| 73 | + .register(JacksonFeature.class) |
| 74 | + .register(RESOLVER) |
| 75 | + .register(new RequestFilter()); |
| 76 | + |
| 77 | + if (config.getSslContext() != null) { |
| 78 | + cb.sslContext(config.getSslContext()); |
| 79 | + } else if (config.isIgnoreSSLVerification()) { |
| 80 | + cb.sslContext(UntrustedSSL.getSSLContext()); |
| 81 | + } |
| 82 | + |
| 83 | + if (config.getHostNameVerifier() != null) { |
| 84 | + cb.hostnameVerifier(config.getHostNameVerifier()); |
| 85 | + } else if (config.isIgnoreSSLVerification()) { |
| 86 | + cb.hostnameVerifier(UntrustedSSL.getHostnameVerifier()); |
| 87 | + } |
| 88 | + |
| 89 | + if (config.getReadTimeout() > 0) { |
| 90 | + cb.property(ClientProperties.READ_TIMEOUT, config.getReadTimeout()); |
| 91 | + } |
| 92 | + |
| 93 | + if (config.getConnectTimeout() > 0) { |
| 94 | + cb.property(ClientProperties.CONNECT_TIMEOUT, config.getConnectTimeout()); |
| 95 | + } |
| 96 | + |
| 97 | + return cb.build(); |
| 98 | + } |
| 99 | + |
| 100 | + private static void addProxy(ClientConfig cc, Config config) { |
| 101 | + if (config.getProxy() != null) { |
| 102 | + HttpUrlConnectorProvider cp = new HttpUrlConnectorProvider(); |
| 103 | + cc.connectorProvider(cp); |
| 104 | + final Proxy proxy = new Proxy(Type.HTTP, |
| 105 | + new InetSocketAddress(config.getProxy().getRawHost(), config.getProxy().getPort())); |
| 106 | + |
| 107 | + cp.connectionFactory(new ConnectionFactory() { |
| 108 | + |
| 109 | + @Override |
| 110 | + public HttpURLConnection getConnection(URL url) throws IOException { |
| 111 | + return (HttpURLConnection) url.openConnection(proxy); |
| 112 | + } |
| 113 | + }); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + private static final class RequestFilter implements ClientRequestFilter { |
| 118 | + |
| 119 | + /** |
| 120 | + * {@inheritDoc} |
| 121 | + */ |
| 122 | + @Override |
| 123 | + public void filter(ClientRequestContext requestContext) throws IOException { |
| 124 | + requestContext.getHeaders().remove(ClientConstants.HEADER_CONTENT_LANGUAGE); |
| 125 | + requestContext.getHeaders().remove(ClientConstants.HEADER_CONTENT_ENCODING); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + private static final class CustomContextResolver implements ContextResolver<ObjectMapper> { |
| 130 | + |
| 131 | + /** |
| 132 | + * {@inheritDoc} |
| 133 | + */ |
| 134 | + @Override |
| 135 | + public ObjectMapper getContext(Class<?> type) { |
| 136 | + return ObjectMapperSingleton.getContext(type); |
| 137 | + } |
| 138 | + |
| 139 | + } |
| 140 | +} |
0 commit comments