Example #1
0
 protected RequestConfig configureRequest(RequestConfig.Builder rcb, Settings settings)
     throws HTTPException {
   // Configure the RequestConfig
   for (Prop key : settings.getKeys()) {
     Object value = settings.getParameter(key);
     boolean tf = (value instanceof Boolean ? (Boolean) value : false);
     if (key == Prop.ALLOW_CIRCULAR_REDIRECTS) {
       rcb.setCircularRedirectsAllowed(tf);
     } else if (key == Prop.HANDLE_REDIRECTS) {
       rcb.setRedirectsEnabled(tf);
       rcb.setRelativeRedirectsAllowed(tf);
     } else if (key == Prop.MAX_REDIRECTS) {
       rcb.setMaxRedirects((Integer) value);
     } else if (key == Prop.SO_TIMEOUT) {
       rcb.setSocketTimeout((Integer) value);
     } else if (key == Prop.CONN_TIMEOUT) {
       rcb.setConnectTimeout((Integer) value);
     } else if (key == Prop.CONN_REQ_TIMEOUT) {
       rcb.setConnectionRequestTimeout((Integer) value);
     } else if (key == Prop.MAX_THREADS) {
       connmgr.setMaxTotal((Integer) value);
       connmgr.setDefaultMaxPerRoute((Integer) value);
     } /* else ignore */
   }
   RequestConfig cfg = rcb.build();
   return cfg;
 }
 /**
  * Sets the timeouts of the HTTP client.
  *
  * @param connectionTimeout timeout until connection established in milliseconds. Zero means no
  *     timeout.
  * @param socketTimeout timeout for waiting for data in milliseconds. Zero means no timeout.
  * @param maxRequests maximum number of connections to a particuar host
  */
 public static void setParams(int connectionTimeout, int socketTimeout, int maxRequests) {
   PrudentHttpEntityResolver.maxRequests = maxRequests;
   PoolingHttpClientConnectionManager phcConnMgr;
   Registry<ConnectionSocketFactory> registry = //
       RegistryBuilder.<ConnectionSocketFactory>create() //
           .register("http", PlainConnectionSocketFactory.getSocketFactory()) //
           .register("https", SSLConnectionSocketFactory.getSocketFactory()) //
           .build();
   HttpClientBuilder builder = HttpClients.custom();
   builder.setRedirectStrategy(new LaxRedirectStrategy());
   builder.setMaxConnPerRoute(maxRequests);
   builder.setMaxConnTotal(200);
   if ("true".equals(System.getProperty("nu.validator.xml.promiscuous-ssl", "false"))) { //
     try {
       SSLContext promiscuousSSLContext =
           new SSLContextBuilder() //
               .loadTrustMaterial(
                   null,
                   new TrustStrategy() {
                     @Override
                     public boolean isTrusted(X509Certificate[] arg0, String arg1)
                         throws CertificateException {
                       return true;
                     }
                   })
               .build();
       builder.setSslcontext(promiscuousSSLContext);
       HostnameVerifier verifier = //
           SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
       SSLConnectionSocketFactory promiscuousSSLConnSocketFactory = //
           new SSLConnectionSocketFactory(promiscuousSSLContext, verifier);
       registry =
           RegistryBuilder.<ConnectionSocketFactory>create() //
               .register("https", promiscuousSSLConnSocketFactory) //
               .register("http", PlainConnectionSocketFactory.getSocketFactory()) //
               .build();
     } catch (KeyManagementException
         | KeyStoreException
         | NoSuchAlgorithmException
         | NumberFormatException e) {
       e.printStackTrace();
     }
   }
   phcConnMgr = new PoolingHttpClientConnectionManager(registry);
   phcConnMgr.setDefaultMaxPerRoute(maxRequests);
   phcConnMgr.setMaxTotal(200);
   builder.setConnectionManager(phcConnMgr);
   RequestConfig.Builder config = RequestConfig.custom();
   config.setCircularRedirectsAllowed(true);
   config.setMaxRedirects(20); // Gecko default
   config.setConnectTimeout(connectionTimeout);
   config.setCookieSpec(CookieSpecs.BEST_MATCH);
   config.setSocketTimeout(socketTimeout);
   client = builder.setDefaultRequestConfig(config.build()).build();
 }