Yair Zaslavsky has uploaded a new change for review. Change subject: uutils: Extract connection class ......................................................................
uutils: Extract connection class Change-Id: I85ea4e7301b3a018b0438fff25cefad80ebd7256 Signed-off-by: Yair Zaslavsky <[email protected]> --- A backend/manager/modules/uutils/src/main/java/org/ovirt/engine/core/uutils/http/Connection.java M backend/manager/modules/uutils/src/main/java/org/ovirt/engine/core/uutils/servlet/ProxyServletBase.java 2 files changed, 222 insertions(+), 1 deletion(-) git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/79/33479/1 diff --git a/backend/manager/modules/uutils/src/main/java/org/ovirt/engine/core/uutils/http/Connection.java b/backend/manager/modules/uutils/src/main/java/org/ovirt/engine/core/uutils/http/Connection.java new file mode 100644 index 0000000..505a0fe --- /dev/null +++ b/backend/manager/modules/uutils/src/main/java/org/ovirt/engine/core/uutils/http/Connection.java @@ -0,0 +1,194 @@ +package org.ovirt.engine.core.uutils.http; + +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.HttpURLConnection; +import java.net.URL; +import java.net.URLConnection; +import java.security.GeneralSecurityException; +import java.security.KeyStore; +import java.util.List; +import java.util.Map; + +import javax.net.ssl.HostnameVerifier; +import javax.net.ssl.HttpsURLConnection; +import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLSession; +import javax.net.ssl.TrustManager; +import javax.net.ssl.TrustManagerFactory; +import javax.net.ssl.X509TrustManager; + +public class Connection { + + private Boolean verifyHost = true; + private Boolean verifyChain = true; + private String httpsProtocol; + private String trustManagerAlgorithm; + private String trustStore; + private String trustStoreType; + private String trustStorePassword = "changeit"; + private Integer readTimeout; + private String url; + private URLConnection connection; + + public Connection() { + } + + + public Connection(URL url) { + this(url.toString()); + } + + public Connection(String url) { + setURL(url); + } + + public Connection setURL(String url) { + this.url = url; + return this; + } + + public Connection setVerifyHost(Boolean verifyHost) { + this.verifyHost = verifyHost; + return this; + } + + public Connection setVerifyChain(Boolean verifyChain) { + this.verifyChain = verifyChain; + return this; + } + + public Connection setHttpsProtocol(String httpsProtocol) { + this.httpsProtocol = httpsProtocol; + return this; + } + + public Connection setTrustManagerAlgorithm(String trustManagerAlgorithm) { + this.trustManagerAlgorithm = trustManagerAlgorithm; + return this; + } + + public Connection setTrustStore(String trustStore) { + this.trustStore = trustStore; + return this; + } + + public Connection setTrustStoreType(String trustStoreType) { + this.trustStoreType = trustStoreType; + return this; + } + + public Connection setTrustStorePassword(String trustStorePassword) { + this.trustStorePassword = trustStorePassword; + return this; + } + + public Connection setReadTimeout(Integer readTimeout) { + this.readTimeout = readTimeout; + return this; + } + + + public void create() throws IOException, GeneralSecurityException { + connection = new URL(url).openConnection(); + connection.setDoInput(true); + connection.setDoOutput(false); + connection.setAllowUserInteraction(false); + connection.setUseCaches(false); + if (readTimeout != null) { + connection.setReadTimeout(readTimeout); + } + if (connection instanceof HttpsURLConnection) { + HttpsURLConnection httpsConnection = (HttpsURLConnection) connection; + TrustManager[] tm = null; + if (verifyChain) { + if (trustStore != null) { + try (InputStream is = new FileInputStream(trustStore)) { + KeyStore ks = KeyStore.getInstance(trustStoreType); + ks.load(is, trustStorePassword.toCharArray()); + TrustManagerFactory tmf = TrustManagerFactory.getInstance(trustManagerAlgorithm); + tmf.init(ks); + tm = tmf.getTrustManagers(); + } + } else { + tm = new TrustManager[] { + new X509TrustManager() { + public java.security.cert.X509Certificate[] getAcceptedIssuers() { + return new java.security.cert.X509Certificate[] {}; + } + + public void checkClientTrusted( + java.security.cert.X509Certificate[] certs, String authType) { + } + + public void checkServerTrusted( + java.security.cert.X509Certificate[] certs, String authType) { + } + } + }; + } + } + + SSLContext sslContext = SSLContext.getInstance(httpsProtocol); + sslContext.init(null, tm, null); + httpsConnection.setSSLSocketFactory(sslContext.getSocketFactory()); + + if (!verifyHost) { + httpsConnection.setHostnameVerifier( + new HostnameVerifier() { + public boolean verify(String hostname, SSLSession session) { + return true; + } + } + ); + } + } + } + + public URLConnection getURLConnection() { + return connection; + } + + public int getResponseCode() throws IOException { + return asHttpUrlConnection().getResponseCode(); + } + + public Map<String, List<String>> getHeaderFields() { + return asHttpUrlConnection().getHeaderFields(); + } + + public void connect() throws IOException { + asHttpUrlConnection().connect(); + } + + public InputStream getInputStream() throws IOException { + return asHttpUrlConnection().getInputStream(); + } + + public void disconnect() { + asHttpUrlConnection().disconnect(); + } + + + protected static long copy(final InputStream input, final OutputStream output) throws IOException { + final byte[] buffer = new byte[8 * 1024]; + long count = 0; + int n; + while ((n = input.read(buffer)) != -1) { + output.write(buffer, 0, n); + count += n; + } + return count; + } + + private HttpURLConnection asHttpUrlConnection() { + if (!(connection instanceof HttpsURLConnection)) { + throw new RuntimeException("The connection is not an HTTP or HTTPS connection"); + } + return (HttpURLConnection) connection; + } + + +} diff --git a/backend/manager/modules/uutils/src/main/java/org/ovirt/engine/core/uutils/servlet/ProxyServletBase.java b/backend/manager/modules/uutils/src/main/java/org/ovirt/engine/core/uutils/servlet/ProxyServletBase.java index e2e48be..544baec 100644 --- a/backend/manager/modules/uutils/src/main/java/org/ovirt/engine/core/uutils/servlet/ProxyServletBase.java +++ b/backend/manager/modules/uutils/src/main/java/org/ovirt/engine/core/uutils/servlet/ProxyServletBase.java @@ -7,7 +7,6 @@ import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; -import java.net.URLConnection; import java.security.GeneralSecurityException; import java.security.KeyStore; import java.util.List; @@ -86,6 +85,7 @@ this.url = url; } +<<<<<<< HEAD protected URLConnection createConnection(URL url) throws IOException, GeneralSecurityException { URLConnection connection = url.openConnection(); connection.setDoInput(true); @@ -140,6 +140,18 @@ } return connection; +======= + protected Connection createConnection(URL url) throws IOException, GeneralSecurityException { + return new Connection(url).setHttpsProtocol(httpsProtocol) + .setReadTimeout(readTimeout) + .setTrustManagerAlgorithm(trustManagerAlgorithm) + .setTrustStore(trustStore) + .setTrustStorePassword(trustStorePassword) + .setTrustStoreType(trustStoreType) + .setURL(url.toString()) + .setVerifyChain(verifyChain) + .setVerifyHost(verifyHost); +>>>>>>> 31c5092... uutils: Extract connection class } private String mergeQuery(String url, String queryString) throws MalformedURLException { @@ -187,17 +199,28 @@ if (url == null) { response.sendError(response.SC_NOT_FOUND, "Cannot proxy, no URL is configured."); } else { +<<<<<<< HEAD URLConnection connection; try { connection = createConnection(new URL(mergeQuery(url, request.getQueryString()))); } catch(Exception e) { +======= + Connection connection; + try { + connection = createConnection(new URL(mergeQuery(url, request.getQueryString()))); + } catch (Exception e) { +>>>>>>> 31c5092... uutils: Extract connection class throw new ServletException(e); } connection.connect(); try { +<<<<<<< HEAD if (connection instanceof HttpURLConnection) { response.setStatus(((HttpURLConnection)connection).getResponseCode()); } +======= + response.setStatus(connection.getResponseCode()); +>>>>>>> 31c5092... uutils: Extract connection class for (Map.Entry<String, List<String>> entry : connection.getHeaderFields().entrySet()) { if (entry.getKey() != null) { boolean first = true; @@ -213,9 +236,13 @@ } copy(connection.getInputStream(), response.getOutputStream()); } finally { +<<<<<<< HEAD if (connection instanceof HttpURLConnection) { ((HttpURLConnection)connection).disconnect(); } +======= + connection.disconnect(); +>>>>>>> 31c5092... uutils: Extract connection class } } } -- To view, visit http://gerrit.ovirt.org/33479 To unsubscribe, visit http://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I85ea4e7301b3a018b0438fff25cefad80ebd7256 Gerrit-PatchSet: 1 Gerrit-Project: ovirt-engine Gerrit-Branch: master Gerrit-Owner: Yair Zaslavsky <[email protected]> _______________________________________________ Engine-patches mailing list [email protected] http://lists.ovirt.org/mailman/listinfo/engine-patches
