This project has retired. For details please refer to its Attic page.
ProxyHttpServletRequestWrapper xref

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.chemistry.opencmis.server.filter;
20  
21  import javax.servlet.http.HttpServletRequest;
22  import javax.servlet.http.HttpServletRequestWrapper;
23  
24  public class ProxyHttpServletRequestWrapper extends HttpServletRequestWrapper {
25  
26      public static final String FORWARDED_HOST_HEADER = "X-Forwarded-Host";
27      public static final String FORWARDED_PROTO_HEADER = "X-Forwarded-Proto";
28      public static final String HTTPS_SCHEME = "https";
29      public static final String HTTP_SCHEME = "http";
30  
31      private String scheme;
32      private String serverName;
33      private int serverPort;
34  
35      public ProxyHttpServletRequestWrapper(HttpServletRequest request) {
36          super(request);
37  
38          scheme = request.getHeader(FORWARDED_PROTO_HEADER);
39  
40          if (!HTTP_SCHEME.equalsIgnoreCase(scheme) && !HTTPS_SCHEME.equalsIgnoreCase(scheme)) {
41              scheme = request.getScheme();
42          }
43  
44          serverName = request.getServerName();
45          serverPort = request.getServerPort();
46  
47          String host = request.getHeader(FORWARDED_HOST_HEADER);
48          if ((host != null) && (host.length() > 0)) {
49              int index = host.indexOf(':');
50              if (index < 0) {
51                  serverName = host;
52                  serverPort = getDefaultPort(scheme);
53              } else {
54                  serverName = host.substring(0, index);
55                  try {
56                      serverPort = Integer.parseInt(host.substring(index + 1));
57                  } catch (NumberFormatException e) {
58                      serverPort = getDefaultPort(scheme);
59                  }
60              }
61          }
62      }
63  
64      private int getDefaultPort(String scheme) {
65          if (HTTPS_SCHEME.equalsIgnoreCase(scheme)) {
66              return 443;
67          }
68  
69          return 80;
70      }
71  
72      @Override
73      public String getScheme() {
74          return scheme;
75      }
76  
77      @Override
78      public String getServerName() {
79          return serverName;
80      }
81  
82      @Override
83      public int getServerPort() {
84          return serverPort;
85      }
86  }