1/*2 * Licensed to the Apache Software Foundation (ASF) under one or more3 * contributor license agreements. See the NOTICE file distributed with4 * this work for additional information regarding copyright ownership.5 * The ASF licenses this file to You under the Apache License, Version 2.06 * (the "License"); you may not use this file except in compliance with7 * the License. You may obtain a copy of the License at8 * 9 * http://www.apache.org/licenses/LICENSE-2.010 * 11 * Unless required by applicable law or agreed to in writing, software12 * distributed under the License is distributed on an "AS IS" BASIS,13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.14 * See the License for the specific language governing permissions and15 * limitations under the License.16 */17packageorg.apache.jetspeed.request;
1819import java.lang.reflect.Constructor;
20import java.util.HashMap;
21import java.util.Map;
2223import javax.servlet.ServletConfig;
24import javax.servlet.http.HttpServletRequest;
25import javax.servlet.http.HttpServletResponse;
2627import org.apache.commons.logging.Log;
28import org.apache.commons.logging.LogFactory;
29import org.apache.jetspeed.PortalReservedParameters;
30import org.apache.jetspeed.aggregator.CurrentWorkerContext;
31import org.apache.jetspeed.userinfo.UserInfoManager;
3233/***34 * JetspeedRequestContextComponent35 *36 * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>37 * @version $Id: JetspeedRequestContextComponent.java 587064 2007-10-22 11:54:11Z woonsan $38 */39publicclassJetspeedRequestContextComponent implements RequestContextComponent
40 {
41private String contextClassName = null;
42private Class contextClass = null;
43/*** The user info manager. */44private UserInfoManager userInfoMgr;
45private ThreadLocal tlRequestContext = new ThreadLocal();
46private Map requestContextObjects;
4748privatefinalstatic Log log = LogFactory.getLog(JetspeedRequestContextComponent.class);
4950publicJetspeedRequestContextComponent(String contextClassName)
51 {
52this.contextClassName = contextClassName;
53this.requestContextObjects = new HashMap();
54 }
5556publicJetspeedRequestContextComponent(String contextClassName,
57 UserInfoManager userInfoMgr)
58 {
59this.contextClassName = contextClassName;
60this.userInfoMgr = userInfoMgr;
61this.requestContextObjects = new HashMap();
62 }
6364publicJetspeedRequestContextComponent(String contextClassName,
65 UserInfoManager userInfoMgr,
66 Map requestContextObjects)
67 {
68this.contextClassName = contextClassName;
69this.userInfoMgr = userInfoMgr;
70this.requestContextObjects = requestContextObjects;
71 }
7273public RequestContext create(HttpServletRequest req, HttpServletResponse resp, ServletConfig config)
74 {
75 RequestContext context = null;
7677try78 {
79if (null == contextClass)
80 {
81 contextClass = Class.forName(contextClassName);
82 }
8384 Constructor constructor =
85 contextClass.getConstructor(
86new Class[] {
87 HttpServletRequest.class,
88 HttpServletResponse.class,
89 ServletConfig.class,
90 UserInfoManager.class,
91 Map.class});
92 context = (RequestContext) constructor.newInstance(new Object[] { req, resp, config, userInfoMgr, requestContextObjects});
9394 }
95catch (Exception e)
96 {
97 String msg = "JetspeedRequestContextComponent: Failed to create a Class object for RequestContext: " + e.toString();
98 log.error(msg);
99 }
100 tlRequestContext.set(context);
101return context;
102 }
103104publicvoid release(RequestContext context)
105 {
106 tlRequestContext.set(null);
107 }
108109/***110 * The servlet request can always get you back to the Request Context if you need it111 * This static accessor provides this capability112 *113 * @param request114 * @return RequestContext115 */116public RequestContext getRequestContext(HttpServletRequest request)
117 {
118 RequestContext rc = (RequestContext) request.getAttribute(PortalReservedParameters.REQUEST_CONTEXT_ATTRIBUTE);
119if(rc != null)
120 {
121return rc;
122 }
123else124 {
125 log.error("Cannot call getRequestContext(HttpServletRequest request) before it has been created and set for this thread.");
126thrownew IllegalStateException("Cannot call getRequestContext(HttpServletRequest request) before it has been created and set for this thread.");
127 }
128 }
129130public RequestContext getRequestContext()
131 {
132 RequestContext rc = null;
133134if (CurrentWorkerContext.getParallelRenderingMode())
135 {
136 rc = (RequestContext) CurrentWorkerContext.getAttribute(PortalReservedParameters.REQUEST_CONTEXT_ATTRIBUTE);
137 }
138else139 {
140 rc = (RequestContext) tlRequestContext.get();
141 }
142143if(rc != null)
144 {
145return rc;
146 }
147else148 {
149 log.error("Cannot call getRequestContext() before it has been created and set for this thread.");
150thrownew IllegalStateException("Cannot call getRequestContext() before it has been created and set for this thread.");
151 }
152 }
153154 }