01: /*
02: * Created on Sep 21, 2005
03: */
04: package uk.org.ponder.rsac.servlet;
05:
06: import javax.servlet.ServletRequest;
07: import javax.servlet.http.HttpServletRequest;
08: import javax.servlet.http.HttpServletResponse;
09:
10: import uk.org.ponder.beanutil.BeanLocator;
11: import uk.org.ponder.beanutil.WriteableBeanLocator;
12: import uk.org.ponder.rsac.RSACBeanLocator;
13:
14: /**
15: * @author andrew, Antranig
16: *
17: */
18: public class RSACUtils {
19: public static final String REQUEST_SCOPE_APP_CONTEXT_ATTRIBUTE = "requestScopeApplicationContext";
20: /** The default name for the bean representing the HttpServletFactory * */
21: public static final String HTTP_SERVLET_FACTORY = "httpServletFactory";
22:
23: /**
24: * This method is to be used in the awkward situation where a request is
25: * subject to a RequestDispatch partway through its lifecycle, and the request
26: * visible to it is not the one for which the RSAC context is required to
27: * execute, but client nonetheless wishes to populate or amend some beans into
28: * the request scope. Clearly none of these beans should be RequestAware. <br>
29: * This method has the effect of marking the bean container both onto the
30: * current thread and onto a request attributes, which it is assumed the
31: * request dispatcher has the sense not to trash.
32: *
33: * @param rsacbl
34: */
35: public static void protoStartServletRequest(
36: HttpServletRequest request, RSACBeanLocator rsacbl) {
37: rsacbl.startRequest();
38: }
39:
40: public static void startServletRequest(HttpServletRequest request,
41: HttpServletResponse response, RSACBeanLocator rsacbl,
42: String factorybeanname) {
43: // Logger.log.info("Got rsacbg " + rsacbl);
44: if (!rsacbl.isStarted()) {
45: rsacbl.startRequest();
46: }
47: WriteableBeanLocator locator = rsacbl.getBeanLocator();
48: HttpServletFactory factory = (HttpServletFactory) locator
49: .locateBean(factorybeanname);
50: factory.setHttpServletRequest(request);
51: factory.setHttpServletResponse(response);
52: locator.set(factorybeanname, factory);
53: // notify the "seed list" of the change.
54: setRequestApplicationContext(request, locator);
55: }
56:
57: public static void setRequestApplicationContext(
58: ServletRequest request, BeanLocator context) {
59: request.setAttribute(REQUEST_SCOPE_APP_CONTEXT_ATTRIBUTE,
60: context);
61: }
62:
63: public static BeanLocator getRequestApplicationContext(
64: ServletRequest request) {
65: return (BeanLocator) request
66: .getAttribute(REQUEST_SCOPE_APP_CONTEXT_ATTRIBUTE);
67: }
68:
69: public static void removeRequestApplicationContext(
70: ServletRequest request) {
71: request.removeAttribute(REQUEST_SCOPE_APP_CONTEXT_ATTRIBUTE);
72: }
73: }
|