Java 类org.apache.http.protocol.BasicHttpContext 实例源码

项目:ProBOT    文件:ClientAuthentication2.java   
public static void main(String... args) throws IOException {
    DefaultHttpClient httpclient = new DefaultHttpClient();
    List<String> authpref = new ArrayList<String>();
    authpref.add(AuthPolicy.NTLM);
    httpclient.getParams().setParameter(AuthPNames.TARGET_AUTH_PREF, authpref);
    NTCredentials creds = new NTCredentials("abhisheks", "abhiProJul17", "", "");
    httpclient.getCredentialsProvider().setCredentials(AuthScope.ANY, creds);
    HttpHost target = new HttpHost("apps.prorigo.com", 80, "http");

    // Make sure the same context is used to execute logically related requests
    HttpContext localContext = new BasicHttpContext();
    // Execute a cheap method first. This will trigger NTLM authentication
    HttpGet httpget = new HttpGet("/conference/Booking");
    HttpResponse response = httpclient.execute(target, httpget, localContext);
    HttpEntity entity = response.getEntity();
    System.out.println(EntityUtils.toString(entity));
}
项目:ProBOT    文件:ClientAuthentication.java   
public static void main(String... args) throws IOException {
    DefaultHttpClient httpclient = new DefaultHttpClient();
    List<String> authpref = new ArrayList<String>();
    authpref.add(AuthPolicy.NTLM);
    httpclient.getParams().setParameter(AuthPNames.TARGET_AUTH_PREF, authpref);
    NTCredentials creds = new NTCredentials("abhisheks", "abhiProJul17", "", "");
    httpclient.getCredentialsProvider().setCredentials(AuthScope.ANY, creds);
    HttpHost target = new HttpHost("apps.prorigo.com", 80, "http");

    // Make sure the same context is used to execute logically related requests
    HttpContext localContext = new BasicHttpContext();
    // Execute a cheap method first. This will trigger NTLM authentication
    HttpGet httpget = new HttpGet("/conference/Booking");
    HttpResponse response = httpclient.execute(target, httpget, localContext);
    HttpEntity entity = response.getEntity();
    System.out.println(EntityUtils.toString(entity));
}
项目:q-mail    文件:WebDavStore.java   
public QMailHttpClient getHttpClient() throws MessagingException {
    if (httpClient == null) {
        httpClient = httpClientFactory.create();
        // Disable automatic redirects on the http client.
        httpClient.getParams().setBooleanParameter("http.protocol.handle-redirects", false);

        // Setup a cookie store for forms-based authentication.
        httpContext = new BasicHttpContext();
        authCookies = new BasicCookieStore();
        httpContext.setAttribute(ClientContext.COOKIE_STORE, authCookies);

        SchemeRegistry reg = httpClient.getConnectionManager().getSchemeRegistry();
        try {
            Scheme s = new Scheme("https", new WebDavSocketFactory(hostname, 443), 443);
            reg.register(s);
        } catch (NoSuchAlgorithmException nsa) {
            Timber.e(nsa, "NoSuchAlgorithmException in getHttpClient");
            throw new MessagingException("NoSuchAlgorithmException in getHttpClient: ", nsa);
        } catch (KeyManagementException kme) {
            Timber.e(kme, "KeyManagementException in getHttpClient");
            throw new MessagingException("KeyManagementException in getHttpClient: ", kme);
        }
    }
    return httpClient;
}
项目:JInsight    文件:RequestExecutorBasedClientInstrumentationTest.java   
public HttpResponse execute(HttpRequest request) throws IOException, HttpException {
  HttpParams params = new BasicHttpParams();
  HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);

  HttpProcessor processor = new ImmutableHttpProcessor(new RequestContent());

  HttpRequestExecutor executor = new HttpRequestExecutor();

  HttpContext context = new BasicHttpContext(null);
  context.setAttribute(ExecutionContext.HTTP_CONNECTION, connection);

  if (!connection.isOpen()) {
    Socket socket = new Socket(address.getAddress(), address.getPort());
    connection.bind(socket, params);
  }

  context.setAttribute(ExecutionContext.HTTP_REQUEST, request);
  request.setParams(params);
  executor.preProcess(request, processor, context);
  HttpResponse response = executor.execute(request, connection, context);
  executor.postProcess(response, processor, context);

  return response;
}
项目:lams    文件:AbstractHttpClient.java   
protected HttpContext createHttpContext() {
    HttpContext context = new BasicHttpContext();
    context.setAttribute(
            ClientContext.SCHEME_REGISTRY,
            getConnectionManager().getSchemeRegistry());
    context.setAttribute(
            ClientContext.AUTHSCHEME_REGISTRY,
            getAuthSchemes());
    context.setAttribute(
            ClientContext.COOKIESPEC_REGISTRY,
            getCookieSpecs());
    context.setAttribute(
            ClientContext.COOKIE_STORE,
            getCookieStore());
    context.setAttribute(
            ClientContext.CREDS_PROVIDER,
            getCredentialsProvider());
    return context;
}
项目:java-logging    文件:OutboundRequestLoggingInterceptorTest.java   
@Test
public void logsRequestAndResponseFields() {
    HttpContext context = new BasicHttpContext();
    context.setAttribute(HTTP_TARGET_HOST, "http://www.google.com");

    OutboundRequestLoggingInterceptor interceptor = new OutboundRequestLoggingInterceptor(new FakeClock(20));

    interceptor.process(new BasicHttpRequest("GET", "/something"), context);
    interceptor.process(new BasicHttpResponse(new BasicStatusLine(ANY_PROTOCOL, 200, "any")), context);

    Map<String, Object> fields = new ConcurrentHashMap<>();
    fields.put("requestMethod", "GET");
    fields.put("requestURI", "http://www.google.com/something");
    testAppender.assertEvent(0, INFO, "Outbound request start", appendEntries(fields));

    fields.put("responseTime", 20L);
    fields.put("responseCode", 200);
    testAppender.assertEvent(1, INFO, "Outbound request finish", appendEntries(fields));
}
项目:java-logging    文件:OutboundRequestLoggingInterceptorTest.java   
@Test
public void allowEmptyConstructorToBuildDefaultClock() {
    testAppender.clearEvents();

    HttpContext context = new BasicHttpContext();
    context.setAttribute(HTTP_TARGET_HOST, "http://www.google.com");

    OutboundRequestLoggingInterceptor interceptor = new OutboundRequestLoggingInterceptor();

    interceptor.process(new BasicHttpRequest("GET", "/something"), context);
    interceptor.process(new BasicHttpResponse(new BasicStatusLine(ANY_PROTOCOL, 200, "any")), context);

    assertThat(testAppender.getEvents()).extracting("message")
        .contains("Outbound request start", Index.atIndex(0))
        .contains("Outbound request finish", Index.atIndex(1));
}
项目:java-apache-httpclient    文件:TracingHttpClientBuilderTest.java   
@Test
public void testManualParentSpan() throws IOException {
    MockSpan parent = mockTracer.buildSpan("parent")
            .startManual();

    {
        ActiveSpan parentSpan = mockTracer.buildSpan("parent")
                .startActive();

        HttpContext context = new BasicHttpContext();
        context.setAttribute(Constants.PARENT_CONTEXT, parent.context());

        CloseableHttpClient client = clientBuilder.build();
        client.execute(new HttpGet(serverUrl("/echo/a")), context);
    }

    List<MockSpan> mockSpans = mockTracer.finishedSpans();
    Assert.assertEquals(2, mockSpans.size());

    Assert.assertEquals(parent.context().traceId(), mockSpans.get(1).context().traceId());
    Assert.assertEquals(parent.context().spanId(), mockSpans.get(1).parentId());

    assertLocalSpan(mockSpans.get(1));
}
项目:CurseSync    文件:CurseAPI.java   
@Nullable
private String getModSlug0(int id)
{
    try
    {
        log.debug("Getting mod slug from server...");
        URI uri = getURI(CURSEFORGE_URL, String.format(PROJECT_PATH, id), null);
        HttpGet request = new HttpGet(uri.toURL().toString());
        HttpContext context = new BasicHttpContext();
        HttpResponse response = http.execute(request, context);
        EntityUtils.consume(response.getEntity());
        HttpUriRequest currentReq = (HttpUriRequest) context.getAttribute(HttpCoreContext.HTTP_REQUEST);
        HttpHost currentHost = (HttpHost) context.getAttribute(HttpCoreContext.HTTP_TARGET_HOST);
        String currentUrl = (currentReq.getURI().isAbsolute()) ? currentReq.getURI().toString() : (currentHost.toURI() + currentReq.getURI());
        Splitter splitter = Splitter.on('/').omitEmptyStrings();
        List<String> pathParts = splitter.splitToList(currentUrl);
        return pathParts.get(pathParts.size() - 1);
    }
    catch (Exception e)
    {
        log.error("Failed to perform request from CurseForge site.", e);
        return null;
    }
}
项目:jeeves    文件:StatefullRestTemplate.java   
StatefullRestTemplate(HttpContext httpContext) {
    super();
    HttpClient httpClient = HttpClientBuilder.create().build();
    this.httpContext = httpContext == null ? new BasicHttpContext() : httpContext;
    StatefullHttpComponentsClientHttpRequestFactory statefullHttpComponentsClientHttpRequestFactory
            = new StatefullHttpComponentsClientHttpRequestFactory(httpClient, httpContext);
    super.setRequestFactory(statefullHttpComponentsClientHttpRequestFactory);
    List<HttpMessageConverter<?>> converters = this.getMessageConverters();
    for (HttpMessageConverter<?> converter : converters) {
        if (converter instanceof MappingJackson2HttpMessageConverter) {
            List<MediaType> mediaTypes = converter.getSupportedMediaTypes();
            List<MediaType> newMediaTypes = new ArrayList<>(mediaTypes);
            newMediaTypes.add(MediaType.TEXT_HTML);
            ((MappingJackson2HttpMessageConverter) converter).setSupportedMediaTypes(newMediaTypes);
        }
    }
}
项目:helium    文件:FileDownloader.java   
private byte[] downloadHTTPfile_post(String formToDownloadLocation, List<NameValuePair> params) throws IOException, NullPointerException, URISyntaxException {
    BasicHttpContext localContext = new BasicHttpContext();

      LOG.info("Mimic WebDriver cookie state: " + this.mimicWebDriverCookieState);
      if (this.mimicWebDriverCookieState) {
          localContext.setAttribute(ClientContext.COOKIE_STORE, mimicCookieState(this.driver.manage().getCookies()));
      }

      HttpPost httppost = new HttpPost(formToDownloadLocation);
      HttpParams httpRequestParameters = httppost.getParams();
      httpRequestParameters.setParameter(ClientPNames.HANDLE_REDIRECTS, this.followRedirects);
      httppost.setParams(httpRequestParameters);
      httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));

      LOG.info("Sending POST request for: " + httppost.getURI());
      @SuppressWarnings("resource")
HttpResponse response = new DefaultHttpClient().execute(httppost, localContext);
      this.httpStatusOfLastDownloadAttempt = response.getStatusLine().getStatusCode();
      LOG.info("HTTP GET request status: " + this.httpStatusOfLastDownloadAttempt);

      byte[] file = IOUtils.toByteArray(response.getEntity().getContent());
      response.getEntity().getContent().close();
      return file;
  }
项目:purecloud-iot    文件:TestSystemDefaultRoutePlanner.java   
@Test
public void testProxy() throws Exception {

    final InetAddress ia = InetAddress.getByAddress(new byte[] {
        (byte)127, (byte)0, (byte)0, (byte)1
    });
    final InetSocketAddress isa1 = new InetSocketAddress(ia, 11111);
    final InetSocketAddress isa2 = new InetSocketAddress(ia, 22222);

    final List<Proxy> proxies = new ArrayList<Proxy>(2);
    proxies.add(new Proxy(Proxy.Type.HTTP, isa1));
    proxies.add(new Proxy(Proxy.Type.HTTP, isa2));

    Mockito.when(proxySelector.select(new URI("http://somehost:80"))).thenReturn(proxies);

    final HttpHost target = new HttpHost("somehost", 80, "http");
    final HttpRequest request =
        new BasicHttpRequest("GET", "/", HttpVersion.HTTP_1_1);

    final HttpContext context = new BasicHttpContext();
    final HttpRoute route = routePlanner.determineRoute(target, request, context);

    Assert.assertEquals(target, route.getTargetHost());
    Assert.assertEquals(2, route.getHopCount());
    Assert.assertEquals(isa1.getPort(), route.getProxyHost().getPort());
}
项目:codePay    文件:HttpClientUtil.java   
/**
 * 向指定URL发送POST方法的数据请求.
 *
 * @param url     发送请求的URL
 * @param params  请求参数k1=v1&k2=v2
 * @param encode  编码格式
 * @param isProxy 是否使用代理
 * @return
 */
public static String sendPost(String url, String params, String encode, boolean isProxy) {
    SafeHttpClient httpClient = HttpClientFactory.getHttpClient(isProxy);
    HttpPost httpReq = new HttpPost(url);
    try {
        if (StringUtils.isNotBlank(params)) {
            // 构造最简单的字符串数据
            StringEntity reqEntity = new StringEntity(params);
            reqEntity.setContentType("application/x-www-form-urlencoded");
            // 设置请求的数据
            httpReq.setEntity(reqEntity);
        }
        ResponseHandler<String> responseHandler = new SimpleResponseHandler(encode);
        return httpClient.execute(httpReq, responseHandler, new BasicHttpContext());
    } catch (Exception e) {
        LOGGER.error("POST请求远程地址失败,url:{},params:{},encode:{},isProxy:{},Exception:{}", url, params, encode,
                isProxy, ExceptionUtil.getException(e));
        httpReq.abort();
        httpClient.closeExpiredConnections();
    }
    return null;
}
项目:codePay    文件:HttpClientUtil.java   
/**
 * 获取远程数据bytes
 *
 * @param url     发送请求的URL
 * @param isProxy 是否使用代理
 * @return
 */
public static byte[] sendGetBytes(String url, boolean isProxy) {
    SafeHttpClient httpClient = HttpClientFactory.getHttpClient(isProxy);
    HttpUriRequest httpReq = new HttpGet(url);
    try {
        HttpContext context = new BasicHttpContext();
        ResponseHandler<byte[]> responseHandler = new BytesResponseHandler();
        return httpClient.execute(httpReq, responseHandler, context);
    } catch (Exception e) {
        LOGGER.error("sendGetBytes请求远程地址失败,url:{},encode:{},Exception:{}", url, isProxy,
                ExceptionUtil.getException(e));
        httpReq.abort();
        httpClient.closeExpiredConnections();
    }
    return null;
}
项目:CuiMarket    文件:HttpHelper.java   
/** 执行网络访问 */
private static HttpResult execute(String url, HttpRequestBase requestBase) {
    boolean isHttps = url.startsWith("https://");//判断是否需要采用https
    AbstractHttpClient httpClient = HttpClientFactory.create(isHttps);
    HttpContext httpContext = new SyncBasicHttpContext(new BasicHttpContext());
    HttpRequestRetryHandler retryHandler = httpClient.getHttpRequestRetryHandler();//获取重试机制
    int retryCount = 0;
    boolean retry = true;
    while (retry) {
        try {
            HttpResponse response = httpClient.execute(requestBase, httpContext);//访问网络
            if (response != null) {
                return new HttpResult(response, httpClient, requestBase);
            }
        } catch (Exception e) {
            IOException ioException = new IOException(e.getMessage());
            retry = retryHandler.retryRequest(ioException, ++retryCount, httpContext);//把错误异常交给重试机制,以判断是否需要采取从事
            LogUtils.e(e);
        }
    }
    return null;
}
项目:eSDK_EC_SDK_Java    文件:RestfulAdapterImplHttpClient.java   
private synchronized void checkLocalContext()
{
    if (null != sdkProtocolAdatperCustProvider && null != target)
    {
        // Create AuthCache instance
        AuthCache authCache = new BasicAuthCache();
        // Generate DIGEST scheme object, initialize it and add it to the local auth cache
        String authType = (String) ThreadLocalHolder.get().getEntities().get("AuthType");
        if ("Basic".equals(authType))
        {
            LOGGER.debug("authentication type: basic");
        }
        else
        {
            DigestScheme digestAuth = new DigestScheme();
            digestAuth.overrideParamter("nc", String.valueOf(serverNounceCount++));
            digestAuth.overrideParamter("cnonce", UUID.randomUUID().toString().replaceAll("-", ""));
            digestAuth.overrideParamter("qop", "auth");
            authCache.put(target, digestAuth);
        }

        // Add AuthCache to the execution context
        localContext = new BasicHttpContext();
        localContext.setAttribute(ClientContext.AUTH_CACHE, authCache);
    }
}
项目:eSDK_EC_SDK_Java    文件:RestfulAdapterImplHttpClientHTLS.java   
private synchronized void checkLocalContext()
{
    if (null != sdkProtocolAdatperCustProvider && null != target)
    {
        // Create AuthCache instance
        AuthCache authCache = new BasicAuthCache();
        // Generate DIGEST scheme object, initialize it and add it to the local auth cache
        String authType = (String)ThreadLocalHolder.get().getEntities().get("AuthType");
        if ("Basic".equals(authType))
        {
            LOGGER.debug("authentication type: basic");
        }
        else
        {
            DigestScheme digestAuth = new DigestScheme();
            digestAuth.overrideParamter("nc", String.valueOf(serverNounceCount++));
            digestAuth.overrideParamter("cnonce", UUID.randomUUID().toString().replaceAll("-", ""));
            digestAuth.overrideParamter("qop", "auth");
            authCache.put(target, digestAuth);
        }

        // Add AuthCache to the execution context
        localContext = new BasicHttpContext();
        localContext.setAttribute(ClientContext.AUTH_CACHE, authCache);
    }
}
项目:purecloud-iot    文件:TestSSLSocketFactory.java   
@Test
public void testBasicSSL() throws Exception {
    this.server = ServerBootstrap.bootstrap()
            .setServerInfo(LocalServerTestBase.ORIGIN)
            .setSslContext(SSLTestContexts.createServerSSLContext())
            .create();
    this.server.start();

    final HttpContext context = new BasicHttpContext();
    final TestX509HostnameVerifier hostVerifier = new TestX509HostnameVerifier();
    final SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(
            SSLTestContexts.createClientSSLContext(), hostVerifier);
    final Socket socket = socketFactory.createSocket(context);
    final InetSocketAddress remoteAddress = new InetSocketAddress("localhost", this.server.getLocalPort());
    final HttpHost target = new HttpHost("localhost", this.server.getLocalPort(), "https");
    final SSLSocket sslSocket = (SSLSocket) socketFactory.connectSocket(0, socket, target, remoteAddress, null, context);
    try {
        final SSLSession sslsession = sslSocket.getSession();

        Assert.assertNotNull(sslsession);
        Assert.assertTrue(hostVerifier.isFired());
    } finally {
        sslSocket.close();
    }
}
项目:K9-MailClient    文件:WebDavStore.java   
public WebDavHttpClient getHttpClient() throws MessagingException {
    if (mHttpClient == null) {
        mHttpClient = mHttpClientFactory.create();
        // Disable automatic redirects on the http client.
        mHttpClient.getParams().setBooleanParameter("http.protocol.handle-redirects", false);

        // Setup a cookie store for forms-based authentication.
        mContext = new BasicHttpContext();
        mAuthCookies = new BasicCookieStore();
        mContext.setAttribute(ClientContext.COOKIE_STORE, mAuthCookies);

        SchemeRegistry reg = mHttpClient.getConnectionManager().getSchemeRegistry();
        try {
            Scheme s = new Scheme("https", new WebDavSocketFactory(mHost, 443), 443);
            reg.register(s);
        } catch (NoSuchAlgorithmException nsa) {
            Log.e(LOG_TAG, "NoSuchAlgorithmException in getHttpClient: " + nsa);
            throw new MessagingException("NoSuchAlgorithmException in getHttpClient: " + nsa);
        } catch (KeyManagementException kme) {
            Log.e(LOG_TAG, "KeyManagementException in getHttpClient: " + kme);
            throw new MessagingException("KeyManagementException in getHttpClient: " + kme);
        }
    }
    return mHttpClient;
}
项目:eSDK_IVS_Java    文件:RestfulAdapterImplHttpClient.java   
private synchronized void checkLocalContext()
{
    if (null != sdkProtocolAdatperCustProvider && null != target)
    {
        // Create AuthCache instance
        AuthCache authCache = new BasicAuthCache();
        // Generate DIGEST scheme object, initialize it and add it to the local auth cache
        String authType = (String) ThreadLocalHolder.get().getEntities().get("AuthType");
        if ("Basic".equals(authType))
        {
            LOGGER.debug("authentication type: basic");
        }
        else
        {
            DigestScheme digestAuth = new DigestScheme();
            digestAuth.overrideParamter("nc", String.valueOf(serverNounceCount++));
            digestAuth.overrideParamter("cnonce", UUID.randomUUID().toString().replaceAll("-", ""));
            digestAuth.overrideParamter("qop", "auth");
            authCache.put(target, digestAuth);
        }

        // Add AuthCache to the execution context
        localContext = new BasicHttpContext();
        localContext.setAttribute(ClientContext.AUTH_CACHE, authCache);
    }
}
项目:eSDK_IVS_Java    文件:RestfulAdapterImplHttpClientHTLS.java   
private synchronized void checkLocalContext()
{
    if (null != sdkProtocolAdatperCustProvider && null != target)
    {
        // Create AuthCache instance
        AuthCache authCache = new BasicAuthCache();
        // Generate DIGEST scheme object, initialize it and add it to the local auth cache
        String authType = (String)ThreadLocalHolder.get().getEntities().get("AuthType");
        if ("Basic".equals(authType))
        {
            LOGGER.debug("authentication type: basic");
        }
        else
        {
            DigestScheme digestAuth = new DigestScheme();
            digestAuth.overrideParamter("nc", String.valueOf(serverNounceCount++));
            digestAuth.overrideParamter("cnonce", UUID.randomUUID().toString().replaceAll("-", ""));
            digestAuth.overrideParamter("qop", "auth");
            authCache.put(target, digestAuth);
        }

        // Add AuthCache to the execution context
        localContext = new BasicHttpContext();
        localContext.setAttribute(ClientContext.AUTH_CACHE, authCache);
    }
}
项目:wingtips    文件:WingtipsHttpClientBuilderTest.java   
@Before
public void beforeMethod() {
    resetTracing();

    spanRecorder = new SpanRecorder();
    Tracer.getInstance().addSpanLifecycleListener(spanRecorder);

    builder = new WingtipsHttpClientBuilder();

    requestMock = mock(HttpRequest.class);
    responseMock = mock(HttpResponse.class);
    httpContext = new BasicHttpContext();
    requestLineMock = mock(RequestLine.class);

    method = "GET";
    uri = "http://localhost:4242/foo/bar";

    doReturn(requestLineMock).when(requestMock).getRequestLine();
    doReturn(method).when(requestLineMock).getMethod();
    doReturn(uri).when(requestLineMock).getUri();
    doReturn(HTTP_1_1).when(requestLineMock).getProtocolVersion();
}
项目:wingtips    文件:WingtipsApacheHttpClientInterceptorTest.java   
@Before
public void beforeMethod() {
    resetTracing();

    interceptor = new WingtipsApacheHttpClientInterceptor();

    requestMock = mock(HttpRequest.class);
    responseMock = mock(HttpResponse.class);
    httpContext = new BasicHttpContext();
    requestLineMock = mock(RequestLine.class);

    method = "GET";
    uri = "http://localhost:4242/foo/bar";

    doReturn(requestLineMock).when(requestMock).getRequestLine();
    doReturn(method).when(requestLineMock).getMethod();
    doReturn(uri).when(requestLineMock).getUri();
}
项目:CSReporter-API-Java    文件:UserAgent.java   
private RawResponse openRaw(HttpUriRequest request) {
    try {
        CloseableHttpResponse response
                = httpClient.execute(request, new BasicHttpContext());

        try {
            HttpEntity entity = response.getEntity();
            return (entity != null)
                    ? new RawResponse(IOUtils.toString(entity.getContent()),
                            response.getStatusLine().getStatusCode())
                    : null;
        } finally {
            response.close();
        }

    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
项目:playground    文件:BasicAuthConfigurator.java   
@Override
    public BasicHttpContext createContext(HttpHost targetHost) {

        final AuthCache authCache = new BasicAuthCache();

        final BasicScheme basicAuth = new BasicScheme();
        authCache.put(targetHost, basicAuth);

        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();

        // OPTIMIZED
        credentialsProvider
                .setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));

        // NON-OPTIMIZED
//        credentialsProvider.setCredentials(
//                new AuthScope(targetHost.getHostName(), targetHost.getPort()),
//                new UsernamePasswordCredentials(username, password));

        final BasicHttpContext context = new BasicHttpContext();
        context.setAttribute(HttpClientContext.AUTH_CACHE, authCache);
        context.setAttribute(HttpClientContext.CREDS_PROVIDER, credentialsProvider);

        return context;
    }
项目:playground    文件:UseCookieConfigurator.java   
@Override
public BasicHttpContext createContext(final HttpHost targetHost) {

    final CookieStore cookieStore = new BasicCookieStore();

    final BasicClientCookie clientCookie =
            new BasicClientCookie(cookie.getName(), cookie.getValue());
    clientCookie.setDomain(targetHost.getHostName());
    clientCookie.setPath("/");
    cookieStore.addCookie(clientCookie);

    final BasicHttpContext context = new BasicHttpContext();
    context.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);

    return context;
}
项目:playground    文件:SystemUserConfigurator.java   
@Override
public BasicHttpContext createContext(final HttpHost targetHost) {

    BasicHttpContext context = CACHED_CONTEXTS.get(targetHost);

    if (context == null) {

        log.trace("creating and configuring new HttpContext for system user");
        context = basicAuthConfigurator.createContext(targetHost);

        final CookieStore cookieStore = new BasicCookieStore();
        context.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);

        log.trace("caching HttpContext for system user");
        CACHED_CONTEXTS.put(targetHost, context);
    }
    else {

        log.trace("using cached HttpContext for system user");
    }

    return context;
}
项目:purecloud-iot    文件:TestBasicScheme.java   
@Test
public void testBasicAuthenticationWith88591Chars() throws Exception {
    final int[] germanChars = { 0xE4, 0x2D, 0xF6, 0x2D, 0xFc };
    final StringBuilder buffer = new StringBuilder();
    for (final int germanChar : germanChars) {
        buffer.append((char)germanChar);
    }

    final UsernamePasswordCredentials creds = new UsernamePasswordCredentials("dh", buffer.toString());
    final BasicScheme authscheme = new BasicScheme(Consts.ISO_8859_1);

    final HttpRequest request = new BasicHttpRequest("GET", "/");
    final HttpContext context = new BasicHttpContext();
    final Header header = authscheme.authenticate(creds, request, context);
    Assert.assertEquals("Basic ZGg65C32Lfw=", header.getValue());
}
项目:purecloud-iot    文件:AbstractHttpClient.java   
protected HttpContext createHttpContext() {
    final HttpContext context = new BasicHttpContext();
    context.setAttribute(
            ClientContext.SCHEME_REGISTRY,
            getConnectionManager().getSchemeRegistry());
    context.setAttribute(
            ClientContext.AUTHSCHEME_REGISTRY,
            getAuthSchemes());
    context.setAttribute(
            ClientContext.COOKIESPEC_REGISTRY,
            getCookieSpecs());
    context.setAttribute(
            ClientContext.COOKIE_STORE,
            getCookieStore());
    context.setAttribute(
            ClientContext.CREDS_PROVIDER,
            getCredentialsProvider());
    return context;
}
项目:purecloud-iot    文件:TestDefaultRedirectStrategy.java   
@Test
public void testGetRedirectRequestForTemporaryRedirect() throws Exception {
    final DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
    final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1,
            HttpStatus.SC_TEMPORARY_REDIRECT, "Temporary Redirect");
    response.addHeader("Location", "http://localhost/stuff");
    final HttpContext context1 = new BasicHttpContext();
    final HttpUriRequest redirect1 = redirectStrategy.getRedirect(
            new HttpTrace("http://localhost/"), response, context1);
    Assert.assertEquals("TRACE", redirect1.getMethod());
    final HttpContext context2 = new BasicHttpContext();
    final HttpPost httppost = new HttpPost("http://localhost/");
    final HttpEntity entity = new BasicHttpEntity();
    httppost.setEntity(entity);
    final HttpUriRequest redirect2 = redirectStrategy.getRedirect(
            httppost, response, context2);
    Assert.assertEquals("POST", redirect2.getMethod());
    Assert.assertTrue(redirect2 instanceof HttpEntityEnclosingRequest);
    Assert.assertSame(entity, ((HttpEntityEnclosingRequest) redirect2).getEntity());
}
项目:purecloud-iot    文件:TestSSLSocketFactory.java   
@Test(expected=SSLException.class)
public void testSSLTrustVerification() throws Exception {
    this.server = ServerBootstrap.bootstrap()
            .setServerInfo(LocalServerTestBase.ORIGIN)
            .setSslContext(SSLTestContexts.createServerSSLContext())
            .create();
    this.server.start();

    final HttpContext context = new BasicHttpContext();
    // Use default SSL context
    final SSLContext defaultsslcontext = SSLContexts.createDefault();

    final SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(defaultsslcontext,
            NoopHostnameVerifier.INSTANCE);

    final Socket socket = socketFactory.createSocket(context);
    final InetSocketAddress remoteAddress = new InetSocketAddress("localhost", this.server.getLocalPort());
    final HttpHost target = new HttpHost("localhost", this.server.getLocalPort(), "https");
    final SSLSocket sslSocket = (SSLSocket) socketFactory.connectSocket(0, socket, target, remoteAddress, null, context);
    sslSocket.close();
}
项目:purecloud-iot    文件:TestDefaultRedirectStrategy.java   
@Test
public void testGetRedirectRequest() throws Exception {
    final DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
    final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1,
            HttpStatus.SC_SEE_OTHER, "Redirect");
    response.addHeader("Location", "http://localhost/stuff");
    final HttpContext context1 = new BasicHttpContext();
    final HttpUriRequest redirect1 = redirectStrategy.getRedirect(
            new HttpGet("http://localhost/"), response, context1);
    Assert.assertEquals("GET", redirect1.getMethod());
    final HttpContext context2 = new BasicHttpContext();
    final HttpUriRequest redirect2 = redirectStrategy.getRedirect(
            new HttpPost("http://localhost/"), response, context2);
    Assert.assertEquals("GET", redirect2.getMethod());
    final HttpContext context3 = new BasicHttpContext();
    final HttpUriRequest redirect3 = redirectStrategy.getRedirect(
            new HttpHead("http://localhost/"), response, context3);
    Assert.assertEquals("HEAD", redirect3.getMethod());
}
项目:purecloud-iot    文件:TestDigestScheme.java   
@Test
public void testDigestAuthenticationQopAuthOrAuthIntNonRepeatableEntity() throws Exception {
    final String challenge = "Digest realm=\"realm1\", nonce=\"f2a3f18799759d4f1a1c068b92b573cb\", " +
            "qop=\"auth,auth-int\"";
    final Header authChallenge = new BasicHeader(AUTH.WWW_AUTH, challenge);
    final HttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest("Post", "/");
    request.setEntity(new InputStreamEntity(new ByteArrayInputStream(new byte[] {'a'}), -1));
    final Credentials cred = new UsernamePasswordCredentials("username","password");
    final DigestScheme authscheme = new DigestScheme();
    final HttpContext context = new BasicHttpContext();
    authscheme.processChallenge(authChallenge);
    final Header authResponse = authscheme.authenticate(cred, request, context);

    Assert.assertEquals("Post:/", authscheme.getA2());

    final Map<String, String> table = parseAuthResponse(authResponse);
    Assert.assertEquals("username", table.get("username"));
    Assert.assertEquals("realm1", table.get("realm"));
    Assert.assertEquals("/", table.get("uri"));
    Assert.assertEquals("auth", table.get("qop"));
    Assert.assertEquals("f2a3f18799759d4f1a1c068b92b573cb", table.get("nonce"));
}
项目:purecloud-iot    文件:TestRequestDefaultHeaders.java   
@Test
public void testDefaultHeaders() throws Exception {
    final HttpRequest request = new BasicHttpRequest("GET", "/");
    request.addHeader("custom", "stuff");
    final List<Header> defheaders = new ArrayList<Header>();
    defheaders.add(new BasicHeader("custom", "more stuff"));
    final HttpContext context = new BasicHttpContext();

    final HttpRequestInterceptor interceptor = new RequestDefaultHeaders(defheaders);
    interceptor.process(request, context);
    final Header[] headers = request.getHeaders("custom");
    Assert.assertNotNull(headers);
    Assert.assertEquals(2, headers.length);
    Assert.assertEquals("stuff", headers[0].getValue());
    Assert.assertEquals("more stuff", headers[1].getValue());
}
项目:purecloud-iot    文件:TestHttpAuthenticator.java   
@Before
public void setUp() throws Exception {
    this.defltAuthStrategy = Mockito.mock(AuthenticationStrategy.class);
    this.authState = new AuthState();
    this.authScheme = Mockito.mock(ContextAwareAuthScheme.class);
    Mockito.when(this.authScheme.getSchemeName()).thenReturn("Basic");
    Mockito.when(this.authScheme.isComplete()).thenReturn(Boolean.TRUE);
    this.context = new BasicHttpContext();
    this.defaultHost = new HttpHost("localhost", 80);
    this.context.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, this.defaultHost);
    this.credentials = Mockito.mock(Credentials.class);
    this.credentialsProvider = new BasicCredentialsProvider();
    this.credentialsProvider.setCredentials(AuthScope.ANY, this.credentials);
    this.context.setAttribute(HttpClientContext.CREDS_PROVIDER, this.credentialsProvider);
    this.authSchemeRegistry = RegistryBuilder.<AuthSchemeProvider>create()
        .register("basic", new BasicSchemeFactory())
        .register("digest", new DigestSchemeFactory())
        .register("ntlm", new NTLMSchemeFactory()).build();
    this.context.setAttribute(HttpClientContext.AUTHSCHEME_REGISTRY, this.authSchemeRegistry);
    this.authCache = Mockito.mock(AuthCache.class);
    this.context.setAttribute(HttpClientContext.AUTH_CACHE, this.authCache);
    this.httpAuthenticator = new HttpAuthenticator();
}
项目:purecloud-iot    文件:TestDigestScheme.java   
@Test
public void testDigestAuthentication() throws Exception {
    final String challenge = "Digest realm=\"realm1\", nonce=\"f2a3f18799759d4f1a1c068b92b573cb\"";
    final Header authChallenge = new BasicHeader(AUTH.WWW_AUTH, challenge);
    final HttpRequest request = new BasicHttpRequest("Simple", "/");
    final Credentials cred = new UsernamePasswordCredentials("username","password");
    final DigestScheme authscheme = new DigestScheme();
    final HttpContext context = new BasicHttpContext();
    authscheme.processChallenge(authChallenge);
    final Header authResponse = authscheme.authenticate(cred, request, context);

    final Map<String, String> table = parseAuthResponse(authResponse);
    Assert.assertEquals("username", table.get("username"));
    Assert.assertEquals("realm1", table.get("realm"));
    Assert.assertEquals("/", table.get("uri"));
    Assert.assertEquals("f2a3f18799759d4f1a1c068b92b573cb", table.get("nonce"));
    Assert.assertEquals("e95a7ddf37c2eab009568b1ed134f89a", table.get("response"));
}
项目:purecloud-iot    文件:TestDigestScheme.java   
@Test
public void testDigestAuthenticationWithSHA() throws Exception {
    final String challenge = "Digest realm=\"realm1\", " +
            "nonce=\"f2a3f18799759d4f1a1c068b92b573cb\", " +
            "algorithm=SHA";
    final Header authChallenge = new BasicHeader(AUTH.WWW_AUTH, challenge);
    final HttpRequest request = new BasicHttpRequest("Simple", "/");
    final Credentials cred = new UsernamePasswordCredentials("username","password");
    final HttpContext context = new BasicHttpContext();
    final DigestScheme authscheme = new DigestScheme();
    authscheme.processChallenge(authChallenge);
    final Header authResponse = authscheme.authenticate(cred, request, context);

    final Map<String, String> table = parseAuthResponse(authResponse);
    Assert.assertEquals("username", table.get("username"));
    Assert.assertEquals("realm1", table.get("realm"));
    Assert.assertEquals("/", table.get("uri"));
    Assert.assertEquals("f2a3f18799759d4f1a1c068b92b573cb", table.get("nonce"));
    Assert.assertEquals("8769e82e4e28ecc040b969562b9050580c6d186d", table.get("response"));
}
项目:purecloud-iot    文件:TestDigestScheme.java   
@Test
public void testDigestAuthenticationWithQueryStringInDigestURI() throws Exception {
    final String challenge = "Digest realm=\"realm1\", nonce=\"f2a3f18799759d4f1a1c068b92b573cb\"";
    final Header authChallenge = new BasicHeader(AUTH.WWW_AUTH, challenge);
    final HttpRequest request = new BasicHttpRequest("Simple", "/?param=value");
    final Credentials cred = new UsernamePasswordCredentials("username","password");
    final HttpContext context = new BasicHttpContext();
    final DigestScheme authscheme = new DigestScheme();
    authscheme.processChallenge(authChallenge);
    final Header authResponse = authscheme.authenticate(cred, request, context);

    final Map<String, String> table = parseAuthResponse(authResponse);
    Assert.assertEquals("username", table.get("username"));
    Assert.assertEquals("realm1", table.get("realm"));
    Assert.assertEquals("/?param=value", table.get("uri"));
    Assert.assertEquals("f2a3f18799759d4f1a1c068b92b573cb", table.get("nonce"));
    Assert.assertEquals("a847f58f5fef0bc087bcb9c3eb30e042", table.get("response"));
}
项目:spring-cloud-skipper    文件:PreemptiveBasicAuthHttpComponentsClientHttpRequestFactory.java   
@Override
protected HttpContext createHttpContext(HttpMethod httpMethod, URI uri) {
    final AuthCache authCache = new BasicAuthCache();

    final BasicScheme basicAuth = new BasicScheme();
    authCache.put(host, basicAuth);

    final BasicHttpContext localcontext = new BasicHttpContext();
    localcontext.setAttribute(HttpClientContext.AUTH_CACHE, authCache);
    return localcontext;
}
项目:InComb    文件:TxtContentReader.java   
/**
 * Returns the content read form content-source.
 * @return read {@link Content}
 */
@Override
public Content[] read(final FetchHistory lastFetch) {

    final CloseableHttpClient httpClient = HttpClients.createDefault();
    final HttpGet httpget = new HttpGet(contentSource.getUrl());
    final HttpContext context = new BasicHttpContext();

    CloseableHttpResponse response = null;
    String stringRead = null;

    try {
        try {
            LOGGER.info("Loading uri: " + httpget.getURI());
            response = httpClient.execute(httpget, context);
            final HttpEntity entity = response.getEntity();

            if (entity != null) {
                stringRead = IOUtils.toString(entity.getContent());
                LOGGER.info("Read {} bytes from: {}", stringRead.getBytes().length, httpget.getURI());
            }

        } finally {
            CloseUtil.close(response);
            CloseUtil.close(httpClient);
        }

    } catch (final Exception e) {
        LOGGER.warn("Error occurred while reading text document: " + contentSource.getUrl());
    }

    return new Content[] { createContentObject(stringRead) };
}