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.testing.classic;
29  
30  import java.io.IOException;
31  import java.net.URI;
32  import java.net.URISyntaxException;
33  
34  import org.apache.hc.client5.testing.redirect.Redirect;
35  import org.apache.hc.client5.testing.redirect.RedirectResolver;
36  import org.apache.hc.core5.http.ClassicHttpRequest;
37  import org.apache.hc.core5.http.ClassicHttpResponse;
38  import org.apache.hc.core5.http.HeaderElements;
39  import org.apache.hc.core5.http.HttpException;
40  import org.apache.hc.core5.http.HttpHeaders;
41  import org.apache.hc.core5.http.ProtocolException;
42  import org.apache.hc.core5.http.io.HttpServerRequestHandler;
43  import org.apache.hc.core5.http.message.BasicClassicHttpResponse;
44  import org.apache.hc.core5.http.message.BasicHeader;
45  import org.apache.hc.core5.http.protocol.HttpContext;
46  import org.apache.hc.core5.util.Args;
47  
48  public class RedirectingDecorator implements HttpServerRequestHandler {
49  
50      private final HttpServerRequestHandler requestHandler;
51      private final RedirectResolver redirectResolver;
52  
53      public RedirectingDecorator(final HttpServerRequestHandler requestHandler,
54                                  final RedirectResolver redirectResolver) {
55          this.requestHandler = Args.notNull(requestHandler, "Request handler");
56          this.redirectResolver = redirectResolver;
57      }
58  
59      @Override
60      public void handle(final ClassicHttpRequest request,
61                         final ResponseTrigger responseTrigger,
62                         final HttpContext context) throws HttpException, IOException {
63          try {
64              final URI requestURI = request.getUri();
65              final Redirect redirect = redirectResolver != null ? redirectResolver.resolve(requestURI) : null;
66              if (redirect != null) {
67                  final ClassicHttpResponse response = new BasicClassicHttpResponse(redirect.status);
68                  if (redirect.location != null) {
69                      response.addHeader(new BasicHeader(HttpHeaders.LOCATION, redirect.location));
70                  }
71                  switch (redirect.connControl) {
72                      case KEEP_ALIVE:
73                          response.addHeader(new BasicHeader(HttpHeaders.CONNECTION, HeaderElements.KEEP_ALIVE));
74                          break;
75                      case CLOSE:
76                          response.addHeader(new BasicHeader(HttpHeaders.CONNECTION, HeaderElements.CLOSE));
77                  }
78                  responseTrigger.submitResponse(response);
79              } else {
80                  requestHandler.handle(request, responseTrigger, context);
81              }
82          } catch (final URISyntaxException ex) {
83              throw new ProtocolException(ex.getMessage(), ex);
84          }
85      }
86  }