michael@0: /* michael@0: * ==================================================================== michael@0: * Licensed to the Apache Software Foundation (ASF) under one michael@0: * or more contributor license agreements. See the NOTICE file michael@0: * distributed with this work for additional information michael@0: * regarding copyright ownership. The ASF licenses this file michael@0: * to you under the Apache License, Version 2.0 (the michael@0: * "License"); you may not use this file except in compliance michael@0: * with the License. You may obtain a copy of the License at michael@0: * michael@0: * http://www.apache.org/licenses/LICENSE-2.0 michael@0: * michael@0: * Unless required by applicable law or agreed to in writing, michael@0: * software distributed under the License is distributed on an michael@0: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY michael@0: * KIND, either express or implied. See the License for the michael@0: * specific language governing permissions and limitations michael@0: * under the License. michael@0: * ==================================================================== michael@0: * michael@0: * This software consists of voluntary contributions made by many michael@0: * individuals on behalf of the Apache Software Foundation. For more michael@0: * information on the Apache Software Foundation, please see michael@0: * . michael@0: * michael@0: */ michael@0: michael@0: package ch.boye.httpclientandroidlib.client.protocol; michael@0: michael@0: import java.io.IOException; michael@0: import java.util.List; michael@0: michael@0: import ch.boye.httpclientandroidlib.annotation.Immutable; michael@0: michael@0: import ch.boye.httpclientandroidlib.androidextra.HttpClientAndroidLog; michael@0: /* LogFactory removed by HttpClient for Android script. */ michael@0: import ch.boye.httpclientandroidlib.Header; michael@0: import ch.boye.httpclientandroidlib.HeaderIterator; michael@0: import ch.boye.httpclientandroidlib.HttpException; michael@0: import ch.boye.httpclientandroidlib.HttpResponse; michael@0: import ch.boye.httpclientandroidlib.HttpResponseInterceptor; michael@0: import ch.boye.httpclientandroidlib.client.CookieStore; michael@0: import ch.boye.httpclientandroidlib.cookie.Cookie; michael@0: import ch.boye.httpclientandroidlib.cookie.CookieOrigin; michael@0: import ch.boye.httpclientandroidlib.cookie.CookieSpec; michael@0: import ch.boye.httpclientandroidlib.cookie.MalformedCookieException; michael@0: import ch.boye.httpclientandroidlib.cookie.SM; michael@0: import ch.boye.httpclientandroidlib.protocol.HttpContext; michael@0: michael@0: /** michael@0: * Response interceptor that populates the current {@link CookieStore} with data michael@0: * contained in response cookies received in the given the HTTP response. michael@0: * michael@0: * @since 4.0 michael@0: */ michael@0: @Immutable michael@0: public class ResponseProcessCookies implements HttpResponseInterceptor { michael@0: michael@0: public HttpClientAndroidLog log = new HttpClientAndroidLog(getClass()); michael@0: michael@0: public ResponseProcessCookies() { michael@0: super(); michael@0: } michael@0: michael@0: public void process(final HttpResponse response, final HttpContext context) michael@0: throws HttpException, IOException { michael@0: if (response == null) { michael@0: throw new IllegalArgumentException("HTTP request may not be null"); michael@0: } michael@0: if (context == null) { michael@0: throw new IllegalArgumentException("HTTP context may not be null"); michael@0: } michael@0: michael@0: // Obtain actual CookieSpec instance michael@0: CookieSpec cookieSpec = (CookieSpec) context.getAttribute( michael@0: ClientContext.COOKIE_SPEC); michael@0: if (cookieSpec == null) { michael@0: this.log.debug("Cookie spec not specified in HTTP context"); michael@0: return; michael@0: } michael@0: // Obtain cookie store michael@0: CookieStore cookieStore = (CookieStore) context.getAttribute( michael@0: ClientContext.COOKIE_STORE); michael@0: if (cookieStore == null) { michael@0: this.log.debug("Cookie store not specified in HTTP context"); michael@0: return; michael@0: } michael@0: // Obtain actual CookieOrigin instance michael@0: CookieOrigin cookieOrigin = (CookieOrigin) context.getAttribute( michael@0: ClientContext.COOKIE_ORIGIN); michael@0: if (cookieOrigin == null) { michael@0: this.log.debug("Cookie origin not specified in HTTP context"); michael@0: return; michael@0: } michael@0: HeaderIterator it = response.headerIterator(SM.SET_COOKIE); michael@0: processCookies(it, cookieSpec, cookieOrigin, cookieStore); michael@0: michael@0: // see if the cookie spec supports cookie versioning. michael@0: if (cookieSpec.getVersion() > 0) { michael@0: // process set-cookie2 headers. michael@0: // Cookie2 will replace equivalent Cookie instances michael@0: it = response.headerIterator(SM.SET_COOKIE2); michael@0: processCookies(it, cookieSpec, cookieOrigin, cookieStore); michael@0: } michael@0: } michael@0: michael@0: private void processCookies( michael@0: final HeaderIterator iterator, michael@0: final CookieSpec cookieSpec, michael@0: final CookieOrigin cookieOrigin, michael@0: final CookieStore cookieStore) { michael@0: while (iterator.hasNext()) { michael@0: Header header = iterator.nextHeader(); michael@0: try { michael@0: List cookies = cookieSpec.parse(header, cookieOrigin); michael@0: for (Cookie cookie : cookies) { michael@0: try { michael@0: cookieSpec.validate(cookie, cookieOrigin); michael@0: cookieStore.addCookie(cookie); michael@0: michael@0: if (this.log.isDebugEnabled()) { michael@0: this.log.debug("Cookie accepted: \"" michael@0: + cookie + "\". "); michael@0: } michael@0: } catch (MalformedCookieException ex) { michael@0: if (this.log.isWarnEnabled()) { michael@0: this.log.warn("Cookie rejected: \"" michael@0: + cookie + "\". " + ex.getMessage()); michael@0: } michael@0: } michael@0: } michael@0: } catch (MalformedCookieException ex) { michael@0: if (this.log.isWarnEnabled()) { michael@0: this.log.warn("Invalid cookie header: \"" michael@0: + header + "\". " + ex.getMessage()); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: }