001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software GmbH & Co. KG, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.loader;
029
030import org.opencms.configuration.CmsParameterConfiguration;
031import org.opencms.file.CmsFile;
032import org.opencms.file.CmsObject;
033import org.opencms.file.CmsResource;
034import org.opencms.flex.CmsFlexController;
035import org.opencms.main.CmsException;
036import org.opencms.main.CmsLog;
037import org.opencms.main.OpenCms;
038import org.opencms.util.CmsRequestUtil;
039import org.opencms.util.CmsStringUtil;
040import org.opencms.workplace.CmsWorkplaceManager;
041
042import java.io.IOException;
043import java.util.Iterator;
044import java.util.Locale;
045
046import javax.servlet.ServletRequest;
047import javax.servlet.ServletResponse;
048import javax.servlet.http.HttpServletRequest;
049import javax.servlet.http.HttpServletResponse;
050
051/**
052 * Dump loader for binary or other unprocessed resource types.<p>
053 *
054 * This loader is also used to deliver static sub-elements of pages processed
055 * by other loaders.<p>
056 *
057 * @since 6.0.0
058 */
059public class CmsDumpLoader implements I_CmsResourceLoader {
060
061    /** The id of this loader. */
062    public static final int RESOURCE_LOADER_ID = 1;
063
064    /** The maximum age for dumped contents in the clients cache. */
065    private static long m_clientCacheMaxAge;
066
067    /** The resource loader configuration. */
068    private CmsParameterConfiguration m_configuration;
069
070    /**
071     * The constructor of the class is empty and does nothing.<p>
072     */
073    public CmsDumpLoader() {
074
075        m_configuration = new CmsParameterConfiguration();
076    }
077
078    /**
079     * @see org.opencms.configuration.I_CmsConfigurationParameterHandler#addConfigurationParameter(java.lang.String, java.lang.String)
080     */
081    public void addConfigurationParameter(String paramName, String paramValue) {
082
083        m_configuration.put(paramName, paramValue);
084    }
085
086    /**
087     * Destroy this ResourceLoder, this is a NOOP so far.<p>
088     */
089    public void destroy() {
090
091        // NOOP
092    }
093
094    /**
095     * @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)
096     */
097    public byte[] dump(
098        CmsObject cms,
099        CmsResource resource,
100        String element,
101        Locale locale,
102        HttpServletRequest req,
103        HttpServletResponse res) throws CmsException {
104
105        return cms.readFile(resource).getContents();
106    }
107
108    /**
109     * @see org.opencms.loader.I_CmsResourceLoader#export(org.opencms.file.CmsObject, org.opencms.file.CmsResource, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
110     */
111    public byte[] export(CmsObject cms, CmsResource resource, HttpServletRequest req, HttpServletResponse res)
112    throws IOException, CmsException {
113
114        CmsFile file = cms.readFile(resource);
115
116        // if no request and response are given, the resource only must be exported and no
117        // output must be generated
118        if ((req != null) && (res != null)) {
119            // overwrite headers if set as default
120            for (Iterator<String> i = OpenCms.getStaticExportManager().getExportHeaders().listIterator(); i.hasNext();) {
121                String header = i.next();
122
123                // set header only if format is "key: value"
124                String[] parts = CmsStringUtil.splitAsArray(header, ':');
125                if (parts.length == 2) {
126                    res.setHeader(parts[0], parts[1]);
127                }
128            }
129            load(cms, file, req, res);
130        }
131
132        return file.getContents();
133    }
134
135    /**
136     * Will always return <code>null</code> since this loader does not
137     * need to be configured.<p>
138     *
139     * @see org.opencms.configuration.I_CmsConfigurationParameterHandler#getConfiguration()
140     */
141    public CmsParameterConfiguration getConfiguration() {
142
143        // return the configuration in an immutable form
144        return m_configuration;
145    }
146
147    /**
148     * @see org.opencms.loader.I_CmsResourceLoader#getLoaderId()
149     */
150    public int getLoaderId() {
151
152        return RESOURCE_LOADER_ID;
153    }
154
155    /**
156     * Return a String describing the ResourceLoader,
157     * which is (localized to the system default locale)
158     * <code>"The OpenCms default resource loader for unprocessed files"</code>.<p>
159     *
160     * @return a describing String for the ResourceLoader
161     */
162    public String getResourceLoaderInfo() {
163
164        return Messages.get().getBundle().key(Messages.GUI_LOADER_DUMB_DEFAULT_DESC_0);
165    }
166
167    /**
168     * @see org.opencms.configuration.I_CmsConfigurationParameterHandler#initConfiguration()
169     */
170    public void initConfiguration() {
171
172        Object maxAge = m_configuration.get("client.cache.maxage");
173        if (maxAge == null) {
174            m_clientCacheMaxAge = -1;
175        } else {
176            m_clientCacheMaxAge = Long.parseLong(String.valueOf(maxAge));
177        }
178
179        if (CmsLog.INIT.isInfoEnabled()) {
180            if (maxAge != null) {
181                CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_CLIENT_CACHE_MAX_AGE_1, maxAge));
182            }
183            CmsLog.INIT.info(
184                Messages.get().getBundle().key(Messages.INIT_LOADER_INITIALIZED_1, this.getClass().getName()));
185        }
186    }
187
188    /**
189     * @see org.opencms.loader.I_CmsResourceLoader#isStaticExportEnabled()
190     */
191    public boolean isStaticExportEnabled() {
192
193        return true;
194    }
195
196    /**
197     * @see org.opencms.loader.I_CmsResourceLoader#isStaticExportProcessable()
198     */
199    public boolean isStaticExportProcessable() {
200
201        return false;
202    }
203
204    /**
205     * @see org.opencms.loader.I_CmsResourceLoader#isUsableForTemplates()
206     */
207    public boolean isUsableForTemplates() {
208
209        return false;
210    }
211
212    /**
213     * @see org.opencms.loader.I_CmsResourceLoader#isUsingUriWhenLoadingTemplate()
214     */
215    public boolean isUsingUriWhenLoadingTemplate() {
216
217        return false;
218    }
219
220    /**
221     * @see org.opencms.loader.I_CmsResourceLoader#load(org.opencms.file.CmsObject, org.opencms.file.CmsResource, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
222     */
223    public void load(CmsObject cms, CmsResource resource, HttpServletRequest req, HttpServletResponse res)
224    throws IOException, CmsException {
225
226        if (canSendLastModifiedHeader(resource, req, res)) {
227            // no further processing required
228            return;
229        }
230
231        // make sure we have the file contents available
232        CmsFile file = cms.readFile(resource);
233
234        // set response status to "200 - OK" (required for static export "on-demand")
235        res.setStatus(HttpServletResponse.SC_OK);
236        // set content length header
237        res.setContentLength(file.getContents().length);
238
239        if (CmsWorkplaceManager.isWorkplaceUser(req)) {
240            // prevent caching for Workplace users
241            res.setDateHeader(CmsRequestUtil.HEADER_LAST_MODIFIED, System.currentTimeMillis());
242            CmsRequestUtil.setNoCacheHeaders(res);
243        } else {
244            // set date last modified header
245            res.setDateHeader(CmsRequestUtil.HEADER_LAST_MODIFIED, file.getDateLastModified());
246
247            // set "Expires" only if cache control is not already set
248            if (!res.containsHeader(CmsRequestUtil.HEADER_CACHE_CONTROL)) {
249                long expireTime = resource.getDateExpired();
250                if (expireTime == CmsResource.DATE_EXPIRED_DEFAULT) {
251                    expireTime--;
252                    // flex controller will automatically reduce this to a reasonable value
253                }
254                // now set "Expires" header
255                CmsFlexController.setDateExpiresHeader(res, expireTime, m_clientCacheMaxAge);
256            }
257        }
258
259        service(cms, file, req, res);
260    }
261
262    /**
263     * @see org.opencms.loader.I_CmsResourceLoader#service(org.opencms.file.CmsObject, org.opencms.file.CmsResource, javax.servlet.ServletRequest, javax.servlet.ServletResponse)
264     */
265    public void service(CmsObject cms, CmsResource resource, ServletRequest req, ServletResponse res)
266    throws CmsException, IOException {
267
268        res.getOutputStream().write(cms.readFile(resource).getContents());
269    }
270
271    /**
272     * Checks if the requested resource must be send to the client by checking the "If-Modified-Since" http header.<p>
273     *
274     * If the resource has not been modified, the "304 - not modified"
275     * header is send to the client and <code>true</code>
276     * is returned, otherwise nothing is send and <code>false</code> is returned.<p>
277     *
278     * @param resource the resource to check
279     * @param req the current request
280     * @param res the current response
281     *
282     * @return <code>true</code> if the "304 - not modified" header has been send to the client
283     */
284    protected boolean canSendLastModifiedHeader(CmsResource resource, HttpServletRequest req, HttpServletResponse res) {
285
286        // resource state must be unchanged
287        if (resource.getState().isUnchanged()
288            // the request must not have been send by a workplace user (we can't use "304 - not modified" in workplace
289            && !CmsWorkplaceManager.isWorkplaceUser(req)
290            // last modified header must match the time form the resource
291            && CmsFlexController.isNotModifiedSince(req, resource.getDateLastModified())) {
292            long now = System.currentTimeMillis();
293            if ((resource.getDateReleased() < now) && (resource.getDateExpired() > now)) {
294                // resource is available and not expired
295                CmsFlexController.setDateExpiresHeader(res, resource.getDateExpired(), m_clientCacheMaxAge);
296                // set status 304 - not modified
297                res.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
298                return true;
299            }
300        }
301        return false;
302    }
303}