Clover coverage report - Acegi Security System for Spring - 1.0.0-RC1
Coverage timestamp: Mon Dec 5 2005 09:05:15 EST
file stats: LOC: 119   Methods: 6
NCLOC: 64   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
RetryWithHttpsEntryPoint.java 91.7% 95.5% 100% 95%
coverage coverage
 1    /* Copyright 2004 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    import org.springframework.util.Assert;
 28   
 29    import java.io.IOException;
 30   
 31    import javax.servlet.ServletException;
 32    import javax.servlet.ServletRequest;
 33    import javax.servlet.ServletResponse;
 34    import javax.servlet.http.HttpServletRequest;
 35    import javax.servlet.http.HttpServletResponse;
 36   
 37   
 38    /**
 39    * Commences a secure channel by retrying the original request using HTTPS.
 40    *
 41    * <P>
 42    * This entry point should suffice in most circumstances. However, it is not
 43    * intended to properly handle HTTP POSTs or other usage where a standard
 44    * redirect would cause an issue.
 45    * </p>
 46    *
 47    * @author Ben Alex
 48    * @version $Id: RetryWithHttpsEntryPoint.java,v 1.6 2005/11/17 00:55:50 benalex Exp $
 49    */
 50    public class RetryWithHttpsEntryPoint implements InitializingBean,
 51    ChannelEntryPoint {
 52    //~ Static fields/initializers =============================================
 53   
 54    private static final Log logger = LogFactory.getLog(RetryWithHttpsEntryPoint.class);
 55   
 56    //~ Instance fields ========================================================
 57   
 58    private PortMapper portMapper = new PortMapperImpl();
 59    private PortResolver portResolver = new PortResolverImpl();
 60   
 61    //~ Methods ================================================================
 62   
 63  6 public void setPortMapper(PortMapper portMapper) {
 64  6 this.portMapper = portMapper;
 65    }
 66   
 67  1 public PortMapper getPortMapper() {
 68  1 return portMapper;
 69    }
 70   
 71  6 public void setPortResolver(PortResolver portResolver) {
 72  6 this.portResolver = portResolver;
 73    }
 74   
 75  1 public PortResolver getPortResolver() {
 76  1 return portResolver;
 77    }
 78   
 79  6 public void afterPropertiesSet() throws Exception {
 80  6 Assert.notNull(portMapper, "portMapper is required");
 81  5 Assert.notNull(portResolver, "portResolver is required");
 82    }
 83   
 84  5 public void commence(ServletRequest request, ServletResponse response)
 85    throws IOException, ServletException {
 86  5 HttpServletRequest req = (HttpServletRequest) request;
 87   
 88  5 String pathInfo = req.getPathInfo();
 89  5 String queryString = req.getQueryString();
 90  5 String contextPath = req.getContextPath();
 91  5 String destination = req.getServletPath()
 92  5 + ((pathInfo == null) ? "" : pathInfo)
 93  5 + ((queryString == null) ? "" : ("?" + queryString));
 94   
 95  5 String redirectUrl = contextPath;
 96   
 97  5 Integer httpPort = new Integer(portResolver.getServerPort(req));
 98  5 Integer httpsPort = portMapper.lookupHttpsPort(httpPort);
 99   
 100  5 if (httpsPort != null) {
 101  4 boolean includePort = true;
 102   
 103  4 if (httpsPort.intValue() == 443) {
 104  2 includePort = false;
 105    }
 106   
 107  4 redirectUrl = "https://" + req.getServerName()
 108  4 + ((includePort) ? (":" + httpsPort) : "") + contextPath
 109    + destination;
 110    }
 111   
 112  5 if (logger.isDebugEnabled()) {
 113  0 logger.debug("Redirecting to: " + redirectUrl);
 114    }
 115   
 116  5 ((HttpServletResponse) response).sendRedirect(((HttpServletResponse) response)
 117    .encodeRedirectURL(redirectUrl));
 118    }
 119    }