ResponseProcessCookiespublic class ResponseProcessCookies extends Object implements HttpResponseInterceptorResponse interceptor that populates the current {@link CookieStore} with data
contained in response cookies received in the given the HTTP response. |
Fields Summary |
---|
private final Log | log |
Constructors Summary |
---|
public ResponseProcessCookies()
super();
|
Methods Summary |
---|
public void | process(org.apache.http.HttpResponse response, org.apache.http.protocol.HttpContext context)
if (response == null) {
throw new IllegalArgumentException("HTTP request may not be null");
}
if (context == null) {
throw new IllegalArgumentException("HTTP context may not be null");
}
// Obtain cookie store
CookieStore cookieStore = (CookieStore) context.getAttribute(
ClientContext.COOKIE_STORE);
if (cookieStore == null) {
this.log.info("Cookie store not available in HTTP context");
return;
}
// Obtain actual CookieSpec instance
CookieSpec cookieSpec = (CookieSpec) context.getAttribute(
ClientContext.COOKIE_SPEC);
if (cookieSpec == null) {
this.log.info("CookieSpec not available in HTTP context");
return;
}
// Obtain actual CookieOrigin instance
CookieOrigin cookieOrigin = (CookieOrigin) context.getAttribute(
ClientContext.COOKIE_ORIGIN);
if (cookieOrigin == null) {
this.log.info("CookieOrigin not available in HTTP context");
return;
}
HeaderIterator it = response.headerIterator(SM.SET_COOKIE);
processCookies(it, cookieSpec, cookieOrigin, cookieStore);
// see if the cookie spec supports cookie versioning.
if (cookieSpec.getVersion() > 0) {
// process set-cookie2 headers.
// Cookie2 will replace equivalent Cookie instances
it = response.headerIterator(SM.SET_COOKIE2);
processCookies(it, cookieSpec, cookieOrigin, cookieStore);
}
| private void | processCookies(org.apache.http.HeaderIterator iterator, org.apache.http.cookie.CookieSpec cookieSpec, org.apache.http.cookie.CookieOrigin cookieOrigin, org.apache.http.client.CookieStore cookieStore)
while (iterator.hasNext()) {
Header header = iterator.nextHeader();
try {
List<Cookie> cookies = cookieSpec.parse(header, cookieOrigin);
for (Cookie cookie : cookies) {
try {
cookieSpec.validate(cookie, cookieOrigin);
cookieStore.addCookie(cookie);
if (this.log.isDebugEnabled()) {
this.log.debug("Cookie accepted: \""
+ cookie + "\". ");
}
} catch (MalformedCookieException ex) {
if (this.log.isWarnEnabled()) {
this.log.warn("Cookie rejected: \""
+ cookie + "\". " + ex.getMessage());
}
}
}
} catch (MalformedCookieException ex) {
if (this.log.isWarnEnabled()) {
this.log.warn("Invalid cookie header: \""
+ header + "\". " + ex.getMessage());
}
}
}
|
|