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);
}
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);
}
}
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;
}
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);
}
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));*/
}
Aggregations