KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > loader > CmsDumpLoader


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/loader/CmsDumpLoader.java,v $
3  * Date : $Date: 2006/03/27 14:52:37 $
4  * Version: $Revision: 1.65 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (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 GmbH, 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
32 package org.opencms.loader;
33
34 import org.opencms.file.CmsFile;
35 import org.opencms.file.CmsObject;
36 import org.opencms.file.CmsResource;
37 import org.opencms.flex.CmsFlexController;
38 import org.opencms.main.CmsException;
39 import org.opencms.main.CmsLog;
40 import org.opencms.main.OpenCms;
41 import org.opencms.util.CmsRequestUtil;
42 import org.opencms.util.CmsStringUtil;
43 import org.opencms.workplace.CmsWorkplaceManager;
44
45 import java.io.IOException JavaDoc;
46 import java.util.Collections JavaDoc;
47 import java.util.Iterator JavaDoc;
48 import java.util.Locale JavaDoc;
49 import java.util.Map JavaDoc;
50 import java.util.TreeMap JavaDoc;
51
52 import javax.servlet.ServletRequest JavaDoc;
53 import javax.servlet.ServletResponse JavaDoc;
54 import javax.servlet.http.HttpServletRequest JavaDoc;
55 import javax.servlet.http.HttpServletResponse JavaDoc;
56
57 import org.apache.commons.collections.ExtendedProperties;
58
59 /**
60  * Dump loader for binary or other unprocessed resource types.<p>
61  *
62  * This loader is also used to deliver static sub-elements of pages processed
63  * by other loaders.<p>
64  *
65  * @author Alexander Kandzior
66  *
67  * @version $Revision: 1.65 $
68  *
69  * @since 6.0.0
70  */

71 public class CmsDumpLoader implements I_CmsResourceLoader {
72
73     /** The id of this loader. */
74     public static final int RESOURCE_LOADER_ID = 1;
75
76     /** The maximum age for dumped contents in the clients cache. */
77     private static long m_clientCacheMaxAge;
78
79     /** The resource loader configuration. */
80     private Map JavaDoc m_configuration;
81
82     /**
83      * The constructor of the class is empty and does nothing.<p>
84      */

85     public CmsDumpLoader() {
86
87         m_configuration = new TreeMap JavaDoc();
88     }
89
90     /**
91      * @see org.opencms.configuration.I_CmsConfigurationParameterHandler#addConfigurationParameter(java.lang.String, java.lang.String)
92      */

93     public void addConfigurationParameter(String JavaDoc paramName, String JavaDoc paramValue) {
94
95         m_configuration.put(paramName, paramValue);
96     }
97
98     /**
99      * Destroy this ResourceLoder, this is a NOOP so far.<p>
100      */

101     public void destroy() {
102
103         // NOOP
104
}
105
106     /**
107      * @see org.opencms.loader.I_CmsResourceLoader#dump(org.opencms.file.CmsObject, org.opencms.file.CmsResource, java.lang.String, java.util.Locale, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
108      */

109     public byte[] dump(
110         CmsObject cms,
111         CmsResource resource,
112         String JavaDoc element,
113         Locale JavaDoc locale,
114         HttpServletRequest JavaDoc req,
115         HttpServletResponse JavaDoc res) throws CmsException {
116
117         return CmsFile.upgrade(resource, cms).getContents();
118     }
119
120     /**
121      * @see org.opencms.loader.I_CmsResourceLoader#export(org.opencms.file.CmsObject, org.opencms.file.CmsResource, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
122      */

123     public byte[] export(CmsObject cms, CmsResource resource, HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res)
124     throws IOException JavaDoc, CmsException {
125
126         CmsFile file = CmsFile.upgrade(resource, cms);
127
128         // if no request and response are given, the resource only must be exported and no
129
// output must be generated
130
if ((req != null) && (res != null)) {
131             // overwrite headers if set as default
132
for (Iterator JavaDoc i = OpenCms.getStaticExportManager().getExportHeaders().listIterator(); i.hasNext();) {
133                 String JavaDoc header = (String JavaDoc)i.next();
134
135                 // set header only if format is "key: value"
136
String JavaDoc[] parts = CmsStringUtil.splitAsArray(header, ':');
137                 if (parts.length == 2) {
138                     res.setHeader(parts[0], parts[1]);
139                 }
140             }
141             load(cms, file, req, res);
142         }
143
144         return file.getContents();
145     }
146
147     /**
148      * Will always return <code>null</code> since this loader does not
149      * need to be cnofigured.<p>
150      *
151      * @see org.opencms.configuration.I_CmsConfigurationParameterHandler#getConfiguration()
152      */

153     public Map JavaDoc getConfiguration() {
154
155         // return the configuration in an immutable form
156
return Collections.unmodifiableMap(m_configuration);
157     }
158
159     /**
160      * @see org.opencms.loader.I_CmsResourceLoader#getLoaderId()
161      */

162     public int getLoaderId() {
163
164         return RESOURCE_LOADER_ID;
165     }
166
167     /**
168      * Return a String describing the ResourceLoader,
169      * which is (localized to the system default locale)
170      * <code>"The OpenCms default resource loader for unprocessed files"</code>.<p>
171      *
172      * @return a describing String for the ResourceLoader
173      */

174     public String JavaDoc getResourceLoaderInfo() {
175
176         return Messages.get().getBundle().key(Messages.GUI_LOADER_DUMB_DEFAULT_DESC_0);
177     }
178
179     /**
180      * @see org.opencms.configuration.I_CmsConfigurationParameterHandler#initConfiguration()
181      */

182     public void initConfiguration() {
183
184         ExtendedProperties config = new ExtendedProperties();
185         config.putAll(m_configuration);
186
187         String JavaDoc maxAge = config.getString("client.cache.maxage");
188         if (maxAge == null) {
189             m_clientCacheMaxAge = -1;
190         } else {
191             m_clientCacheMaxAge = Long.parseLong(maxAge);
192         }
193
194         if (CmsLog.INIT.isInfoEnabled()) {
195             if (maxAge != null) {
196                 CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_CLIENT_CACHE_MAX_AGE_1, maxAge));
197             }
198             CmsLog.INIT.info(Messages.get().getBundle().key(
199                 Messages.INIT_LOADER_INITIALIZED_1,
200                 this.getClass().getName()));
201         }
202     }
203
204     /**
205      * @see org.opencms.loader.I_CmsResourceLoader#isStaticExportEnabled()
206      */

207     public boolean isStaticExportEnabled() {
208
209         return true;
210     }
211
212     /**
213      * @see org.opencms.loader.I_CmsResourceLoader#isStaticExportProcessable()
214      */

215     public boolean isStaticExportProcessable() {
216
217         return false;
218     }
219
220     /**
221      * @see org.opencms.loader.I_CmsResourceLoader#isUsableForTemplates()
222      */

223     public boolean isUsableForTemplates() {
224
225         return false;
226     }
227
228     /**
229      * @see org.opencms.loader.I_CmsResourceLoader#isUsingUriWhenLoadingTemplate()
230      */

231     public boolean isUsingUriWhenLoadingTemplate() {
232
233         return false;
234     }
235
236     /**
237      * @see org.opencms.loader.I_CmsResourceLoader#load(org.opencms.file.CmsObject, org.opencms.file.CmsResource, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
238      */

239     public void load(CmsObject cms, CmsResource resource, HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res)
240     throws IOException JavaDoc, CmsException {
241
242         if (canSendLastModifiedHeader(resource, req, res)) {
243             // no further processing required
244
return;
245         }
246
247         // make sure we have the file contents available
248
CmsFile file = CmsFile.upgrade(resource, cms);
249
250         // set response status to "200 - OK" (required for static export "on-demand")
251
res.setStatus(HttpServletResponse.SC_OK);
252         // set content length header
253
res.setContentLength(file.getContents().length);
254
255         if (CmsWorkplaceManager.isWorkplaceUser(req)) {
256             // prevent caching for Workplace users
257
res.setDateHeader(CmsRequestUtil.HEADER_LAST_MODIFIED, System.currentTimeMillis());
258             CmsRequestUtil.setNoCacheHeaders(res);
259         } else {
260             // set date last modified header
261
res.setDateHeader(CmsRequestUtil.HEADER_LAST_MODIFIED, file.getDateLastModified());
262
263             // set "Expires" only if cache control is not already set
264
if (!res.containsHeader(CmsRequestUtil.HEADER_CACHE_CONTROL)) {
265                 long expireTime = resource.getDateExpired();
266                 if (expireTime == CmsResource.DATE_EXPIRED_DEFAULT) {
267                     expireTime--;
268                     // flex controller will automatically reduce this to a reasonable value
269
}
270                 // now set "Expires" header
271
CmsFlexController.setDateExpiresHeader(res, expireTime, m_clientCacheMaxAge);
272             }
273         }
274
275         service(cms, file, req, res);
276     }
277
278     /**
279      * @see org.opencms.loader.I_CmsResourceLoader#service(org.opencms.file.CmsObject, org.opencms.file.CmsResource, javax.servlet.ServletRequest, javax.servlet.ServletResponse)
280      */

281     public void service(CmsObject cms, CmsResource resource, ServletRequest JavaDoc req, ServletResponse JavaDoc res)
282     throws CmsException, IOException JavaDoc {
283
284         res.getOutputStream().write(CmsFile.upgrade(resource, cms).getContents());
285     }
286
287     /**
288      * Checks id the requested resource must be send to the client by checking the "If-Modified-Since" http header.<p>
289      *
290      * If the resource has not been modified, the header is send to the client and <code>true</code>
291      * is returned, otherwise nothing is send and <code>false</code> is returned.<p>
292      *
293      * @param resource the resource to check
294      * @param req the current request
295      * @param res the current response
296      *
297      * @return <code>true</code> if the last modified header has been send to the client
298      */

299     protected boolean canSendLastModifiedHeader(CmsResource resource, HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) {
300
301         // check if the current request was done by a workplace user
302
boolean isWorkplaceUser = CmsWorkplaceManager.isWorkplaceUser(req);
303
304         if (!isWorkplaceUser) {
305             // check if the request contains a last modified header
306
long lastModifiedHeader = req.getDateHeader(CmsRequestUtil.HEADER_IF_MODIFIED_SINCE);
307             if (lastModifiedHeader > -1) {
308                 // last modified header is set, compare it to the requested resource
309
if ((resource.getState() == CmsResource.STATE_UNCHANGED)
310                     && (resource.getDateLastModified() == lastModifiedHeader)) {
311                     long now = System.currentTimeMillis();
312                     if ((resource.getDateReleased() < now) && (resource.getDateExpired() > now)) {
313                         CmsFlexController.setDateExpiresHeader(res, resource.getDateExpired(), m_clientCacheMaxAge);
314                         res.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
315                         return true;
316                     }
317                 }
318             }
319         }
320         return false;
321     }
322 }
Popular Tags