1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.esigate;
17
18 import java.io.Serializable;
19
20 import org.esigate.api.Session;
21 import org.esigate.http.IncomingRequest;
22
23
24
25
26
27
28
29
30
31
32 public class UserContext {
33 private static final String USER_KEY = "user";
34 private final IncomingRequest httpRequest;
35 private final String key;
36
37 public UserContext(IncomingRequest httpRequest, String key) {
38 this.httpRequest = httpRequest;
39 this.key = UserContext.class.getName() + "#" + key;
40 }
41
42 private String prefixAttributeName(String name) {
43 return key + "#" + name;
44 }
45
46 public Object getAttribute(String name) {
47 Session session = httpRequest.getSession();
48 if (session == null) {
49 return null;
50 }
51 return session.getAttribute(prefixAttributeName(name));
52 }
53
54 public void setAttribute(String name, Serializable value) {
55 httpRequest.getSession().setAttribute(prefixAttributeName(name), value);
56 }
57
58 public String getUser() {
59 return (String) getAttribute(USER_KEY);
60 }
61
62 public void setUser(String user) {
63 setAttribute(USER_KEY, user);
64 }
65
66
67
68
69 @Override
70 public String toString() {
71 return "User=" + getUser();
72 }
73
74
75
76
77
78
79
80
81 public static String getUserSessionKey(String providerName) {
82 return UserContext.class.getName() + "#" + providerName + "#" + USER_KEY;
83 }
84 }