View Javadoc

1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.http.impl.client;
29  
30  import java.net.URI;
31  import java.net.URISyntaxException;
32  
33  import org.apache.commons.logging.Log;
34  import org.apache.commons.logging.LogFactory;
35  import org.apache.http.Header;
36  import org.apache.http.HttpHost;
37  import org.apache.http.HttpRequest;
38  import org.apache.http.HttpResponse;
39  import org.apache.http.HttpStatus;
40  import org.apache.http.ProtocolException;
41  import org.apache.http.annotation.Immutable;
42  import org.apache.http.client.CircularRedirectException;
43  import org.apache.http.client.RedirectHandler;
44  import org.apache.http.client.methods.HttpGet;
45  import org.apache.http.client.methods.HttpHead;
46  import org.apache.http.client.params.ClientPNames;
47  import org.apache.http.client.utils.URIUtils;
48  import org.apache.http.params.HttpParams;
49  import org.apache.http.protocol.ExecutionContext;
50  import org.apache.http.protocol.HttpContext;
51  import org.apache.http.util.Args;
52  import org.apache.http.util.Asserts;
53  
54  /**
55   * Default implementation of {@link RedirectHandler}.
56   *
57   * @since 4.0
58   *
59   * @deprecated (4.1)  use {@link DefaultRedirectStrategy}.
60   */
61  @Immutable
62  @Deprecated
63  public class DefaultRedirectHandler implements RedirectHandler {
64  
65      private final Log log = LogFactory.getLog(getClass());
66  
67      private static final String REDIRECT_LOCATIONS = "http.protocol.redirect-locations";
68  
69      public DefaultRedirectHandler() {
70          super();
71      }
72  
73      public boolean isRedirectRequested(
74              final HttpResponse response,
75              final HttpContext context) {
76          Args.notNull(response, "HTTP response");
77  
78          final int statusCode = response.getStatusLine().getStatusCode();
79          switch (statusCode) {
80          case HttpStatus.SC_MOVED_TEMPORARILY:
81          case HttpStatus.SC_MOVED_PERMANENTLY:
82          case HttpStatus.SC_TEMPORARY_REDIRECT:
83              final HttpRequest request = (HttpRequest) context.getAttribute(
84                      ExecutionContext.HTTP_REQUEST);
85              final String method = request.getRequestLine().getMethod();
86              return method.equalsIgnoreCase(HttpGet.METHOD_NAME)
87                  || method.equalsIgnoreCase(HttpHead.METHOD_NAME);
88          case HttpStatus.SC_SEE_OTHER:
89              return true;
90          default:
91              return false;
92          } //end of switch
93      }
94  
95      public URI getLocationURI(
96              final HttpResponse response,
97              final HttpContext context) throws ProtocolException {
98          Args.notNull(response, "HTTP response");
99          //get the location header to find out where to redirect to
100         final Header locationHeader = response.getFirstHeader("location");
101         if (locationHeader == null) {
102             // got a redirect response, but no location header
103             throw new ProtocolException(
104                     "Received redirect response " + response.getStatusLine()
105                     + " but no location header");
106         }
107         final String location = locationHeader.getValue();
108         if (this.log.isDebugEnabled()) {
109             this.log.debug("Redirect requested to location '" + location + "'");
110         }
111 
112         URI uri;
113         try {
114             uri = new URI(location);
115         } catch (final URISyntaxException ex) {
116             throw new ProtocolException("Invalid redirect URI: " + location, ex);
117         }
118 
119         final HttpParams params = response.getParams();
120         // rfc2616 demands the location value be a complete URI
121         // Location       = "Location" ":" absoluteURI
122         if (!uri.isAbsolute()) {
123             if (params.isParameterTrue(ClientPNames.REJECT_RELATIVE_REDIRECT)) {
124                 throw new ProtocolException("Relative redirect location '"
125                         + uri + "' not allowed");
126             }
127             // Adjust location URI
128             final HttpHost target = (HttpHost) context.getAttribute(
129                     ExecutionContext.HTTP_TARGET_HOST);
130             Asserts.notNull(target, "Target host");
131 
132             final HttpRequest request = (HttpRequest) context.getAttribute(
133                     ExecutionContext.HTTP_REQUEST);
134 
135             try {
136                 final URI requestURI = new URI(request.getRequestLine().getUri());
137                 final URI absoluteRequestURI = URIUtils.rewriteURI(requestURI, target, true);
138                 uri = URIUtils.resolve(absoluteRequestURI, uri);
139             } catch (final URISyntaxException ex) {
140                 throw new ProtocolException(ex.getMessage(), ex);
141             }
142         }
143 
144         if (params.isParameterFalse(ClientPNames.ALLOW_CIRCULAR_REDIRECTS)) {
145 
146             RedirectLocations redirectLocations = (RedirectLocations) context.getAttribute(
147                     REDIRECT_LOCATIONS);
148 
149             if (redirectLocations == null) {
150                 redirectLocations = new RedirectLocations();
151                 context.setAttribute(REDIRECT_LOCATIONS, redirectLocations);
152             }
153 
154             final URI redirectURI;
155             if (uri.getFragment() != null) {
156                 try {
157                     final HttpHost target = new HttpHost(
158                             uri.getHost(),
159                             uri.getPort(),
160                             uri.getScheme());
161                     redirectURI = URIUtils.rewriteURI(uri, target, true);
162                 } catch (final URISyntaxException ex) {
163                     throw new ProtocolException(ex.getMessage(), ex);
164                 }
165             } else {
166                 redirectURI = uri;
167             }
168 
169             if (redirectLocations.contains(redirectURI)) {
170                 throw new CircularRedirectException("Circular redirect to '" +
171                         redirectURI + "'");
172             } else {
173                 redirectLocations.add(redirectURI);
174             }
175         }
176 
177         return uri;
178     }
179 
180 }