ResponseProcessCookies.java revision 069490a5ca2fd1988d29daf45d892f47ad665115
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
32package org.apache.http.client.protocol;
33
34import java.io.IOException;
35import java.util.List;
36
37import org.apache.commons.logging.Log;
38import org.apache.commons.logging.LogFactory;
39import org.apache.http.Header;
40import org.apache.http.HeaderIterator;
41import org.apache.http.HttpException;
42import org.apache.http.HttpResponse;
43import org.apache.http.HttpResponseInterceptor;
44import org.apache.http.client.CookieStore;
45import org.apache.http.cookie.Cookie;
46import org.apache.http.cookie.CookieOrigin;
47import org.apache.http.cookie.CookieSpec;
48import org.apache.http.cookie.MalformedCookieException;
49import org.apache.http.cookie.SM;
50import 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 */
62public class ResponseProcessCookies implements HttpResponseInterceptor {
63
64    private final Log log = LogFactory.getLog(getClass());
65
66    public ResponseProcessCookies() {
67        super();
68    }
69
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
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                            this.log.debug("Cookie accepted: \""
128                                    + cookie + "\". ");
129                        }
130                    } catch (MalformedCookieException ex) {
131                        if (this.log.isWarnEnabled()) {
132                            this.log.warn("Cookie rejected: \""
133                                    + cookie + "\". " + ex.getMessage());
134                        }
135                    }
136                }
137            } catch (MalformedCookieException ex) {
138                if (this.log.isWarnEnabled()) {
139                    this.log.warn("Invalid cookie header: \""
140                            + header + "\". " + ex.getMessage());
141                }
142            }
143        }
144    }
145
146}
147