From 8596b9b566745ca65b3a75fe8b6d4c091369fedc Mon Sep 17 00:00:00 2001
From: Trygve Laugstøl
- * EasySSLProtocolSocketFactory can be used to creats SSL {@link Socket}s
- * that accept self-signed certificates.
- *
- * This socket factory SHOULD NOT be used for productive systems
- * due to security reasons, unless it is a concious decision and
- * you are perfectly aware of security implications of accepting
- * self-signed certificates
- *
- * Example of using custom protocol socket factory for a specific host:
- *
- * Protocol easyhttps = new Protocol("https", new EasySSLProtocolSocketFactory(), 443);
- *
- * URI uri = new URI("https://localhost/", true);
- * // use relative url only
- * GetMethod httpget = new GetMethod(uri.getPathQuery());
- * HostConfiguration hc = new HostConfiguration();
- * hc.setHost(uri.getHost(), uri.getPort(), easyhttps);
- * HttpClient client = new HttpClient();
- * client.executeMethod(hc, httpget);
- *
- *
- * Example of using custom protocol socket factory per default instead of the standard one: - *
- * Protocol easyhttps = new Protocol("https", new EasySSLProtocolSocketFactory(), 443); - * Protocol.registerProtocol("https", easyhttps); - * - * HttpClient client = new HttpClient(); - * GetMethod httpget = new GetMethod("https://localhost/"); - * client.executeMethod(httpget); - *- * - * - * @author Oleg Kalnichevski - * - *
- * DISCLAIMER: HttpClient developers DO NOT actively support this component. - * The component is provided as a reference material, which may be inappropriate - * for use without additional customization. - *
- */ - -public class EasySSLProtocolSocketFactory implements SecureProtocolSocketFactory { - - /** Log object for this class. */ - private static final Log LOG = LogFactory.getLog(EasySSLProtocolSocketFactory.class); - - private SSLContext sslcontext = null; - - /** - * Constructor for EasySSLProtocolSocketFactory. - */ - public EasySSLProtocolSocketFactory() { - super(); - } - - private static SSLContext createEasySSLContext() { - try { - SSLContext context = SSLContext.getInstance("SSL"); - context.init( - null, - new TrustManager[] {new EasyX509TrustManager(null)}, - null); - return context; - } catch (Exception e) { - LOG.error(e.getMessage(), e); - throw new HttpClientError(e.toString()); - } - } - - private SSLContext getSSLContext() { - if (this.sslcontext == null) { - this.sslcontext = createEasySSLContext(); - } - return this.sslcontext; - } - - /** - * @see SecureProtocolSocketFactory#createSocket(java.lang.String,int,java.net.InetAddress,int) - */ - public Socket createSocket( - String host, - int port, - InetAddress clientHost, - int clientPort) - throws IOException, UnknownHostException { - - return getSSLContext().getSocketFactory().createSocket( - host, - port, - clientHost, - clientPort - ); - } - - /** - * Attempts to get a new socket connection to the given host within the given time limit. - *- * To circumvent the limitations of older JREs that do not support connect timeout a - * controller thread is executed. The controller thread attempts to create a new socket - * within the given limit of time. If socket constructor does not return until the - * timeout expires, the controller terminates and throws an {@link ConnectTimeoutException} - *
- * - * @param host the host name/IP - * @param port the port on the host - * @param clientHost the local host name/IP to bind the socket to - * @param clientPort the port on the local machine - * @param params {@link HttpConnectionParams Http connection parameters} - * - * @return Socket a new socket - * - * @throws IOException if an I/O error occurs while creating the socket - * @throws UnknownHostException if the IP address of the host cannot be - * determined - */ - public Socket createSocket( - final String host, - final int port, - final InetAddress localAddress, - final int localPort, - final HttpConnectionParams params - ) throws IOException, UnknownHostException, ConnectTimeoutException { - if (params == null) { - throw new IllegalArgumentException("Parameters may not be null"); - } - int timeout = params.getConnectionTimeout(); - SocketFactory socketfactory = getSSLContext().getSocketFactory(); - if (timeout == 0) { - return socketfactory.createSocket(host, port, localAddress, localPort); - } else { - Socket socket = socketfactory.createSocket(); - SocketAddress localaddr = new InetSocketAddress(localAddress, localPort); - SocketAddress remoteaddr = new InetSocketAddress(host, port); - socket.bind(localaddr); - socket.connect(remoteaddr, timeout); - return socket; - } - } - - /** - * @see SecureProtocolSocketFactory#createSocket(java.lang.String,int) - */ - public Socket createSocket(String host, int port) - throws IOException, UnknownHostException { - return getSSLContext().getSocketFactory().createSocket( - host, - port - ); - } - - /** - * @see SecureProtocolSocketFactory#createSocket(java.net.Socket,java.lang.String,int,boolean) - */ - public Socket createSocket( - Socket socket, - String host, - int port, - boolean autoClose) - throws IOException, UnknownHostException { - return getSSLContext().getSocketFactory().createSocket( - socket, - host, - port, - autoClose - ); - } - - public boolean equals(Object obj) { - return ((obj != null) && obj.getClass().equals(EasySSLProtocolSocketFactory.class)); - } - - public int hashCode() { - return EasySSLProtocolSocketFactory.class.hashCode(); - } - -} diff --git a/src/main/java/io/trygvis/esper/testing/EasySSLSocketFactory.java b/src/main/java/io/trygvis/esper/testing/EasySSLSocketFactory.java new file mode 100755 index 0000000..b8a2904 --- /dev/null +++ b/src/main/java/io/trygvis/esper/testing/EasySSLSocketFactory.java @@ -0,0 +1,104 @@ +package io.trygvis.esper.testing; + +/* + * ==================================================================== + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + *- * EasyX509TrustManager unlike default {@link X509TrustManager} accepts - * self-signed certificates. - *
- *- * This trust manager SHOULD NOT be used for productive systems - * due to security reasons, unless it is a concious decision and - * you are perfectly aware of security implications of accepting - * self-signed certificates - *
- * - * @author Adrian Sutton - * @author Oleg Kalnichevski - * - *- * DISCLAIMER: HttpClient developers DO NOT actively support this component. - * The component is provided as a reference material, which may be inappropriate - * for use without additional customization. - *
- */ public class EasyX509TrustManager implements X509TrustManager { private X509TrustManager standardTrustManager = null; - /** Log object for this class. */ -// private static final Log LOG = LogFactory.getLog(EasyX509TrustManager.class); - - /** - * Constructor for EasyX509TrustManager. - */ public EasyX509TrustManager(KeyStore keystore) throws NoSuchAlgorithmException, KeyStoreException { super(); TrustManagerFactory factory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); @@ -78,16 +51,10 @@ public class EasyX509TrustManager implements X509TrustManager this.standardTrustManager = (X509TrustManager)trustmanagers[0]; } - /** - * @see javax.net.ssl.X509TrustManager#checkClientTrusted(X509Certificate[],String authType) - */ public void checkClientTrusted(X509Certificate[] certificates,String authType) throws CertificateException { standardTrustManager.checkClientTrusted(certificates,authType); } - /** - * @see javax.net.ssl.X509TrustManager#checkServerTrusted(X509Certificate[],String authType) - */ public void checkServerTrusted(X509Certificate[] certificates,String authType) throws CertificateException { // if (certificates != null) { // System.out.println("Server certificate chain:"); @@ -103,9 +70,6 @@ public class EasyX509TrustManager implements X509TrustManager } } - /** - * @see javax.net.ssl.X509TrustManager#getAcceptedIssuers() - */ public X509Certificate[] getAcceptedIssuers() { return this.standardTrustManager.getAcceptedIssuers(); } diff --git a/src/main/java/io/trygvis/esper/testing/Http.java b/src/main/java/io/trygvis/esper/testing/Http.java old mode 100644 new mode 100755 index 55f4714..38a66a4 --- a/src/main/java/io/trygvis/esper/testing/Http.java +++ b/src/main/java/io/trygvis/esper/testing/Http.java @@ -15,7 +15,7 @@ public class Http { static { SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); - schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443)); + schemeRegistry.register(new Scheme("https", new EasySSLSocketFactory(), 443)); BasicHttpParams params = new BasicHttpParams(); ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(params, schemeRegistry); diff --git a/src/main/java/io/trygvis/esper/testing/HttpClient.java b/src/main/java/io/trygvis/esper/testing/HttpClient.java index d9adc8d..79be578 100755 --- a/src/main/java/io/trygvis/esper/testing/HttpClient.java +++ b/src/main/java/io/trygvis/esper/testing/HttpClient.java @@ -1,9 +1,13 @@ package io.trygvis.esper.testing; import static java.lang.System.*; + +import org.apache.http.conn.scheme.*; +import org.apache.http.impl.client.*; +import org.apache.http.impl.conn.tsccm.*; +import org.apache.http.params.*; import org.codehaus.httpcache4j.*; import org.codehaus.httpcache4j.cache.*; -import org.codehaus.httpcache4j.client.*; import org.codehaus.httpcache4j.resolver.*; import java.io.*; @@ -11,11 +15,12 @@ import java.io.*; public class HttpClient { public static HTTPCache createHttpClient(Config config) { - return new HTTPCache(new MemoryCacheStorage(), createResponseResolver(config)); - } - - private static ResponseResolver createResponseResolver(final Config config) { - ResponseResolver responseResolver = HTTPClientResponseResolver.createMultithreadedInstance(); + SchemeRegistry schemeRegistry = new SchemeRegistry(); + schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); + schemeRegistry.register(new Scheme("https", new EasySSLSocketFactory(), 443)); + BasicHttpParams params = new BasicHttpParams(); + ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(params, schemeRegistry); + ResponseResolver responseResolver = new HTTPClientResponseResolver(new DefaultHttpClient(cm, new BasicHttpParams())); if (config.gitoriousSessionValue.isSome()) { responseResolver = new GitoriousResponseResolver(config.gitoriousSessionValue.some(), responseResolver); @@ -23,7 +28,7 @@ public class HttpClient { responseResolver = new TimingResponseResolver(responseResolver); - return responseResolver; + return new HTTPCache(new MemoryCacheStorage(), responseResolver); } private static class TimingResponseResolver implements ResponseResolver { diff --git a/src/main/java/io/trygvis/esper/testing/Main.java b/src/main/java/io/trygvis/esper/testing/Main.java old mode 100644 new mode 100755 index 683b950..323e44f --- a/src/main/java/io/trygvis/esper/testing/Main.java +++ b/src/main/java/io/trygvis/esper/testing/Main.java @@ -1,40 +1,17 @@ package io.trygvis.esper.testing; -import ch.qos.logback.classic.*; -import ch.qos.logback.core.util.*; import com.espertech.esper.client.*; -import org.apache.log4j.*; -import org.slf4j.*; - -import java.util.*; public class Main { // private static final String JDBC_URL = "jdbc:h2:mem:esper;DB_CLOSE_DELAY=-1"; private static final String JDBC_URL = "jdbc:h2:tcp://127.0.0.1/esper;DB_CLOSE_DELAY=-1"; public static void main(String[] args) throws Exception { - configureLog4j(); + Config.loadFromDisk(); Main main = new Main(); main.work(); } - public static void configureLog4j() { - LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory(); - StatusPrinter.print(lc); - } - -// public static void configureLog4j() { -// Properties properties = new Properties(); -// properties.setProperty("log4j.rootLogger", "DEBUG, A1"); -// properties.setProperty("log4j.logger.httpclient.wire.content", "INFO"); -// properties.setProperty("log4j.logger.httpclient.wire.header", "INFO"); -// properties.setProperty("log4j.logger.org.apache.commons.httpclient", "INFO"); -// properties.setProperty("log4j.appender.A1", "org.apache.log4j.ConsoleAppender"); -// properties.setProperty("log4j.appender.A1.layout", "org.apache.log4j.PatternLayout"); -// properties.setProperty("log4j.appender.A1.layout.ConversionPattern", "%-4r [%t] %-5p %c %x - %m%n"); -// PropertyConfigurator.configure(properties); -// } - private void work() throws Exception { Configuration config = new Configuration(); diff --git a/src/main/java/io/trygvis/esper/testing/gitorious/GitoriousImporter.java b/src/main/java/io/trygvis/esper/testing/gitorious/GitoriousImporter.java index 1e7a7fd..74e39ea 100755 --- a/src/main/java/io/trygvis/esper/testing/gitorious/GitoriousImporter.java +++ b/src/main/java/io/trygvis/esper/testing/gitorious/GitoriousImporter.java @@ -6,13 +6,11 @@ import static fj.data.Option.*; import io.trygvis.esper.testing.*; import static java.lang.System.*; import org.apache.abdera.parser.*; -import org.apache.commons.httpclient.protocol.*; import java.io.*; import java.net.*; import java.sql.*; import java.util.Date; -import java.util.*; import java.util.List; import java.util.Set; import java.util.concurrent.*; @@ -23,14 +21,14 @@ public class GitoriousImporter { public static void main(String[] args) throws Exception { Config config = Config.loadFromDisk(); - new GitoriousImporter(config, DbMain.JDBC_URL, "esper", "esper"); + new GitoriousImporter(config); } - public GitoriousImporter(Config config, final String jdbcUrl, final String jdbcUsername, final String jdbcPassword) throws Exception { + public GitoriousImporter(final Config config) throws Exception { BoneCPConfig boneCPConfig = new BoneCPConfig(){{ - setJdbcUrl(jdbcUrl); - setUsername(jdbcUsername); - setPassword(jdbcPassword); + setJdbcUrl(config.databaseUrl); + setUsername(config.databaseUsername); + setPassword(config.databasePassword); setDefaultAutoCommit(false); setMaxConnectionsPerPartition(10); }}; diff --git a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsImporter.java b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsImporter.java index 247dfe3..6a132d8 100755 --- a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsImporter.java +++ b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsImporter.java @@ -3,7 +3,6 @@ package io.trygvis.esper.testing.jenkins; import fj.*; import fj.data.*; import io.trygvis.esper.testing.*; -import static io.trygvis.esper.testing.Http.http; import io.trygvis.esper.testing.object.*; import org.joda.time.*; @@ -13,9 +12,9 @@ import java.util.concurrent.*; public class JenkinsImporter { public static void main(String[] args) throws Exception { - Config.configureLog4j(); + Config config = Config.loadFromDisk(); - final JenkinsClient jenkinsClient = new JenkinsClient(http); + final JenkinsClient jenkinsClient = new JenkinsClient(HttpClient.createHttpClient(config)); jenkinsClient.setDebugXml(false); diff --git a/src/main/resources/logback-test.xml b/src/main/resources/logback-test.xml old mode 100644 new mode 100755 index 1dad1b1..a8416ff --- a/src/main/resources/logback-test.xml +++ b/src/main/resources/logback-test.xml @@ -9,6 +9,8 @@