001: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
002: *
003: * Licensed under the Apache License, Version 2.0 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software
010: * distributed under the License is distributed on an "AS IS" BASIS,
011: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: * See the License for the specific language governing permissions and
013: * limitations under the License.
014: */
015:
016: package org.acegisecurity.ui;
017:
018: import org.acegisecurity.concurrent.SessionIdentifierAware;
019:
020: import java.io.Serializable;
021:
022: import javax.servlet.http.HttpServletRequest;
023: import javax.servlet.http.HttpSession;
024:
025: /**
026: * A holder of selected HTTP details related to a web authentication request.
027: *
028: * @author Ben Alex
029: * @version $Id: WebAuthenticationDetails.java 1496 2006-05-23 13:38:33Z benalex $
030: */
031: public class WebAuthenticationDetails implements
032: SessionIdentifierAware, Serializable {
033: //~ Instance fields ================================================================================================
034:
035: private String remoteAddress;
036: private String sessionId;
037:
038: //~ Constructors ===================================================================================================
039:
040: /**
041: * Constructor.
042: *
043: * <p>
044: * NB: This constructor will cause a <code>HttpSession</code> to be created
045: * (this is considered reasonable as all Acegi Security authentication
046: * requests rely on <code>HttpSession</code> to store the
047: * <code>Authentication</code> between requests
048: * </p>
049: *
050: * @param request that the authentication request was received from
051: */
052: public WebAuthenticationDetails(HttpServletRequest request) {
053: this .remoteAddress = request.getRemoteAddr();
054:
055: HttpSession session = request.getSession(false);
056: this .sessionId = (session != null) ? session.getId() : null;
057:
058: doPopulateAdditionalInformation(request);
059: }
060:
061: protected WebAuthenticationDetails() {
062: throw new IllegalArgumentException(
063: "Cannot use default constructor");
064: }
065:
066: //~ Methods ========================================================================================================
067:
068: /**
069: * Provided so that subclasses can populate additional information.
070: *
071: * @param request that the authentication request was received from
072: */
073: protected void doPopulateAdditionalInformation(
074: HttpServletRequest request) {
075: }
076:
077: public boolean equals(Object obj) {
078: if (obj instanceof WebAuthenticationDetails) {
079: WebAuthenticationDetails rhs = (WebAuthenticationDetails) obj;
080:
081: if ((remoteAddress == null)
082: && (rhs.getRemoteAddress() != null)) {
083: return false;
084: }
085:
086: if ((remoteAddress != null)
087: && (rhs.getRemoteAddress() == null)) {
088: return false;
089: }
090:
091: if (remoteAddress != null) {
092: if (!remoteAddress.equals(rhs.getRemoteAddress())) {
093: return false;
094: }
095: }
096:
097: if ((sessionId == null) && (rhs.getSessionId() != null)) {
098: return false;
099: }
100:
101: if ((sessionId != null) && (rhs.getSessionId() == null)) {
102: return false;
103: }
104:
105: if (sessionId != null) {
106: if (!sessionId.equals(rhs.getSessionId())) {
107: return false;
108: }
109: }
110:
111: return true;
112: }
113:
114: return false;
115: }
116:
117: /**
118: * Indicates the TCP/IP address the authentication request was received from.
119: *
120: * @return the address
121: */
122: public String getRemoteAddress() {
123: return remoteAddress;
124: }
125:
126: /**
127: * Indicates the <code>HttpSession</code> id the authentication request was received from.
128: *
129: * @return the session ID
130: */
131: public String getSessionId() {
132: return sessionId;
133: }
134:
135: public int hashCode() {
136: int code = 7654;
137:
138: if (this .remoteAddress != null) {
139: code = code * (this .remoteAddress.hashCode() % 7);
140: }
141:
142: if (this .sessionId != null) {
143: code = code * (this .sessionId.hashCode() % 7);
144: }
145:
146: return code;
147: }
148:
149: public String toString() {
150: StringBuffer sb = new StringBuffer();
151: sb.append(super .toString() + ": ");
152: sb.append("RemoteIpAddress: " + this .getRemoteAddress() + "; ");
153: sb.append("SessionId: " + this.getSessionId());
154:
155: return sb.toString();
156: }
157: }
|