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.api.KewApiServiceLocator; |
32 | |
import org.kuali.rice.kew.api.preferences.Preferences; |
33 | |
import org.kuali.rice.kew.api.preferences.PreferencesService; |
34 | |
import org.kuali.rice.kew.service.KEWServiceLocator; |
35 | |
import org.kuali.rice.kew.api.KewApiConstants; |
36 | |
import org.kuali.rice.krad.UserSession; |
37 | |
import org.kuali.rice.krad.util.KRADUtils; |
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | 0 | public class UserPreferencesFilter implements Filter { |
49 | |
|
50 | 0 | private static final Log LOG = LogFactory.getLog(UserPreferencesFilter.class); |
51 | |
|
52 | |
private FilterConfig filterConfig; |
53 | |
private PreferencesService preferencesService; |
54 | |
|
55 | |
@Override |
56 | |
public void init(FilterConfig config) throws ServletException { |
57 | 0 | this.filterConfig = config; |
58 | 0 | } |
59 | |
|
60 | |
@Override |
61 | |
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { |
62 | 0 | this.doFilter((HttpServletRequest) request, (HttpServletResponse) response, chain); |
63 | 0 | } |
64 | |
|
65 | |
private void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException { |
66 | 0 | final UserSession session = KRADUtils.getUserSessionFromRequest(request); |
67 | |
|
68 | 0 | if (session == null) { |
69 | 0 | throw new IllegalStateException("A user session has not been established"); |
70 | |
} |
71 | |
|
72 | 0 | final String principalId = session.getPrincipalId(); |
73 | |
|
74 | 0 | if (session.retrieveObject(KewApiConstants.PREFERENCES) == null) { |
75 | 0 | final Preferences preferences = retrievePreferences(principalId); |
76 | 0 | session.addObject(KewApiConstants.PREFERENCES, preferences); |
77 | |
} |
78 | 0 | chain.doFilter(request, response); |
79 | 0 | } |
80 | |
|
81 | |
@Override |
82 | |
public void destroy() { |
83 | 0 | filterConfig = null; |
84 | 0 | } |
85 | |
|
86 | |
private Preferences retrievePreferences(String principalId) { |
87 | 0 | Preferences preferences = this.getPreferenceService().getPreferences(principalId); |
88 | 0 | if (preferences.isRequiresSave()) { |
89 | 0 | LOG.info("Detected that user preferences require saving."); |
90 | 0 | this.getPreferenceService().savePreferences(principalId, preferences); |
91 | 0 | preferences = this.getPreferenceService().getPreferences(principalId); |
92 | |
} |
93 | |
|
94 | 0 | return preferences; |
95 | |
} |
96 | |
|
97 | |
|
98 | |
private PreferencesService getPreferenceService() { |
99 | 0 | if (this.preferencesService == null) { |
100 | 0 | this.preferencesService = KewApiServiceLocator.getPreferencesService(); |
101 | |
} |
102 | |
|
103 | 0 | return this.preferencesService; |
104 | |
} |
105 | |
|
106 | |
} |