Advertisement
Guest User

Funambol FakeSocketFactory.java

a guest
Dec 26th, 2011
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.17 KB | None | 0 0
  1. package com.funambol.platform;
  2. import java.io.IOException;
  3. import java.net.InetAddress;
  4. import java.net.InetSocketAddress;
  5. import java.net.Socket;
  6. import java.net.UnknownHostException;
  7.  
  8. import javax.net.ssl.SSLContext;
  9. import javax.net.ssl.SSLSocket;
  10. import javax.net.ssl.TrustManager;
  11.  
  12. import org.apache.http.conn.ConnectTimeoutException;
  13. import org.apache.http.conn.scheme.LayeredSocketFactory;
  14. import org.apache.http.conn.scheme.SocketFactory;
  15. import org.apache.http.params.HttpConnectionParams;
  16. import org.apache.http.params.HttpParams;
  17.  
  18. public class FakeSocketFactory implements SocketFactory, LayeredSocketFactory {
  19.         private String certKey = null;
  20.     private SSLContext sslcontext = null;
  21.  
  22.     public FakeSocketFactory(String certKey){
  23.         this.certKey = certKey;
  24.     }
  25.     public FakeSocketFactory() { }
  26.  
  27.     private static SSLContext createEasySSLContext(String certKey) throws IOException {
  28.             try {
  29.                     SSLContext context = SSLContext.getInstance("TLS");
  30.                     context.init(null, new TrustManager[] { new FakeTrustManager(certKey) }, null);
  31.                     return context;
  32.             } catch (Exception e) {
  33.                     throw new IOException(e.getMessage());
  34.             }
  35.     }
  36.  
  37.     private SSLContext getSSLContext() throws IOException {
  38.             if (this.sslcontext == null) {
  39.                     this.sslcontext = createEasySSLContext(this.certKey);
  40.             }
  41.             return this.sslcontext;
  42.     }
  43.  
  44.  
  45.  
  46.         @Override
  47.         public Socket connectSocket(Socket sock, String host, int port,
  48.                         InetAddress localAddress, int localPort, HttpParams params) throws IOException,
  49.                         UnknownHostException, ConnectTimeoutException {
  50.         int connTimeout = HttpConnectionParams.getConnectionTimeout(params);
  51.         int soTimeout = HttpConnectionParams.getSoTimeout(params);
  52.  
  53.         InetSocketAddress remoteAddress = new InetSocketAddress(host, port);
  54.         SSLSocket sslsock = (SSLSocket) ((sock != null) ? sock : createSocket());
  55.  
  56.         if ((localAddress != null) || (localPort > 0)) {
  57.                 // we need to bind explicitly
  58.                 if (localPort < 0) {
  59.                         localPort = 0; // indicates "any"
  60.                 }
  61.                 InetSocketAddress isa = new InetSocketAddress(localAddress,
  62.                                 localPort);
  63.                 sslsock.bind(isa);
  64.         }
  65.  
  66.         sslsock.connect(remoteAddress, connTimeout);
  67.         sslsock.setSoTimeout(soTimeout);
  68.         return sslsock;
  69.         }
  70.  
  71.         @Override
  72.         public Socket createSocket() throws IOException {
  73.                 return getSSLContext().getSocketFactory().createSocket();
  74.         }  
  75.  
  76.         @Override
  77.         public boolean isSecure(Socket arg0) throws IllegalArgumentException {
  78.                 return true;
  79.         }
  80.  
  81.         @Override
  82.         public Socket createSocket(Socket socket, String host, int port, boolean autoClose)
  83.                         throws IOException, UnknownHostException {
  84.                 return getSSLContext().getSocketFactory().createSocket(socket, host, port, autoClose);
  85.         }
  86.  
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement