1
2
3
4
5
6
7
8
9
10
11
12
13
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
41
42 private Map httpsPortMappings;
43
44
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
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 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 }