Source code: org/acegisecurity/ui/WebAuthenticationDetails.java
1 /* Copyright 2004, 2005 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 package org.acegisecurity.ui;
16
17 import java.io.Serializable;
18
19 import javax.servlet.http.HttpServletRequest;
20 import javax.servlet.http.HttpSession;
21
22
23 /**
24 * A holder of selected HTTP details related to a web authentication request.
25 *
26 * @author Ben Alex
27 * @version $Id: WebAuthenticationDetails.java,v 1.4 2005/11/17 00:56:10 benalex Exp $
28 */
29 public class WebAuthenticationDetails implements Serializable {
30 private String remoteAddress;
31 private String sessionId;
32
33 /**
34 * Constructor.
35 *
36 * <p>
37 * NB: This constructor will cause a <code>HttpSession</code> to be created
38 * (this is considered reasonable as all Acegi Security authentication
39 * requests rely on <code>HttpSession</code> to store the
40 * <code>Authentication</code> between requests
41 * </p>
42 *
43 * @param request that the authentication request was received from
44 */
45 public WebAuthenticationDetails(HttpServletRequest request) {
46 this.remoteAddress = request.getRemoteAddr();
47 this.sessionId = request.getSession(true).getId();
48 doPopulateAdditionalInformation(request);
49 }
50
51 public WebAuthenticationDetails(HttpServletRequest request,
52 boolean forceSessionCreation) {
53 this.remoteAddress = request.getRemoteAddr();
54 HttpSession session = request.getSession(forceSessionCreation);
55 this.sessionId = session != null ? session.getId() : null;
56
57 doPopulateAdditionalInformation(request);
58 }
59
60 protected WebAuthenticationDetails() {
61 throw new IllegalArgumentException("Cannot use default constructor");
62 }
63
64 /**
65 * Indicates the TCP/IP address the authentication request was received
66 * from.
67 *
68 * @return the address
69 */
70 public String getRemoteAddress() {
71 return remoteAddress;
72 }
73
74 /**
75 * Indicates the <code>HttpSession</code> id the authentication request was
76 * received from.
77 *
78 * @return the session ID
79 */
80 public String getSessionId() {
81 return sessionId;
82 }
83
84 public String toString() {
85 StringBuffer sb = new StringBuffer();
86 sb.append(super.toString() + ": ");
87 sb.append("RemoteIpAddress: " + this.getRemoteAddress() + "; ");
88 sb.append("SessionId: " + this.getSessionId());
89
90 return sb.toString();
91 }
92
93 /**
94 * Provided so that subclasses can populate additional information.
95 *
96 * @param request that the authentication request was received from
97 */
98 protected void doPopulateAdditionalInformation(HttpServletRequest request) {
99 }
100 }