Package iqq.im.http

Source Code of iqq.im.http.QQSSLSocketFactory$TrustAnyTrustManager

package iqq.im.http;

import iqq.im.core.QQConstants;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.UnknownHostException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.SocketFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.params.HttpConnectionParams;

/**
* <p>QQSSLSocketFactory class.</p>
*/
public class QQSSLSocketFactory {
  static {
    System.out.println(">>>>in MySSLSocketFactory>>");
  }
  /**
   * <p>Constructor for QQSSLSocketFactory.</p>
   */
  public QQSSLSocketFactory() {
  }
 
  private SSLContext sslcontext = null;

  private SSLContext createSSLContext() {
    SSLContext sslcontext = null;
    try {
      sslcontext = SSLContext.getInstance("SSL");
      sslcontext.init(null,
          new TrustManager[] { new TrustAnyTrustManager() },
          new java.security.SecureRandom());
    } catch (NoSuchAlgorithmException e) {
      e.printStackTrace();
    } catch (KeyManagementException e) {
      e.printStackTrace();
    }
    return sslcontext;
  }

  /**
   * <p>getSSLContext.</p>
   *
   * @return a {@link javax.net.ssl.SSLContext} object.
   */
  public SSLContext getSSLContext() {
    if (this.sslcontext == null) {
      this.sslcontext = createSSLContext();
    }
    return this.sslcontext;
  }

  /**
   * <p>createSocket.</p>
   *
   * @param socket a {@link java.net.Socket} object.
   * @param host a {@link java.lang.String} object.
   * @param port a int.
   * @param autoClose a boolean.
   * @throws java.io.IOException if any.
   * @throws java.net.UnknownHostException if any.
   * @return a {@link java.net.Socket} object.
   */
  public Socket createSocket(Socket socket, String host, int port,
      boolean autoClose) throws IOException, UnknownHostException {
    return getSSLContext().getSocketFactory().createSocket(socket, host,
        port, autoClose);
  }

  /**
   * <p>createSocket.</p>
   *
   * @param host a {@link java.lang.String} object.
   * @param port a int.
   * @throws java.io.IOException if any.
   * @throws java.net.UnknownHostException if any.
   * @return a {@link java.net.Socket} object.
   */
  public Socket createSocket(String host, int port) throws IOException,
      UnknownHostException {
    return getSSLContext().getSocketFactory().createSocket(host, port);
  }

  /**
   * <p>createSocket.</p>
   *
   * @param host a {@link java.lang.String} object.
   * @param port a int.
   * @param clientHost a {@link java.net.InetAddress} object.
   * @param clientPort a int.
   * @throws java.io.IOException if any.
   * @throws java.net.UnknownHostException if any.
   * @return a {@link java.net.Socket} object.
   */
  public Socket createSocket(String host, int port, InetAddress clientHost,
      int clientPort) throws IOException, UnknownHostException {
    return getSSLContext().getSocketFactory().createSocket(host, port,
        clientHost, clientPort);
  }

  /**
   * <p>createSocket.</p>
   *
   * @param host a {@link java.lang.String} object.
   * @param port a int.
   * @param localAddress a {@link java.net.InetAddress} object.
   * @param localPort a int.
   * @param params a {@link org.apache.http.params.HttpConnectionParams} object.
   * @throws java.io.IOException if any.
   * @throws java.net.UnknownHostException if any.
   * @throws org.apache.http.conn.ConnectTimeoutException if any.
   * @return a {@link java.net.Socket} object.
   */
  public Socket createSocket(String host, int port, InetAddress localAddress,
      int localPort, HttpConnectionParams params) throws IOException,
      UnknownHostException, ConnectTimeoutException {
    int timeout = QQConstants.HTTP_TIME_OUT;
    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;
    }
  }

  private static class TrustAnyTrustManager implements X509TrustManager {

    public void checkClientTrusted(X509Certificate[] chain, String authType)
        throws CertificateException {
    }

    public void checkServerTrusted(X509Certificate[] chain, String authType)
        throws CertificateException {
    }

    public X509Certificate[] getAcceptedIssuers() {
      return new X509Certificate[] {};
    }
  }
}
TOP

Related Classes of iqq.im.http.QQSSLSocketFactory$TrustAnyTrustManager

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.