|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| PortMapperImpl.java | 100% | 100% | 100% | 100% |
|
||||||||||||||
| 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 | 163 | public PortMapperImpl() { |
| 47 | 163 | httpsPortMappings = new HashMap(); |
| 48 | 163 | httpsPortMappings.put(new Integer(80), new Integer(443)); |
| 49 | 163 | 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 | * <property name="portMappings"> | |
| 63 | * <map> | |
| 64 | * <entry key="80"><value>443</value></entry> | |
| 65 | * <entry key="8080"><value>8443</value></entry> | |
| 66 | * </map> | |
| 67 | * </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 | 8 | public void setPortMappings(Map newMappings) { |
| 80 | 8 | Assert.notNull(newMappings, "A valid list of HTTPS port mappings must be provided"); |
| 81 | ||
| 82 | 7 | httpsPortMappings.clear(); |
| 83 | ||
| 84 | 7 | Iterator it = newMappings.entrySet().iterator(); |
| 85 | ||
| 86 | 7 | while (it.hasNext()) { |
| 87 | 6 | Map.Entry entry = (Map.Entry) it.next(); |
| 88 | 6 | Integer httpPort = new Integer((String) entry.getKey()); |
| 89 | 6 | Integer httpsPort = new Integer((String) entry.getValue()); |
| 90 | ||
| 91 | 6 | if ((httpPort.intValue() < 1) || (httpPort.intValue() > 65535) |
| 92 | || (httpsPort.intValue() < 1) || (httpsPort.intValue() > 65535)) { | |
| 93 | 1 | throw new IllegalArgumentException("one or both ports out of legal range: " + httpPort + ", " |
| 94 | + httpsPort); | |
| 95 | } | |
| 96 | ||
| 97 | 5 | httpsPortMappings.put(httpPort, httpsPort); |
| 98 | } | |
| 99 | ||
| 100 | 6 | if (httpsPortMappings.size() < 1) { |
| 101 | 1 | 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 | 3 | public Map getTranslatedPortMappings() { |
| 112 | 3 | return httpsPortMappings; |
| 113 | } | |
| 114 | ||
| 115 | 15 | public Integer lookupHttpPort(Integer httpsPort) { |
| 116 | 15 | Iterator iter = httpsPortMappings.keySet().iterator(); |
| 117 | ||
| 118 | 15 | while (iter.hasNext()) { |
| 119 | 25 | Integer httpPort = (Integer) iter.next(); |
| 120 | ||
| 121 | 25 | if (httpsPortMappings.get(httpPort).equals(httpsPort)) { |
| 122 | 8 | return httpPort; |
| 123 | } | |
| 124 | } | |
| 125 | ||
| 126 | 7 | return null; |
| 127 | } | |
| 128 | ||
| 129 | 21 | public Integer lookupHttpsPort(Integer httpPort) { |
| 130 | 21 | return (Integer) httpsPortMappings.get(httpPort); |
| 131 | } | |
| 132 | } |
|
||||||||||