diff options
Diffstat (limited to 'src/org/apache/http/client/protocol')
8 files changed, 785 insertions, 0 deletions
diff --git a/src/org/apache/http/client/protocol/ClientContext.java b/src/org/apache/http/client/protocol/ClientContext.java new file mode 100644 index 0000000..1859f9e --- /dev/null +++ b/src/org/apache/http/client/protocol/ClientContext.java @@ -0,0 +1,52 @@ +/* + * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/client/protocol/ClientContext.java $ + * $Revision: 658759 $ + * $Date: 2008-05-21 10:06:17 -0700 (Wed, 21 May 2008) $ + * + * ==================================================================== + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * <http://www.apache.org/>. + * + */ + +package org.apache.http.client.protocol; + + +/** + * {@link org.apache.http.protocol.HttpContext Context} + * attribute names for client. + */ +public interface ClientContext { + + public static final String COOKIESPEC_REGISTRY = "http.cookiespec-registry"; + public static final String AUTHSCHEME_REGISTRY = "http.authscheme-registry"; + public static final String COOKIE_STORE = "http.cookie-store"; + public static final String COOKIE_SPEC = "http.cookie-spec"; + public static final String COOKIE_ORIGIN = "http.cookie-origin"; + public static final String CREDS_PROVIDER = "http.auth.credentials-provider"; + public static final String TARGET_AUTH_STATE = "http.auth.target-scope"; + public static final String PROXY_AUTH_STATE = "http.auth.proxy-scope"; + public static final String AUTH_SCHEME_PREF = "http.auth.scheme-pref"; + public static final String USER_TOKEN = "http.user-token"; + +} diff --git a/src/org/apache/http/client/protocol/ClientContextConfigurer.java b/src/org/apache/http/client/protocol/ClientContextConfigurer.java new file mode 100644 index 0000000..f2ced63 --- /dev/null +++ b/src/org/apache/http/client/protocol/ClientContextConfigurer.java @@ -0,0 +1,72 @@ +/* + * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/client/protocol/ClientContextConfigurer.java $ + * $Revision: 654886 $ + * $Date: 2008-05-09 10:06:12 -0700 (Fri, 09 May 2008) $ + * + * ==================================================================== + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * <http://www.apache.org/>. + * + */ + +package org.apache.http.client.protocol; + +import java.util.List; + +import org.apache.http.auth.AuthSchemeRegistry; +import org.apache.http.client.CookieStore; +import org.apache.http.client.CredentialsProvider; +import org.apache.http.cookie.CookieSpecRegistry; +import org.apache.http.protocol.HttpContext; + +public class ClientContextConfigurer implements ClientContext { + + private final HttpContext context; + + public ClientContextConfigurer (final HttpContext context) { + if (context == null) + throw new IllegalArgumentException("HTTP context may not be null"); + this.context = context; + } + + public void setCookieSpecRegistry(final CookieSpecRegistry registry) { + this.context.setAttribute(COOKIESPEC_REGISTRY, registry); + } + + public void setAuthSchemeRegistry(final AuthSchemeRegistry registry) { + this.context.setAttribute(AUTHSCHEME_REGISTRY, registry); + } + + public void setCookieStore(final CookieStore store) { + this.context.setAttribute(COOKIE_STORE, store); + } + + public void setCredentialsProvider(final CredentialsProvider provider) { + this.context.setAttribute(CREDS_PROVIDER, provider); + } + + public void setAuthSchemePref(final List<String> list) { + this.context.setAttribute(AUTH_SCHEME_PREF, list); + } + +} diff --git a/src/org/apache/http/client/protocol/RequestAddCookies.java b/src/org/apache/http/client/protocol/RequestAddCookies.java new file mode 100644 index 0000000..0de8c40 --- /dev/null +++ b/src/org/apache/http/client/protocol/RequestAddCookies.java @@ -0,0 +1,192 @@ +/* + * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/client/protocol/RequestAddCookies.java $ + * $Revision: 673450 $ + * $Date: 2008-07-02 10:35:05 -0700 (Wed, 02 Jul 2008) $ + * + * ==================================================================== + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * <http://www.apache.org/>. + * + */ + +package org.apache.http.client.protocol; + +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; +import java.util.ArrayList; +import java.util.List; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.http.Header; +import org.apache.http.HttpException; +import org.apache.http.HttpHost; +import org.apache.http.HttpRequest; +import org.apache.http.HttpRequestInterceptor; +import org.apache.http.ProtocolException; +import org.apache.http.client.CookieStore; +import org.apache.http.client.methods.HttpUriRequest; +import org.apache.http.client.params.HttpClientParams; +import org.apache.http.conn.ManagedClientConnection; +import org.apache.http.cookie.Cookie; +import org.apache.http.cookie.CookieOrigin; +import org.apache.http.cookie.CookieSpec; +import org.apache.http.cookie.CookieSpecRegistry; +import org.apache.http.protocol.HttpContext; +import org.apache.http.protocol.ExecutionContext; + +/** + * Request interceptor that matches cookies available in the current + * {@link CookieStore} to the request being executed and generates + * corresponding cookierequest headers. + * + * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a> + * + * @version $Revision: 673450 $ + * + * @since 4.0 + */ +public class RequestAddCookies implements HttpRequestInterceptor { + + private final Log log = LogFactory.getLog(getClass()); + + public RequestAddCookies() { + super(); + } + + public void process(final HttpRequest request, final HttpContext context) + throws HttpException, IOException { + if (request == 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 the registry of cookie specs + CookieSpecRegistry registry= (CookieSpecRegistry) context.getAttribute( + ClientContext.COOKIESPEC_REGISTRY); + if (registry == null) { + this.log.info("CookieSpec registry not available in HTTP context"); + return; + } + + // Obtain the target host (required) + HttpHost targetHost = (HttpHost) context.getAttribute( + ExecutionContext.HTTP_TARGET_HOST); + if (targetHost == null) { + throw new IllegalStateException("Target host not specified in HTTP context"); + } + + // Obtain the client connection (required) + ManagedClientConnection conn = (ManagedClientConnection) context.getAttribute( + ExecutionContext.HTTP_CONNECTION); + if (conn == null) { + throw new IllegalStateException("Client connection not specified in HTTP context"); + } + + String policy = HttpClientParams.getCookiePolicy(request.getParams()); + if (this.log.isDebugEnabled()) { + this.log.debug("CookieSpec selected: " + policy); + } + + URI requestURI; + if (request instanceof HttpUriRequest) { + requestURI = ((HttpUriRequest) request).getURI(); + } else { + try { + requestURI = new URI(request.getRequestLine().getUri()); + } catch (URISyntaxException ex) { + throw new ProtocolException("Invalid request URI: " + + request.getRequestLine().getUri(), ex); + } + } + + String hostName = targetHost.getHostName(); + int port = targetHost.getPort(); + if (port < 0) { + port = conn.getRemotePort(); + } + + CookieOrigin cookieOrigin = new CookieOrigin( + hostName, + port, + requestURI.getPath(), + conn.isSecure()); + + // Get an instance of the selected cookie policy + CookieSpec cookieSpec = registry.getCookieSpec(policy, request.getParams()); + // Get all cookies available in the HTTP state + List<Cookie> cookies = new ArrayList<Cookie>(cookieStore.getCookies()); + // Find cookies matching the given origin + List<Cookie> matchedCookies = new ArrayList<Cookie>(); + for (Cookie cookie : cookies) { + if (cookieSpec.match(cookie, cookieOrigin)) { + if (this.log.isDebugEnabled()) { + this.log.debug("Cookie " + cookie + " match " + cookieOrigin); + } + matchedCookies.add(cookie); + } + } + // Generate Cookie request headers + if (!matchedCookies.isEmpty()) { + List<Header> headers = cookieSpec.formatCookies(matchedCookies); + for (Header header : headers) { + request.addHeader(header); + } + } + + int ver = cookieSpec.getVersion(); + if (ver > 0) { + boolean needVersionHeader = false; + for (Cookie cookie : matchedCookies) { + if (ver != cookie.getVersion()) { + needVersionHeader = true; + } + } + + if (needVersionHeader) { + Header header = cookieSpec.getVersionHeader(); + if (header != null) { + // Advertise cookie version support + request.addHeader(header); + } + } + } + + // Stick the CookieSpec and CookieOrigin instances to the HTTP context + // so they could be obtained by the response interceptor + context.setAttribute(ClientContext.COOKIE_SPEC, cookieSpec); + context.setAttribute(ClientContext.COOKIE_ORIGIN, cookieOrigin); + } + +} diff --git a/src/org/apache/http/client/protocol/RequestDefaultHeaders.java b/src/org/apache/http/client/protocol/RequestDefaultHeaders.java new file mode 100644 index 0000000..27d5cc7 --- /dev/null +++ b/src/org/apache/http/client/protocol/RequestDefaultHeaders.java @@ -0,0 +1,74 @@ +/* + * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/client/protocol/RequestDefaultHeaders.java $ + * $Revision: 653041 $ + * $Date: 2008-05-03 03:39:28 -0700 (Sat, 03 May 2008) $ + * + * ==================================================================== + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * <http://www.apache.org/>. + * + */ + +package org.apache.http.client.protocol; + +import java.io.IOException; +import java.util.Collection; + +import org.apache.http.Header; +import org.apache.http.HttpException; +import org.apache.http.HttpRequest; +import org.apache.http.HttpRequestInterceptor; +import org.apache.http.client.params.ClientPNames; +import org.apache.http.protocol.HttpContext; + +/** + * Request interceptor that adds default request headers. + * + * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a> + * + * @version $Revision: 653041 $ + * + * @since 4.0 + */ +public class RequestDefaultHeaders implements HttpRequestInterceptor { + + public RequestDefaultHeaders() { + super(); + } + + public void process(final HttpRequest request, final HttpContext context) + throws HttpException, IOException { + if (request == null) { + throw new IllegalArgumentException("HTTP request may not be null"); + } + // Add default headers + Collection<?> defHeaders = (Collection<?>) request.getParams().getParameter( + ClientPNames.DEFAULT_HEADERS); + if (defHeaders != null) { + for (Object defHeader : defHeaders) { + request.addHeader((Header) defHeader); + } + } + } + +} diff --git a/src/org/apache/http/client/protocol/RequestProxyAuthentication.java b/src/org/apache/http/client/protocol/RequestProxyAuthentication.java new file mode 100644 index 0000000..b4dfe76 --- /dev/null +++ b/src/org/apache/http/client/protocol/RequestProxyAuthentication.java @@ -0,0 +1,104 @@ +/* + * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/client/protocol/RequestProxyAuthentication.java $ + * $Revision: 673450 $ + * $Date: 2008-07-02 10:35:05 -0700 (Wed, 02 Jul 2008) $ + * + * ==================================================================== + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * <http://www.apache.org/>. + * + */ + +package org.apache.http.client.protocol; + +import java.io.IOException; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.http.HttpException; +import org.apache.http.HttpRequest; +import org.apache.http.HttpRequestInterceptor; +import org.apache.http.auth.AUTH; +import org.apache.http.auth.AuthScheme; +import org.apache.http.auth.AuthState; +import org.apache.http.auth.AuthenticationException; +import org.apache.http.auth.Credentials; +import org.apache.http.protocol.HttpContext; + +/** + * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a> + * + * @version $Revision: 673450 $ + * + * @since 4.0 + */ +public class RequestProxyAuthentication implements HttpRequestInterceptor { + + private final Log log = LogFactory.getLog(getClass()); + + public RequestProxyAuthentication() { + super(); + } + + public void process(final HttpRequest request, final HttpContext context) + throws HttpException, IOException { + if (request == null) { + throw new IllegalArgumentException("HTTP request may not be null"); + } + if (context == null) { + throw new IllegalArgumentException("HTTP context may not be null"); + } + + if (request.containsHeader(AUTH.PROXY_AUTH_RESP)) { + return; + } + + // Obtain authentication state + AuthState authState = (AuthState) context.getAttribute( + ClientContext.PROXY_AUTH_STATE); + if (authState == null) { + return; + } + + AuthScheme authScheme = authState.getAuthScheme(); + if (authScheme == null) { + return; + } + + Credentials creds = authState.getCredentials(); + if (creds == null) { + this.log.debug("User credentials not available"); + return; + } + if (authState.getAuthScope() != null || !authScheme.isConnectionBased()) { + try { + request.addHeader(authScheme.authenticate(creds, request)); + } catch (AuthenticationException ex) { + if (this.log.isErrorEnabled()) { + this.log.error("Proxy authentication error: " + ex.getMessage()); + } + } + } + } + +} diff --git a/src/org/apache/http/client/protocol/RequestTargetAuthentication.java b/src/org/apache/http/client/protocol/RequestTargetAuthentication.java new file mode 100644 index 0000000..c140183 --- /dev/null +++ b/src/org/apache/http/client/protocol/RequestTargetAuthentication.java @@ -0,0 +1,105 @@ +/* + * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/client/protocol/RequestTargetAuthentication.java $ + * $Revision: 673450 $ + * $Date: 2008-07-02 10:35:05 -0700 (Wed, 02 Jul 2008) $ + * + * ==================================================================== + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * <http://www.apache.org/>. + * + */ + +package org.apache.http.client.protocol; + +import java.io.IOException; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.http.HttpException; +import org.apache.http.HttpRequest; +import org.apache.http.HttpRequestInterceptor; +import org.apache.http.auth.AUTH; +import org.apache.http.auth.AuthScheme; +import org.apache.http.auth.AuthState; +import org.apache.http.auth.AuthenticationException; +import org.apache.http.auth.Credentials; +import org.apache.http.protocol.HttpContext; + +/** + * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a> + * + * @version $Revision: 673450 $ + * + * @since 4.0 + */ +public class RequestTargetAuthentication implements HttpRequestInterceptor { + + private final Log log = LogFactory.getLog(getClass()); + + public RequestTargetAuthentication() { + super(); + } + + public void process(final HttpRequest request, final HttpContext context) + throws HttpException, IOException { + if (request == null) { + throw new IllegalArgumentException("HTTP request may not be null"); + } + if (context == null) { + throw new IllegalArgumentException("HTTP context may not be null"); + } + + if (request.containsHeader(AUTH.WWW_AUTH_RESP)) { + return; + } + + // Obtain authentication state + AuthState authState = (AuthState) context.getAttribute( + ClientContext.TARGET_AUTH_STATE); + if (authState == null) { + return; + } + + AuthScheme authScheme = authState.getAuthScheme(); + if (authScheme == null) { + return; + } + + Credentials creds = authState.getCredentials(); + if (creds == null) { + this.log.debug("User credentials not available"); + return; + } + + if (authState.getAuthScope() != null || !authScheme.isConnectionBased()) { + try { + request.addHeader(authScheme.authenticate(creds, request)); + } catch (AuthenticationException ex) { + if (this.log.isErrorEnabled()) { + this.log.error("Authentication error: " + ex.getMessage()); + } + } + } + } + +} diff --git a/src/org/apache/http/client/protocol/ResponseProcessCookies.java b/src/org/apache/http/client/protocol/ResponseProcessCookies.java new file mode 100644 index 0000000..0689e93 --- /dev/null +++ b/src/org/apache/http/client/protocol/ResponseProcessCookies.java @@ -0,0 +1,146 @@ +/* + * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/client/protocol/ResponseProcessCookies.java $ + * $Revision: 673450 $ + * $Date: 2008-07-02 10:35:05 -0700 (Wed, 02 Jul 2008) $ + * + * ==================================================================== + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * <http://www.apache.org/>. + * + */ + +package org.apache.http.client.protocol; + +import java.io.IOException; +import java.util.List; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.http.Header; +import org.apache.http.HeaderIterator; +import org.apache.http.HttpException; +import org.apache.http.HttpResponse; +import org.apache.http.HttpResponseInterceptor; +import org.apache.http.client.CookieStore; +import org.apache.http.cookie.Cookie; +import org.apache.http.cookie.CookieOrigin; +import org.apache.http.cookie.CookieSpec; +import org.apache.http.cookie.MalformedCookieException; +import org.apache.http.cookie.SM; +import org.apache.http.protocol.HttpContext; + +/** + * Response interceptor that populates the current {@link CookieStore} with data + * contained in response cookies received in the given the HTTP response. + * + * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a> + * + * @version $Revision: 673450 $ + * + * @since 4.0 + */ +public class ResponseProcessCookies implements HttpResponseInterceptor { + + private final Log log = LogFactory.getLog(getClass()); + + public ResponseProcessCookies() { + super(); + } + + public void process(final HttpResponse response, final HttpContext context) + throws HttpException, IOException { + 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( + final HeaderIterator iterator, + final CookieSpec cookieSpec, + final CookieOrigin cookieOrigin, + final 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()); + } + } + } + } + +} diff --git a/src/org/apache/http/client/protocol/package.html b/src/org/apache/http/client/protocol/package.html new file mode 100644 index 0000000..43dd0d6 --- /dev/null +++ b/src/org/apache/http/client/protocol/package.html @@ -0,0 +1,40 @@ +<html> +<head> +<!-- +/* + * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/client/protocol/package.html $ + * $Revision: 555193 $ + * $Date: 2007-07-11 00:36:47 -0700 (Wed, 11 Jul 2007) $ + * + * ==================================================================== + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * <http://www.apache.org/>. + * + */ +--> +</head> +<body> +Additional request and response interceptors. + +</body> +</html> |