View Javadoc

1   /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
2    *
3    * Licensed under the Apache License, Version 2.0 (the "License");
4    * you may not use this file except in compliance with the License.
5    * You may obtain a copy of the License at
6    *
7    *     http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  
16  package org.springframework.security.util;
17  
18  import org.springframework.util.Assert;
19  
20  import javax.servlet.ServletRequest;
21  
22  
23  /**
24   * Concrete implementation of {@link PortResolver} that obtains the port from <tt>ServletRequest.getServerPort()</tt>.
25   * <p>
26   * This class is capable of handling the IE bug which results in an
27   * incorrect URL being presented in the header subsequent to a redirect to a different scheme and port where the port
28   * is not a well-known number (ie 80 or 443). Handling involves detecting an incorrect response from
29   * <code>ServletRequest.getServerPort()</code> for the scheme (eg a HTTP request on 8443) and then determining the
30   * real server port (eg HTTP request is really on 8080). The map of valid ports is obtained from the configured
31   * {@link PortMapper}.
32   *
33   * @author Ben Alex
34   * @version $Id: PortResolverImpl.java 2458 2008-01-15 17:22:10Z luke_t $
35   */
36  public class PortResolverImpl implements PortResolver {
37      //~ Instance fields ================================================================================================
38  
39      private PortMapper portMapper = new PortMapperImpl();
40  
41      //~ Methods ========================================================================================================
42  
43      public PortMapper getPortMapper() {
44          return portMapper;
45      }
46  
47      public int getServerPort(ServletRequest request) {
48          int serverPort = request.getServerPort();
49          Integer portLookup = null;
50  
51          String scheme = request.getScheme().toLowerCase();
52  
53          if ("http".equals(scheme)) {
54              portLookup = portMapper.lookupHttpPort(new Integer(serverPort));
55  
56          } else if ("https".equals(scheme)) {
57              portLookup = portMapper.lookupHttpsPort(new Integer(serverPort));
58          }
59  
60          if (portLookup != null) {
61              // IE 6 bug
62              serverPort = portLookup.intValue();
63          }
64  
65          return serverPort;
66      }
67  
68      public void setPortMapper(PortMapper portMapper) {
69          Assert.notNull(portMapper, "portMapper cannot be null");
70          this.portMapper = portMapper;
71      }
72  }