List of usage examples for org.apache.http.impl.client AbstractHttpClient getCookieStore
public synchronized final CookieStore getCookieStore()
From source file:com.redprairie.moca.mad.TI_MadFilter.java
private Cookie getSessionCookie(J4pClient j4pClient) { // Find the session cookie set by the filter, have to downcast to AbstractHttpClient // to be able to access cookies. AbstractHttpClient httpClient = (AbstractHttpClient) j4pClient.getHttpClient(); Cookie sessionCookie = null;// w w w . j ava 2 s .c o m for (Cookie cookie : httpClient.getCookieStore().getCookies()) { if (cookie.getName().equals(MadFilter.SESSION_COOKIE_NAME)) { sessionCookie = cookie; } } return sessionCookie; }
From source file:org.apache.jena.atlas.web.auth.FormsAuthenticator.java
@Override public void apply(AbstractHttpClient client, HttpContext httpContext, URI target) { if (client == null) return;/*from w ww . j av a 2s . c o m*/ // Do we have a login available for the target server? FormLogin login = this.findCredentials(target); if (login == null) return; // We need to synchronize on the login because making a login attempt // may take a while and there is no point making multiple login attempts // against the same server synchronized (login) { // Have we already logged into this server? if (login.hasCookies()) { // Use existing cookies LOG.info("Using existing cookies to authenticate access to " + target.toString()); CookieStore store = login.getCookies(); if (store != null) client.setCookieStore(store); // Check if any of the cookies have expired if (!store.clearExpired(Calendar.getInstance().getTime())) { // No cookies were cleared so our cookies are still fresh // and we don't need to login again return; } // If we reach here then some of our cookies have expired and we // may no longer be logged in and should login again instead of // proceeding with the existing cookies } try { // Use a fresh Cookie Store for new login attempts CookieStore store = new BasicCookieStore(); client.setCookieStore(store); // Try to login LOG.info("Making login attempt against " + login.getLoginFormURL() + " to obtain authentication for access to " + target.toString()); HttpPost post = new HttpPost(login.getLoginFormURL()); post.setEntity(login.getLoginEntity()); HttpResponse response = client.execute(post, httpContext); // Always read the payload to ensure reusable connections final String payload = HttpOp.readPayload(response.getEntity()); // Check for successful login if (response.getStatusLine().getStatusCode() >= 400) { LOG.warn("Failed to login against " + login.getLoginFormURL() + " to obtain authentication for access to " + target.toString()); throw new HttpException(response.getStatusLine().getStatusCode(), "Login attempt failed - " + response.getStatusLine().getReasonPhrase(), payload); } // Otherwise assume a successful login LOG.info("Successfully logged in against " + login.getLoginFormURL() + " and obtained authentication for access to " + target.toString()); login.setCookies(client.getCookieStore()); } catch (UnsupportedEncodingException e) { throw new HttpException("UTF-8 encoding not supported on your platform", e); } catch (IOException e) { throw new HttpException("Error making login request", e); } } }