View Javadoc
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  
25  public class ProxyHttpServletRequestWrapper extends HttpServletRequestWrapper
26  {
27      public static final String FORWARDED_HOST_HEADER = "X-Forwarded-Host";
28      public static final String FORWARDED_PROTO_HEADER = "X-Forwarded-Proto";
29      public static final String HTTPS_SCHEME = "https";
30      public static final String HTTP_SCHEME = "http";
31      private String scheme;
32      private String serverName;
33      private int serverPort;
34  
35      public ProxyHttpServletRequestWrapper( HttpServletRequest request )
36      {
37          super( request );
38  
39          scheme = request.getHeader( FORWARDED_PROTO_HEADER );
40  
41          if ( !HTTP_SCHEME.equalsIgnoreCase( scheme ) && !HTTPS_SCHEME.equalsIgnoreCase( scheme ) )
42          {
43              scheme = request.getScheme(  );
44          }
45  
46          serverName = request.getServerName(  );
47          serverPort = request.getServerPort(  );
48  
49          String host = request.getHeader( FORWARDED_HOST_HEADER );
50  
51          if ( ( host != null ) && ( host.length(  ) > 0 ) )
52          {
53              int index = host.indexOf( ':' );
54  
55              if ( index < 0 )
56              {
57                  serverName = host;
58                  serverPort = getDefaultPort( scheme );
59              }
60              else
61              {
62                  serverName = host.substring( 0, index );
63  
64                  try
65                  {
66                      serverPort = Integer.parseInt( host.substring( index + 1 ) );
67                  }
68                  catch ( NumberFormatException e )
69                  {
70                      serverPort = getDefaultPort( scheme );
71                  }
72              }
73          }
74      }
75  
76      private int getDefaultPort( String scheme )
77      {
78          if ( HTTPS_SCHEME.equalsIgnoreCase( scheme ) )
79          {
80              return 443;
81          }
82  
83          return 80;
84      }
85  
86      @Override
87      public String getScheme(  )
88      {
89          return scheme;
90      }
91  
92      @Override
93      public String getServerName(  )
94      {
95          return serverName;
96      }
97  
98      @Override
99      public int getServerPort(  )
100     {
101         return serverPort;
102     }
103 }