org.apache.commons.httpclient

Class HttpState


public class HttpState
extends Object

A container for HTTP attributes that may persist from request to request, such as cookies and authentication credentials.

Field Summary

static String
PREEMPTIVE_DEFAULT
Deprecated. This field and feature will be removed following HttpClient 3.0.
static String
PREEMPTIVE_PROPERTY
Deprecated. This field and feature will be removed following HttpClient 3.0.
protected ArrayList<E>
cookies
Array of cookies that this HTTP state contains.
protected HashMap<K,V>
credMap
Map of credentials by realm that this HTTP state contains.
protected HashMap<K,V>
proxyCred
Map of proxy credentials by realm that this HTTP state contains

Constructor Summary

HttpState()
Default constructor.

Method Summary

void
addCookie(Cookie cookie)
Adds an HTTP cookie, replacing any existing equivalent cookies.
void
addCookies(Cookie[] cookies)
Adds an array of HTTP cookies.
void
clear()
Clears the state information (all cookies, credentials and proxy credentials).
void
clearCookies()
Clears all cookies.
void
clearCredentials()
Clears all credentials.
void
clearProxyCredentials()
Clears all proxy credentials.
int
getCookiePolicy()
Deprecated. Use HttpMethodParams.getCookiePolicy(), HttpMethod.getParams().
Cookie[]
getCookies()
Returns an array of cookies that this HTTP state currently contains.
Cookie[]
getCookies(String domain, int port, String path, boolean secure)
Deprecated. use CookieSpec#match(String, int, String, boolean, Cookie)
Credentials
getCredentials(String realm, String host)
Deprecated. use #getCredentials(AuthScope)
Credentials
getCredentials(AuthScope authscope)
Get the credentials for the given authentication scope.
Credentials
getProxyCredentials(String realm, String proxyHost)
Deprecated. use #getProxyCredentials(AuthScope)
Credentials
getProxyCredentials(AuthScope authscope)
Get the proxy credentials for the given authentication scope.
boolean
isAuthenticationPreemptive()
Deprecated. Use HttpClientParams.isAuthenticationPreemptive(), HttpClient.getParams().
boolean
purgeExpiredCookies()
Removes all of cookies in this HTTP state that have expired according to the current system time.
boolean
purgeExpiredCookies(Date date)
Removes all of cookies in this HTTP state that have expired by the specified date.
void
setAuthenticationPreemptive(boolean value)
Deprecated. Use HttpClientParams.setAuthenticationPreemptive(boolean), HttpClient.getParams().
void
setCookiePolicy(int policy)
Deprecated. Use HttpMethodParams.setCookiePolicy(String), HttpMethod.getParams().
void
setCredentials(String realm, String host, Credentials credentials)
Deprecated. use #setCredentials(AuthScope, Credentials)
void
setCredentials(AuthScope authscope, Credentials credentials)
Sets the credentials for the given authentication scope.
void
setProxyCredentials(String realm, String proxyHost, Credentials credentials)
Deprecated. use #setProxyCredentials(AuthScope, Credentials)
void
setProxyCredentials(AuthScope authscope, Credentials credentials)
Sets the proxy credentials for the given authentication realm.
String
toString()
Returns a string representation of this HTTP state.

Methods inherited from class java.lang.Object

clone, equals, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait

Field Details

PREEMPTIVE_DEFAULT

public static final String PREEMPTIVE_DEFAULT

Deprecated. This field and feature will be removed following HttpClient 3.0.

The default value for PREEMPTIVE_PROPERTY.
Field Value:
"false"

PREEMPTIVE_PROPERTY

public static final String PREEMPTIVE_PROPERTY

Deprecated. This field and feature will be removed following HttpClient 3.0.

The boolean system property name to turn on preemptive authentication.
Field Value:
"httpclient.authentication.preemptive"

cookies

protected ArrayList<E> cookies
Array of cookies that this HTTP state contains.

credMap

protected HashMap<K,V> credMap
Map of credentials by realm that this HTTP state contains.

proxyCred

protected HashMap<K,V> proxyCred
Map of proxy credentials by realm that this HTTP state contains

Constructor Details

HttpState

public HttpState()
Default constructor.
Usages and Demos :

View More Examples of HttpState()
   1:     public HttpState getStateForUrl(URL url) {
   2:         return new HttpState();
   3:     }
   4: 
   5: }

View Full Code Here
   1:         WebRequest request = new WebRequestImpl();
   2:         HttpState state = new HttpState(); 
   3:         state.addCookies(CookieUtil.createHttpClientCookies(request, 
   4:         ...
   5:         
   6:         HttpState state = new HttpState(); 
   7:         state.addCookies(CookieUtil.createHttpClientCookies(request, 
   8: 
   9:         assertEquals(2, state.getCookies().length);

View Full Code Here
   1:     protected DaisyHttpClient getClient(AuthenticatedUser user) {
   2:         HttpState httpState = new HttpState();
   3:         httpState.setAuthenticationPreemptive(true);
   4:         String login = user.getLogin().replaceAll("@", "@@");
   5:         UsernamePasswordCredentials credentials =

View Full Code Here
   1:     public void testWrapper() throws Exception {
   2:         final HttpState state = new HttpState();
   3:         final WebResponseData data = new WebResponseData(new byte[]{}, 200, "", Collections.EMPTY_LIST);
   4:         final WebResponse response = new WebResponseImpl(data, URL_FIRST, SubmitMethod.GET, 0);
   5:         final WebClient webClient = new WebClient();

View Full Code Here
   1:             
   2:             HttpState initialState = new HttpState();            
   3:             initialState.addCookie(mycookie);
   4:             client = new HttpClient();
   5:             client.setState(initialState);

View Full Code Here

Method Details

addCookie

public void addCookie(Cookie cookie)
Adds an HTTP cookie, replacing any existing equivalent cookies. If the given cookie has already expired it will not be added, but existing values will still be removed.
Parameters:
cookie - the cookie to be added
Usages and Demos :

View More Examples of addCookie(Cookie cookie)
   1: import org.apache.commons.httpclient.HttpClient;
   2: import org.apache.commons.httpclient.HttpState;
   3: import org.apache.commons.httpclient.methods.GetMethod;
   4:         ...
   5:       assertTrue("X-AccessCount == 1", count == 1);
   6:       HttpState state = httpConn.getState();
   7:       servletGet0 = new GetMethod(baseURL0+"dist-ss/StatefulSessionServlet/");
   8:         ...
   9:       sessionID.setDomain(servers[1]);
  10:       state.addCookie(sessionID);

View Full Code Here
   1: import org.apache.commons.httpclient.HttpConnection;
   2: import org.apache.commons.httpclient.HttpState;
   3: import org.apache.commons.httpclient.UsernamePasswordCredentials;
   4:         ...
   5:             
   6:             HttpState initialState = new HttpState();            
   7:         ...
   8:             initialState.addCookie(mycookie);
   9:             client = new HttpClient();

View Full Code Here
   1: import org.apache.commons.httpclient.HttpClient;
   2: import org.apache.commons.httpclient.HttpState;
   3: import org.apache.commons.httpclient.cookie.CookiePolicy;
   4:         ...
   5: 
   6:         HttpState initialState = new HttpState();
   7:         Cookie mycookie = new Cookie(".foobar.com", "mycookie", "stuff", "/", null, false);
   8:         ...
   9:         initialState.addCookie(mycookie);

View Full Code Here
   1: import org.apache.commons.httpclient.HttpClient;
   2: import org.apache.commons.httpclient.HttpState;
   3: import org.apache.commons.httpclient.HttpStatus;
   4:         ...
   5:             
   6:             HttpState initialState = new HttpState();
   7:             Cookie sessionCookie = new Cookie(filePost.getHostConfiguration().getHost(), C_JSESSIONID, sessionId, "/", null, false);
   8:         ...
   9:             initialState.addCookie(sessionCookie);
  10:             initialState.setCookiePolicy(CookiePolicy.COMPATIBILITY);

View Full Code Here
   1: import org.apache.commons.httpclient.HttpMethodBase;
   2: import org.apache.commons.httpclient.HttpState;
   3: import org.apache.commons.httpclient.HttpVersion;
   4:         ...
   5:         if (cookieManager!=null){
   6:             HttpState state = client.getState();
   7:             for (int i = cookieManager.getCookies().size() - 1; i >= 0; i--) {
   8:         ...
   9:                          cookieValue, cookiePath, null, false);
  10:                     state.addCookie(newCookie);
  11:                     cookieHeader.append(cookieName);

View Full Code Here

addCookies

public void addCookies(Cookie[] cookies)
Adds an array of HTTP cookies. Cookies are added individually and in the given array order. If any of the given cookies has already expired it will not be added, but existing values will still be removed.
Parameters:
cookies - the cookies to be added
Usages and Demos :

View More Examples of addCookies(Cookie[] cookies)
   1: import org.apache.commons.httpclient.HttpClient;
   2: import org.apache.commons.httpclient.HttpState;
   3: import org.apache.commons.httpclient.methods.PostMethod;
   4:         ...
   5:             if ((transferedCookies != null) && (transferedCookies.length > 0)) {
   6:                 HttpState httpState = new HttpState();
   7:         ...
   8:                 httpState.addCookies(transferedCookies);
   9:                 httpClient.setState(httpState);

View Full Code Here
   1: import org.apache.commons.httpclient.HttpException;
   2: import org.apache.commons.httpclient.HttpState;
   3: import org.apache.commons.httpclient.NameValuePair;
   4:         ...
   5:         Cookie[] parsed = cookieParse(cookiespec, ".apache.org", 80, "/commons/httpclient", true, header);
   6:         HttpState state = new HttpState();
   7:         ...
   8:         state.addCookies(parsed);
   9:         Cookie[] cookies = state.getCookies();
  10:         ...
  11:         HttpState state = new HttpState();
  12:         state.addCookies(parsed);

View Full Code Here

clear

public void clear()
Clears the state information (all cookies, credentials and proxy credentials).

clearCookies

public void clearCookies()
Clears all cookies.

clearCredentials

public void clearCredentials()
Clears all credentials.

clearProxyCredentials

public void clearProxyCredentials()
Clears all proxy credentials.

getCookiePolicy

public int getCookiePolicy()

Deprecated. Use HttpMethodParams.getCookiePolicy(), HttpMethod.getParams().

Returns the current cookie policy for this HTTP state.
Returns:
The cookie policy.

getCookies

public Cookie[] getCookies()
Returns an array of cookies that this HTTP state currently contains.
Returns:
an array of cookies.

getCookies

public Cookie[] getCookies(String domain,
                           int port,
                           String path,
                           boolean secure)

Deprecated. use CookieSpec#match(String, int, String, boolean, Cookie)

Returns an array of cookies in this HTTP state that match the given request parameters.
Parameters:
domain - the request domain
port - the request port
path - the request path
secure - true when using HTTPS
Returns:
an array of cookies.
See Also:
getCookies()

getCredentials

public Credentials getCredentials(String realm,
                                  String host)

Deprecated. use #getCredentials(AuthScope)

Get the credentials for the given authentication scope on the given host. If the realm exists on host, return the coresponding credentials. If the host exists with a null realm, return the corresponding credentials. If the realm exists with a null host, return the corresponding credentials. If the realm does not exist, return the default Credentials. If there are no default credentials, return null.
Parameters:
realm - the authentication realm
host - the host the realm is on
Returns:
the credentials

getCredentials

public Credentials getCredentials(AuthScope authscope)
Get the credentials for the given authentication scope.
Parameters:
authscope - the authentication scope
Returns:
the credentials
Since:
3.0

getProxyCredentials

public Credentials getProxyCredentials(String realm,
                                       String proxyHost)

Deprecated. use #getProxyCredentials(AuthScope)

Get the credentials for the proxy host with the given authentication scope. If the realm exists on host, return the coresponding credentials. If the host exists with a null realm, return the corresponding credentials. If the realm exists with a null host, return the corresponding credentials. If the realm does not exist, return the default Credentials. If there are no default credentials, return null.
Parameters:
realm - the authentication realm
proxyHost - the proxy host the realm is on
Returns:
the credentials

getProxyCredentials

public Credentials getProxyCredentials(AuthScope authscope)
Get the proxy credentials for the given authentication scope.
Parameters:
authscope - the authentication scope
Returns:
the credentials
Since:
3.0

isAuthenticationPreemptive

public boolean isAuthenticationPreemptive()

Deprecated. Use HttpClientParams.isAuthenticationPreemptive(), HttpClient.getParams().

Returns true if preemptive authentication should be attempted, false otherwise.
Returns:
boolean flag.

purgeExpiredCookies

public boolean purgeExpiredCookies()
Removes all of cookies in this HTTP state that have expired according to the current system time.

purgeExpiredCookies

public boolean purgeExpiredCookies(Date date)
Removes all of cookies in this HTTP state that have expired by the specified date.
Parameters:
date - The date to compare against.
Returns:
true if any cookies were purged.

setAuthenticationPreemptive

public void setAuthenticationPreemptive(boolean value)

Deprecated. Use HttpClientParams.setAuthenticationPreemptive(boolean), HttpClient.getParams().

Defines whether preemptive authentication should be attempted.
Parameters:
value - true if preemptive authentication should be attempted, false otherwise.
Usages and Demos :

View More Examples of setAuthenticationPreemptive(boolean value)
   1: import org.apache.commons.httpclient.NameValuePair;
   2: import org.apache.commons.httpclient.HttpState;
   3: import org.apache.commons.httpclient.util.URIUtil;
   4:         ...
   5:     protected DaisyHttpClient getClient(AuthenticatedUser user) {
   6:         HttpState httpState = new HttpState();
   7:         ...
   8:         httpState.setAuthenticationPreemptive(true);
   9:         String login = user.getLogin().replaceAll("@", "@@");

View Full Code Here
   1:     private HttpClient sharedHttpClient;
   2:     private HttpState httpState;
   3:     private HostConfiguration sharedHostConfiguration;
   4:         ...
   5: 
   6:     public DaisyHttpClient(HttpClient sharedHttpClient, HostConfiguration sharedHostConfiguration, HttpState httpState, String login) {
   7:         this.sharedHttpClient = sharedHttpClient;
   8:         ...
   9:         HttpState httpState = new HttpState();
  10:         httpState.setAuthenticationPreemptive(true);
  11:         login = login.replaceAll("@", "@@");

View Full Code Here
   1:     public AuthenticatedUser getUser(Credentials credentials) throws RepositoryException {
   2:         HttpState httpState = new HttpState();
   3:         ...
   4:         httpState.setAuthenticationPreemptive(true);
   5:         String login = credentials.getLogin().replaceAll("@", "@@");
   6:         UsernamePasswordCredentials httpClientCredentials = new UsernamePasswordCredentials(login, credentials.getPassword());
   7:         httpState.setCredentials(null, null, httpClientCredentials);

View Full Code Here

setCookiePolicy

public void setCookiePolicy(int policy)

Deprecated. Use HttpMethodParams.setCookiePolicy(String), HttpMethod.getParams().

Sets the current cookie policy for this HTTP state to one of the following supported policies: CookiePolicy.COMPATIBILITY, CookiePolicy.NETSCAPE_DRAFT or CookiePolicy.RFC2109.
Parameters:
policy - new cookie policy
Usages and Demos :

View More Examples of setCookiePolicy(int policy)
   1: import org.apache.commons.httpclient.HttpClient;
   2: import org.apache.commons.httpclient.HttpState;
   3: import org.apache.commons.httpclient.HttpStatus;
   4:         ...
   5:             
   6:             HttpState initialState = new HttpState();
   7:             Cookie sessionCookie = new Cookie(filePost.getHostConfiguration().getHost(), C_JSESSIONID, sessionId, "/", null, false);
   8:         ...
   9:             initialState.addCookie(sessionCookie);
  10:             initialState.setCookiePolicy(CookiePolicy.COMPATIBILITY);
  11:             client.setState(initialState);            

View Full Code Here

setCredentials

public void setCredentials(String realm,
                           String host,
                           Credentials credentials)

Deprecated. use #setCredentials(AuthScope, Credentials)

Sets the credentials for the given authentication realm on the given host. The null realm signifies default credentials for the given host, which should be used when no credentials have been explictly supplied for the challenging realm. The null host signifies default credentials, which should be used when no credentials have been explictly supplied for the challenging host. Any previous credentials for the given realm on the given host will be overwritten.
Parameters:
realm - the authentication realm
host - the host the realm belongs to
credentials - the authentication credentials for the given realm.
Usages and Demos :

View More Examples of setCredentials(String realm,String host,Credentials credentials)
   1: import org.apache.commons.httpclient.HttpMethod;
   2: import org.apache.commons.httpclient.HttpState;
   3: import org.apache.commons.httpclient.UsernamePasswordCredentials;
   4:         ...
   5:         HttpClient client = new HttpClient();
   6:         HttpState state = client.getState();
   7:         HttpClientParams params = client.getParams();
   8:         ...
   9:             new UsernamePasswordCredentials( "testuser", "crazypass" );
  10:         state.setCredentials( null, null, credentials );
  11:         params.setAuthenticationPreemptive( true );

View Full Code Here
   1: import org.apache.commons.httpclient.NameValuePair;
   2: import org.apache.commons.httpclient.HttpState;
   3: import org.apache.commons.httpclient.util.URIUtil;
   4:         ...
   5:     protected DaisyHttpClient getClient(AuthenticatedUser user) {
   6:         HttpState httpState = new HttpState();
   7:         httpState.setAuthenticationPreemptive(true);
   8:         ...
   9:                 new UsernamePasswordCredentials(login + "@" + getActiveRoleString(user.getActiveRoleIds()), user.getPassword());
  10:         httpState.setCredentials(null, null, credentials);

View Full Code Here
   1:     private HttpClient sharedHttpClient;
   2:     private HttpState httpState;
   3:     private HostConfiguration sharedHostConfiguration;
   4:         ...
   5: 
   6:     public DaisyHttpClient(HttpClient sharedHttpClient, HostConfiguration sharedHostConfiguration, HttpState httpState, String login) {
   7:         this.sharedHttpClient = sharedHttpClient;
   8:         ...
   9:         UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(login, password);
  10:         httpState.setCredentials(null, null, credentials);

View Full Code Here
   1:     public AuthenticatedUser getUser(Credentials credentials) throws RepositoryException {
   2:         HttpState httpState = new HttpState();
   3:         httpState.setAuthenticationPreemptive(true);
   4:         ...
   5:         UsernamePasswordCredentials httpClientCredentials = new UsernamePasswordCredentials(login, credentials.getPassword());
   6:         httpState.setCredentials(null, null, httpClientCredentials);
   7:         DaisyHttpClient httpClient = new DaisyHttpClient(context.getSharedHttpClient(), context.getSharedHostConfiguration(), httpState);
   8: 
   9:         HttpMethod method = new GetMethod("/repository/userinfo");

View Full Code Here

setCredentials

public void setCredentials(AuthScope authscope,
                           Credentials credentials)
Sets the credentials for the given authentication scope. Any previous credentials for the given scope will be overwritten.
Parameters:
authscope - the authentication scope
credentials - the authentication credentials for the given scope.
Since:
3.0
Usages and Demos :

View More Examples of setCredentials(AuthScope authscope,Credentials credentials)
   1: import org.apache.commons.httpclient.HttpClientTestBase;
   2: import org.apache.commons.httpclient.HttpState;
   3: import org.apache.commons.httpclient.HttpStatus;
   4:         ...
   5:         
   6:         HttpState state = new HttpState();
   7:         ...
   8:         state.setCredentials(AuthScope.ANY, creds);
   9:         this.client.setState(state);

View Full Code Here
   1: import org.apache.commons.httpclient.HttpConnection;
   2: import org.apache.commons.httpclient.HttpState;
   3: import org.apache.commons.httpclient.UsernamePasswordCredentials;
   4:         ...
   5:             HttpConnection conn = null;
   6:             HttpState state = new HttpState();
   7: 
   8:         ...
   9:             Credentials cred = new UsernamePasswordCredentials(userID, passwd);
  10:             state.setCredentials(null, cred);

View Full Code Here
   1: import org.apache.commons.httpclient.HttpClientTestBase;
   2: import org.apache.commons.httpclient.HttpState;
   3: import org.apache.commons.httpclient.HttpStatus;
   4:         ...
   5:         try {
   6:             this.client.getState().setCredentials(AuthScope.ANY, creds);
   7:             this.client.executeMethod(httpget);
   8:         ...
   9: 
  10:         HttpState state = new HttpState();
  11:         ...
  12:         state.setCredentials(AuthScope.ANY, creds);

View Full Code Here

setProxyCredentials

public void setProxyCredentials(String realm,
                                String proxyHost,
                                Credentials credentials)

Deprecated. use #setProxyCredentials(AuthScope, Credentials)

Sets the credentials for the given proxy authentication realm on the given proxy host. The null proxy realm signifies default credentials for the given proxy host, which should be used when no credentials have been explictly supplied for the challenging proxy realm. The null proxy host signifies default credentials, which should be used when no credentials have been explictly supplied for the challenging proxy host. Any previous credentials for the given proxy realm on the given proxy host will be overwritten.
Parameters:
realm - the authentication realm
proxyHost - the proxy host
credentials - the authentication credentials for the given realm

setProxyCredentials

public void setProxyCredentials(AuthScope authscope,
                                Credentials credentials)
Sets the proxy credentials for the given authentication realm. Any previous credentials for the given realm will be overwritten.
Parameters:
authscope - the authentication scope
credentials - the authentication credentials for the given realm.
Since:
3.0
Usages and Demos :

View More Examples of setProxyCredentials(AuthScope authscope,Credentials credentials)
   1: import org.apache.commons.httpclient.HttpConnection;
   2: import org.apache.commons.httpclient.HttpState;
   3: import org.apache.commons.httpclient.UsernamePasswordCredentials;
   4:         ...
   5:             HttpConnection conn = null;
   6:             HttpState state = new HttpState();
   7: 
   8:         ...
   9: 
  10:                 state.setProxyCredentials(null, proxyCred);
  11:             }
  12:         ...
  13: 
  14:                 state.setProxyCredentials(null, proxyCred);

View Full Code Here

toString

public String toString()
Returns a string representation of this HTTP state.
Overrides:
toString in interface Object
Returns:
The string representation of the HTTP state.