Search in sources :

Example 71 with BasicCookieStore

use of org.apache.http.impl.client.BasicCookieStore in project zm-mailbox by Zimbra.

the class TestProvIDN method testBasicAuth.

@Test
public void testBasicAuth() throws Exception {
    Names.IDNName domainName = new Names.IDNName(makeTestDomainName("basicAuthTest."));
    Domain domain = createDomain(domainName.uName(), domainName.uName());
    Names.IDNName acctName = new Names.IDNName("acct", domainName.uName());
    Account acct = (Account) createTest(EntryType.ACCOUNT, NameType.UNAME, acctName);
    BasicCookieStore initialState = new BasicCookieStore();
    /*
        Cookie authCookie = new Cookie(restURL.getURL().getHost(), "ZM_AUTH_TOKEN", mAuthToken, "/", null, false);
        Cookie sessionCookie = new Cookie(restURL.getURL().getHost(), "JSESSIONID", mSessionId, "/zimbra", null, false);
        initialState.addCookie(authCookie);
        initialState.addCookie(sessionCookie);
        */
    String guestName = acct.getUnicodeName();
    String guestPassword = "test123";
    Credentials loginCredentials = new UsernamePasswordCredentials(guestName, guestPassword);
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(AuthScope.ANY, loginCredentials);
    Registry<AuthSchemeProvider> authSchemeRegistry = RegistryBuilder.<AuthSchemeProvider>create().register(AuthSchemes.BASIC, new BasicSchemeFactory(Consts.UTF_8)).build();
    HttpClientBuilder clientBuilder = HttpClientBuilder.create();
    clientBuilder.setDefaultCookieStore(initialState);
    clientBuilder.setDefaultCredentialsProvider(credsProvider);
    clientBuilder.setDefaultAuthSchemeRegistry(authSchemeRegistry);
    String url = UserServlet.getRestUrl(acct) + "/Calendar";
    System.out.println("REST URL: " + url);
    HttpRequestBase method = new HttpGet(url);
    HttpClient client = clientBuilder.build();
    try {
        HttpResponse response = HttpClientUtil.executeMethod(client, method);
        int respCode = response.getStatusLine().getStatusCode();
        if (respCode != HttpStatus.SC_OK) {
            System.out.println("failed, respCode=" + respCode);
        } else {
            boolean chunked = false;
            boolean textContent = false;
        /*
                 System.out.println("Headers:");
                 System.out.println("--------");
                 for (Header header : method.getRequestHeaders()) {
                     System.out.print("    " + header.toString());
                 }
                 System.out.println();
                 
                 System.out.println("Body:");
                 System.out.println("-----");
                 String respBody = method.getResponseBodyAsString();
                 System.out.println(respBody);
                 */
        }
    } finally {
        // Release the connection.
        method.releaseConnection();
    }
}
Also used : Account(com.zimbra.cs.account.Account) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) BasicSchemeFactory(org.apache.http.impl.auth.BasicSchemeFactory) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) Names(com.zimbra.qa.unittest.prov.Names) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) HttpClient(org.apache.http.client.HttpClient) AuthSchemeProvider(org.apache.http.auth.AuthSchemeProvider) Domain(com.zimbra.cs.account.Domain) Credentials(org.apache.http.auth.Credentials) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) Test(org.junit.Test)

Example 72 with BasicCookieStore

use of org.apache.http.impl.client.BasicCookieStore in project ddf by codice.

the class HttpClientBuilder method get.

@Override
public final org.apache.http.impl.client.HttpClientBuilder get() {
    final org.apache.http.impl.client.HttpClientBuilder httpClientBuilder = HttpClients.custom().setDefaultCookieStore(new BasicCookieStore()).setMaxConnTotal(128).setMaxConnPerRoute(32);
    if (useTls()) {
        String[] defaultProtocols = AccessController.doPrivileged((PrivilegedAction<String[]>) () -> commaSeparatedToArray(System.getProperty(HTTPS_PROTOCOLS)));
        String[] defaultCipherSuites = AccessController.doPrivileged((PrivilegedAction<String[]>) () -> commaSeparatedToArray(System.getProperty(HTTPS_CIPHER_SUITES)));
        httpClientBuilder.setSSLSocketFactory(new SSLConnectionSocketFactory(getSslContext(), defaultProtocols, defaultCipherSuites, SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER));
    }
    if (isConfiguredForBasicAuth()) {
        httpClientBuilder.setDefaultCredentialsProvider(getCredentialsProvider());
        httpClientBuilder.addInterceptorFirst(new PreemptiveAuth(new BasicScheme()));
    }
    return httpClientBuilder;
}
Also used : BasicScheme(org.apache.http.impl.auth.BasicScheme) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) PreemptiveAuth(org.apache.solr.client.solrj.impl.PreemptiveAuth)

Example 73 with BasicCookieStore

use of org.apache.http.impl.client.BasicCookieStore in project ddf by codice.

the class LogoutMessageImpl method sendSamlLogoutRequest.

@Override
public String sendSamlLogoutRequest(LogoutWrapper request, String targetUri, boolean isSoap, @Nullable Cookie cookie) throws IOException, LogoutSecurityException {
    XMLObject xmlObject = isSoap ? SamlProtocol.createSoapMessage((SignableSAMLObject) request.getMessage()) : (XMLObject) request;
    Element requestElement = getElementFromSaml(new LogoutWrapperImpl(xmlObject));
    String requestMessage = DOM2Writer.nodeToString(requestElement);
    try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
        HttpPost post = new HttpPost(targetUri);
        post.addHeader("Cache-Control", "no-cache, no-store");
        post.addHeader("Pragma", "no-cache");
        post.addHeader("SOAPAction", SAML_SOAP_ACTION);
        post.addHeader("Content-Type", "application/soap+xml");
        post.setEntity(new StringEntity(requestMessage, "utf-8"));
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        BasicHttpContext context = new BasicHttpContext();
        if (cookie != null) {
            BasicClientCookie basicClientCookie = new BasicClientCookie(cookie.getName(), cookie.getValue());
            basicClientCookie.setDomain(cookie.getDomain());
            basicClientCookie.setPath(cookie.getPath());
            BasicCookieStore cookieStore = new BasicCookieStore();
            cookieStore.addCookie(basicClientCookie);
            context.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);
        }
        return httpClient.execute(post, responseHandler, context);
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpPost(org.apache.http.client.methods.HttpPost) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) Element(org.w3c.dom.Element) BasicResponseHandler(org.apache.http.impl.client.BasicResponseHandler) XMLObject(org.opensaml.core.xml.XMLObject) BasicClientCookie(org.apache.http.impl.cookie.BasicClientCookie) StringEntity(org.apache.http.entity.StringEntity) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) SignableSAMLObject(org.opensaml.saml.common.SignableSAMLObject)

Example 74 with BasicCookieStore

use of org.apache.http.impl.client.BasicCookieStore in project oxTrust by GluuFederation.

the class ShibbolethReloadService method getHttpClient.

private CloseableHttpClient getHttpClient() {
    final CookieStore cookieStore = new BasicCookieStore();
    final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    final RequestConfig defaultRequestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.DEFAULT).setExpectContinueEnabled(true).build();
    return HttpClients.custom().setConnectionManager(connectionManager).setDefaultCookieStore(cookieStore).setDefaultCredentialsProvider(credentialsProvider).setDefaultRequestConfig(defaultRequestConfig).build();
}
Also used : CookieStore(org.apache.http.client.CookieStore) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) RequestConfig(org.apache.http.client.config.RequestConfig) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider)

Example 75 with BasicCookieStore

use of org.apache.http.impl.client.BasicCookieStore in project cxf by apache.

the class AsyncHTTPConduitFactory method setupNIOClient.

public synchronized void setupNIOClient(HTTPClientPolicy clientPolicy) throws IOReactorException {
    if (client != null) {
        return;
    }
    IOReactorConfig config = IOReactorConfig.custom().setIoThreadCount(ioThreadCount).setSelectInterval(selectInterval).setInterestOpQueued(interestOpQueued).setSoLinger(soLinger).setSoTimeout(soTimeout).setSoKeepAlive(soKeepalive).setTcpNoDelay(tcpNoDelay).build();
    Registry<SchemeIOSessionStrategy> ioSessionFactoryRegistry = RegistryBuilder.<SchemeIOSessionStrategy>create().register("http", NoopIOSessionStrategy.INSTANCE).register("https", SSLIOSessionStrategy.getSystemDefaultStrategy()).build();
    ManagedNHttpClientConnectionFactory connectionFactory = new ManagedNHttpClientConnectionFactory();
    DefaultConnectingIOReactor ioreactor = new DefaultConnectingIOReactor(config);
    connectionManager = new PoolingNHttpClientConnectionManager(ioreactor, connectionFactory, ioSessionFactoryRegistry, DefaultSchemePortResolver.INSTANCE, SystemDefaultDnsResolver.INSTANCE, connectionTTL, TimeUnit.MILLISECONDS);
    connectionManager.setDefaultMaxPerRoute(maxPerRoute);
    connectionManager.setMaxTotal(maxConnections);
    ConnectionConfig connectionConfig = ConnectionConfig.custom().setBufferSize(clientPolicy.getChunkLength() > 0 ? clientPolicy.getChunkLength() : 16332).build();
    connectionManager.setDefaultConnectionConfig(connectionConfig);
    RedirectStrategy redirectStrategy = new RedirectStrategy() {

        public boolean isRedirected(HttpRequest request, HttpResponse response, HttpContext context) throws ProtocolException {
            return false;
        }

        public HttpUriRequest getRedirect(HttpRequest request, HttpResponse response, HttpContext context) throws ProtocolException {
            return null;
        }
    };
    HttpAsyncClientBuilder httpAsyncClientBuilder = HttpAsyncClients.custom().setConnectionManager(connectionManager).setRedirectStrategy(redirectStrategy).setDefaultCookieStore(new BasicCookieStore() {

        private static final long serialVersionUID = 1L;

        public void addCookie(Cookie cookie) {
        }
    });
    adaptClientBuilder(httpAsyncClientBuilder);
    client = httpAsyncClientBuilder.build();
    // Start the client thread
    client.start();
    // Always start the idle checker thread to validate pending requests and
    // use the ConnectionMaxIdle to close the idle connection
    new CloseIdleConnectionThread(connectionManager, client).start();
}
Also used : HttpRequest(org.apache.http.HttpRequest) Cookie(org.apache.http.cookie.Cookie) SchemeIOSessionStrategy(org.apache.http.nio.conn.SchemeIOSessionStrategy) HttpContext(org.apache.http.protocol.HttpContext) HttpResponse(org.apache.http.HttpResponse) ManagedNHttpClientConnectionFactory(org.apache.http.impl.nio.conn.ManagedNHttpClientConnectionFactory) HttpAsyncClientBuilder(org.apache.http.impl.nio.client.HttpAsyncClientBuilder) IOReactorConfig(org.apache.http.impl.nio.reactor.IOReactorConfig) DefaultConnectingIOReactor(org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) RedirectStrategy(org.apache.http.client.RedirectStrategy) PoolingNHttpClientConnectionManager(org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager) ConnectionConfig(org.apache.http.config.ConnectionConfig)

Aggregations

BasicCookieStore (org.apache.http.impl.client.BasicCookieStore)138 HttpResponse (org.apache.http.HttpResponse)54 HttpGet (org.apache.http.client.methods.HttpGet)51 Test (org.junit.Test)44 BasicClientCookie (org.apache.http.impl.cookie.BasicClientCookie)40 RequestConfig (org.apache.http.client.config.RequestConfig)36 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)36 CookieStore (org.apache.http.client.CookieStore)26 Header (org.apache.http.Header)25 HttpClient (org.apache.http.client.HttpClient)25 IOException (java.io.IOException)23 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)22 Cookie (org.apache.http.cookie.Cookie)19 HttpPost (org.apache.http.client.methods.HttpPost)15 CredentialsProvider (org.apache.http.client.CredentialsProvider)14 HttpClientContext (org.apache.http.client.protocol.HttpClientContext)14 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)14 BasicHttpContext (org.apache.http.protocol.BasicHttpContext)13 URI (java.net.URI)12 HttpEntity (org.apache.http.HttpEntity)12