001: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
002: *
003: * Licensed under the Apache License, Version 2.0 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software
010: * distributed under the License is distributed on an "AS IS" BASIS,
011: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: * See the License for the specific language governing permissions and
013: * limitations under the License.
014: */
015:
016: package org.acegisecurity.util;
017:
018: import org.springframework.util.Assert;
019:
020: import java.util.HashMap;
021: import java.util.Iterator;
022: import java.util.Map;
023:
024: /**
025: * Concrete implementation of {@link PortMapper} that obtains HTTP:HTTPS pairs from the application context.<P>By
026: * default the implementation will assume 80:443 and 8080:8443 are HTTP:HTTPS pairs respectively. If different pairs
027: * are required, use {@link #setPortMappings(Map)}.</p>
028: *
029: * @author Ben Alex
030: * @author colin sampaleanu
031: * @version $Id: PortMapperImpl.java 1496 2006-05-23 13:38:33Z benalex $
032: */
033: public class PortMapperImpl implements PortMapper {
034: //~ Instance fields ================================================================================================
035:
036: private Map httpsPortMappings;
037:
038: //~ Constructors ===================================================================================================
039:
040: public PortMapperImpl() {
041: httpsPortMappings = new HashMap();
042: httpsPortMappings.put(new Integer(80), new Integer(443));
043: httpsPortMappings.put(new Integer(8080), new Integer(8443));
044: }
045:
046: //~ Methods ========================================================================================================
047:
048: /**
049: * Returns the translated (Integer -> Integer) version of the original port mapping specified via
050: * setHttpsPortMapping()
051: *
052: * @return DOCUMENT ME!
053: */
054: public Map getTranslatedPortMappings() {
055: return httpsPortMappings;
056: }
057:
058: public Integer lookupHttpPort(Integer httpsPort) {
059: Iterator iter = httpsPortMappings.keySet().iterator();
060:
061: while (iter.hasNext()) {
062: Integer httpPort = (Integer) iter.next();
063:
064: if (httpsPortMappings.get(httpPort).equals(httpsPort)) {
065: return httpPort;
066: }
067: }
068:
069: return null;
070: }
071:
072: public Integer lookupHttpsPort(Integer httpPort) {
073: return (Integer) httpsPortMappings.get(httpPort);
074: }
075:
076: /**
077: * <p>Set to override the default HTTP port to HTTPS port mappings of 80:443, and 8080:8443.</p>
078: * In a Spring XML ApplicationContext, a definition would look something like this:<pre>
079: * <property name="portMappings"> <map> <entry key="80"><value>443</value></entry>
080: * <entry key="8080"><value>8443</value></entry> </map> </property></pre>
081: *
082: * @param newMappings A Map consisting of String keys and String values, where for each entry the key is the string
083: * representation of an integer HTTP port number, and the value is the string representation of the
084: * corresponding integer HTTPS port number.
085: *
086: * @throws IllegalArgumentException if input map does not consist of String keys and values, each representing an
087: * integer port number in the range 1-65535 for that mapping.
088: */
089: public void setPortMappings(Map newMappings) {
090: Assert.notNull(newMappings,
091: "A valid list of HTTPS port mappings must be provided");
092:
093: httpsPortMappings.clear();
094:
095: Iterator it = newMappings.entrySet().iterator();
096:
097: while (it.hasNext()) {
098: Map.Entry entry = (Map.Entry) it.next();
099: Integer httpPort = new Integer((String) entry.getKey());
100: Integer httpsPort = new Integer((String) entry.getValue());
101:
102: if ((httpPort.intValue() < 1)
103: || (httpPort.intValue() > 65535)
104: || (httpsPort.intValue() < 1)
105: || (httpsPort.intValue() > 65535)) {
106: throw new IllegalArgumentException(
107: "one or both ports out of legal range: "
108: + httpPort + ", " + httpsPort);
109: }
110:
111: httpsPortMappings.put(httpPort, httpsPort);
112: }
113:
114: if (httpsPortMappings.size() < 1) {
115: throw new IllegalArgumentException(
116: "must map at least one port");
117: }
118: }
119: }
|