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.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      public WebAuthenticationDetails(HttpServletRequest request) {
53          this.remoteAddress = request.getRemoteAddr();
54  
55          HttpSession session = request.getSession(false);
56          this.sessionId = (session != null) ? session.getId() : null;
57  
58          doPopulateAdditionalInformation(request);
59      }
60  
61      protected WebAuthenticationDetails() {
62          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      protected void doPopulateAdditionalInformation(HttpServletRequest request) {}
73  
74      public boolean equals(Object obj) {
75          if (obj instanceof WebAuthenticationDetails) {
76              WebAuthenticationDetails rhs = (WebAuthenticationDetails) obj;
77  
78              if ((remoteAddress == null) && (rhs.getRemoteAddress() != null)) {
79                  return false;
80              }
81  
82              if ((remoteAddress != null) && (rhs.getRemoteAddress() == null)) {
83                  return false;
84              }
85  
86              if (remoteAddress != null) {
87                  if (!remoteAddress.equals(rhs.getRemoteAddress())) {
88                      return false;
89                  }
90              }
91  
92              if ((sessionId == null) && (rhs.getSessionId() != null)) {
93                  return false;
94              }
95  
96              if ((sessionId != null) && (rhs.getSessionId() == null)) {
97                  return false;
98              }
99  
100             if (sessionId != null) {
101                 if (!sessionId.equals(rhs.getSessionId())) {
102                     return false;
103                 }
104             }
105 
106             return true;
107         }
108 
109         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         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         return sessionId;
128     }
129 
130     public int hashCode() {
131         int code = 7654;
132 
133         if (this.remoteAddress != null) {
134             code = code * (this.remoteAddress.hashCode() % 7);
135         }
136 
137         if (this.sessionId != null) {
138             code = code * (this.sessionId.hashCode() % 7);
139         }
140 
141         return code;
142     }
143 
144     public String toString() {
145         StringBuffer sb = new StringBuffer();
146         sb.append(super.toString() + ": ");
147         sb.append("RemoteIpAddress: " + this.getRemoteAddress() + "; ");
148         sb.append("SessionId: " + this.getSessionId());
149 
150         return sb.toString();
151     }
152 }