1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
package org.apache.jetspeed.request; |
18 |
|
|
19 |
|
import java.lang.reflect.Constructor; |
20 |
|
import java.util.HashMap; |
21 |
|
import java.util.Map; |
22 |
|
|
23 |
|
import javax.servlet.ServletConfig; |
24 |
|
import javax.servlet.http.HttpServletRequest; |
25 |
|
import javax.servlet.http.HttpServletResponse; |
26 |
|
|
27 |
|
import org.apache.commons.logging.Log; |
28 |
|
import org.apache.commons.logging.LogFactory; |
29 |
|
import org.apache.jetspeed.PortalReservedParameters; |
30 |
|
import org.apache.jetspeed.aggregator.CurrentWorkerContext; |
31 |
|
import org.apache.jetspeed.userinfo.UserInfoManager; |
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
public class JetspeedRequestContextComponent implements RequestContextComponent |
40 |
|
{ |
41 |
0 |
private String contextClassName = null; |
42 |
0 |
private Class contextClass = null; |
43 |
|
|
44 |
|
private UserInfoManager userInfoMgr; |
45 |
0 |
private ThreadLocal tlRequestContext = new ThreadLocal(); |
46 |
|
private Map requestContextObjects; |
47 |
|
|
48 |
0 |
private final static Log log = LogFactory.getLog(JetspeedRequestContextComponent.class); |
49 |
|
|
50 |
|
public JetspeedRequestContextComponent(String contextClassName) |
51 |
0 |
{ |
52 |
0 |
this.contextClassName = contextClassName; |
53 |
0 |
this.requestContextObjects = new HashMap(); |
54 |
0 |
} |
55 |
|
|
56 |
|
public JetspeedRequestContextComponent(String contextClassName, |
57 |
|
UserInfoManager userInfoMgr) |
58 |
0 |
{ |
59 |
0 |
this.contextClassName = contextClassName; |
60 |
0 |
this.userInfoMgr = userInfoMgr; |
61 |
0 |
this.requestContextObjects = new HashMap(); |
62 |
0 |
} |
63 |
|
|
64 |
|
public JetspeedRequestContextComponent(String contextClassName, |
65 |
|
UserInfoManager userInfoMgr, |
66 |
|
Map requestContextObjects) |
67 |
0 |
{ |
68 |
0 |
this.contextClassName = contextClassName; |
69 |
0 |
this.userInfoMgr = userInfoMgr; |
70 |
0 |
this.requestContextObjects = requestContextObjects; |
71 |
0 |
} |
72 |
|
|
73 |
|
public RequestContext create(HttpServletRequest req, HttpServletResponse resp, ServletConfig config) |
74 |
|
{ |
75 |
0 |
RequestContext context = null; |
76 |
|
|
77 |
|
try |
78 |
|
{ |
79 |
0 |
if (null == contextClass) |
80 |
|
{ |
81 |
0 |
contextClass = Class.forName(contextClassName); |
82 |
|
} |
83 |
|
|
84 |
0 |
Constructor constructor = |
85 |
|
contextClass.getConstructor( |
86 |
|
new Class[] { |
87 |
|
HttpServletRequest.class, |
88 |
|
HttpServletResponse.class, |
89 |
|
ServletConfig.class, |
90 |
|
UserInfoManager.class, |
91 |
|
Map.class}); |
92 |
0 |
context = (RequestContext) constructor.newInstance(new Object[] { req, resp, config, userInfoMgr, requestContextObjects}); |
93 |
|
|
94 |
|
} |
95 |
0 |
catch (Exception e) |
96 |
|
{ |
97 |
0 |
String msg = "JetspeedRequestContextComponent: Failed to create a Class object for RequestContext: " + e.toString(); |
98 |
0 |
log.error(msg); |
99 |
0 |
} |
100 |
0 |
tlRequestContext.set(context); |
101 |
0 |
return context; |
102 |
|
} |
103 |
|
|
104 |
|
public void release(RequestContext context) |
105 |
|
{ |
106 |
0 |
tlRequestContext.set(null); |
107 |
0 |
} |
108 |
|
|
109 |
|
|
110 |
|
|
111 |
|
|
112 |
|
|
113 |
|
|
114 |
|
|
115 |
|
|
116 |
|
public RequestContext getRequestContext(HttpServletRequest request) |
117 |
|
{ |
118 |
0 |
RequestContext rc = (RequestContext) request.getAttribute(PortalReservedParameters.REQUEST_CONTEXT_ATTRIBUTE); |
119 |
0 |
if(rc != null) |
120 |
|
{ |
121 |
0 |
return rc; |
122 |
|
} |
123 |
|
else |
124 |
|
{ |
125 |
0 |
log.error("Cannot call getRequestContext(HttpServletRequest request) before it has been created and set for this thread."); |
126 |
0 |
throw new IllegalStateException("Cannot call getRequestContext(HttpServletRequest request) before it has been created and set for this thread."); |
127 |
|
} |
128 |
|
} |
129 |
|
|
130 |
|
public RequestContext getRequestContext() |
131 |
|
{ |
132 |
0 |
RequestContext rc = null; |
133 |
|
|
134 |
0 |
if (CurrentWorkerContext.getParallelRenderingMode()) |
135 |
|
{ |
136 |
0 |
rc = (RequestContext) CurrentWorkerContext.getAttribute(PortalReservedParameters.REQUEST_CONTEXT_ATTRIBUTE); |
137 |
|
} |
138 |
|
else |
139 |
|
{ |
140 |
0 |
rc = (RequestContext) tlRequestContext.get(); |
141 |
|
} |
142 |
|
|
143 |
0 |
if(rc != null) |
144 |
|
{ |
145 |
0 |
return rc; |
146 |
|
} |
147 |
|
else |
148 |
|
{ |
149 |
0 |
log.error("Cannot call getRequestContext() before it has been created and set for this thread."); |
150 |
0 |
throw new IllegalStateException("Cannot call getRequestContext() before it has been created and set for this thread."); |
151 |
|
} |
152 |
|
} |
153 |
|
|
154 |
|
} |