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.util.Assert;
    19
    20  import java.util.HashMap;
    21  import java.util.Iterator;
    22  import java.util.Map;
    23
    24
    25  /***
    26   * Concrete implementation of {@link PortMapper} that obtains HTTP:HTTPS pairs
    27   * from the application context.
    28   *
    29   * <P>
    30   * By default the implementation will assume 80:443 and 8080:8443 are
    31   * HTTP:HTTPS pairs respectively. If different pairs are required, use {@link
    32   * #setPortMappings(Map)}.
    33   * </p>
    34   *
    35   * @author Ben Alex
    36   * @author colin sampaleanu
    37   * @version $Id: PortMapperImpl.java,v 1.4 2005/11/25 04:29:32 benalex Exp $
    38   */
    39  public class PortMapperImpl implements PortMapper {
    40      //~ Instance fields ========================================================
    41
    42      private Map httpsPortMappings;
    43
    44      //~ Constructors ===========================================================
    45
    46      public PortMapperImpl() {
    47          httpsPortMappings = new HashMap();
    48          httpsPortMappings.put(new Integer(80), new Integer(443));
    49          httpsPortMappings.put(new Integer(8080), new Integer(8443));
    50      }
    51
    52      //~ Methods ================================================================
    53
    54      /***
    55       * <p>
    56       * Set to override the default HTTP port to HTTPS port mappings of 80:443,
    57       * and  8080:8443.
    58       * </p>
    59       * In a Spring XML ApplicationContext, a definition would look something
    60       * like this:
    61       * <pre>
    62       *   &lt;property name="portMappings">
    63       *     &lt;map>
    64       *       &lt;entry key="80">&lt;value>443&lt;/value>&lt;/entry>
    65       *       &lt;entry key="8080">&lt;value>8443&lt;/value>&lt;/entry>
    66       *     &lt;/map>
    67       *   &lt;/property>
    68       * </pre>
    69       *
    70       * @param newMappings A Map consisting of String keys and String values,
    71       *        where for each entry the key is the string representation of an
    72       *        integer HTTP port number, and the value is the string
    73       *        representation of the corresponding integer HTTPS port number.
    74       *
    75       * @throws IllegalArgumentException if input map does not consist of String
    76       *         keys and values, each representing an integer port number in
    77       *         the range 1-65535 for that mapping.
    78       */
    79      public void setPortMappings(Map newMappings) {
    80          Assert.notNull(newMappings, "A valid list of HTTPS port mappings must be provided");
    81
    82          httpsPortMappings.clear();
    83
    84          Iterator it = newMappings.entrySet().iterator();
    85
    86          while (it.hasNext()) {
    87              Map.Entry entry = (Map.Entry) it.next();
    88              Integer httpPort = new Integer((String) entry.getKey());
    89              Integer httpsPort = new Integer((String) entry.getValue());
    90
    91              if ((httpPort.intValue() < 1) || (httpPort.intValue() > 65535)
    92                      || (httpsPort.intValue() < 1) || (httpsPort.intValue() > 65535)) {
    93                  throw new IllegalArgumentException("one or both ports out of legal range: " + httpPort + ", "
    94                          + httpsPort);
    95              }
    96
    97              httpsPortMappings.put(httpPort, httpsPort);
    98          }
    99
    100         if (httpsPortMappings.size() < 1) {
    101             throw new IllegalArgumentException("must map at least one port");
    102         }
    103     }
    104
    105     /***
    106      * Returns the translated (Integer -> Integer) version of the original port
    107      * mapping specified via setHttpsPortMapping()
    108      *
    109      * @return DOCUMENT ME!
    110      */
    111     public Map getTranslatedPortMappings() {
    112         return httpsPortMappings;
    113     }
    114
    115     public Integer lookupHttpPort(Integer httpsPort) {
    116         Iterator iter = httpsPortMappings.keySet().iterator();
    117
    118         while (iter.hasNext()) {
    119             Integer httpPort = (Integer) iter.next();
    120
    121             if (httpsPortMappings.get(httpPort).equals(httpsPort)) {
    122                 return httpPort;
    123             }
    124         }
    125
    126         return null;
    127     }
    128
    129     public Integer lookupHttpsPort(Integer httpPort) {
    130         return (Integer) httpsPortMappings.get(httpPort);
    131     }
    132 }