1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.kew.web; |
17 | |
|
18 | |
import java.io.IOException; |
19 | |
|
20 | |
import javax.servlet.Filter; |
21 | |
import javax.servlet.FilterChain; |
22 | |
import javax.servlet.FilterConfig; |
23 | |
import javax.servlet.ServletException; |
24 | |
import javax.servlet.ServletRequest; |
25 | |
import javax.servlet.ServletResponse; |
26 | |
import javax.servlet.http.HttpServletRequest; |
27 | |
import javax.servlet.http.HttpServletResponse; |
28 | |
|
29 | |
import org.apache.commons.logging.Log; |
30 | |
import org.apache.commons.logging.LogFactory; |
31 | |
import org.kuali.rice.kew.preferences.Preferences; |
32 | |
import org.kuali.rice.kew.preferences.service.PreferencesService; |
33 | |
import org.kuali.rice.kew.service.KEWServiceLocator; |
34 | |
import org.kuali.rice.kew.util.KEWConstants; |
35 | |
import org.kuali.rice.krad.UserSession; |
36 | |
import org.kuali.rice.krad.util.KRADUtils; |
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | 0 | public class UserPreferencesFilter implements Filter { |
48 | |
|
49 | 0 | private static final Log LOG = LogFactory.getLog(UserPreferencesFilter.class); |
50 | |
|
51 | |
private FilterConfig filterConfig; |
52 | |
private PreferencesService preferencesService; |
53 | |
|
54 | |
@Override |
55 | |
public void init(FilterConfig config) throws ServletException { |
56 | 0 | this.filterConfig = config; |
57 | 0 | } |
58 | |
|
59 | |
@Override |
60 | |
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { |
61 | 0 | this.doFilter((HttpServletRequest) request, (HttpServletResponse) response, chain); |
62 | 0 | } |
63 | |
|
64 | |
private void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException { |
65 | 0 | final UserSession session = KRADUtils.getUserSessionFromRequest(request); |
66 | |
|
67 | 0 | if (session == null) { |
68 | 0 | throw new IllegalStateException("A user session has not been established"); |
69 | |
} |
70 | |
|
71 | 0 | final String principalId = session.getPrincipalId(); |
72 | |
|
73 | 0 | if (session.retrieveObject(KEWConstants.PREFERENCES) == null) { |
74 | 0 | final Preferences preferences = retrievePreferences(principalId); |
75 | 0 | session.addObject(KEWConstants.PREFERENCES, preferences); |
76 | |
} |
77 | 0 | chain.doFilter(request, response); |
78 | 0 | } |
79 | |
|
80 | |
@Override |
81 | |
public void destroy() { |
82 | 0 | filterConfig = null; |
83 | 0 | } |
84 | |
|
85 | |
private Preferences retrievePreferences(String principalId) { |
86 | 0 | Preferences preferences = this.getPreferenceService().getPreferences(principalId); |
87 | 0 | if (preferences.isRequiresSave()) { |
88 | 0 | LOG.info("Detected that user preferences require saving."); |
89 | 0 | this.getPreferenceService().savePreferences(principalId, preferences); |
90 | 0 | preferences = this.getPreferenceService().getPreferences(principalId); |
91 | |
} |
92 | |
|
93 | 0 | return preferences; |
94 | |
} |
95 | |
|
96 | |
|
97 | |
private PreferencesService getPreferenceService() { |
98 | 0 | if (this.preferencesService == null) { |
99 | 0 | this.preferencesService = KEWServiceLocator.getPreferencesService(); |
100 | |
} |
101 | |
|
102 | 0 | return this.preferencesService; |
103 | |
} |
104 | |
|
105 | |
} |