Home | History | Annotate | Download | only in protocol
      1 /*
      2  * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/client/protocol/ResponseProcessCookies.java $
      3  * $Revision: 673450 $
      4  * $Date: 2008-07-02 10:35:05 -0700 (Wed, 02 Jul 2008) $
      5  *
      6  * ====================================================================
      7  * Licensed to the Apache Software Foundation (ASF) under one
      8  * or more contributor license agreements.  See the NOTICE file
      9  * distributed with this work for additional information
     10  * regarding copyright ownership.  The ASF licenses this file
     11  * to you under the Apache License, Version 2.0 (the
     12  * "License"); you may not use this file except in compliance
     13  * with the License.  You may obtain a copy of the License at
     14  *
     15  *   http://www.apache.org/licenses/LICENSE-2.0
     16  *
     17  * Unless required by applicable law or agreed to in writing,
     18  * software distributed under the License is distributed on an
     19  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
     20  * KIND, either express or implied.  See the License for the
     21  * specific language governing permissions and limitations
     22  * under the License.
     23  * ====================================================================
     24  *
     25  * This software consists of voluntary contributions made by many
     26  * individuals on behalf of the Apache Software Foundation.  For more
     27  * information on the Apache Software Foundation, please see
     28  * <http://www.apache.org/>.
     29  *
     30  */
     31 
     32 package org.apache.http.client.protocol;
     33 
     34 import java.io.IOException;
     35 import java.util.List;
     36 
     37 import org.apache.commons.logging.Log;
     38 import org.apache.commons.logging.LogFactory;
     39 import org.apache.http.Header;
     40 import org.apache.http.HeaderIterator;
     41 import org.apache.http.HttpException;
     42 import org.apache.http.HttpResponse;
     43 import org.apache.http.HttpResponseInterceptor;
     44 import org.apache.http.client.CookieStore;
     45 import org.apache.http.cookie.Cookie;
     46 import org.apache.http.cookie.CookieOrigin;
     47 import org.apache.http.cookie.CookieSpec;
     48 import org.apache.http.cookie.MalformedCookieException;
     49 import org.apache.http.cookie.SM;
     50 import org.apache.http.protocol.HttpContext;
     51 
     52 /**
     53  * Response interceptor that populates the current {@link CookieStore} with data
     54  * contained in response cookies received in the given the HTTP response.
     55  *
     56  * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
     57  *
     58  * @version $Revision: 673450 $
     59  *
     60  * @since 4.0
     61  *
     62  * @deprecated Please use {@link java.net.URL#openConnection} instead.
     63  *     Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a>
     64  *     for further details.
     65  */
     66 @Deprecated
     67 public class ResponseProcessCookies implements HttpResponseInterceptor {
     68 
     69     private final Log log = LogFactory.getLog(getClass());
     70 
     71     public ResponseProcessCookies() {
     72         super();
     73     }
     74 
     75     public void process(final HttpResponse response, final HttpContext context)
     76             throws HttpException, IOException {
     77         if (response == null) {
     78             throw new IllegalArgumentException("HTTP request may not be null");
     79         }
     80         if (context == null) {
     81             throw new IllegalArgumentException("HTTP context may not be null");
     82         }
     83 
     84         // Obtain cookie store
     85         CookieStore cookieStore = (CookieStore) context.getAttribute(
     86                 ClientContext.COOKIE_STORE);
     87         if (cookieStore == null) {
     88             this.log.info("Cookie store not available in HTTP context");
     89             return;
     90         }
     91         // Obtain actual CookieSpec instance
     92         CookieSpec cookieSpec = (CookieSpec) context.getAttribute(
     93                 ClientContext.COOKIE_SPEC);
     94         if (cookieSpec == null) {
     95             this.log.info("CookieSpec not available in HTTP context");
     96             return;
     97         }
     98         // Obtain actual CookieOrigin instance
     99         CookieOrigin cookieOrigin = (CookieOrigin) context.getAttribute(
    100                 ClientContext.COOKIE_ORIGIN);
    101         if (cookieOrigin == null) {
    102             this.log.info("CookieOrigin not available in HTTP context");
    103             return;
    104         }
    105         HeaderIterator it = response.headerIterator(SM.SET_COOKIE);
    106         processCookies(it, cookieSpec, cookieOrigin, cookieStore);
    107 
    108         // see if the cookie spec supports cookie versioning.
    109         if (cookieSpec.getVersion() > 0) {
    110             // process set-cookie2 headers.
    111             // Cookie2 will replace equivalent Cookie instances
    112             it = response.headerIterator(SM.SET_COOKIE2);
    113             processCookies(it, cookieSpec, cookieOrigin, cookieStore);
    114         }
    115     }
    116 
    117     private void processCookies(
    118             final HeaderIterator iterator,
    119             final CookieSpec cookieSpec,
    120             final CookieOrigin cookieOrigin,
    121             final CookieStore cookieStore) {
    122         while (iterator.hasNext()) {
    123             Header header = iterator.nextHeader();
    124             try {
    125                 List<Cookie> cookies = cookieSpec.parse(header, cookieOrigin);
    126                 for (Cookie cookie : cookies) {
    127                     try {
    128                         cookieSpec.validate(cookie, cookieOrigin);
    129                         cookieStore.addCookie(cookie);
    130 
    131                         if (this.log.isDebugEnabled()) {
    132                             // BEGIN android-changed
    133                             this.log.debug("Cookie accepted: \""
    134                                     + cookieToString(cookie) + "\". ");
    135                             // END android-changed
    136                         }
    137                     } catch (MalformedCookieException ex) {
    138                         if (this.log.isWarnEnabled()) {
    139                             // BEGIN android-changed
    140                             this.log.warn("Cookie rejected: \""
    141                                     + cookieToString(cookie) + "\". " + ex.getMessage());
    142                             // END android-changed
    143                         }
    144                     }
    145                 }
    146             } catch (MalformedCookieException ex) {
    147                 if (this.log.isWarnEnabled()) {
    148                     this.log.warn("Invalid cookie header: \""
    149                             + header + "\". " + ex.getMessage());
    150                 }
    151             }
    152         }
    153     }
    154 
    155     // BEGIN android-added
    156     /**
    157      * Don't log the cookie's value; that's potentially sensitive information.
    158      */
    159     private String cookieToString(Cookie cookie) {
    160         return cookie.getClass().getSimpleName()
    161                 + "[version=" + cookie.getVersion()
    162                 + ",name=" + cookie.getName()
    163                 + ",domain=" + cookie.getDomain()
    164                 + ",path=" + cookie.getPath()
    165                 + ",expiry=" + cookie.getExpiryDate()
    166                 + "]";
    167     }
    168     // END android-added
    169 }
    170