Ejemplo n.º 1
0
 /* (non-Javadoc)
  * @see java.net.URLConnection#connect()
  */
 @Override
 public void connect() throws IOException {
   if (m_client != null) {
     return;
   }
   final HttpParams httpParams = new BasicHttpParams();
   if (m_request != null) {
     int timeout = m_request.getParameterAsInt("timeout");
     if (timeout > 0) {
       HttpConnectionParams.setConnectionTimeout(httpParams, timeout);
       HttpConnectionParams.setSoTimeout(httpParams, timeout);
     }
   }
   m_client = new DefaultHttpClient(httpParams);
   m_client.addRequestInterceptor(new RequestAcceptEncoding());
   m_client.addResponseInterceptor(new ResponseContentEncoding());
   if (m_request != null) {
     int retries = m_request.getParameterAsInt("retries");
     if (retries > 0) {
       m_client.setHttpRequestRetryHandler(
           new DefaultHttpRequestRetryHandler() {
             @Override
             public boolean retryRequest(
                 IOException exception, int executionCount, HttpContext context) {
               if (executionCount <= getRetryCount()
                   && (exception instanceof SocketTimeoutException
                       || exception instanceof ConnectTimeoutException)) {
                 return true;
               }
               return super.retryRequest(exception, executionCount, context);
             }
           });
     }
     String disableSslVerification = m_request.getParameter("disable-ssl-verification");
     if (Boolean.parseBoolean(disableSslVerification)) {
       final SchemeRegistry registry = m_client.getConnectionManager().getSchemeRegistry();
       final Scheme https = registry.getScheme("https");
       try {
         SSLSocketFactory factory =
             new SSLSocketFactory(
                 SSLContext.getInstance(EmptyKeyRelaxedTrustSSLContext.ALGORITHM),
                 SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
         final Scheme lenient = new Scheme(https.getName(), https.getDefaultPort(), factory);
         registry.register(lenient);
       } catch (NoSuchAlgorithmException e) {
         LOG.warn(e.getMessage());
       }
     }
   }
 }
  // non-javadoc, see interface ClientConnectionOperator
  public void updateSecureConnection(
      OperatedClientConnection conn, HttpHost target, HttpContext context, HttpParams params)
      throws IOException {

    if (conn == null) {
      throw new IllegalArgumentException("Connection must not be null.");
    }
    if (target == null) {
      throw new IllegalArgumentException("Target host must not be null.");
    }
    // @@@ is context allowed to be null?
    if (params == null) {
      throw new IllegalArgumentException("Parameters must not be null.");
    }
    if (!conn.isOpen()) {
      throw new IllegalArgumentException("Connection must be open.");
    }

    final Scheme schm = schemeRegistry.getScheme(target.getSchemeName());
    if (!(schm.getSocketFactory() instanceof LayeredSocketFactory)) {
      throw new IllegalArgumentException(
          "Target scheme (" + schm.getName() + ") must have layered socket factory.");
    }

    final LayeredSocketFactory lsf = (LayeredSocketFactory) schm.getSocketFactory();
    final Socket sock;
    try {
      sock =
          lsf.createSocket(
              conn.getSocket(), target.getHostName(), schm.resolvePort(target.getPort()), true);
    } catch (ConnectException ex) {
      throw new HttpHostConnectException(target, ex);
    }
    prepareSocket(sock, context, params);
    conn.update(sock, target, lsf.isSecure(sock), params);
    // @@@ error handling: close the layered socket in case of exception?

  } // updateSecureConnection