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