Coverage Report - org.acegisecurity.ui.WebAuthenticationDetails
 
Classes in this File Line Coverage Branch Coverage Complexity
WebAuthenticationDetails
75% 
92% 
3.375
 
 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.ui;
 17  
 
 18  
 import org.acegisecurity.concurrent.SessionIdentifierAware;
 19  
 
 20  
 import java.io.Serializable;
 21  
 
 22  
 import javax.servlet.http.HttpServletRequest;
 23  
 import javax.servlet.http.HttpSession;
 24  
 
 25  
 
 26  
 /**
 27  
  * A holder of selected HTTP details related to a web authentication request.
 28  
  *
 29  
  * @author Ben Alex
 30  
  * @version $Id: WebAuthenticationDetails.java 1496 2006-05-23 13:38:33Z benalex $
 31  
  */
 32  
 public class WebAuthenticationDetails implements SessionIdentifierAware, Serializable {
 33  
     //~ Instance fields ================================================================================================
 34  
 
 35  
     private String remoteAddress;
 36  
     private String sessionId;
 37  
 
 38  
     //~ Constructors ===================================================================================================
 39  
 
 40  
 /**
 41  
      * Constructor.
 42  
      * 
 43  
      * <p>
 44  
      * NB: This constructor will cause a <code>HttpSession</code> to be created
 45  
      * (this is considered reasonable as all Acegi Security authentication
 46  
      * requests rely on <code>HttpSession</code> to store the
 47  
      * <code>Authentication</code> between requests
 48  
      * </p>
 49  
      *
 50  
      * @param request that the authentication request was received from
 51  
      */
 52  32
     public WebAuthenticationDetails(HttpServletRequest request) {
 53  32
         this.remoteAddress = request.getRemoteAddr();
 54  
 
 55  32
         HttpSession session = request.getSession(false);
 56  32
         this.sessionId = (session != null) ? session.getId() : null;
 57  
 
 58  32
         doPopulateAdditionalInformation(request);
 59  32
     }
 60  
 
 61  0
     protected WebAuthenticationDetails() {
 62  0
         throw new IllegalArgumentException("Cannot use default constructor");
 63  
     }
 64  
 
 65  
     //~ Methods ========================================================================================================
 66  
 
 67  
     /**
 68  
      * Provided so that subclasses can populate additional information.
 69  
      *
 70  
      * @param request that the authentication request was received from
 71  
      */
 72  32
     protected void doPopulateAdditionalInformation(HttpServletRequest request) {}
 73  
 
 74  
     public boolean equals(Object obj) {
 75  1
         if (obj instanceof WebAuthenticationDetails) {
 76  1
             WebAuthenticationDetails rhs = (WebAuthenticationDetails) obj;
 77  
 
 78  1
             if ((remoteAddress == null) && (rhs.getRemoteAddress() != null)) {
 79  0
                 return false;
 80  
             }
 81  
 
 82  1
             if ((remoteAddress != null) && (rhs.getRemoteAddress() == null)) {
 83  0
                 return false;
 84  
             }
 85  
 
 86  1
             if (remoteAddress != null) {
 87  1
                 if (!remoteAddress.equals(rhs.getRemoteAddress())) {
 88  0
                     return false;
 89  
                 }
 90  
             }
 91  
 
 92  1
             if ((sessionId == null) && (rhs.getSessionId() != null)) {
 93  0
                 return false;
 94  
             }
 95  
 
 96  1
             if ((sessionId != null) && (rhs.getSessionId() == null)) {
 97  0
                 return false;
 98  
             }
 99  
 
 100  1
             if (sessionId != null) {
 101  0
                 if (!sessionId.equals(rhs.getSessionId())) {
 102  0
                     return false;
 103  
                 }
 104  
             }
 105  
 
 106  1
             return true;
 107  
         }
 108  
 
 109  0
         return false;
 110  
     }
 111  
 
 112  
     /**
 113  
      * Indicates the TCP/IP address the authentication request was received from.
 114  
      *
 115  
      * @return the address
 116  
      */
 117  
     public String getRemoteAddress() {
 118  12
         return remoteAddress;
 119  
     }
 120  
 
 121  
     /**
 122  
      * Indicates the <code>HttpSession</code> id the authentication request was received from.
 123  
      *
 124  
      * @return the session ID
 125  
      */
 126  
     public String getSessionId() {
 127  17
         return sessionId;
 128  
     }
 129  
 
 130  
     public int hashCode() {
 131  7
         int code = 7654;
 132  
 
 133  7
         if (this.remoteAddress != null) {
 134  7
             code = code * (this.remoteAddress.hashCode() % 7);
 135  
         }
 136  
 
 137  7
         if (this.sessionId != null) {
 138  7
             code = code * (this.sessionId.hashCode() % 7);
 139  
         }
 140  
 
 141  7
         return code;
 142  
     }
 143  
 
 144  
     public String toString() {
 145  7
         StringBuffer sb = new StringBuffer();
 146  7
         sb.append(super.toString() + ": ");
 147  7
         sb.append("RemoteIpAddress: " + this.getRemoteAddress() + "; ");
 148  7
         sb.append("SessionId: " + this.getSessionId());
 149  
 
 150  7
         return sb.toString();
 151  
     }
 152  
 }