Coverage Report - org.acegisecurity.securechannel.RetryWithHttpsEntryPoint
 
Classes in this File Line Coverage Branch Coverage Complexity
RetryWithHttpsEntryPoint
97% 
100% 
1.5
 
 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  14
 public class RetryWithHttpsEntryPoint implements InitializingBean, ChannelEntryPoint {
 48  
     //~ Static fields/initializers =====================================================================================
 49  
 
 50  2
     private static final Log logger = LogFactory.getLog(RetryWithHttpsEntryPoint.class);
 51  
 
 52  
     //~ Instance fields ================================================================================================
 53  
 
 54  14
     private PortMapper portMapper = new PortMapperImpl();
 55  14
     private PortResolver portResolver = new PortResolverImpl();
 56  
 
 57  
     //~ Methods ========================================================================================================
 58  
 
 59  
     public void afterPropertiesSet() throws Exception {
 60  6
         Assert.notNull(portMapper, "portMapper is required");
 61  5
         Assert.notNull(portResolver, "portResolver is required");
 62  4
     }
 63  
 
 64  
     public void commence(ServletRequest request, ServletResponse response)
 65  
         throws IOException, ServletException {
 66  5
         HttpServletRequest req = (HttpServletRequest) request;
 67  
 
 68  5
         String pathInfo = req.getPathInfo();
 69  5
         String queryString = req.getQueryString();
 70  5
         String contextPath = req.getContextPath();
 71  5
         String destination = req.getServletPath() + ((pathInfo == null) ? "" : pathInfo)
 72  
             + ((queryString == null) ? "" : ("?" + queryString));
 73  
 
 74  5
         String redirectUrl = contextPath;
 75  
 
 76  5
         Integer httpPort = new Integer(portResolver.getServerPort(req));
 77  5
         Integer httpsPort = portMapper.lookupHttpsPort(httpPort);
 78  
 
 79  5
         if (httpsPort != null) {
 80  4
             boolean includePort = true;
 81  
 
 82  4
             if (httpsPort.intValue() == 443) {
 83  2
                 includePort = false;
 84  
             }
 85  
 
 86  4
             redirectUrl = "https://" + req.getServerName() + ((includePort) ? (":" + httpsPort) : "") + contextPath
 87  
                 + destination;
 88  
         }
 89  
 
 90  5
         if (logger.isDebugEnabled()) {
 91  0
             logger.debug("Redirecting to: " + redirectUrl);
 92  
         }
 93  
 
 94  5
         ((HttpServletResponse) response).sendRedirect(((HttpServletResponse) response).encodeRedirectURL(redirectUrl));
 95  5
     }
 96  
 
 97  
     public PortMapper getPortMapper() {
 98  1
         return portMapper;
 99  
     }
 100  
 
 101  
     public PortResolver getPortResolver() {
 102  1
         return portResolver;
 103  
     }
 104  
 
 105  
     public void setPortMapper(PortMapper portMapper) {
 106  6
         this.portMapper = portMapper;
 107  6
     }
 108  
 
 109  
     public void setPortResolver(PortResolver portResolver) {
 110  6
         this.portResolver = portResolver;
 111  6
     }
 112  
 }