Source code: com/opencms/flex/cache/CmsFlexController.java
1 /*
2 * File : $Source: /usr/local/cvs/opencms/src/com/opencms/flex/cache/Attic/CmsFlexController.java,v $
3 * Date : $Date: 2003/05/13 13:18:20 $
4 * Version: $Revision: 1.1.2.1 $
5 *
6 * This library is part of OpenCms -
7 * the Open Source Content Mananagement System
8 *
9 * Copyright (C) 2002 - 2003 Alkacon Software (http://www.alkacon.com)
10 *
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
20 *
21 * For further information about Alkacon Software, please see the
22 * company website: http://www.alkacon.com
23 *
24 * For further information about OpenCms, please see the
25 * project website: http://www.opencms.org
26 *
27 * You should have received a copy of the GNU Lesser General Public
28 * License along with this library; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 */
31 package com.opencms.flex.cache;
32
33 import com.opencms.file.CmsFile;
34 import com.opencms.file.CmsObject;
35
36 import java.util.ArrayList;
37 import java.util.Collections;
38 import java.util.Iterator;
39 import java.util.List;
40
41 import javax.servlet.ServletRequest;
42 import javax.servlet.http.HttpServletRequest;
43 import javax.servlet.http.HttpServletResponse;
44
45 /**
46 * Controller for getting access to the CmsObject, should be used as a
47 * request attribute.<p>
48 *
49 * @author Alexander Kandzior (a.kandzior@alkacon.com)
50 *
51 * @version $Revision: 1.1.2.1 $
52 */
53 public class CmsFlexController {
54
55 /** Constant for the controller request attribute name */
56 public static final String ATTRIBUTE_NAME = "__com.opencms.flex.cache.CmsFlexController";
57
58 /** The wrapped CmsObject provides JSP with access to the core system */
59 private CmsObject m_cmsObject;
60
61 /** The CmsFlexCache where the result will be cached in, required for the dispatcher */
62 private CmsFlexCache m_cache;
63
64 /** The CmsFile that was initialized by the original request, required for URI actions */
65 private CmsFile m_file;
66
67 /** Wrapped top request */
68 private HttpServletRequest m_req;
69
70 /** Wrapped to response */
71 private HttpServletResponse m_res;
72
73 /** List of wrapped CmsFlexRequests */
74 private List m_flexRequestList;
75
76 /** List of wrapped CmsFlexResponses */
77 private List m_flexResponseList;
78
79 /**
80 * Default constructor.<p>
81 *
82 * @param cms the initial CmsObject to wrap in the controller
83 */
84 public CmsFlexController(
85 CmsObject cms,
86 CmsFile file,
87 CmsFlexCache cache,
88 HttpServletRequest req,
89 HttpServletResponse res
90 ) {
91 m_cmsObject = cms;
92 m_file = file;
93 m_cache = cache;
94 m_req = req;
95 m_res = res;
96 m_flexRequestList = Collections.synchronizedList(new ArrayList());
97 m_flexResponseList = Collections.synchronizedList(new ArrayList());
98 }
99
100 /**
101 * Returns the wrapped CmsObject.<p>
102 *
103 * @return the wrapped CmsObject
104 */
105 public CmsObject getCmsObject() {
106 return m_cmsObject;
107 }
108
109 /**
110 * Returns the wrapped CmsObject form the provided request, or null if the
111 * request is not running inside OpenCms.<p>
112 *
113 * @return the wrapped CmsObject
114 */
115 public static CmsObject getCmsObject(ServletRequest req) {
116 CmsFlexController controller = (CmsFlexController)req.getAttribute(ATTRIBUTE_NAME);
117 if (controller != null) {
118 return controller.getCmsObject();
119 } else {
120 return null;
121 }
122 }
123
124 /**
125 * Checks if the provided request is running in OpenCms.<p>
126 *
127 * @return true if the request is running in OpenCms, false otherwise
128 */
129 public static boolean isCmsRequest(ServletRequest req) {
130 return ((req != null) && (req.getAttribute(ATTRIBUTE_NAME) != null));
131 }
132
133 /**
134 * Returns the CmsFlexCache instance where all results from this request will be cached in.<p>
135 *
136 * This is public so that pages like the Flex Cache Administration page
137 * have a way to access the cache object.<p>
138 *
139 * @return the CmsFlexCache instance where all results from this request will be cached in
140 */
141 public CmsFlexCache getCmsCache() {
142 return m_cache;
143 }
144
145 /**
146 * This method provides access to the top-level CmsFile of the request
147 * which is of a type that supports the FlexCache,
148 * i.e. usually the CmsFile that is identical to the file uri requested by the user,
149 * not he current included element.<p>
150 *
151 * In case a JSP is used as a sub-element in a XMLTemplate,
152 * this method will not return the top-level uri but
153 * the "topmost" file of a type that is supported by the FlexCache.
154 * In case you need the top uri, use
155 * getCmsObject().getRequestContext().getUri().
156 *
157 * @return the requested top-level CmsFile
158 */
159 public CmsFile getCmsFile() {
160 return m_file;
161 }
162
163 public CmsFlexRequest getCurrentRequest() {
164 return (CmsFlexRequest)m_flexRequestList.get(m_flexRequestList.size()-1);
165 }
166
167 public void pushRequest(CmsFlexRequest req) {
168 m_flexRequestList.add(req);
169 }
170
171 public CmsFlexRequest popRequest() {
172 CmsFlexRequest result = null;
173 if (m_flexRequestList.size() > 0) {
174 result = getCurrentRequest();
175 m_flexRequestList.remove(m_flexRequestList.size()-1);
176 }
177 return result;
178 }
179
180 public CmsFlexResponse getCurrentResponse() {
181 return (CmsFlexResponse)m_flexResponseList.get(m_flexResponseList.size()-1);
182 }
183
184 public void pushResponse(CmsFlexResponse res) {
185 m_flexResponseList.add(res);
186 }
187
188 public CmsFlexResponse popResponse() {
189 CmsFlexResponse result = null;
190 if (m_flexResponseList.size() > 0) {
191 result = getCurrentResponse();
192 m_flexResponseList.remove(m_flexResponseList.size()-1);
193 }
194 return result;
195 }
196
197 public void suspendFlexResponse() {
198 Iterator i = m_flexResponseList.iterator();
199 while (i.hasNext()) {
200 CmsFlexResponse res = (CmsFlexResponse)i.next();
201 res.setSuspended(true);
202 }
203 }
204
205 public int getResponseQueueSize() {
206 return m_flexResponseList.size();
207 }
208
209 public HttpServletRequest getTopRequest() {
210 return m_req;
211 }
212
213 public HttpServletResponse getTopResponse() {
214 return m_res;
215 }
216 }