-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexampl.txt
101 lines (89 loc) · 3.61 KB
/
exampl.txt
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
String key = "userProfile";
@Autowired
transient UserProfileRepository userProfileRepository;
@Autowired
transient UserRepository userRepository;
@Override
public UserProfile initilizeOrFind() {
RequestContext context = org.springframework.webflow.execution.RequestContextHolder.getRequestContext();
ServletExternalContext externalContext = (ServletExternalContext) context.getExternalContext();
HttpServletResponse response = (HttpServletResponse) externalContext.getNativeResponse();
HttpServletRequest request = (HttpServletRequest) externalContext.getNativeRequest();
return doInit(response, request, context);
}
public UserProfile doInit(HttpServletResponse response, HttpServletRequest request, RequestContext context) {
UserProfile sessionUserProfile = getUserProfile(request);
UserProfile ownedUserProfile = null;
User user = null;
if (SecurityContextHolder.getContext() != null && SecurityContextHolder.getContext().getAuthentication() != null) {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication.getPrincipal() instanceof CustomUserDetails) {
CustomUserDetails customUserDetails = (CustomUserDetails) authentication.getPrincipal();
user = customUserDetails.getUser();
ownedUserProfile = user.getUserProfile();
}
}
if (ownedUserProfile != null) {
sessionUserProfile = ownedUserProfile;
} else {
if (sessionUserProfile == null) {
sessionUserProfile = createNewInstance();
}
if (user != null) {
user.setUserProfile(sessionUserProfile);
sessionUserProfile.setUser(user);
userRepository.save(user);
userProfileRepository.save(sessionUserProfile);
}
}
if (sessionUserProfile != null) {
installUserProfile((HttpServletResponse) response, sessionUserProfile);
} else {
throw new RuntimeException();
}
return sessionUserProfile;
}
void installUserProfile(HttpServletResponse response, UserProfile userProfile) {
Cookie cookie = new Cookie(key, userProfile.getId().toString());
cookie.setMaxAge(31536000);
response.addCookie(cookie);
}
UserProfile getUserProfile(HttpServletRequest request) {
UserProfile userProfile = null;
userProfile = (UserProfile) request.getSession().getAttribute(key);
if (userProfile == null) {
Integer id = getProfileId(request);
if (id != null) {
userProfile = userProfileRepository.findOne(id);
}
request.getSession().setAttribute(key, userProfile);
}
return userProfile;
}
Integer getProfileId(HttpServletRequest request) {
Integer id = null;
Cookie cookie = findCookie(request);
if (cookie != null) {
return new Integer(cookie.getValue());
}
return id;
}
Cookie findCookie(HttpServletRequest request) {
if (request.getCookies() != null) {
for (Cookie c : request.getCookies()) {
if (key.equals(c.getName())) {
return c;
}
}
}
return null;
}
Cookie createCookie(Integer idProfile) {
return new Cookie(key, idProfile.toString());
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}