Search in sources :

Example 1 with BasicClientCookie

use of org.apache.http.impl.cookie.BasicClientCookie in project ats-framework by Axway.

the class HttpClient method addCookie.

/**
     * Add Cookie
     *
     * @param name cookie name
     * @param value cookie value
     * @param domain cookie domain
     * @param isSecure whether the cookie is secure or not
     * @param expirationDate cookie expiration date
     * @param path cookie path
     */
public void addCookie(String name, String value, String domain, String path, Date expirationDate, boolean isSecure) {
    if (httpContext == null) {
        httpContext = new BasicHttpContext();
    }
    BasicCookieStore cookieStore = (BasicCookieStore) httpContext.getAttribute(HttpClientContext.COOKIE_STORE);
    if (cookieStore == null) {
        cookieStore = new BasicCookieStore();
        httpContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);
    }
    BasicClientCookie cookie = new BasicClientCookie(name, value);
    cookie.setDomain(domain);
    cookie.setPath(path);
    cookie.setExpiryDate(expirationDate);
    cookie.setSecure(isSecure);
    cookieStore.addCookie(cookie);
}
Also used : BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) BasicClientCookie(org.apache.http.impl.cookie.BasicClientCookie)

Example 2 with BasicClientCookie

use of org.apache.http.impl.cookie.BasicClientCookie in project android-player-samples by BrightcoveOS.

the class MainActivity method httpGet.

public String httpGet(String url) {
    String domain = getResources().getString(R.string.ais_domain);
    String result = "";
    CookieStore cookieStore = new BasicCookieStore();
    BasicHttpContext localContext = new BasicHttpContext();
    localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
    // If we have a cookie stored, parse and use it. Otherwise, use a default http client.
    try {
        HttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);
        if (!authorizationCookie.equals("")) {
            String[] cookies = authorizationCookie.split(";");
            for (int i = 0; i < cookies.length; i++) {
                String[] kvp = cookies[i].split("=");
                if (kvp.length != 2) {
                    throw new Exception("Illegal cookie: missing key/value pair.");
                }
                BasicClientCookie c = new BasicClientCookie(kvp[0], kvp[1]);
                c.setDomain(domain);
                cookieStore.addCookie(c);
            }
        }
        HttpResponse httpResponse = httpClient.execute(httpGet, localContext);
        result = EntityUtils.toString(httpResponse.getEntity());
    } catch (Exception e) {
        Log.e(TAG, e.getLocalizedMessage());
    }
    return result;
}
Also used : CookieStore(org.apache.http.client.CookieStore) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) BasicClientCookie(org.apache.http.impl.cookie.BasicClientCookie) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient)

Example 3 with BasicClientCookie

use of org.apache.http.impl.cookie.BasicClientCookie in project lucene-solr by apache.

the class SolrPortAwareCookieSpecTest method testDomainValidate1.

@Test
public void testDomainValidate1() throws Exception {
    final BasicClientCookie cookie = new BasicClientCookie("name", "value");
    final CookieOrigin origin = new CookieOrigin("somehost", 80, "/", false);
    final CookieAttributeHandler h = new SolrPortAwareCookieSpecFactory.PortAwareDomainHandler();
    cookie.setDomain("somehost");
    h.validate(cookie, origin);
    cookie.setDomain("otherhost");
    try {
        h.validate(cookie, origin);
        Assert.fail("MalformedCookieException should have been thrown");
    } catch (final MalformedCookieException ex) {
    // expected
    }
}
Also used : CookieAttributeHandler(org.apache.http.cookie.CookieAttributeHandler) MalformedCookieException(org.apache.http.cookie.MalformedCookieException) BasicClientCookie(org.apache.http.impl.cookie.BasicClientCookie) CookieOrigin(org.apache.http.cookie.CookieOrigin) Test(org.junit.Test)

Example 4 with BasicClientCookie

use of org.apache.http.impl.cookie.BasicClientCookie in project lucene-solr by apache.

the class SolrPortAwareCookieSpecTest method testDomainMatch1.

@Test
public void testDomainMatch1() throws Exception {
    final BasicClientCookie cookie = new BasicClientCookie("name", "value");
    final CookieOrigin origin = new CookieOrigin("www.somedomain.com", 80, "/", false);
    final CookieAttributeHandler h = new SolrPortAwareCookieSpecFactory.PortAwareDomainHandler();
    cookie.setDomain(null);
    Assert.assertFalse(h.match(cookie, origin));
    cookie.setDomain(".somedomain.com");
    Assert.assertTrue(h.match(cookie, origin));
}
Also used : CookieAttributeHandler(org.apache.http.cookie.CookieAttributeHandler) BasicClientCookie(org.apache.http.impl.cookie.BasicClientCookie) CookieOrigin(org.apache.http.cookie.CookieOrigin) Test(org.junit.Test)

Example 5 with BasicClientCookie

use of org.apache.http.impl.cookie.BasicClientCookie in project lucene-solr by apache.

the class SolrPortAwareCookieSpecTest method testDomainHostPortValidate.

@Test
public void testDomainHostPortValidate() throws Exception {
    final BasicClientCookie cookie = new BasicClientCookie("name", "value");
    final CookieOrigin origin = new CookieOrigin("somehost", 80, "/", false);
    final CookieAttributeHandler h = new SolrPortAwareCookieSpecFactory.PortAwareDomainHandler();
    cookie.setDomain("somehost:80");
    h.validate(cookie, origin);
    cookie.setDomain("somehost:1234");
    try {
        h.validate(cookie, origin);
        Assert.fail("MalformedCookieException should have been thrown");
    } catch (final MalformedCookieException ex) {
    // expected
    }
}
Also used : CookieAttributeHandler(org.apache.http.cookie.CookieAttributeHandler) MalformedCookieException(org.apache.http.cookie.MalformedCookieException) BasicClientCookie(org.apache.http.impl.cookie.BasicClientCookie) CookieOrigin(org.apache.http.cookie.CookieOrigin) Test(org.junit.Test)

Aggregations

BasicClientCookie (org.apache.http.impl.cookie.BasicClientCookie)88 BasicCookieStore (org.apache.http.impl.client.BasicCookieStore)36 Test (org.junit.Test)24 HttpResponse (org.apache.http.HttpResponse)19 Cookie (org.apache.http.cookie.Cookie)17 HttpClient (org.apache.http.client.HttpClient)15 HttpGet (org.apache.http.client.methods.HttpGet)15 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)15 Map (java.util.Map)14 RequestConfig (org.apache.http.client.config.RequestConfig)13 HttpPost (org.apache.http.client.methods.HttpPost)9 CookieAttributeHandler (org.apache.http.cookie.CookieAttributeHandler)9 CookieOrigin (org.apache.http.cookie.CookieOrigin)9 URL (java.net.URL)8 HttpEntity (org.apache.http.HttpEntity)8 CookieStore (org.apache.http.client.CookieStore)8 IOException (java.io.IOException)7 Date (java.util.Date)7 ClientCookie (org.apache.http.cookie.ClientCookie)7 HashMap (java.util.HashMap)6