01: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
02: *
03: * Licensed under the Apache License, Version 2.0 (the "License");
04: * you may not use this file except in compliance with the License.
05: * You may obtain a copy of the License at
06: *
07: * http://www.apache.org/licenses/LICENSE-2.0
08: *
09: * 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.beans.factory.InitializingBean;
19:
20: import org.springframework.util.Assert;
21:
22: import javax.servlet.ServletRequest;
23:
24: /**
25: * Concrete implementation of {@link PortResolver} that obtains the port from
26: * <code>ServletRequest.getServerPort()</code>.<P>This class is capable of handling the IE bug which results in an
27: * incorrect URL being presented in the header subsequent to a redirect to a different scheme and port where the port
28: * is not a well-known number (ie 80 or 443). Handling involves detecting an incorrect response from
29: * <code>ServletRequest.getServerPort()</code> for the scheme (eg a HTTP request on 8443) and then determining the
30: * real server port (eg HTTP request is really on 8080). The map of valid ports is obtained from the configured {@link
31: * PortMapper}.</p>
32: *
33: * @author Ben Alex
34: * @version $Id: PortResolverImpl.java 1496 2006-05-23 13:38:33Z benalex $
35: */
36: public class PortResolverImpl implements InitializingBean, PortResolver {
37: //~ Instance fields ================================================================================================
38:
39: private PortMapper portMapper = new PortMapperImpl();
40:
41: //~ Methods ========================================================================================================
42:
43: public void afterPropertiesSet() throws Exception {
44: Assert.notNull(portMapper, "portMapper required");
45: }
46:
47: public PortMapper getPortMapper() {
48: return portMapper;
49: }
50:
51: public int getServerPort(ServletRequest request) {
52: int result = request.getServerPort();
53:
54: if ("http".equals(request.getScheme().toLowerCase())) {
55: Integer http = portMapper
56: .lookupHttpPort(new Integer(result));
57:
58: if (http != null) {
59: // IE 6 bug
60: result = http.intValue();
61: }
62: }
63:
64: if ("https".equals(request.getScheme().toLowerCase())) {
65: Integer https = portMapper.lookupHttpsPort(new Integer(
66: result));
67:
68: if (https != null) {
69: // IE 6 bug
70: result = https.intValue();
71: }
72: }
73:
74: return result;
75: }
76:
77: public void setPortMapper(PortMapper portMapper) {
78: this.portMapper = portMapper;
79: }
80: }
|