类org.apache.http.impl.conn.BasicHttpClientConnectionManager源码实例Demo

下面列出了怎么用org.apache.http.impl.conn.BasicHttpClientConnectionManager的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: netcdf-java   文件: HTTPConnections.java
@Override
public void freeManager(HTTPMethod method) {
  synchronized (this) {
    HttpClientConnectionManager mgr = methodmap.get(method);
    if (mgr == null)
      throw new IllegalStateException();
    mgrmap.remove(mgr, method);
    methodmap.remove(method);
    ((BasicHttpClientConnectionManager) mgr).close();
    if (method.executed) {
      // only decrement if method was executed
      this.actualconnections--;
    }
    if (TRACE)
      System.err.println("HTTPConnections: close connection: " + method.hashCode());
  }
}
 
源代码2 项目: lucene-solr   文件: HttpReplicatorTest.java
@Test  
public void testServerErrors() throws Exception {
  // tests the behaviour of the client when the server sends an error
  // must use BasicClientConnectionManager to test whether the client is closed correctly
  BasicHttpClientConnectionManager conMgr = new BasicHttpClientConnectionManager();
  Replicator replicator = new HttpReplicator(host, port, ReplicationService.REPLICATION_CONTEXT + "/s1", conMgr);
  ReplicationClient client = new ReplicationClient(replicator, new IndexReplicationHandler(handlerIndexDir, null), 
      new PerSessionDirectoryFactory(clientWorkDir));
  
  try {
    publishRevision(5);

    replicationServlet.setRespondWithError(true);
    expectThrows(Exception.class, client::updateNow);
    
    replicationServlet.setRespondWithError(false);
    client.updateNow(); // now it should work
    reopenReader();
    assertEquals(5, Integer.parseInt(reader.getIndexCommit().getUserData().get("ID"), 16));
    
    client.close();
  } finally {
    replicationServlet.setRespondWithError(false);
  }
}
 
源代码3 项目: jasperreports   文件: HttpDataService.java
protected CloseableHttpClient createHttpClient(Map<String, Object> parameters)
{
	HttpClientBuilder clientBuilder = HttpClients.custom();
	
	// single connection
	BasicHttpClientConnectionManager connManager = new BasicHttpClientConnectionManager();
	clientBuilder.setConnectionManager(connManager);
	
	// ignore cookies for now
	RequestConfig requestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.IGNORE_COOKIES).build();
	clientBuilder.setDefaultRequestConfig(requestConfig);
	
	setAuthentication(parameters, clientBuilder);
	
	CloseableHttpClient client = clientBuilder.build();
	return client;
}
 
源代码4 项目: jasperreports   文件: ProcessConnection.java
public ProcessConnection(ProcessDirector director, PhantomJSProcess process)
{
	this.process = process;
	
	HttpClientBuilder clientBuilder = HttpClients.custom();
	
	// single connection
	BasicHttpClientConnectionManager connManager = new BasicHttpClientConnectionManager();
	clientBuilder.setConnectionManager(connManager);
	
	RequestConfig requestConfig = RequestConfig.custom()
			// ignore cookies for now
			.setCookieSpec(CookieSpecs.IGNORE_COOKIES)
			.setSocketTimeout(director.getRequestTimeout()).build();
	clientBuilder.setDefaultRequestConfig(requestConfig);
	
	this.httpClient = clientBuilder.build();
}
 
源代码5 项目: vespa   文件: SimpleHttpClient.java
public SimpleHttpClient(SSLContext sslContext, List<String> enabledProtocols, List<String> enabledCiphers,
                        int listenPort, boolean useCompression) {
    HttpClientBuilder builder = HttpClientBuilder.create();
    if (!useCompression) {
        builder.disableContentCompression();
    }
    if (sslContext != null) {
        SSLConnectionSocketFactory sslConnectionFactory = new SSLConnectionSocketFactory(
                sslContext,
                toArray(enabledProtocols),
                toArray(enabledCiphers),
                new DefaultHostnameVerifier());
        builder.setSSLSocketFactory(sslConnectionFactory);

        Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
                .register("https", sslConnectionFactory)
                .build();
        builder.setConnectionManager(new BasicHttpClientConnectionManager(registry));
        scheme = "https";
    } else {
        scheme = "http";
    }
    this.delegate = builder.build();
    this.listenPort = listenPort;
}
 
源代码6 项目: sql-layer   文件: HttpMonitorVerifySSLIT.java
/**
 * This code sets up the httpclient to accept any SSL certificate. The 
 * SSL certificate generated by the instructions above is not correctly
 * signed, so we need ignore the problem. 
 * This code should not, under any circumstances, be allowed anywhere 
 * the production code. 
 * @return
 */
private CloseableHttpClient createClient () {
    try {
        HttpClientBuilder builder = HttpClientBuilder.create();
        SSLContext ctx = SSLContext.getInstance("TLS");
        ctx.init(null, new TrustManager[]{getTrustManager()}, null);
        SSLConnectionSocketFactory scsf = new SSLConnectionSocketFactory(ctx, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        builder.setSSLSocketFactory(scsf);
        Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
                .register("https", scsf)
                .build();

        HttpClientConnectionManager ccm = new BasicHttpClientConnectionManager(registry);

        builder.setConnectionManager(ccm);
        return builder.build();
    } catch (Exception ex) {
        ex.printStackTrace();
        return null;
    }
}
 
源代码7 项目: Silence   文件: LegacyMmsConnection.java
protected CloseableHttpClient constructHttpClient() throws IOException {
  RequestConfig config = RequestConfig.custom()
                                      .setConnectTimeout(20 * 1000)
                                      .setConnectionRequestTimeout(20 * 1000)
                                      .setSocketTimeout(20 * 1000)
                                      .setMaxRedirects(20)
                                      .build();

  URL                 mmsc          = new URL(apn.getMmsc());
  CredentialsProvider credsProvider = new BasicCredentialsProvider();

  if (apn.hasAuthentication()) {
    credsProvider.setCredentials(new AuthScope(mmsc.getHost(), mmsc.getPort() > -1 ? mmsc.getPort() : mmsc.getDefaultPort()),
                                 new UsernamePasswordCredentials(apn.getUsername(), apn.getPassword()));
  }

  return HttpClients.custom()
                    .setConnectionReuseStrategy(new NoConnectionReuseStrategyHC4())
                    .setRedirectStrategy(new LaxRedirectStrategy())
                    .setUserAgent(SilencePreferences.getMmsUserAgent(context, USER_AGENT))
                    .setConnectionManager(new BasicHttpClientConnectionManager())
                    .setDefaultRequestConfig(config)
                    .setDefaultCredentialsProvider(credsProvider)
                    .build();
}
 
@Test
// @Ignore
// 2.2 IN ARTICLE
public final void whenOpeningLowLevelConnectionWithSocketTimeout_thenNoExceptions() throws InterruptedException, ExecutionException, IOException, HttpException {
    basicConnManager = new BasicHttpClientConnectionManager();
    context = HttpClientContext.create();
    final ConnectionRequest connRequest = basicConnManager.requestConnection(route, null);
    conn = connRequest.get(1000, TimeUnit.SECONDS);
    if (!conn.isOpen()) {
        basicConnManager.connect(conn, route, 1000, context);
    }
    conn.setSocketTimeout(30000);

    assertTrue(conn.getSocketTimeout() == 30000);
    assertTrue(conn.isOpen());
}
 
源代码9 项目: tutorials   文件: RestClientLiveManualTest.java
@Test
public final void givenAcceptingAllCertificates_whenHttpsUrlIsConsumed_thenOk_2() throws GeneralSecurityException {

    final TrustStrategy acceptingTrustStrategy = (cert, authType) -> true;
    final SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy).build();
    final SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);
    final Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory> create()
            .register("https", sslsf)
            .register("http", new PlainConnectionSocketFactory())
            .build();

    final BasicHttpClientConnectionManager connectionManager = new BasicHttpClientConnectionManager(socketFactoryRegistry);
    final CloseableHttpClient httpClient = HttpClients.custom()
            .setSSLSocketFactory(sslsf)
            .setConnectionManager(connectionManager)
            .build();

    final HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
    final ResponseEntity<String> response = new RestTemplate(requestFactory).exchange(urlOverHttps, HttpMethod.GET, null, String.class);
    assertThat(response.getStatusCode().value(), equalTo(200));
}
 
源代码10 项目: act   文件: BingSearchResults.java
public BingSearchResults(String accountKeyFilepath) {
  this.cacheOnly = false;
  this.bingCacheMongoDB = new BingCacheMongoDB(BING_CACHE_HOST, BING_CACHE_MONGO_PORT, BING_CACHE_MONGO_DATABASE);
  this.basicConnManager = new BasicHttpClientConnectionManager();
  try {
    this.accountKey = getAccountKey(accountKeyFilepath);
  } catch (IOException e) {
    String msg = String.format("Bing Searcher could not find account key at %s", accountKeyFilepath);
    LOGGER.error(msg);
    throw new RuntimeException(msg);
  }
}
 
源代码11 项目: BUbiNG   文件: FetchingThread.java
/** Creates a new fetching thread.
 *
 * @param frontier a reference to the {@link Frontier}.
 * @param index  the index of this thread (only for logging purposes).
 */
public FetchingThread(final Frontier frontier, final int index) throws NoSuchAlgorithmException, IllegalArgumentException, IOException {
	setName(this.getClass().getSimpleName() + '-' + index);
	setPriority(Thread.MIN_PRIORITY); // Low priority; there will be thousands of this guys around.
	this.frontier = frontier;

	final BasicHttpClientConnectionManager connManager = new BasicHttpClientConnectionManagerWithAlternateDNS(frontier.rc.dnsResolver);
	connManager.closeIdleConnections(0, TimeUnit.MILLISECONDS);
	connManager.setConnectionConfig(ConnectionConfig.custom().setBufferSize(8 * 1024).build()); // TODO: make this configurable

	cookieStore = new BasicCookieStore();

	final BasicHeader[] headers = {
		new BasicHeader("From", frontier.rc.userAgentFrom),
		new BasicHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.95,text/*;q=0.9,*/*;q=0.8")
	};

	httpClient = HttpClients.custom()
			.setSSLContext(frontier.rc.acceptAllCertificates ? TRUST_ALL_CERTIFICATES_SSL_CONTEXT : TRUST_SELF_SIGNED_SSL_CONTEXT)
			.setConnectionManager(connManager)
			.setConnectionReuseStrategy(frontier.rc.keepAliveTime == 0 ? NoConnectionReuseStrategy.INSTANCE : DefaultConnectionReuseStrategy.INSTANCE)
			.setUserAgent(frontier.rc.userAgent)
			.setDefaultCookieStore(cookieStore)
			.setDefaultHeaders(ObjectArrayList.wrap(headers))
			.build();
  		fetchData = new FetchData(frontier.rc);
}
 
源代码12 项目: Bastion   文件: TestWithProxiedEmbeddedServer.java
@BeforeClass
public static void setupProxying() {
    DnsResolver dnsResolver = prepareProxiedDnsResolver();
    DefaultSchemePortResolver schemePortResolver = prepareSchemePortResolver();
    BasicHttpClientConnectionManager connManager = prepareConnectionManager(dnsResolver, schemePortResolver);
    HttpClient httpClient = prepareHttpClient(connManager);
    originalHttpClient = (HttpClient) Options.getOption(Option.HTTPCLIENT);
    Unirest.setHttpClient(httpClient);
}
 
源代码13 项目: Bastion   文件: TestWithProxiedEmbeddedServer.java
private static BasicHttpClientConnectionManager prepareConnectionManager(DnsResolver dnsResolver, DefaultSchemePortResolver schemePortResolver) {
    return new BasicHttpClientConnectionManager(
            RegistryBuilder.<ConnectionSocketFactory>create()
                    .register("http", PlainConnectionSocketFactory.getSocketFactory())
                    .register("https", SSLConnectionSocketFactory.getSocketFactory())
                    .build(),
            null,
            schemePortResolver,
            dnsResolver
    );
}
 
源代码14 项目: cuba   文件: LogDataProvider.java
/**
* You should call {@link LogDataProvider#obtainUrl()} before
* */
@Override
public InputStream provide() {
    HttpClientConnectionManager connectionManager = new BasicHttpClientConnectionManager();
    HttpClient httpClient = HttpClientBuilder.create()
            .setConnectionManager(connectionManager)
            .build();

    String uri = url + "?s=" + userSessionSource.getUserSession().getId();
    if (downloadFullLog) {
        uri += "&full=true";
    }

    HttpGet httpGet = new HttpGet(uri);

    try {
        HttpResponse httpResponse = httpClient.execute(httpGet);
        int httpStatus = httpResponse.getStatusLine().getStatusCode();
        if (httpStatus == HttpStatus.SC_OK) {
            HttpEntity httpEntity = httpResponse.getEntity();
            if (httpEntity != null) {
                inputStream = httpEntity.getContent();
            } else {
                log.debug("Unable to download log from " + url + "\nHttpEntity is null");

                throw new RuntimeException("Unable to download log from " + url + "\nHttpEntity is null");
            }
        } else {
            log.debug("Unable to download log from " + url + "\n" + httpResponse.getStatusLine());
            throw new RuntimeException("Unable to download log from " + url + "\n" + httpResponse.getStatusLine());
        }
    } catch (IOException e) {
        log.debug("Unable to download log from " + url + "\n" + e);
        throw new RuntimeException(e);
    }

    return inputStream;
}
 
源代码15 项目: rxp-remote-java   文件: HttpUtils.java
/**
 * Get a default HttpClient based on the HttpConfiguration object. If required the defaults can 
 * be altered to meet the requirements of the SDK user. The default client does not use connection 
 * pooling and does not reuse connections. Timeouts for connection and socket are taken from the 
 * {@link HttpConfiguration} object.
 * 
 * @param httpConfiguration
 * @return CloseableHttpClient
 */
public static CloseableHttpClient getDefaultClient(HttpConfiguration httpConfiguration) {

    RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(httpConfiguration.getTimeout())
            .setSocketTimeout(httpConfiguration.getTimeout()).build();

    HttpClientConnectionManager connectionManager = new BasicHttpClientConnectionManager();
    ConnectionReuseStrategy connectionResuseStrategy = new NoConnectionReuseStrategy();

    logger.debug("Creating HttpClient with simple no pooling/no connection reuse default settings.");
    CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(requestConfig).setConnectionManager(connectionManager)
            .setConnectionReuseStrategy(connectionResuseStrategy).build();
    return httpClient;
}
 
源代码16 项目: docker-client   文件: DefaultDockerClient.java
private HttpClientConnectionManager getConnectionManager(Builder builder) {
  if (builder.uri.getScheme().equals(NPIPE_SCHEME)) {
    final BasicHttpClientConnectionManager bm = 
        new BasicHttpClientConnectionManager(getSchemeRegistry(builder));
    return bm;
  } else {
    final PoolingHttpClientConnectionManager cm =
        new PoolingHttpClientConnectionManager(getSchemeRegistry(builder));
    // Use all available connections instead of artificially limiting ourselves to 2 per server.
    cm.setMaxTotal(builder.connectionPoolSize);
    cm.setDefaultMaxPerRoute(cm.getMaxTotal());
    return cm;
  }
}
 
源代码17 项目: incubator-gobblin   文件: AzkabanClient.java
/**
 * Create a {@link CloseableHttpClient} used to communicate with Azkaban server.
 * Derived class can configure different http client by overriding this method.
 *
 * @return A closeable http client.
 */
private CloseableHttpClient createHttpClient() throws AzkabanClientException {
  try {
  // SSLSocketFactory using custom TrustStrategy that ignores warnings about untrusted certificates
  // Self sign SSL
  SSLContextBuilder sslcb = new SSLContextBuilder();
  sslcb.loadTrustMaterial(null, (TrustStrategy) new TrustSelfSignedStrategy());
  SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcb.build());

  HttpClientBuilder builder = HttpClientBuilder.create();
  RequestConfig requestConfig = RequestConfig.copy(RequestConfig.DEFAULT)
        .setSocketTimeout(10000)
        .setConnectTimeout(10000)
        .setConnectionRequestTimeout(10000)
        .build();

    builder.disableCookieManagement()
        .useSystemProperties()
        .setDefaultRequestConfig(requestConfig)
        .setConnectionManager(new BasicHttpClientConnectionManager())
        .setSSLSocketFactory(sslsf);

    return builder.build();
  } catch (Exception e) {
    throw new AzkabanClientException("HttpClient cannot be created", e);
  }
}
 
public B fromConfig(Config config) {
  config = config.withFallback(FALLBACK);
  RequestConfig requestConfig = RequestConfig.copy(RequestConfig.DEFAULT)
                                             .setSocketTimeout(config.getInt(REQUEST_TIME_OUT_MS_KEY))
                                             .setConnectTimeout(config.getInt(CONNECTION_TIME_OUT_MS_KEY))
                                             .setConnectionRequestTimeout(config.getInt(CONNECTION_TIME_OUT_MS_KEY))
                                             .build();

  getHttpClientBuilder().setDefaultRequestConfig(requestConfig);

  if (config.hasPath(STATIC_SVC_ENDPOINT)) {
    try {
      svcEndpoint = Optional.of(new URI(config.getString(AbstractHttpWriterBuilder.STATIC_SVC_ENDPOINT)));
    } catch (URISyntaxException e) {
      throw new RuntimeException(e);
    }
  }

  String connMgrStr = config.getString(HTTP_CONN_MANAGER);
  switch (ConnManager.valueOf(connMgrStr.toUpperCase())) {
    case BASIC:
      httpConnManager = new BasicHttpClientConnectionManager();
      break;
    case POOLING:
      PoolingHttpClientConnectionManager poolingConnMgr = new PoolingHttpClientConnectionManager();
      poolingConnMgr.setMaxTotal(config.getInt(POOLING_CONN_MANAGER_MAX_TOTAL_CONN));
      poolingConnMgr.setDefaultMaxPerRoute(config.getInt(POOLING_CONN_MANAGER_MAX_PER_CONN));
      httpConnManager = poolingConnMgr;
      break;
    default:
      throw new IllegalArgumentException(connMgrStr + " is not supported");
  }
  LOG.info("Using " + httpConnManager.getClass().getSimpleName());
  return typedSelf();
}
 
源代码19 项目: ews-java-api   文件: ExchangeServiceBase.java
private void initializeHttpClient() {
  Registry<ConnectionSocketFactory> registry = createConnectionSocketFactoryRegistry();
  HttpClientConnectionManager httpConnectionManager = new BasicHttpClientConnectionManager(registry);
  AuthenticationStrategy authStrategy = new CookieProcessingTargetAuthenticationStrategy();

  httpClient = HttpClients.custom()
    .setConnectionManager(httpConnectionManager)
    .setTargetAuthenticationStrategy(authStrategy)
    .build();
}
 
public static HttpClientConnectionManager withTimeoutMs(int timeoutMs) {
  BasicHttpClientConnectionManager httpClientConnectionManager = new BasicHttpClientConnectionManager(
      RegistryBuilder.<ConnectionSocketFactory>create()
          .register("http", PlainConnectionSocketFactory.getSocketFactory())
          .register("https", new SSLConnectionSocketFactoryWithTimeout(timeoutMs))
          .build());

  httpClientConnectionManager.setSocketConfig(
      SocketConfig.custom().setSoTimeout(timeoutMs).build());

  return httpClientConnectionManager;
}
 
源代码21 项目: signalfx-java   文件: ServerSentEventsTransport.java
public TransportConnection(SignalFxEndpoint endpoint, int timeoutMs) {
    super(endpoint, timeoutMs, new BasicHttpClientConnectionManager());

    this.transportRequestConfig = RequestConfig.custom().setSocketTimeout(0)
            .setConnectionRequestTimeout(this.requestConfig.getConnectionRequestTimeout())
            .setConnectTimeout(this.requestConfig.getConnectTimeout())
            .setProxy(this.requestConfig.getProxy()).build();

    log.debug("constructed request config: {}", this.transportRequestConfig.toString());
}
 
@Test
// 2.1 IN ARTCLE
public final void whenLowLevelConnectionIsEstablished_thenNoExceptions() throws IOException, HttpException, InterruptedException, ExecutionException {
    basicConnManager = new BasicHttpClientConnectionManager();
    final ConnectionRequest connRequest = basicConnManager.requestConnection(route, null);
    assertTrue(connRequest.get(1000, TimeUnit.SECONDS) != null);
}
 
源代码23 项目: jkube   文件: HttpClientBuilder.java
private static HttpClientConnectionManager getBasicConnectionFactory(String certPath) throws IOException {
    return certPath != null ?
        new BasicHttpClientConnectionManager(getSslFactoryRegistry(certPath)) :
        new BasicHttpClientConnectionManager();
}
 
源代码24 项目: jkube   文件: AbstractNativeClientBuilder.java
@Override
public CloseableHttpClient buildBasicClient() throws IOException {
    BasicHttpClientConnectionManager manager = new BasicHttpClientConnectionManager(registry, null, null, dnsResolver);
    return HttpClients.custom().setConnectionManager(manager).build();
}
 
源代码25 项目: Bastion   文件: TestWithProxiedEmbeddedServer.java
private static CloseableHttpClient prepareHttpClient(BasicHttpClientConnectionManager connManager) {
    return HttpClientBuilder.create()
            .setConnectionManager(connManager)
            .build();
}
 
源代码26 项目: cuba   文件: FileLoaderClientImpl.java
protected InputStream openStreamWithServlet(FileDescriptor fd) throws FileStorageException {
    ClientConfig clientConfig = configuration.getConfig(ClientConfig.class);
    String fileDownloadContext = clientConfig.getFileDownloadContext();

    Object context = serverSelector.initContext();
    String selectedUrl = serverSelector.getUrl(context);
    if (selectedUrl == null) {
        throw new FileStorageException(FileStorageException.Type.IO_EXCEPTION, fd.getName());
    }
    while (true) {
        String url = selectedUrl + fileDownloadContext +
                "?s=" + userSessionSource.getUserSession().getId() +
                "&f=" + fd.getId().toString();

        HttpClientConnectionManager connectionManager = new BasicHttpClientConnectionManager();
        HttpClient httpClient = HttpClientBuilder.create()
                .setConnectionManager(connectionManager)
                .build();

        HttpGet httpGet = new HttpGet(url);

        try {
            HttpResponse httpResponse = httpClient.execute(httpGet);
            int httpStatus = httpResponse.getStatusLine().getStatusCode();
            if (httpStatus == HttpStatus.SC_OK) {
                HttpEntity httpEntity = httpResponse.getEntity();
                if (httpEntity != null) {
                    return httpEntity.getContent();
                } else {
                    log.debug("Unable to download file from {}\nHttpEntity is null", url);
                    selectedUrl = failAndGetNextUrl(context);
                    if (selectedUrl == null) {
                        throw new FileStorageException(FileStorageException.Type.IO_EXCEPTION, fd.getName());
                    }
                }
            } else {
                log.debug("Unable to download file from {}\n{}", url, httpResponse.getStatusLine());
                selectedUrl = failAndGetNextUrl(context);
                if (selectedUrl == null) {
                    throw new FileStorageException(FileStorageException.Type.fromHttpStatus(httpStatus), fd.getName());
                }
            }
        } catch (InterruptedIOException e) {
            log.trace("Downloading has been interrupted");
            throw new FileStorageException(FileStorageException.Type.IO_EXCEPTION, fd.getName(), e);
        } catch (IOException ex) {
            log.debug("Unable to download file from {}\n{}", url, ex);
            selectedUrl = failAndGetNextUrl(context);
            if (selectedUrl == null) {
                throw new FileStorageException(FileStorageException.Type.IO_EXCEPTION, fd.getName(), ex);
            }
        }
    }
}
 
源代码27 项目: cuba   文件: SimpleFileDataProvider.java
@Override
public InputStream provide() {
    if (filePath == null)
        throw new IllegalStateException("File path is null");

    Object context = serverSelector.initContext();
    String selectedUrl = serverSelector.getUrl(context);
    if (selectedUrl == null) {
        throw new RuntimeException(String.format("Unable to download file '%s': no available server URLs", filePath));
    }
    while (true) {
        String url = selectedUrl + fileDownloadContext +
                "?s=" + userSessionSource.getUserSession().getId() +
                "&p=" + URLEncodeUtils.encodeUtf8(filePath);

        HttpClientConnectionManager connectionManager = new BasicHttpClientConnectionManager();
        HttpClient httpClient = HttpClientBuilder.create()
                .setConnectionManager(connectionManager)
                .build();
        HttpGet httpGet = new HttpGet(url);

        try {
            HttpResponse httpResponse = httpClient.execute(httpGet);
            int httpStatus = httpResponse.getStatusLine().getStatusCode();
            if (httpStatus == HttpStatus.SC_OK) {
                HttpEntity httpEntity = httpResponse.getEntity();
                if (httpEntity != null) {
                    inputStream = httpEntity.getContent();
                    break;
                } else {
                    log.debug("Unable to download file from " + url + "\nHttpEntity is null");
                    selectedUrl = failAndGetNextUrl(context);
                    if (selectedUrl == null)
                        throw new RuntimeException(String.format("Unable to download file '%s': HttpEntity is null", filePath));
                }
            } else {
                log.debug("Unable to download file from " + url + "\n" + httpResponse.getStatusLine());
                selectedUrl = failAndGetNextUrl(context);
                if (selectedUrl == null)
                    throw new RuntimeException(String.format("Unable to download file '%s': HTTP status is %d", filePath, httpStatus));
            }
        } catch (IOException ex) {
            log.debug("Unable to download file from " + url + "\n" + ex);
            selectedUrl = failAndGetNextUrl(context);
            if (selectedUrl == null)
                throw new RuntimeException(String.format("Unable to download file '%s'", filePath), ex);
        }
    }

    return inputStream;
}
 
源代码28 项目: nexus-public   文件: CertificateRetriever.java
/**
 * Retrieves certificate chain of specified host:port using https protocol.
 *
 * @param host to get certificate chain from (cannot be null)
 * @param port of host to connect to
 * @return certificate chain
 * @throws Exception Re-thrown from accessing the remote host
 */
public Certificate[] retrieveCertificatesFromHttpsServer(final String host, final int port) throws Exception {
  checkNotNull(host);

  log.info("Retrieving certificate from https://{}:{}", host, port);

  // setup custom connection manager so we can configure SSL to trust-all
  SSLContext sc = SSLContext.getInstance("TLS");
  sc.init(trustStore.getKeyManagers(), new TrustManager[]{ACCEPT_ALL_TRUST_MANAGER}, null);
  SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sc, NoopHostnameVerifier.INSTANCE);
  Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
      .register(HttpSchemes.HTTP, PlainConnectionSocketFactory.getSocketFactory())
      .register(HttpSchemes.HTTPS, sslSocketFactory).build();
  final HttpClientConnectionManager connectionManager = new BasicHttpClientConnectionManager(registry);

  try {
    final AtomicReference<Certificate[]> certificates = new AtomicReference<>();

    HttpClient httpClient = httpClientManager.create(new Customizer()
    {
      @Override
      public void customize(final HttpClientPlan plan) {
        // replace connection-manager with customized version needed to fetch SSL certificates
        plan.getClient().setConnectionManager(connectionManager);

        // add interceptor to grab peer-certificates
        plan.getClient().addInterceptorFirst(new HttpResponseInterceptor()
        {
          @Override
          public void process(final HttpResponse response, final HttpContext context)
              throws HttpException, IOException
          {
            ManagedHttpClientConnection connection =
                HttpCoreContext.adapt(context).getConnection(ManagedHttpClientConnection.class);

            // grab the peer-certificates from the session
            if (connection != null) {
              SSLSession session = connection.getSSLSession();
              if (session != null) {
                certificates.set(session.getPeerCertificates());
              }
            }
          }
        });
      }
    });

    httpClient.execute(new HttpGet("https://" + host + ":" + port));

    return certificates.get();
  }
  finally {
    // shutdown single-use connection manager
    connectionManager.shutdown();
  }
}
 
源代码29 项目: OpenAs2App   文件: HTTPUtil.java
/**
 * Execute a request via HTTP
 *
 * @param method         GET, PUT, POST, DELETE, etc
 * @param url            The remote connection string
 * @param headers        HTTP headers to be sent
 * @param params         Parameters for the get. Can be null.
 * @param inputStream    Source stream for retrieving request data
 * @param options        Any additional options for affecting request behaviour. Can NOT be null.
 * @param noChunkMaxSize The maximum size before chunking would need to be utilised. 0 disables check for chunking
 * @return ResponseWrapper
 * @throws Exception
 */
public static ResponseWrapper execRequest(String method, String url, Enumeration<Header> headers, NameValuePair[] params, InputStream inputStream, Map<String, String> options, long noChunkMaxSize) throws Exception {

    HttpClientBuilder httpBuilder = HttpClientBuilder.create();
    URL urlObj = new URL(url);
    /*
     * httpClient is used for this request only,
     * set a connection manager that manages just one connection.
     */
    if (urlObj.getProtocol().equalsIgnoreCase("https")) {
        /*
         * Note: registration of a custom SSLSocketFactory via httpBuilder.setSSLSocketFactory is ignored when a connection manager is set.
         * The custom SSLSocketFactory needs to be registered together with the connection manager.
         */
        SSLConnectionSocketFactory sslCsf = buildSslFactory(urlObj, options);
        httpBuilder.setConnectionManager(new BasicHttpClientConnectionManager(RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", sslCsf).build()));
    } else {
        httpBuilder.setConnectionManager(new BasicHttpClientConnectionManager());
    }

    RequestBuilder rb = getRequestBuilder(method, urlObj, params, headers);
    RequestConfig.Builder rcBuilder = buildRequestConfig(options);
    setProxyConfig(httpBuilder, rcBuilder, urlObj.getProtocol());
    rb.setConfig(rcBuilder.build());

    if (inputStream != null) {
        if (noChunkMaxSize > 0L) {
            ByteArrayOutputStream bout = new ByteArrayOutputStream();
            long copied = IOUtils.copyLarge(inputStream, bout, 0L, noChunkMaxSize + 1, new byte[8192]);
            if (copied > noChunkMaxSize) {
                throw new IOException("Mime inputstream too big to put in memory (more than " + noChunkMaxSize + " bytes).");
            }
            ByteArrayEntity bae = new ByteArrayEntity(bout.toByteArray(), null);
            rb.setEntity(bae);
        } else {
            InputStreamEntity ise = new InputStreamEntity(inputStream);
            rb.setEntity(ise);
        }
    }
    final HttpUriRequest request = rb.build();

    String httpUser = options.get(HTTPUtil.PARAM_HTTP_USER);
    String httpPwd = options.get(HTTPUtil.PARAM_HTTP_PWD);
    if (httpUser != null) {
        CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(httpUser, httpPwd));
        httpBuilder.setDefaultCredentialsProvider(credentialsProvider);
    }
    BasicHttpContext localcontext = new BasicHttpContext();
    BasicScheme basicAuth = new BasicScheme();
    localcontext.setAttribute("preemptive-auth", basicAuth);
    try (CloseableHttpClient httpClient = httpBuilder.build()) {
        ProfilerStub transferStub = Profiler.startProfile();
        try (CloseableHttpResponse response = httpClient.execute(request, localcontext)) {
            ResponseWrapper resp = new ResponseWrapper(response);
            Profiler.endProfile(transferStub);
            resp.setTransferTimeMs(transferStub.getMilliseconds());
            for (org.apache.http.Header header : response.getAllHeaders()) {
                resp.addHeaderLine(header.toString());
            }
            return resp;
        }
    }
}
 
源代码30 项目: docker-maven-plugin   文件: HttpClientBuilder.java
private static HttpClientConnectionManager getBasicConnectionFactory(String certPath) throws IOException {
    return certPath != null ?
        new BasicHttpClientConnectionManager(getSslFactoryRegistry(certPath)) :
        new BasicHttpClientConnectionManager();
}
 
 类所在包
 类方法
 同包方法