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.hc.client5.http.impl;
29  
30  import java.net.URI;
31  import java.net.URISyntaxException;
32  import java.util.Locale;
33  
34  import org.apache.hc.client5.http.protocol.RedirectStrategy;
35  import org.apache.hc.client5.http.utils.URIUtils;
36  import org.apache.hc.core5.annotation.Contract;
37  import org.apache.hc.core5.annotation.ThreadingBehavior;
38  import org.apache.hc.core5.http.Header;
39  import org.apache.hc.core5.http.HttpException;
40  import org.apache.hc.core5.http.HttpHeaders;
41  import org.apache.hc.core5.http.HttpRequest;
42  import org.apache.hc.core5.http.HttpResponse;
43  import org.apache.hc.core5.http.HttpStatus;
44  import org.apache.hc.core5.http.ProtocolException;
45  import org.apache.hc.core5.http.protocol.HttpContext;
46  import org.apache.hc.core5.net.URIBuilder;
47  import org.apache.hc.core5.util.Args;
48  import org.apache.hc.core5.util.TextUtils;
49  
50  /**
51   * Default implementation of {@link RedirectStrategy}.
52   *
53   * @since 4.1
54   */
55  @Contract(threading = ThreadingBehavior.STATELESS)
56  public class DefaultRedirectStrategy implements RedirectStrategy {
57  
58      public static final DefaultRedirectStrategydirectStrategy.html#DefaultRedirectStrategy">DefaultRedirectStrategy INSTANCE = new DefaultRedirectStrategy();
59  
60      @Override
61      public boolean isRedirected(
62              final HttpRequest request,
63              final HttpResponse response,
64              final HttpContext context) throws ProtocolException {
65          Args.notNull(request, "HTTP request");
66          Args.notNull(response, "HTTP response");
67  
68          if (!response.containsHeader(HttpHeaders.LOCATION)) {
69              return false;
70          }
71          final int statusCode = response.getCode();
72          switch (statusCode) {
73              case HttpStatus.SC_MOVED_PERMANENTLY:
74              case HttpStatus.SC_MOVED_TEMPORARILY:
75              case HttpStatus.SC_SEE_OTHER:
76              case HttpStatus.SC_TEMPORARY_REDIRECT:
77              case HttpStatus.SC_PERMANENT_REDIRECT:
78                  return true;
79              default:
80                  return false;
81          }
82      }
83  
84      @Override
85      public URI getLocationURI(
86              final HttpRequest request,
87              final HttpResponse response,
88              final HttpContext context) throws HttpException {
89          Args.notNull(request, "HTTP request");
90          Args.notNull(response, "HTTP response");
91          Args.notNull(context, "HTTP context");
92  
93          //get the location header to find out where to redirect to
94          final Header locationHeader = response.getFirstHeader("location");
95          if (locationHeader == null) {
96              throw new HttpException("Redirect location is missing");
97          }
98          final String location = locationHeader.getValue();
99          URI uri = createLocationURI(location);
100         try {
101             if (!uri.isAbsolute()) {
102                 // Resolve location URI
103                 uri = URIUtils.resolve(request.getUri(), uri);
104             }
105         } catch (final URISyntaxException ex) {
106             throw new ProtocolException(ex.getMessage(), ex);
107         }
108 
109         return uri;
110     }
111 
112     /**
113      * @since 4.1
114      */
115     protected URI createLocationURI(final String location) throws ProtocolException {
116         try {
117             final URIBuilder b = new URIBuilder(new URI(location).normalize());
118             final String host = b.getHost();
119             if (host != null) {
120                 b.setHost(host.toLowerCase(Locale.ROOT));
121             }
122             final String path = b.getPath();
123             if (TextUtils.isEmpty(path)) {
124                 b.setPath("/");
125             }
126             return b.build();
127         } catch (final URISyntaxException ex) {
128             throw new ProtocolException("Invalid redirect URI: " + location, ex);
129         }
130     }
131 
132 }