You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@manifoldcf.apache.org by kw...@apache.org on 2013/06/23 15:08:42 UTC

svn commit: r1495813 - in /manifoldcf/branches/CONNECTORS-723: connectors/jira/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/jira/ connectors/livelink/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/livelink/ connect...

Author: kwright
Date: Sun Jun 23 13:08:42 2013
New Revision: 1495813

URL: http://svn.apache.org/r1495813
Log:
Fix up the https handling everywhere. 

Added:
    manifoldcf/branches/CONNECTORS-723/framework/core/src/main/java/org/apache/manifoldcf/core/common/InterruptibleSocketFactory.java   (with props)
Modified:
    manifoldcf/branches/CONNECTORS-723/connectors/jira/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/jira/JiraSession.java
    manifoldcf/branches/CONNECTORS-723/connectors/livelink/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/livelink/LivelinkConnector.java
    manifoldcf/branches/CONNECTORS-723/connectors/rss/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/rss/ThrottledFetcher.java
    manifoldcf/branches/CONNECTORS-723/connectors/webcrawler/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/webcrawler/ThrottledFetcher.java
    manifoldcf/branches/CONNECTORS-723/connectors/wiki/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/wiki/WikiConnector.java

Modified: manifoldcf/branches/CONNECTORS-723/connectors/jira/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/jira/JiraSession.java
URL: http://svn.apache.org/viewvc/manifoldcf/branches/CONNECTORS-723/connectors/jira/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/jira/JiraSession.java?rev=1495813&r1=1495812&r2=1495813&view=diff
==============================================================================
--- manifoldcf/branches/CONNECTORS-723/connectors/jira/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/jira/JiraSession.java (original)
+++ manifoldcf/branches/CONNECTORS-723/connectors/jira/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/jira/JiraSession.java Sun Jun 23 13:08:42 2013
@@ -18,6 +18,8 @@
 package org.apache.manifoldcf.crawler.connectors.jira;
 
 import org.apache.manifoldcf.core.common.*;
+import org.apache.manifoldcf.core.interfaces.KeystoreManagerFactory;
+import org.apache.manifoldcf.core.interfaces.ManifoldCFException;
 
 import java.io.Reader;
 import java.io.Writer;
@@ -52,6 +54,10 @@ import org.apache.http.params.CoreConnec
 import org.apache.http.client.params.ClientPNames;
 import org.apache.http.client.HttpRequestRetryHandler;
 import org.apache.http.protocol.HttpContext;
+import org.apache.http.conn.scheme.Scheme;
+import org.apache.http.conn.ssl.SSLSocketFactory;
+import org.apache.http.conn.ssl.AllowAllHostnameVerifier;
+import org.apache.http.params.CoreProtocolPNames;
 
 import org.json.simple.JSONObject;
 import org.json.simple.JSONValue;
@@ -75,20 +81,33 @@ public class JiraSession {
   /**
    * Constructor. Create a session.
    */
-  public JiraSession(String clientId, String clientSecret, String URLbase) {
+  public JiraSession(String clientId, String clientSecret, String URLbase)
+    throws ManifoldCFException {
     this.URLbase = URLbase;
     this.clientId = clientId;
     this.clientSecret = clientSecret;
 
+    int socketTimeout = 900000;
+    int connectionTimeout = 60000;
+
+    javax.net.ssl.SSLSocketFactory httpsSocketFactory = KeystoreManagerFactory.getTrustingSecureSocketFactory();
+    SSLSocketFactory myFactory = new SSLSocketFactory(new InterruptibleSocketFactory(httpsSocketFactory,connectionTimeout),
+      new AllowAllHostnameVerifier());
+    Scheme myHttpsProtocol = new Scheme("https", 443, myFactory);
+
     PoolingClientConnectionManager localConnectionManager = new PoolingClientConnectionManager();
     localConnectionManager.setMaxTotal(1);
     connectionManager = localConnectionManager;
+    // Set up protocol registry
+    connectionManager.getSchemeRegistry().register(myHttpsProtocol);
 
     BasicHttpParams params = new BasicHttpParams();
+    params.setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE,true);
+    params.setIntParameter(CoreProtocolPNames.WAIT_FOR_CONTINUE,socketTimeout);
     params.setBooleanParameter(CoreConnectionPNames.TCP_NODELAY,true);
     params.setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK,false);
-    params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,60000);
-    params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT,900000);
+    params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,connectionTimeout);
+    params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT,socketTimeout);
     params.setBooleanParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS,true);
     DefaultHttpClient localHttpClient = new DefaultHttpClient(connectionManager,params);
     // No retries

Modified: manifoldcf/branches/CONNECTORS-723/connectors/livelink/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/livelink/LivelinkConnector.java
URL: http://svn.apache.org/viewvc/manifoldcf/branches/CONNECTORS-723/connectors/livelink/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/livelink/LivelinkConnector.java?rev=1495813&r1=1495812&r2=1495813&view=diff
==============================================================================
--- manifoldcf/branches/CONNECTORS-723/connectors/livelink/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/livelink/LivelinkConnector.java (original)
+++ manifoldcf/branches/CONNECTORS-723/connectors/livelink/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/livelink/LivelinkConnector.java Sun Jun 23 13:08:42 2013
@@ -25,6 +25,7 @@ import org.apache.manifoldcf.crawler.sys
 import org.apache.manifoldcf.crawler.system.ManifoldCF;
 import org.apache.manifoldcf.core.common.XThreadInputStream;
 import org.apache.manifoldcf.core.common.XThreadOutputStream;
+import org.apache.manifoldcf.core.common.InterruptibleSocketFactory;
 
 import java.io.*;
 import java.util.*;
@@ -473,6 +474,9 @@ public class LivelinkConnector extends o
     getSessionParameters();
     if (hasConnected == false)
     {
+      int socketTimeout = 900000;
+      int connectionTimeout = 300000;
+
       // Set up connection manager
       PoolingClientConnectionManager localConnectionManager = new PoolingClientConnectionManager();
       localConnectionManager.setMaxTotal(1);
@@ -480,7 +484,7 @@ public class LivelinkConnector extends o
       // Set up ingest ssl if indicated
       if (ingestKeystoreManager != null)
       {
-        SSLSocketFactory myFactory = new SSLSocketFactory(ingestKeystoreManager.getSecureSocketFactory(),
+        SSLSocketFactory myFactory = new SSLSocketFactory(new InterruptibleSocketFactory(ingestKeystoreManager.getSecureSocketFactory(), connectionTimeout),
           new BrowserCompatHostnameVerifier());
         Scheme myHttpsProtocol = new Scheme("https", 443, myFactory);
         localConnectionManager.getSchemeRegistry().register(myHttpsProtocol);

Modified: manifoldcf/branches/CONNECTORS-723/connectors/rss/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/rss/ThrottledFetcher.java
URL: http://svn.apache.org/viewvc/manifoldcf/branches/CONNECTORS-723/connectors/rss/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/rss/ThrottledFetcher.java?rev=1495813&r1=1495812&r2=1495813&view=diff
==============================================================================
--- manifoldcf/branches/CONNECTORS-723/connectors/rss/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/rss/ThrottledFetcher.java (original)
+++ manifoldcf/branches/CONNECTORS-723/connectors/rss/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/rss/ThrottledFetcher.java Sun Jun 23 13:08:42 2013
@@ -20,6 +20,7 @@ package org.apache.manifoldcf.crawler.co
 
 import org.apache.manifoldcf.core.interfaces.*;
 import org.apache.manifoldcf.core.common.XThreadInputStream;
+import org.apache.manifoldcf.core.common.InterruptibleSocketFactory;
 import org.apache.manifoldcf.agents.interfaces.*;
 import org.apache.manifoldcf.crawler.interfaces.*;
 import org.apache.manifoldcf.crawler.system.Logging;
@@ -1485,176 +1486,5 @@ public class ThrottledFetcher
 
   }
 
-  /** SSL Socket factory which wraps another socket factory but allows timeout on socket
-  * creation.
-  */
-  protected static class InterruptibleSocketFactory extends javax.net.ssl.SSLSocketFactory
-  {
-    protected final javax.net.ssl.SSLSocketFactory wrappedFactory;
-    protected final long connectTimeoutMilliseconds;
-    
-    public InterruptibleSocketFactory(javax.net.ssl.SSLSocketFactory wrappedFactory, long connectTimeoutMilliseconds)
-    {
-      this.wrappedFactory = wrappedFactory;
-      this.connectTimeoutMilliseconds = connectTimeoutMilliseconds;
-    }
-
-    @Override
-    public Socket createSocket()
-      throws IOException
-    {
-      // Socket isn't open
-      return wrappedFactory.createSocket();
-    }
-    
-    @Override
-    public Socket createSocket(String host, int port)
-      throws IOException, UnknownHostException
-    {
-      return fireOffThread(InetAddress.getByName(host),port,null,-1);
-    }
-
-    @Override
-    public Socket createSocket(InetAddress host, int port)
-      throws IOException
-    {
-      return fireOffThread(host,port,null,-1);
-    }
-    
-    @Override
-    public Socket createSocket(String host, int port, InetAddress localHost, int localPort)
-      throws IOException, UnknownHostException
-    {
-      return fireOffThread(InetAddress.getByName(host),port,localHost,localPort);
-    }
-    
-    @Override
-    public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort)
-      throws IOException
-    {
-      return fireOffThread(address,port,localAddress,localPort);
-    }
-    
-    @Override
-    public Socket createSocket(Socket s, String host, int port, boolean autoClose)
-      throws IOException
-    {
-      // Socket's already open
-      return wrappedFactory.createSocket(s,host,port,autoClose);
-    }
-    
-    @Override
-    public String[] getDefaultCipherSuites()
-    {
-      return wrappedFactory.getDefaultCipherSuites();
-    }
-    
-    @Override
-    public String[] getSupportedCipherSuites()
-    {
-      return wrappedFactory.getSupportedCipherSuites();
-    }
-    
-    protected Socket fireOffThread(InetAddress address, int port, InetAddress localHost, int localPort)
-      throws IOException
-    {
-      SocketCreateThread thread = new SocketCreateThread(wrappedFactory,address,port,localHost,localPort);
-      thread.start();
-      try
-      {
-        // Wait for thread to complete for only a certain amount of time!
-        thread.join(connectTimeoutMilliseconds);
-        // If join() times out, then the thread is going to still be alive.
-        if (thread.isAlive())
-        {
-          // Kill the thread - not that this will necessarily work, but we need to try
-          thread.interrupt();
-          throw new ConnectTimeoutException("Secure connection timed out");
-        }
-        // The thread terminated.  Throw an error if there is one, otherwise return the result.
-        Throwable t = thread.getException();
-        if (t != null)
-        {
-          if (t instanceof java.net.SocketTimeoutException)
-            throw (java.net.SocketTimeoutException)t;
-          else if (t instanceof ConnectTimeoutException)
-            throw (ConnectTimeoutException)t;
-          else if (t instanceof InterruptedIOException)
-            throw (InterruptedIOException)t;
-          else if (t instanceof IOException)
-            throw (IOException)t;
-          else if (t instanceof Error)
-            throw (Error)t;
-          else if (t instanceof RuntimeException)
-            throw (RuntimeException)t;
-          throw new Error("Received an unexpected exception: "+t.getMessage(),t);
-        }
-        return thread.getResult();
-      }
-      catch (InterruptedException e)
-      {
-        throw new InterruptedIOException("Interrupted: "+e.getMessage());
-      }
-
-    }
-    
-  }
-  
-  /** Create a secure socket in a thread, so that we can "give up" after a while if the socket fails to connect.
-  */
-  protected static class SocketCreateThread extends Thread
-  {
-    // Socket factory
-    protected javax.net.ssl.SSLSocketFactory socketFactory;
-    protected InetAddress host;
-    protected int port;
-    protected InetAddress clientHost;
-    protected int clientPort;
-
-    // The return socket
-    protected Socket rval = null;
-    // The return error
-    protected Throwable throwable = null;
-
-    /** Create the thread */
-    public SocketCreateThread(javax.net.ssl.SSLSocketFactory socketFactory,
-      InetAddress host,
-      int port,
-      InetAddress clientHost,
-      int clientPort)
-    {
-      this.socketFactory = socketFactory;
-      this.host = host;
-      this.port = port;
-      this.clientHost = clientHost;
-      this.clientPort = clientPort;
-      setDaemon(true);
-    }
-
-    public void run()
-    {
-      try
-      {
-        if (clientHost == null)
-          rval = socketFactory.createSocket(host,port);
-        else
-          rval = socketFactory.createSocket(host,port,clientHost,clientPort);
-      }
-      catch (Throwable e)
-      {
-        throwable = e;
-      }
-    }
-
-    public Throwable getException()
-    {
-      return throwable;
-    }
-
-    public Socket getResult()
-    {
-      return rval;
-    }
-  }
 
 }

Modified: manifoldcf/branches/CONNECTORS-723/connectors/webcrawler/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/webcrawler/ThrottledFetcher.java
URL: http://svn.apache.org/viewvc/manifoldcf/branches/CONNECTORS-723/connectors/webcrawler/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/webcrawler/ThrottledFetcher.java?rev=1495813&r1=1495812&r2=1495813&view=diff
==============================================================================
--- manifoldcf/branches/CONNECTORS-723/connectors/webcrawler/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/webcrawler/ThrottledFetcher.java (original)
+++ manifoldcf/branches/CONNECTORS-723/connectors/webcrawler/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/webcrawler/ThrottledFetcher.java Sun Jun 23 13:08:42 2013
@@ -21,6 +21,7 @@ package org.apache.manifoldcf.crawler.co
 import org.apache.manifoldcf.core.interfaces.*;
 import org.apache.manifoldcf.core.common.DeflateInputStream;
 import org.apache.manifoldcf.core.common.XThreadInputStream;
+import org.apache.manifoldcf.core.common.InterruptibleSocketFactory;
 import org.apache.manifoldcf.agents.interfaces.*;
 import org.apache.manifoldcf.crawler.interfaces.*;
 import org.apache.manifoldcf.crawler.system.Logging;
@@ -2259,178 +2260,6 @@ public class ThrottledFetcher
     }
   }
 
-  /** SSL Socket factory which wraps another socket factory but allows timeout on socket
-  * creation.
-  */
-  protected static class InterruptibleSocketFactory extends javax.net.ssl.SSLSocketFactory
-  {
-    protected final javax.net.ssl.SSLSocketFactory wrappedFactory;
-    protected final long connectTimeoutMilliseconds;
-    
-    public InterruptibleSocketFactory(javax.net.ssl.SSLSocketFactory wrappedFactory, long connectTimeoutMilliseconds)
-    {
-      this.wrappedFactory = wrappedFactory;
-      this.connectTimeoutMilliseconds = connectTimeoutMilliseconds;
-    }
-
-    @Override
-    public Socket createSocket()
-      throws IOException
-    {
-      // Socket isn't open
-      return wrappedFactory.createSocket();
-    }
-    
-    @Override
-    public Socket createSocket(String host, int port)
-      throws IOException, UnknownHostException
-    {
-      return fireOffThread(InetAddress.getByName(host),port,null,-1);
-    }
-
-    @Override
-    public Socket createSocket(InetAddress host, int port)
-      throws IOException
-    {
-      return fireOffThread(host,port,null,-1);
-    }
-    
-    @Override
-    public Socket createSocket(String host, int port, InetAddress localHost, int localPort)
-      throws IOException, UnknownHostException
-    {
-      return fireOffThread(InetAddress.getByName(host),port,localHost,localPort);
-    }
-    
-    @Override
-    public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort)
-      throws IOException
-    {
-      return fireOffThread(address,port,localAddress,localPort);
-    }
-    
-    @Override
-    public Socket createSocket(Socket s, String host, int port, boolean autoClose)
-      throws IOException
-    {
-      // Socket's already open
-      return wrappedFactory.createSocket(s,host,port,autoClose);
-    }
-    
-    @Override
-    public String[] getDefaultCipherSuites()
-    {
-      return wrappedFactory.getDefaultCipherSuites();
-    }
-    
-    @Override
-    public String[] getSupportedCipherSuites()
-    {
-      return wrappedFactory.getSupportedCipherSuites();
-    }
-    
-    protected Socket fireOffThread(InetAddress address, int port, InetAddress localHost, int localPort)
-      throws IOException
-    {
-      SocketCreateThread thread = new SocketCreateThread(wrappedFactory,address,port,localHost,localPort);
-      thread.start();
-      try
-      {
-        // Wait for thread to complete for only a certain amount of time!
-        thread.join(connectTimeoutMilliseconds);
-        // If join() times out, then the thread is going to still be alive.
-        if (thread.isAlive())
-        {
-          // Kill the thread - not that this will necessarily work, but we need to try
-          thread.interrupt();
-          throw new ConnectTimeoutException("Secure connection timed out");
-        }
-        // The thread terminated.  Throw an error if there is one, otherwise return the result.
-        Throwable t = thread.getException();
-        if (t != null)
-        {
-          if (t instanceof java.net.SocketTimeoutException)
-            throw (java.net.SocketTimeoutException)t;
-          else if (t instanceof ConnectTimeoutException)
-            throw (ConnectTimeoutException)t;
-          else if (t instanceof InterruptedIOException)
-            throw (InterruptedIOException)t;
-          else if (t instanceof IOException)
-            throw (IOException)t;
-          else if (t instanceof Error)
-            throw (Error)t;
-          else if (t instanceof RuntimeException)
-            throw (RuntimeException)t;
-          throw new Error("Received an unexpected exception: "+t.getMessage(),t);
-        }
-        return thread.getResult();
-      }
-      catch (InterruptedException e)
-      {
-        throw new InterruptedIOException("Interrupted: "+e.getMessage());
-      }
-
-    }
-    
-  }
-  
-  /** Create a secure socket in a thread, so that we can "give up" after a while if the socket fails to connect.
-  */
-  protected static class SocketCreateThread extends Thread
-  {
-    // Socket factory
-    protected javax.net.ssl.SSLSocketFactory socketFactory;
-    protected InetAddress host;
-    protected int port;
-    protected InetAddress clientHost;
-    protected int clientPort;
-
-    // The return socket
-    protected Socket rval = null;
-    // The return error
-    protected Throwable throwable = null;
-
-    /** Create the thread */
-    public SocketCreateThread(javax.net.ssl.SSLSocketFactory socketFactory,
-      InetAddress host,
-      int port,
-      InetAddress clientHost,
-      int clientPort)
-    {
-      this.socketFactory = socketFactory;
-      this.host = host;
-      this.port = port;
-      this.clientHost = clientHost;
-      this.clientPort = clientPort;
-      setDaemon(true);
-    }
-
-    public void run()
-    {
-      try
-      {
-        if (clientHost == null)
-          rval = socketFactory.createSocket(host,port);
-        else
-          rval = socketFactory.createSocket(host,port,clientHost,clientPort);
-      }
-      catch (Throwable e)
-      {
-        throwable = e;
-      }
-    }
-
-    public Throwable getException()
-    {
-      return throwable;
-    }
-
-    public Socket getResult()
-    {
-      return rval;
-    }
-  }
-
   /** Class to override browser compatibility to make it not check cookie paths.  See CONNECTORS-97.
   */
   protected static class LaxBrowserCompatSpec extends BrowserCompatSpec

Modified: manifoldcf/branches/CONNECTORS-723/connectors/wiki/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/wiki/WikiConnector.java
URL: http://svn.apache.org/viewvc/manifoldcf/branches/CONNECTORS-723/connectors/wiki/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/wiki/WikiConnector.java?rev=1495813&r1=1495812&r2=1495813&view=diff
==============================================================================
--- manifoldcf/branches/CONNECTORS-723/connectors/wiki/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/wiki/WikiConnector.java (original)
+++ manifoldcf/branches/CONNECTORS-723/connectors/wiki/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/wiki/WikiConnector.java Sun Jun 23 13:08:42 2013
@@ -57,6 +57,9 @@ import org.apache.http.message.BasicHead
 import org.apache.http.client.params.ClientPNames;
 import org.apache.http.client.HttpRequestRetryHandler;
 import org.apache.http.protocol.HttpContext;
+import org.apache.http.conn.scheme.Scheme;
+import org.apache.http.conn.ssl.SSLSocketFactory;
+import org.apache.http.conn.ssl.AllowAllHostnameVerifier;
 
 import org.apache.http.conn.ConnectTimeoutException;
 import org.apache.http.client.CircularRedirectException;
@@ -181,13 +184,20 @@ public class WikiConnector extends org.a
       
       baseURL = protocol + "://" + server + ((portString!=null)?":" + portString:"") + path + "/api.php?format=xml&";
 
+      int socketTimeout = 900000;
+      int connectionTimeout = 300000;
+
+      javax.net.ssl.SSLSocketFactory httpsSocketFactory = KeystoreManagerFactory.getTrustingSecureSocketFactory();
+      SSLSocketFactory myFactory = new SSLSocketFactory(new InterruptibleSocketFactory(httpsSocketFactory,connectionTimeout),
+        new AllowAllHostnameVerifier());
+      Scheme myHttpsProtocol = new Scheme("https", 443, myFactory);
+
       // Set up connection manager
       PoolingClientConnectionManager localConnectionManager = new PoolingClientConnectionManager();
       localConnectionManager.setMaxTotal(1);
       connectionManager = localConnectionManager;
-
-      int socketTimeout = 900000;
-      int connectionTimeout = 300000;
+      // Set up protocol registry
+      connectionManager.getSchemeRegistry().register(myHttpsProtocol);
 
       BasicHttpParams params = new BasicHttpParams();
       params.setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE,true);

Added: manifoldcf/branches/CONNECTORS-723/framework/core/src/main/java/org/apache/manifoldcf/core/common/InterruptibleSocketFactory.java
URL: http://svn.apache.org/viewvc/manifoldcf/branches/CONNECTORS-723/framework/core/src/main/java/org/apache/manifoldcf/core/common/InterruptibleSocketFactory.java?rev=1495813&view=auto
==============================================================================
--- manifoldcf/branches/CONNECTORS-723/framework/core/src/main/java/org/apache/manifoldcf/core/common/InterruptibleSocketFactory.java (added)
+++ manifoldcf/branches/CONNECTORS-723/framework/core/src/main/java/org/apache/manifoldcf/core/common/InterruptibleSocketFactory.java Sun Jun 23 13:08:42 2013
@@ -0,0 +1,196 @@
+/* $Id$ */
+
+/**
+* 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.
+*/
+package org.apache.manifoldcf.core.common;
+
+import java.io.*;
+import java.net.*;
+
+import org.apache.http.conn.ConnectTimeoutException;
+
+/** SSL Socket factory which wraps another socket factory but allows timeout on socket
+* creation.
+*/
+public class InterruptibleSocketFactory extends javax.net.ssl.SSLSocketFactory
+{
+  protected final javax.net.ssl.SSLSocketFactory wrappedFactory;
+  protected final long connectTimeoutMilliseconds;
+    
+  public InterruptibleSocketFactory(javax.net.ssl.SSLSocketFactory wrappedFactory, long connectTimeoutMilliseconds)
+  {
+    this.wrappedFactory = wrappedFactory;
+    this.connectTimeoutMilliseconds = connectTimeoutMilliseconds;
+  }
+
+  @Override
+  public Socket createSocket()
+    throws IOException
+  {
+    // Socket isn't open
+    return wrappedFactory.createSocket();
+  }
+    
+  @Override
+  public Socket createSocket(String host, int port)
+    throws IOException, UnknownHostException
+  {
+    return fireOffThread(InetAddress.getByName(host),port,null,-1);
+  }
+
+  @Override
+  public Socket createSocket(InetAddress host, int port)
+    throws IOException
+  {
+    return fireOffThread(host,port,null,-1);
+  }
+    
+  @Override
+  public Socket createSocket(String host, int port, InetAddress localHost, int localPort)
+    throws IOException, UnknownHostException
+  {
+    return fireOffThread(InetAddress.getByName(host),port,localHost,localPort);
+  }
+    
+  @Override
+  public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort)
+    throws IOException
+  {
+    return fireOffThread(address,port,localAddress,localPort);
+  }
+    
+  @Override
+  public Socket createSocket(Socket s, String host, int port, boolean autoClose)
+    throws IOException
+  {
+    // Socket's already open
+    return wrappedFactory.createSocket(s,host,port,autoClose);
+  }
+    
+  @Override
+  public String[] getDefaultCipherSuites()
+  {
+    return wrappedFactory.getDefaultCipherSuites();
+  }
+    
+  @Override
+  public String[] getSupportedCipherSuites()
+  {
+    return wrappedFactory.getSupportedCipherSuites();
+  }
+    
+  protected Socket fireOffThread(InetAddress address, int port, InetAddress localHost, int localPort)
+    throws IOException
+  {
+    SocketCreateThread thread = new SocketCreateThread(wrappedFactory,address,port,localHost,localPort);
+    thread.start();
+    try
+    {
+      // Wait for thread to complete for only a certain amount of time!
+      thread.join(connectTimeoutMilliseconds);
+      // If join() times out, then the thread is going to still be alive.
+      if (thread.isAlive())
+      {
+        // Kill the thread - not that this will necessarily work, but we need to try
+        thread.interrupt();
+        throw new ConnectTimeoutException("Secure connection timed out");
+      }
+      // The thread terminated.  Throw an error if there is one, otherwise return the result.
+      Throwable t = thread.getException();
+      if (t != null)
+      {
+        if (t instanceof java.net.SocketTimeoutException)
+          throw (java.net.SocketTimeoutException)t;
+        else if (t instanceof ConnectTimeoutException)
+          throw (ConnectTimeoutException)t;
+        else if (t instanceof InterruptedIOException)
+          throw (InterruptedIOException)t;
+        else if (t instanceof IOException)
+          throw (IOException)t;
+        else if (t instanceof Error)
+          throw (Error)t;
+        else if (t instanceof RuntimeException)
+          throw (RuntimeException)t;
+        throw new Error("Received an unexpected exception: "+t.getMessage(),t);
+      }
+      return thread.getResult();
+    }
+    catch (InterruptedException e)
+    {
+      throw new InterruptedIOException("Interrupted: "+e.getMessage());
+    }
+
+  }
+
+  /** Create a secure socket in a thread, so that we can "give up" after a while if the socket fails to connect.
+  */
+  protected static class SocketCreateThread extends Thread
+  {
+    // Socket factory
+    protected javax.net.ssl.SSLSocketFactory socketFactory;
+    protected InetAddress host;
+    protected int port;
+    protected InetAddress clientHost;
+    protected int clientPort;
+
+    // The return socket
+    protected Socket rval = null;
+    // The return error
+    protected Throwable throwable = null;
+
+    /** Create the thread */
+    public SocketCreateThread(javax.net.ssl.SSLSocketFactory socketFactory,
+      InetAddress host,
+      int port,
+      InetAddress clientHost,
+      int clientPort)
+    {
+      this.socketFactory = socketFactory;
+      this.host = host;
+      this.port = port;
+      this.clientHost = clientHost;
+      this.clientPort = clientPort;
+      setDaemon(true);
+    }
+
+    public void run()
+    {
+      try
+      {
+        if (clientHost == null)
+          rval = socketFactory.createSocket(host,port);
+        else
+          rval = socketFactory.createSocket(host,port,clientHost,clientPort);
+      }
+      catch (Throwable e)
+      {
+        throwable = e;
+      }
+    }
+
+    public Throwable getException()
+    {
+      return throwable;
+    }
+
+    public Socket getResult()
+    {
+      return rval;
+    }
+  }
+
+}

Propchange: manifoldcf/branches/CONNECTORS-723/framework/core/src/main/java/org/apache/manifoldcf/core/common/InterruptibleSocketFactory.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: manifoldcf/branches/CONNECTORS-723/framework/core/src/main/java/org/apache/manifoldcf/core/common/InterruptibleSocketFactory.java
------------------------------------------------------------------------------
    svn:keywords = Id