View Javadoc

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.securechannel;
17  
18  import org.acegisecurity.util.PortMapper;
19  import org.acegisecurity.util.PortMapperImpl;
20  import org.acegisecurity.util.PortResolver;
21  import org.acegisecurity.util.PortResolverImpl;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  
26  import org.springframework.beans.factory.InitializingBean;
27  
28  import org.springframework.util.Assert;
29  
30  import java.io.IOException;
31  
32  import javax.servlet.ServletException;
33  import javax.servlet.ServletRequest;
34  import javax.servlet.ServletResponse;
35  import javax.servlet.http.HttpServletRequest;
36  import javax.servlet.http.HttpServletResponse;
37  
38  
39  /**
40   * Commences a secure channel by retrying the original request using HTTPS.<P>This entry point should suffice in
41   * most circumstances. However, it is not intended to properly handle HTTP POSTs or other usage where a standard
42   * redirect would cause an issue.</p>
43   *
44   * @author Ben Alex
45   * @version $Id: RetryWithHttpsEntryPoint.java 1496 2006-05-23 13:38:33Z benalex $
46   */
47  public class RetryWithHttpsEntryPoint implements InitializingBean, ChannelEntryPoint {
48      //~ Static fields/initializers =====================================================================================
49  
50      private static final Log logger = LogFactory.getLog(RetryWithHttpsEntryPoint.class);
51  
52      //~ Instance fields ================================================================================================
53  
54      private PortMapper portMapper = new PortMapperImpl();
55      private PortResolver portResolver = new PortResolverImpl();
56  
57      //~ Methods ========================================================================================================
58  
59      public void afterPropertiesSet() throws Exception {
60          Assert.notNull(portMapper, "portMapper is required");
61          Assert.notNull(portResolver, "portResolver is required");
62      }
63  
64      public void commence(ServletRequest request, ServletResponse response)
65          throws IOException, ServletException {
66          HttpServletRequest req = (HttpServletRequest) request;
67  
68          String pathInfo = req.getPathInfo();
69          String queryString = req.getQueryString();
70          String contextPath = req.getContextPath();
71          String destination = req.getServletPath() + ((pathInfo == null) ? "" : pathInfo)
72              + ((queryString == null) ? "" : ("?" + queryString));
73  
74          String redirectUrl = contextPath;
75  
76          Integer httpPort = new Integer(portResolver.getServerPort(req));
77          Integer httpsPort = portMapper.lookupHttpsPort(httpPort);
78  
79          if (httpsPort != null) {
80              boolean includePort = true;
81  
82              if (httpsPort.intValue() == 443) {
83                  includePort = false;
84              }
85  
86              redirectUrl = "https://" + req.getServerName() + ((includePort) ? (":" + httpsPort) : "") + contextPath
87                  + destination;
88          }
89  
90          if (logger.isDebugEnabled()) {
91              logger.debug("Redirecting to: " + redirectUrl);
92          }
93  
94          ((HttpServletResponse) response).sendRedirect(((HttpServletResponse) response).encodeRedirectURL(redirectUrl));
95      }
96  
97      public PortMapper getPortMapper() {
98          return portMapper;
99      }
100 
101     public PortResolver getPortResolver() {
102         return portResolver;
103     }
104 
105     public void setPortMapper(PortMapper portMapper) {
106         this.portMapper = portMapper;
107     }
108 
109     public void setPortResolver(PortResolver portResolver) {
110         this.portResolver = portResolver;
111     }
112 }