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