01: /*
02: * de.jwic.web.RemoteUserBinding
03: * Created on 22.12.2004
04: * $Id: RemoteUserBinding.java,v 1.2 2006/08/14 09:34:59 lordsam Exp $
05: */
06: package de.jwic.web;
07:
08: import javax.servlet.http.HttpServletRequest;
09: import javax.servlet.http.HttpServletResponse;
10:
11: /**
12: * Stores the HttpServletRequest object in a ThreadLocal object to
13: * allow jWic applications to gather information about the current user.
14: * This class must be registerd as ServletInterceptor to the
15: * DispatcherServlet.
16: *
17: * <init-param>
18: * <param-name>servlet.interceptors</param-name>
19: * <param-value>de.jwic.web.RemoteUserBinding</param-value>
20: * </init-param>
21: *
22: *
23: * @author Florian Lippisch
24: * @version $Revision: 1.2 $
25: */
26: public class RemoteUserBinding implements ServletInterceptor {
27:
28: private static ThreadLocal locale = new ThreadLocal();
29:
30: /**
31: * Returns the remote user.
32: * @return
33: */
34: public static String getRemoteUser() {
35: HttpServletRequest request = (HttpServletRequest) locale.get();
36: if (request != null) {
37: return request.getRemoteUser();
38: }
39: return null;
40: }
41:
42: /**
43: * Returns the result of HttpServletRequest.isUserInRole(role).
44: * @param role
45: * @return
46: */
47: public static boolean isUserInRole(String role) {
48: HttpServletRequest request = (HttpServletRequest) locale.get();
49: if (request != null) {
50: return request.isUserInRole(role);
51: }
52: return false;
53: }
54:
55: /* (non-Javadoc)
56: * @see de.jwic.web.ServletInterceptor#preHandle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
57: */
58: public void preHandle(HttpServletRequest request,
59: HttpServletResponse resp) {
60:
61: locale.set(request);
62:
63: }
64:
65: /* (non-Javadoc)
66: * @see de.jwic.web.ServletInterceptor#postHandle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
67: */
68: public void postHandle(HttpServletRequest request,
69: HttpServletResponse resp) {
70:
71: locale.set(null); // clear reference
72:
73: }
74:
75: }
|