List of usage examples for org.apache.http.impl.client BasicCookieStore addCookies
public synchronized void addCookies(final Cookie[] cookies)
From source file:org.apache.sling.testing.clients.interceptors.StickyCookieInterceptor.java
public void process(HttpRequest httpRequest, HttpContext httpContext) throws HttpException, IOException { final HttpClientContext clientContext = HttpClientContext.adapt(httpContext); List<Cookie> cookies = clientContext.getCookieStore().getCookies(); boolean set = (null != StickyCookieHolder.getTestStickySessionCookie()); boolean found = false; ListIterator<Cookie> it = cookies.listIterator(); while (it.hasNext()) { Cookie cookie = it.next();/*from ww w . jav a 2 s . c o m*/ if (cookie.getName().equals(StickyCookieHolder.COOKIE_NAME)) { found = true; if (set) { // set the cookie with the value saved for each thread using the rule it.set(StickyCookieHolder.getTestStickySessionCookie()); } else { // if the cookie is not set in TestStickySessionRule, remove it from here it.remove(); } } } // if the cookie needs to be set from TestStickySessionRule but did not exist in the client cookie list, add it here. if (!found && set) { cookies.add(StickyCookieHolder.getTestStickySessionCookie()); } BasicCookieStore cs = new BasicCookieStore(); cs.addCookies(cookies.toArray(new Cookie[cookies.size()])); httpContext.setAttribute(HttpClientContext.COOKIE_STORE, cs); }
From source file:net.fizzl.redditengine.impl.AccountApi.java
/** * Log into a reddit account using SSL * /*from w w w. java2s . c om*/ * @param user username * @param passwd users password * @param remember specifies whether or not the session cookie returned should last beyond the current browser session * @throws RedditEngineException */ public AuthResponse login(String user, String passwd, boolean remember) throws RedditEngineException { StringBuilder sb = new StringBuilder(); sb.append(UrlUtils.REDDIT_SSL); sb.append(LOGIN_PATH); String url = sb.toString(); List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("user", user)); params.add(new BasicNameValuePair("passwd", passwd)); params.add(new BasicNameValuePair("api_type", API_TYPE_JSON)); params.add(new BasicNameValuePair("rem", Boolean.toString(remember))); // The HTTP status code of the response will almost always be a 200 (OK) regardless of authentication success. // To see if login was successful, check the JSON response. // POST requests to /api/login must not include a reddit_session cookie along in the request. If a reddit_session cookie exists, the request may fail with a 409 status. AuthResponse ret = null; SimpleHttpClient client = SimpleHttpClient.getInstance(); // TODO should SimpleHttpClient have some kind of save/restore cookies function instead of accessing the cookiestore directly? BasicCookieStore cookieStore = client.getCookieStore(); List<Cookie> cookies = cookieStore.getCookies(); Cookie[] cookieArray = cookies.toArray(new Cookie[cookies.size()]); try { cookieStore.clear(); InputStream is = client.post(url, params); ret = AuthResponse.fromInputStream(is); if (ret.getJson().getData() != null) { lastPassword = passwd; } else { // an error occurred cookieStore.addCookies(cookieArray); } is.close(); } catch (Exception e) { // if failed, put the cookies back cookieStore.addCookies(cookieArray); throw new RedditEngineException(e); } return ret; }