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.http.annotation.Immutable;
34  
35  import org.apache.commons.logging.Log;
36  import org.apache.commons.logging.LogFactory;
37  import org.apache.http.Header;
38  import org.apache.http.HttpHost;
39  import org.apache.http.HttpRequest;
40  import org.apache.http.HttpResponse;
41  import org.apache.http.HttpStatus;
42  import org.apache.http.ProtocolException;
43  import org.apache.http.client.CircularRedirectException;
44  import org.apache.http.client.RedirectHandler;
45  import org.apache.http.client.methods.HttpGet;
46  import org.apache.http.client.methods.HttpHead;
47  import org.apache.http.client.params.ClientPNames;
48  import org.apache.http.client.utils.URIUtils;
49  import org.apache.http.params.HttpParams;
50  import org.apache.http.protocol.HttpContext;
51  import org.apache.http.protocol.ExecutionContext;
52  
53  /**
54   * Default implementation of {@link RedirectHandler}.
55   *
56   * @since 4.0
57   *
58   * @deprecated (4.1)  use {@link DefaultRedirectStrategy}.
59   */
60  @Immutable
61  @Deprecated
62  public class DefaultRedirectHandler implements RedirectHandler {
63  
64      private final Log log = LogFactory.getLog(getClass());
65  
66      private static final String REDIRECT_LOCATIONS = "http.protocol.redirect-locations";
67  
68      public DefaultRedirectHandler() {
69          super();
70      }
71  
72      public boolean isRedirectRequested(
73              final HttpResponse response,
74              final HttpContext context) {
75          if (response == null) {
76              throw new IllegalArgumentException("HTTP response may not be null");
77          }
78  
79          int statusCode = response.getStatusLine().getStatusCode();
80          switch (statusCode) {
81          case HttpStatus.SC_MOVED_TEMPORARILY:
82          case HttpStatus.SC_MOVED_PERMANENTLY:
83          case HttpStatus.SC_TEMPORARY_REDIRECT:
84              HttpRequest request = (HttpRequest) context.getAttribute(
85                      ExecutionContext.HTTP_REQUEST);
86              String method = request.getRequestLine().getMethod();
87              return method.equalsIgnoreCase(HttpGet.METHOD_NAME)
88                  || method.equalsIgnoreCase(HttpHead.METHOD_NAME);
89          case HttpStatus.SC_SEE_OTHER:
90              return true;
91          default:
92              return false;
93          } //end of switch
94      }
95  
96      public URI getLocationURI(
97              final HttpResponse response,
98              final HttpContext context) throws ProtocolException {
99          if (response == null) {
100             throw new IllegalArgumentException("HTTP response may not be null");
101         }
102         //get the location header to find out where to redirect to
103         Header locationHeader = response.getFirstHeader("location");
104         if (locationHeader == null) {
105             // got a redirect response, but no location header
106             throw new ProtocolException(
107                     "Received redirect response " + response.getStatusLine()
108                     + " but no location header");
109         }
110         String location = locationHeader.getValue();
111         if (this.log.isDebugEnabled()) {
112             this.log.debug("Redirect requested to location '" + location + "'");
113         }
114 
115         URI uri;
116         try {
117             uri = new URI(location);
118         } catch (URISyntaxException ex) {
119             throw new ProtocolException("Invalid redirect URI: " + location, ex);
120         }
121 
122         HttpParams params = response.getParams();
123         // rfc2616 demands the location value be a complete URI
124         // Location       = "Location" ":" absoluteURI
125         if (!uri.isAbsolute()) {
126             if (params.isParameterTrue(ClientPNames.REJECT_RELATIVE_REDIRECT)) {
127                 throw new ProtocolException("Relative redirect location '"
128                         + uri + "' not allowed");
129             }
130             // Adjust location URI
131             HttpHost target = (HttpHost) context.getAttribute(
132                     ExecutionContext.HTTP_TARGET_HOST);
133             if (target == null) {
134                 throw new IllegalStateException("Target host not available " +
135                         "in the HTTP context");
136             }
137 
138             HttpRequest request = (HttpRequest) context.getAttribute(
139                     ExecutionContext.HTTP_REQUEST);
140 
141             try {
142                 URI requestURI = new URI(request.getRequestLine().getUri());
143                 URI absoluteRequestURI = URIUtils.rewriteURI(requestURI, target, true);
144                 uri = URIUtils.resolve(absoluteRequestURI, uri);
145             } catch (URISyntaxException ex) {
146                 throw new ProtocolException(ex.getMessage(), ex);
147             }
148         }
149 
150         if (params.isParameterFalse(ClientPNames.ALLOW_CIRCULAR_REDIRECTS)) {
151 
152             RedirectLocations redirectLocations = (RedirectLocations) context.getAttribute(
153                     REDIRECT_LOCATIONS);
154 
155             if (redirectLocations == null) {
156                 redirectLocations = new RedirectLocations();
157                 context.setAttribute(REDIRECT_LOCATIONS, redirectLocations);
158             }
159 
160             URI redirectURI;
161             if (uri.getFragment() != null) {
162                 try {
163                     HttpHost target = new HttpHost(
164                             uri.getHost(),
165                             uri.getPort(),
166                             uri.getScheme());
167                     redirectURI = URIUtils.rewriteURI(uri, target, true);
168                 } catch (URISyntaxException ex) {
169                     throw new ProtocolException(ex.getMessage(), ex);
170                 }
171             } else {
172                 redirectURI = uri;
173             }
174 
175             if (redirectLocations.contains(redirectURI)) {
176                 throw new CircularRedirectException("Circular redirect to '" +
177                         redirectURI + "'");
178             } else {
179                 redirectLocations.add(redirectURI);
180             }
181         }
182 
183         return uri;
184     }
185 
186 }