View Javadoc

    1   /* Copyright 2004 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.acegisecurity.util;
    17
    18  import org.springframework.beans.factory.InitializingBean;
    19  import org.springframework.util.Assert;
    20
    21  import javax.servlet.ServletRequest;
    22
    23
    24  /***
    25   * Concrete implementation of {@link PortResolver} that obtains the port from
    26   * <code>ServletRequest.getServerPort()</code>.
    27   *
    28   * <P>
    29   * This class is capable of handling the IE bug which results in an incorrect
    30   * URL being presented in the header subsequent to a redirect to a different
    31   * scheme and port where the port is not a well-known number (ie 80 or 443).
    32   * Handling involves detecting an incorrect response from
    33   * <code>ServletRequest.getServerPort()</code> for the scheme (eg a HTTP
    34   * request on 8443) and then determining the real server port (eg HTTP request
    35   * is really on 8080). The map of valid ports is obtained from the configured
    36   * {@link PortMapper}.
    37   * </p>
    38   *
    39   * @author Ben Alex
    40   * @version $Id: PortResolverImpl.java,v 1.4 2005/11/17 00:56:09 benalex Exp $
    41   */
    42  public class PortResolverImpl implements InitializingBean, PortResolver {
    43      //~ Instance fields ========================================================
    44
    45      private PortMapper portMapper = new PortMapperImpl();
    46
    47      //~ Methods ================================================================
    48
    49      public void setPortMapper(PortMapper portMapper) {
    50          this.portMapper = portMapper;
    51      }
    52
    53      public PortMapper getPortMapper() {
    54          return portMapper;
    55      }
    56
    57      public int getServerPort(ServletRequest request) {
    58          int result = request.getServerPort();
    59
    60          if ("http".equals(request.getScheme().toLowerCase())) {
    61              Integer http = portMapper.lookupHttpPort(new Integer(result));
    62
    63              if (http != null) {
    64                  // IE 6 bug
    65                  result = http.intValue();
    66              }
    67          }
    68
    69          if ("https".equals(request.getScheme().toLowerCase())) {
    70              Integer https = portMapper.lookupHttpsPort(new Integer(result));
    71
    72              if (https != null) {
    73                  // IE 6 bug
    74                  result = https.intValue();
    75              }
    76          }
    77
    78          return result;
    79      }
    80
    81      public void afterPropertiesSet() throws Exception {
    82          Assert.notNull(portMapper, "portMapper required");
    83      }
    84  }