List of usage examples for org.apache.http.impl.client AbstractHttpClient setCookieStore
public synchronized void setCookieStore(final CookieStore cookieStore)
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 w w . j a v a2 s . co 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); } } }
From source file:org.expath.httpclient.impl.ApacheHttpConnection.java
/** * Make a new Apache HTTP client, in order to serve this request. *//* ww w. ja v a 2 s . co m*/ private AbstractHttpClient makeClient() { AbstractHttpClient client = new DefaultHttpClient(); HttpParams params = client.getParams(); // use the default JVM proxy settings (http.proxyHost, etc.) HttpRoutePlanner route = new ProxySelectorRoutePlanner(client.getConnectionManager().getSchemeRegistry(), ProxySelector.getDefault()); client.setRoutePlanner(route); // do follow redirections? params.setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, myFollowRedirect); // set the timeout if any if (myTimeout != null) { // See http://blog.jayway.com/2009/03/17/configuring-timeout-with-apache-httpclient-40/ HttpConnectionParams.setConnectionTimeout(params, myTimeout * 1000); HttpConnectionParams.setSoTimeout(params, myTimeout * 1000); } // the shared cookie store client.setCookieStore(COOKIES); // the HTTP version (1.0 or 1.1) params.setParameter("http.protocol.version", myVersion); // return the just built client return client; }