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