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.net.URI;
michael@0: import java.net.URISyntaxException;
michael@0: import java.util.ArrayList;
michael@0: import java.util.Date;
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.HttpException;
michael@0: import ch.boye.httpclientandroidlib.HttpHost;
michael@0: import ch.boye.httpclientandroidlib.HttpRequest;
michael@0: import ch.boye.httpclientandroidlib.HttpRequestInterceptor;
michael@0: import ch.boye.httpclientandroidlib.ProtocolException;
michael@0: import ch.boye.httpclientandroidlib.client.CookieStore;
michael@0: import ch.boye.httpclientandroidlib.client.methods.HttpUriRequest;
michael@0: import ch.boye.httpclientandroidlib.client.params.HttpClientParams;
michael@0: import ch.boye.httpclientandroidlib.conn.HttpRoutedConnection;
michael@0: import ch.boye.httpclientandroidlib.conn.routing.HttpRoute;
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.CookieSpecRegistry;
michael@0: import ch.boye.httpclientandroidlib.cookie.SetCookie2;
michael@0: import ch.boye.httpclientandroidlib.protocol.HttpContext;
michael@0: import ch.boye.httpclientandroidlib.protocol.ExecutionContext;
michael@0:
michael@0: /**
michael@0: * Request interceptor that matches cookies available in the current
michael@0: * {@link CookieStore} to the request being executed and generates
michael@0: * corresponding Cookie
request headers.
michael@0: *
michael@0: * The following parameters can be used to customize the behavior of this
michael@0: * class:
michael@0: *
michael@0: * - {@link ch.boye.httpclientandroidlib.cookie.params.CookieSpecPNames#DATE_PATTERNS}
michael@0: * - {@link ch.boye.httpclientandroidlib.cookie.params.CookieSpecPNames#SINGLE_COOKIE_HEADER}
michael@0: * - {@link ch.boye.httpclientandroidlib.client.params.ClientPNames#COOKIE_POLICY}
michael@0: *
michael@0: *
michael@0: * @since 4.0
michael@0: */
michael@0: @Immutable
michael@0: public class RequestAddCookies implements HttpRequestInterceptor {
michael@0:
michael@0: public HttpClientAndroidLog log = new HttpClientAndroidLog(getClass());
michael@0:
michael@0: public RequestAddCookies() {
michael@0: super();
michael@0: }
michael@0:
michael@0: public void process(final HttpRequest request, final HttpContext context)
michael@0: throws HttpException, IOException {
michael@0: if (request == 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: String method = request.getRequestLine().getMethod();
michael@0: if (method.equalsIgnoreCase("CONNECT")) {
michael@0: return;
michael@0: }
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:
michael@0: // Obtain the registry of cookie specs
michael@0: CookieSpecRegistry registry = (CookieSpecRegistry) context.getAttribute(
michael@0: ClientContext.COOKIESPEC_REGISTRY);
michael@0: if (registry == null) {
michael@0: this.log.debug("CookieSpec registry not specified in HTTP context");
michael@0: return;
michael@0: }
michael@0:
michael@0: // Obtain the target host (required)
michael@0: HttpHost targetHost = (HttpHost) context.getAttribute(
michael@0: ExecutionContext.HTTP_TARGET_HOST);
michael@0: if (targetHost == null) {
michael@0: this.log.debug("Target host not set in the context");
michael@0: return;
michael@0: }
michael@0:
michael@0: // Obtain the client connection (required)
michael@0: HttpRoutedConnection conn = (HttpRoutedConnection) context.getAttribute(
michael@0: ExecutionContext.HTTP_CONNECTION);
michael@0: if (conn == null) {
michael@0: this.log.debug("HTTP connection not set in the context");
michael@0: return;
michael@0: }
michael@0:
michael@0: String policy = HttpClientParams.getCookiePolicy(request.getParams());
michael@0: if (this.log.isDebugEnabled()) {
michael@0: this.log.debug("CookieSpec selected: " + policy);
michael@0: }
michael@0:
michael@0: URI requestURI;
michael@0: if (request instanceof HttpUriRequest) {
michael@0: requestURI = ((HttpUriRequest) request).getURI();
michael@0: } else {
michael@0: try {
michael@0: requestURI = new URI(request.getRequestLine().getUri());
michael@0: } catch (URISyntaxException ex) {
michael@0: throw new ProtocolException("Invalid request URI: " +
michael@0: request.getRequestLine().getUri(), ex);
michael@0: }
michael@0: }
michael@0:
michael@0: String hostName = targetHost.getHostName();
michael@0: int port = targetHost.getPort();
michael@0: if (port < 0) {
michael@0: HttpRoute route = conn.getRoute();
michael@0: if (route.getHopCount() == 1) {
michael@0: port = conn.getRemotePort();
michael@0: } else {
michael@0: // Target port will be selected by the proxy.
michael@0: // Use conventional ports for known schemes
michael@0: String scheme = targetHost.getSchemeName();
michael@0: if (scheme.equalsIgnoreCase("http")) {
michael@0: port = 80;
michael@0: } else if (scheme.equalsIgnoreCase("https")) {
michael@0: port = 443;
michael@0: } else {
michael@0: port = 0;
michael@0: }
michael@0: }
michael@0: }
michael@0:
michael@0: CookieOrigin cookieOrigin = new CookieOrigin(
michael@0: hostName,
michael@0: port,
michael@0: requestURI.getPath(),
michael@0: conn.isSecure());
michael@0:
michael@0: // Get an instance of the selected cookie policy
michael@0: CookieSpec cookieSpec = registry.getCookieSpec(policy, request.getParams());
michael@0: // Get all cookies available in the HTTP state
michael@0: List cookies = new ArrayList(cookieStore.getCookies());
michael@0: // Find cookies matching the given origin
michael@0: List matchedCookies = new ArrayList();
michael@0: Date now = new Date();
michael@0: for (Cookie cookie : cookies) {
michael@0: if (!cookie.isExpired(now)) {
michael@0: if (cookieSpec.match(cookie, cookieOrigin)) {
michael@0: if (this.log.isDebugEnabled()) {
michael@0: this.log.debug("Cookie " + cookie + " match " + cookieOrigin);
michael@0: }
michael@0: matchedCookies.add(cookie);
michael@0: }
michael@0: } else {
michael@0: if (this.log.isDebugEnabled()) {
michael@0: this.log.debug("Cookie " + cookie + " expired");
michael@0: }
michael@0: }
michael@0: }
michael@0: // Generate Cookie request headers
michael@0: if (!matchedCookies.isEmpty()) {
michael@0: List headers = cookieSpec.formatCookies(matchedCookies);
michael@0: for (Header header : headers) {
michael@0: request.addHeader(header);
michael@0: }
michael@0: }
michael@0:
michael@0: int ver = cookieSpec.getVersion();
michael@0: if (ver > 0) {
michael@0: boolean needVersionHeader = false;
michael@0: for (Cookie cookie : matchedCookies) {
michael@0: if (ver != cookie.getVersion() || !(cookie instanceof SetCookie2)) {
michael@0: needVersionHeader = true;
michael@0: }
michael@0: }
michael@0:
michael@0: if (needVersionHeader) {
michael@0: Header header = cookieSpec.getVersionHeader();
michael@0: if (header != null) {
michael@0: // Advertise cookie version support
michael@0: request.addHeader(header);
michael@0: }
michael@0: }
michael@0: }
michael@0:
michael@0: // Stick the CookieSpec and CookieOrigin instances to the HTTP context
michael@0: // so they could be obtained by the response interceptor
michael@0: context.setAttribute(ClientContext.COOKIE_SPEC, cookieSpec);
michael@0: context.setAttribute(ClientContext.COOKIE_ORIGIN, cookieOrigin);
michael@0: }
michael@0:
michael@0: }