Coverage Report - org.acegisecurity.util.PortMapperImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
PortMapperImpl
100% 
100% 
2.6
 
 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.util;
 17  
 
 18  
 import org.springframework.util.Assert;
 19  
 
 20  
 import java.util.HashMap;
 21  
 import java.util.Iterator;
 22  
 import java.util.Map;
 23  
 
 24  
 
 25  
 /**
 26  
  * Concrete implementation of {@link PortMapper} that obtains HTTP:HTTPS pairs from the application context.<P>By
 27  
  * default the implementation will assume 80:443 and 8080:8443 are HTTP:HTTPS pairs respectively. If different pairs
 28  
  * are required, use {@link #setPortMappings(Map)}.</p>
 29  
  *
 30  
  * @author Ben Alex
 31  
  * @author colin sampaleanu
 32  
  * @version $Id: PortMapperImpl.java 1496 2006-05-23 13:38:33Z benalex $
 33  
  */
 34  
 public class PortMapperImpl implements PortMapper {
 35  
     //~ Instance fields ================================================================================================
 36  
 
 37  
     private Map httpsPortMappings;
 38  
 
 39  
     //~ Constructors ===================================================================================================
 40  
 
 41  167
     public PortMapperImpl() {
 42  167
         httpsPortMappings = new HashMap();
 43  167
         httpsPortMappings.put(new Integer(80), new Integer(443));
 44  167
         httpsPortMappings.put(new Integer(8080), new Integer(8443));
 45  167
     }
 46  
 
 47  
     //~ Methods ========================================================================================================
 48  
 
 49  
     /**
 50  
      * Returns the translated (Integer -> Integer) version of the original port mapping specified via
 51  
      * setHttpsPortMapping()
 52  
      *
 53  
      * @return DOCUMENT ME!
 54  
      */
 55  
     public Map getTranslatedPortMappings() {
 56  3
         return httpsPortMappings;
 57  
     }
 58  
 
 59  
     public Integer lookupHttpPort(Integer httpsPort) {
 60  15
         Iterator iter = httpsPortMappings.keySet().iterator();
 61  
 
 62  32
         while (iter.hasNext()) {
 63  25
             Integer httpPort = (Integer) iter.next();
 64  
 
 65  25
             if (httpsPortMappings.get(httpPort).equals(httpsPort)) {
 66  8
                 return httpPort;
 67  
             }
 68  17
         }
 69  
 
 70  7
         return null;
 71  
     }
 72  
 
 73  
     public Integer lookupHttpsPort(Integer httpPort) {
 74  21
         return (Integer) httpsPortMappings.get(httpPort);
 75  
     }
 76  
 
 77  
     /**
 78  
      * <p>Set to override the default HTTP port to HTTPS port mappings of 80:443, and  8080:8443.</p>
 79  
      *  In a Spring XML ApplicationContext, a definition would look something like this:<pre>
 80  
      *   &lt;property name="portMappings">    &lt;map>      &lt;entry key="80">&lt;value>443&lt;/value>&lt;/entry>
 81  
      *       &lt;entry key="8080">&lt;value>8443&lt;/value>&lt;/entry>    &lt;/map>  &lt;/property></pre>
 82  
      *
 83  
      * @param newMappings A Map consisting of String keys and String values, where for each entry the key is the string
 84  
      *        representation of an integer HTTP port number, and the value is the string representation of the
 85  
      *        corresponding integer HTTPS port number.
 86  
      *
 87  
      * @throws IllegalArgumentException if input map does not consist of String keys and values, each representing an
 88  
      *         integer port number in the range 1-65535 for that mapping.
 89  
      */
 90  
     public void setPortMappings(Map newMappings) {
 91  8
         Assert.notNull(newMappings, "A valid list of HTTPS port mappings must be provided");
 92  
 
 93  7
         httpsPortMappings.clear();
 94  
 
 95  7
         Iterator it = newMappings.entrySet().iterator();
 96  
 
 97  12
         while (it.hasNext()) {
 98  6
             Map.Entry entry = (Map.Entry) it.next();
 99  6
             Integer httpPort = new Integer((String) entry.getKey());
 100  6
             Integer httpsPort = new Integer((String) entry.getValue());
 101  
 
 102  6
             if ((httpPort.intValue() < 1) || (httpPort.intValue() > 65535) || (httpsPort.intValue() < 1)
 103  
                 || (httpsPort.intValue() > 65535)) {
 104  1
                 throw new IllegalArgumentException("one or both ports out of legal range: " + httpPort + ", "
 105  
                     + httpsPort);
 106  
             }
 107  
 
 108  5
             httpsPortMappings.put(httpPort, httpsPort);
 109  5
         }
 110  
 
 111  6
         if (httpsPortMappings.size() < 1) {
 112  1
             throw new IllegalArgumentException("must map at least one port");
 113  
         }
 114  5
     }
 115  
 }