summaryrefslogtreecommitdiffstats
path: root/pki/base/common/src/com/netscape/certsrv/base/SessionContext.java
blob: 79d429d71409884fde159348dbdd5b86a2a94f11 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// --- BEGIN COPYRIGHT BLOCK ---
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; version 2 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// (C) 2007 Red Hat, Inc.
// All rights reserved.
// --- END COPYRIGHT BLOCK ---
package com.netscape.certsrv.base;


import java.util.*;


/**
 * This class specifies the context object that includes
 * authentication environment and connection information.
 * This object is later used in access control evaluation.
 * This is a global object that can be accessible 
 * throughout the server. It is useful for passing 
 * global and per-thread infomration in methods.
 * <P>
 *
 * @version $Revision$, $Date$
 */
public class SessionContext extends Hashtable implements IAuthInfo {

    /**
     * End user locale of the current processing request in the current thread.
     */
    public static final String LOCALE = "locale"; // Locale

    /**
     * Authentication token in the current thread.
     */
    public static final String AUTH_TOKEN = "AuthToken"; // IAuthToken

    /**
     * ID of the authentication manager in the current thread.
     */
    public static final String AUTH_MANAGER_ID = "authManagerId"; // String

    /**
     * User object of the authenticated user in the current thread.
     */
    public static final String USER = "user"; // IUser

    /**
     * User ID of the authenticated user in the current thread.
     */
    public static final String USER_ID = "userid"; // String

    /**
     * Group ID of the authenticated user in the current thread.
     */
    public static final String GROUP_ID = "groupid"; //String

    /**
     * ID of the processing request in the current thread.
     */
    public static final String REQUESTER_ID = "requesterID"; // String

    /**
     * Recovery ID of a recovery operation in KRA in the current thread.
     */
    public static final String RECOVERY_ID = "recoveryID"; // String

    /**
     * IP Address of the requestor of the request in the current thread.
     */
    public static final String IPADDRESS = "ipAddress";

    private static Hashtable mContexts = new Hashtable();

    /**
     * Constructs a session context.
     */
    public SessionContext() {
        super();
    }

    /**
     * Creates a new context and associates it with
     * the current thread. If the current thread is
     * also associated with a old context, the old
     * context will be replaced.
     */
    private static SessionContext createContext() {
        SessionContext sc = new SessionContext();

        setContext(sc);
        return sc;
    }

    /**
     * Sets the current context. This allows the 
     * caller to associate a specific session context 
     * with the current thread.
     * This methods makes custom session context
     * possible.
     *
     * @param sc session context
     */
    public static void setContext(SessionContext sc) {
        mContexts.put(Thread.currentThread(), sc);
    }

    /**
     * Retrieves the session context associated with 
     * the current thread. If no context is associated,
     * a context is created.
     *
     * @return sesssion context
     */
    public static SessionContext getContext() {
        SessionContext sc = (SessionContext) mContexts.get(
                Thread.currentThread());

        if (sc == null) {
            sc = createContext();
        }
        return sc;
    }

    /**
     * Retrieves the session context associated with 
     * the current thread. If no context is associated,
     * null is returned.
     *
     * @return sesssion context
     */
    public static SessionContext getExistingContext() {
        SessionContext sc = (SessionContext)
            mContexts.get(Thread.currentThread());

        if (sc == null) {
            return null;
        }

        return sc;
    }

    /**
     * Releases the current session context.
     */
    public static void releaseContext() {
        SessionContext sc = (SessionContext) mContexts.get(
                Thread.currentThread());

        if (sc != null) {
            mContexts.remove(Thread.currentThread());
        }
    }
}