• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 public class ResponseProcessCookies implements HttpResponseInterceptor {
63 
64     private final Log log = LogFactory.getLog(getClass());
65 
ResponseProcessCookies()66     public ResponseProcessCookies() {
67         super();
68     }
69 
process(final HttpResponse response, final HttpContext context)70     public void process(final HttpResponse response, final HttpContext context)
71             throws HttpException, IOException {
72         if (response == null) {
73             throw new IllegalArgumentException("HTTP request may not be null");
74         }
75         if (context == null) {
76             throw new IllegalArgumentException("HTTP context may not be null");
77         }
78 
79         // Obtain cookie store
80         CookieStore cookieStore = (CookieStore) context.getAttribute(
81                 ClientContext.COOKIE_STORE);
82         if (cookieStore == null) {
83             this.log.info("Cookie store not available in HTTP context");
84             return;
85         }
86         // Obtain actual CookieSpec instance
87         CookieSpec cookieSpec = (CookieSpec) context.getAttribute(
88                 ClientContext.COOKIE_SPEC);
89         if (cookieSpec == null) {
90             this.log.info("CookieSpec not available in HTTP context");
91             return;
92         }
93         // Obtain actual CookieOrigin instance
94         CookieOrigin cookieOrigin = (CookieOrigin) context.getAttribute(
95                 ClientContext.COOKIE_ORIGIN);
96         if (cookieOrigin == null) {
97             this.log.info("CookieOrigin not available in HTTP context");
98             return;
99         }
100         HeaderIterator it = response.headerIterator(SM.SET_COOKIE);
101         processCookies(it, cookieSpec, cookieOrigin, cookieStore);
102 
103         // see if the cookie spec supports cookie versioning.
104         if (cookieSpec.getVersion() > 0) {
105             // process set-cookie2 headers.
106             // Cookie2 will replace equivalent Cookie instances
107             it = response.headerIterator(SM.SET_COOKIE2);
108             processCookies(it, cookieSpec, cookieOrigin, cookieStore);
109         }
110     }
111 
processCookies( final HeaderIterator iterator, final CookieSpec cookieSpec, final CookieOrigin cookieOrigin, final CookieStore cookieStore)112     private void processCookies(
113             final HeaderIterator iterator,
114             final CookieSpec cookieSpec,
115             final CookieOrigin cookieOrigin,
116             final CookieStore cookieStore) {
117         while (iterator.hasNext()) {
118             Header header = iterator.nextHeader();
119             try {
120                 List<Cookie> cookies = cookieSpec.parse(header, cookieOrigin);
121                 for (Cookie cookie : cookies) {
122                     try {
123                         cookieSpec.validate(cookie, cookieOrigin);
124                         cookieStore.addCookie(cookie);
125 
126                         if (this.log.isDebugEnabled()) {
127                             // BEGIN android-changed
128                             this.log.debug("Cookie accepted: \""
129                                     + cookieToString(cookie) + "\". ");
130                             // END android-changed
131                         }
132                     } catch (MalformedCookieException ex) {
133                         if (this.log.isWarnEnabled()) {
134                             // BEGIN android-changed
135                             this.log.warn("Cookie rejected: \""
136                                     + cookieToString(cookie) + "\". " + ex.getMessage());
137                             // END android-changed
138                         }
139                     }
140                 }
141             } catch (MalformedCookieException ex) {
142                 if (this.log.isWarnEnabled()) {
143                     this.log.warn("Invalid cookie header: \""
144                             + header + "\". " + ex.getMessage());
145                 }
146             }
147         }
148     }
149 
150     // BEGIN android-added
151     /**
152      * Don't log the cookie's value; that's potentially sensitive information.
153      */
cookieToString(Cookie cookie)154     private String cookieToString(Cookie cookie) {
155         return cookie.getClass().getSimpleName()
156                 + "[version=" + cookie.getVersion()
157                 + ",name=" + cookie.getName()
158                 + ",domain=" + cookie.getDomain()
159                 + ",path=" + cookie.getPath()
160                 + ",expiry=" + cookie.getExpiryDate()
161                 + "]";
162     }
163     // END android-added
164 }
165