001: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
002: *
003: * Licensed under the Apache License, Version 2.0 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software
010: * distributed under the License is distributed on an "AS IS" BASIS,
011: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: * See the License for the specific language governing permissions and
013: * limitations under the License.
014: */
015:
016: package org.acegisecurity.securechannel;
017:
018: import org.acegisecurity.util.PortMapper;
019: import org.acegisecurity.util.PortMapperImpl;
020: import org.acegisecurity.util.PortResolver;
021: import org.acegisecurity.util.PortResolverImpl;
022:
023: import org.apache.commons.logging.Log;
024: import org.apache.commons.logging.LogFactory;
025:
026: import org.springframework.beans.factory.InitializingBean;
027:
028: import org.springframework.util.Assert;
029:
030: import java.io.IOException;
031:
032: import javax.servlet.ServletException;
033: import javax.servlet.ServletRequest;
034: import javax.servlet.ServletResponse;
035: import javax.servlet.http.HttpServletRequest;
036: import javax.servlet.http.HttpServletResponse;
037:
038: /**
039: * Commences an insecure channel by retrying the original request using HTTP.<P>This entry point should suffice in
040: * most circumstances. However, it is not intended to properly handle HTTP POSTs or other usage where a standard
041: * redirect would cause an issue.</p>
042: *
043: * @author Ben Alex
044: * @version $Id: RetryWithHttpEntryPoint.java 1496 2006-05-23 13:38:33Z benalex $
045: */
046: public class RetryWithHttpEntryPoint implements InitializingBean,
047: ChannelEntryPoint {
048: //~ Static fields/initializers =====================================================================================
049:
050: private static final Log logger = LogFactory
051: .getLog(RetryWithHttpEntryPoint.class);
052:
053: //~ Instance fields ================================================================================================
054:
055: private PortMapper portMapper = new PortMapperImpl();
056: private PortResolver portResolver = new PortResolverImpl();
057:
058: //~ Methods ========================================================================================================
059:
060: public void afterPropertiesSet() throws Exception {
061: Assert.notNull(portMapper, "portMapper is required");
062: Assert.notNull(portResolver, "portResolver is required");
063: }
064:
065: public void commence(ServletRequest request,
066: ServletResponse response) throws IOException,
067: ServletException {
068: HttpServletRequest req = (HttpServletRequest) request;
069:
070: String pathInfo = req.getPathInfo();
071: String queryString = req.getQueryString();
072: String contextPath = req.getContextPath();
073: String destination = req.getServletPath()
074: + ((pathInfo == null) ? "" : pathInfo)
075: + ((queryString == null) ? "" : ("?" + queryString));
076:
077: String redirectUrl = contextPath;
078:
079: Integer httpsPort = new Integer(portResolver.getServerPort(req));
080: Integer httpPort = portMapper.lookupHttpPort(httpsPort);
081:
082: if (httpPort != null) {
083: boolean includePort = true;
084:
085: if (httpPort.intValue() == 80) {
086: includePort = false;
087: }
088:
089: redirectUrl = "http://" + req.getServerName()
090: + ((includePort) ? (":" + httpPort) : "")
091: + contextPath + destination;
092: }
093:
094: if (logger.isDebugEnabled()) {
095: logger.debug("Redirecting to: " + redirectUrl);
096: }
097:
098: ((HttpServletResponse) response)
099: .sendRedirect(((HttpServletResponse) response)
100: .encodeRedirectURL(redirectUrl));
101: }
102:
103: public PortMapper getPortMapper() {
104: return portMapper;
105: }
106:
107: public PortResolver getPortResolver() {
108: return portResolver;
109: }
110:
111: public void setPortMapper(PortMapper portMapper) {
112: this .portMapper = portMapper;
113: }
114:
115: public void setPortResolver(PortResolver portResolver) {
116: this.portResolver = portResolver;
117: }
118: }
|