Search in sources :

Example 81 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
 */
@PublicAtsApi
public void addCookie(String name, String value, String domain, String path, Date expirationDate, boolean isSecure) {
    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) BasicClientCookie(org.apache.http.impl.cookie.BasicClientCookie) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Example 82 with BasicClientCookie

use of org.apache.http.impl.cookie.BasicClientCookie in project hive by apache.

the class TestHttpCookieAuthenticationTest method testHttpJdbcCookies.

@Test
public void testHttpJdbcCookies() throws Exception {
    String sqlQuery = "show tables";
    Class.forName(HiveDriver.class.getCanonicalName());
    String username = System.getProperty("user.name");
    try (Connection connection = DriverManager.getConnection(miniHS2.getJdbcURL(), username, "bar")) {
        assertNotNull(connection);
        CookieStore cookieStore = getCookieStoreFromConnection(connection);
        assertNotNull(cookieStore);
        // Test that basic cookies worked
        List<Cookie> cookies1 = cookieStore.getCookies();
        assertEquals(1, cookies1.size());
        try (Statement statement = connection.createStatement()) {
            assertNotNull(statement);
            try (ResultSet resultSet = statement.executeQuery(sqlQuery)) {
                assertNotNull(resultSet);
            }
        }
        // Check that cookies worked and still the same after a statement
        List<Cookie> cookies2 = cookieStore.getCookies();
        assertEquals(1, cookies2.size());
        assertEquals(cookies1, cookies2);
        // Empty out cookies to make sure same connection gets new cookies
        cookieStore.clear();
        assertTrue(cookieStore.getCookies().isEmpty());
        try (Statement statement = connection.createStatement()) {
            assertNotNull(statement);
            try (ResultSet resultSet = statement.executeQuery(sqlQuery)) {
                assertNotNull(resultSet);
            }
        }
        // Check that cookies worked after clearing and got back new cookie
        List<Cookie> cookies3 = cookieStore.getCookies();
        assertEquals(1, cookies3.size());
        assertNotEquals(cookies1, cookies3);
        // Get original cookie to copy metadata
        Cookie originalCookie = cookies3.get(0);
        // Put in a bad client side cookie - ensure HS2 authenticates and overwrites
        BasicClientCookie badCookie = new BasicClientCookie("hive.server2.auth", "bad");
        badCookie.setDomain(originalCookie.getDomain());
        badCookie.setPath(originalCookie.getPath());
        badCookie.setExpiryDate(originalCookie.getExpiryDate());
        cookieStore.addCookie(badCookie);
        // Check that putting in the bad cookie overrode the original cookie
        List<Cookie> cookies4 = cookieStore.getCookies();
        assertEquals(1, cookies4.size());
        assertTrue(cookies4.contains(badCookie));
        try (Statement statement = connection.createStatement()) {
            assertNotNull(statement);
            try (ResultSet resultSet = statement.executeQuery(sqlQuery)) {
                assertNotNull(resultSet);
            }
        }
        // Check that cookies worked and replaced the bad cookie
        List<Cookie> cookies5 = cookieStore.getCookies();
        assertEquals(1, cookies5.size());
        assertNotEquals(cookies4, cookies5);
        try (Statement statement = connection.createStatement()) {
            assertNotNull(statement);
            try (ResultSet resultSet = statement.executeQuery(sqlQuery)) {
                assertNotNull(resultSet);
            }
        }
        // Check that cookies worked and didn't get replaced
        List<Cookie> cookies6 = cookieStore.getCookies();
        assertEquals(1, cookies6.size());
        assertEquals(cookies5, cookies6);
    }
}
Also used : Cookie(org.apache.http.cookie.Cookie) BasicClientCookie(org.apache.http.impl.cookie.BasicClientCookie) CookieStore(org.apache.http.client.CookieStore) HiveDriver(org.apache.hive.jdbc.HiveDriver) Statement(java.sql.Statement) Connection(java.sql.Connection) HiveConnection(org.apache.hive.jdbc.HiveConnection) ResultSet(java.sql.ResultSet) BasicClientCookie(org.apache.http.impl.cookie.BasicClientCookie) Test(org.junit.Test)

Example 83 with BasicClientCookie

use of org.apache.http.impl.cookie.BasicClientCookie in project wildfly by wildfly.

the class SSOTestBase method copyCookie.

public static Cookie copyCookie(Cookie toCopy, String targetServer) {
    // Parse the target server down to a domain name
    int index = targetServer.indexOf("://");
    if (index > -1) {
        targetServer = targetServer.substring(index + 3);
    }
    // JBAS-8540
    // need to be able to parse IPv6 URLs which have enclosing brackets
    // HttpClient 3.1 creates cookies which include the square brackets
    // index = targetServer.indexOf(":");
    index = targetServer.lastIndexOf(":");
    if (index > -1) {
        targetServer = targetServer.substring(0, index);
    }
    index = targetServer.indexOf("/");
    if (index > -1) {
        targetServer = targetServer.substring(0, index);
    }
    // Cookie copy = new Cookie(targetServer, toCopy.getName(), toCopy.getValue(), "/", null, false);
    BasicClientCookie copy = new BasicClientCookie(toCopy.getName(), toCopy.getValue());
    copy.setDomain(targetServer);
    return copy;
}
Also used : BasicClientCookie(org.apache.http.impl.cookie.BasicClientCookie)

Example 84 with BasicClientCookie

use of org.apache.http.impl.cookie.BasicClientCookie in project warn-report by saaavsaaa.

the class WebRequestClient method setCookies.

private static void setCookies(Map<String, String> cookieKVs) {
    Cookie[] cookies = new Cookie[cookieKVs.size()];
    int i = 0;
    for (Map.Entry<String, String> entry : cookieKVs.entrySet()) {
        cookies[i] = new BasicClientCookie(entry.getKey(), entry.getValue());
        i++;
    }
    setCookies(cookies);
}
Also used : BasicClientCookie(org.apache.http.impl.cookie.BasicClientCookie) Cookie(org.apache.http.cookie.Cookie) BasicClientCookie(org.apache.http.impl.cookie.BasicClientCookie) Map(java.util.Map)

Example 85 with BasicClientCookie

use of org.apache.http.impl.cookie.BasicClientCookie in project warn-report by saaavsaaa.

the class WebRequestClient method setCookie.

private static void setCookie(String loginToken, String loginUserID) {
    Cookie[] cookies = new Cookie[2];
    cookies[0] = new BasicClientCookie(LOGIN_USERTOKEN_KEY, loginToken);
    cookies[1] = new BasicClientCookie(LOGIN_USERID_KEY, loginUserID);
    setCookies(cookies);
/*		// 创建一个本地上下文信息
		HttpContext localContext = new BasicHttpContext();
		// 在本地上下问中绑定一个本地存储
		localContext.setAttribute(HttpClientContext.COOKIE_STORE, cs);
		cs.addCookie(new BasicClientCookie(LOGIN_USERTOKEN_KEY, loginToken));
		cs.addCookie(new BasicClientCookie(LOGIN_USERID_KEY, loginUserID));*/
}
Also used : BasicClientCookie(org.apache.http.impl.cookie.BasicClientCookie) Cookie(org.apache.http.cookie.Cookie) BasicClientCookie(org.apache.http.impl.cookie.BasicClientCookie)

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