001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH (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, 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.CmsObject;
032import org.opencms.file.CmsResource;
033import org.opencms.i18n.CmsLocaleManager;
034import org.opencms.main.CmsException;
035import org.opencms.main.CmsLog;
036import org.opencms.main.CmsRuntimeException;
037import org.opencms.main.OpenCms;
038import org.opencms.util.CmsRequestUtil;
039import org.opencms.util.CmsStringUtil;
040
041import java.io.IOException;
042import java.util.Locale;
043import java.util.Map;
044
045import javax.servlet.ServletRequest;
046import javax.servlet.ServletResponse;
047import javax.servlet.http.HttpServletRequest;
048import javax.servlet.http.HttpServletResponse;
049
050/**
051 * Loader for "pointers" to resources in the VFS or to external resources.<p>
052 * 
053 * @since 6.0.0 
054 */
055public class CmsPointerLoader extends CmsDumpLoader {
056
057    /**
058     * The configuration parameter for the OpenCms XML configuration to enable
059     * that the parameters in requests to pointer resources are appended to the
060     * pointer target link.
061     */
062    public static final String CONFIGURATION_REQUEST_PARAM_SUPPORT_ENABLED = "pointer.requestparamsupport.enabled";
063
064    /** The id of this loader. */
065    public static final int RESOURCE_POINTER_LOADER_ID = 4;
066
067    /**
068     * Flag that controls if parameters in requests to pointer resources are
069     * appended to the target link when redirecting.
070     */
071    protected static boolean m_requestParamSupportEnabled;
072
073    /** The html-code prefix for generating the export file for external links. */
074    private static String EXPORT_PREFIX = "<html>\n<head>\n<meta http-equiv="
075        + '"'
076        + "refresh"
077        + '"'
078        + " content="
079        + '"'
080        + "0; url=";
081
082    /** The html-code suffix for generating the export file for external links. */
083    private static String EXPORT_SUFFIX = '"' + ">\n</head>\n<body></body>\n</html>";
084
085    /**
086     * The constructor of the class is empty and does nothing.<p>
087     */
088    public CmsPointerLoader() {
089
090        // NOOP
091    }
092
093    /**
094     * Returns <code>true</code> if parameters in requests to pointer resources
095     * are appended to the target link when redirecting.
096     * <p>
097     * This is controlled by the configuration of this loader in
098     * <code>opencms-system.xml</code>.
099     * <p>
100     * 
101     * @return <code>true</code> if parameters in requests to pointer resources
102     *         are appended to the target link when redirecting.
103     */
104    public static boolean isRequestParamSupportEnabled() {
105
106        return m_requestParamSupportEnabled;
107    }
108
109    /**
110     * Internal helper that is used by
111     * <code>{@link #load(CmsObject, CmsResource, HttpServletRequest, HttpServletResponse)}</code>
112     * and
113     * <code>{@link #export(CmsObject, CmsResource, HttpServletRequest, HttpServletResponse)}</code>
114     * to handle conditional request parameter support for links to pointer
115     * resources.
116     * <p>
117     * 
118     * @param pointerLink
119     *            the link to append request parameters to
120     * 
121     * @param req
122     *            the original request to the pointer
123     * 
124     * @return the pointer with the parameters
125     */
126    @SuppressWarnings("unchecked")
127    private static String appendLinkParams(String pointerLink, HttpServletRequest req) {
128
129        String result = pointerLink;
130        if (isRequestParamSupportEnabled()) {
131            Map<String, String[]> params = req.getParameterMap();
132            if (params.size() > 0) {
133                result = CmsRequestUtil.appendParameters(result, params, false);
134            }
135        }
136        return result;
137    }
138
139    /**
140     * @see org.opencms.configuration.I_CmsConfigurationParameterHandler#addConfigurationParameter(java.lang.String, java.lang.String)
141     */
142    @Override
143    public void addConfigurationParameter(String paramName, String paramValue) {
144
145        if (CmsStringUtil.isNotEmpty(paramName) && CmsStringUtil.isNotEmpty(paramValue)) {
146            if (CONFIGURATION_REQUEST_PARAM_SUPPORT_ENABLED.equals(paramName)) {
147                m_requestParamSupportEnabled = Boolean.valueOf(paramValue).booleanValue();
148            }
149        }
150    }
151
152    /** 
153     * Destroy this ResourceLoder, this is a NOOP so far.<p>
154     */
155    @Override
156    public void destroy() {
157
158        // NOOP
159    }
160
161    /**
162     * @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)
163     */
164    @Override
165    public byte[] dump(
166        CmsObject cms,
167        CmsResource resource,
168        String element,
169        Locale locale,
170        HttpServletRequest req,
171        HttpServletResponse res) throws CmsException {
172
173        return cms.readFile(resource).getContents();
174    }
175
176    /**
177     * @see org.opencms.loader.I_CmsResourceLoader#export(org.opencms.file.CmsObject, org.opencms.file.CmsResource, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
178     */
179    @Override
180    public byte[] export(CmsObject cms, CmsResource resource, HttpServletRequest req, HttpServletResponse res)
181    throws IOException, CmsException {
182
183        String pointer = new String(cms.readFile(resource).getContents(), CmsLocaleManager.getResourceEncoding(
184            cms,
185            resource));
186        StringBuffer result = new StringBuffer(128);
187        result.append(EXPORT_PREFIX);
188        // conditionally append parameters of the current request:
189        pointer = appendLinkParams(pointer, req);
190        if (pointer.indexOf(':') < 0) {
191            result.append(OpenCms.getLinkManager().substituteLink(cms, pointer));
192        } else {
193            result.append(pointer);
194        }
195        result.append(EXPORT_SUFFIX);
196        load(cms, resource, req, res);
197        return result.toString().getBytes(OpenCms.getSystemInfo().getDefaultEncoding());
198    }
199
200    /**
201     * Will always return <code>null</code> since this loader does not 
202     * need to be cnofigured.<p>
203     * 
204     * @see org.opencms.configuration.I_CmsConfigurationParameterHandler#getConfiguration()
205     */
206    @Override
207    public CmsParameterConfiguration getConfiguration() {
208
209        CmsParameterConfiguration result = new CmsParameterConfiguration();
210        CmsParameterConfiguration config = super.getConfiguration();
211        if (config != null) {
212            result.putAll(config);
213        }
214        result.put(CONFIGURATION_REQUEST_PARAM_SUPPORT_ENABLED, String.valueOf(m_requestParamSupportEnabled));
215        return result;
216    }
217
218    /**
219     * @see org.opencms.loader.I_CmsResourceLoader#getLoaderId()
220     */
221    @Override
222    public int getLoaderId() {
223
224        return RESOURCE_POINTER_LOADER_ID;
225    }
226
227    /**
228     * Return a String describing the ResourceLoader,
229     * which is (localized to the system default locale)
230     * <code>"The OpenCms default resource loader for pointers"</code>.<p>
231     * 
232     * @return a describing String for the ResourceLoader 
233     */
234    @Override
235    public String getResourceLoaderInfo() {
236
237        return Messages.get().getBundle().key(Messages.GUI_LOADER_POINTER_DEFAULT_DESC_0);
238    }
239
240    /**
241     * @see org.opencms.configuration.I_CmsConfigurationParameterHandler#initConfiguration()
242     */
243    @Override
244    public void initConfiguration() {
245
246        if (CmsLog.INIT.isInfoEnabled()) {
247            CmsLog.INIT.info(Messages.get().getBundle().key(
248                Messages.INIT_LOADER_INITIALIZED_1,
249                this.getClass().getName()));
250        }
251    }
252
253    /**
254     * Returns true if request parameter support is disabled. <p>
255     *
256     * @return 
257     *      true if request parameter support is disabled
258     *
259     * @see org.opencms.loader.I_CmsResourceLoader#isStaticExportEnabled()
260     */
261    @Override
262    public boolean isStaticExportEnabled() {
263
264        return !m_requestParamSupportEnabled;
265    }
266
267    /**
268     * @see org.opencms.loader.I_CmsResourceLoader#isStaticExportProcessable()
269     */
270    @Override
271    public boolean isStaticExportProcessable() {
272
273        return false;
274    }
275
276    /**
277     * @see org.opencms.loader.I_CmsResourceLoader#isUsableForTemplates()
278     */
279    @Override
280    public boolean isUsableForTemplates() {
281
282        return false;
283    }
284
285    /**
286     * @see org.opencms.loader.I_CmsResourceLoader#isUsingUriWhenLoadingTemplate()
287     */
288    @Override
289    public boolean isUsingUriWhenLoadingTemplate() {
290
291        return false;
292    }
293
294    /**
295     * @see org.opencms.loader.I_CmsResourceLoader#load(org.opencms.file.CmsObject, org.opencms.file.CmsResource, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
296     */
297    @Override
298    public void load(CmsObject cms, CmsResource resource, HttpServletRequest req, HttpServletResponse res)
299    throws IOException, CmsException {
300
301        if ((res == null) || res.isCommitted()) {
302            // nothing we can do
303            return;
304        }
305
306        String pointer = new String(cms.readFile(resource).getContents(), CmsLocaleManager.getResourceEncoding(
307            cms,
308            resource));
309        if (CmsStringUtil.isEmptyOrWhitespaceOnly(pointer)) {
310            throw new CmsLoaderException(Messages.get().container(
311                Messages.ERR_INVALID_POINTER_FILE_1,
312                resource.getName()));
313        }
314        if (pointer.indexOf(':') < 0) {
315            pointer = OpenCms.getLinkManager().substituteLink(cms, pointer);
316        }
317
318        // conditionally append parameters of the current request:
319        pointer = appendLinkParams(pointer, req);
320        res.sendRedirect(pointer);
321    }
322
323    /**
324     * @see org.opencms.loader.I_CmsResourceLoader#service(org.opencms.file.CmsObject, org.opencms.file.CmsResource, javax.servlet.ServletRequest, javax.servlet.ServletResponse)
325     */
326    @Override
327    public void service(CmsObject cms, CmsResource file, ServletRequest req, ServletResponse res) {
328
329        throw new CmsRuntimeException(
330            Messages.get().container(Messages.ERR_SERVICE_UNSUPPORTED_1, getClass().getName()));
331    }
332}