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.CmsObject;
032import org.opencms.file.CmsResource;
033import org.opencms.i18n.CmsEncoder;
034import org.opencms.i18n.CmsLocaleManager;
035import org.opencms.main.CmsException;
036import org.opencms.main.CmsLog;
037import org.opencms.main.CmsRuntimeException;
038import org.opencms.main.OpenCms;
039import org.opencms.util.CmsRequestUtil;
040import org.opencms.util.CmsStringUtil;
041
042import java.io.IOException;
043import java.util.Locale;
044import java.util.Map;
045
046import javax.servlet.ServletRequest;
047import javax.servlet.ServletResponse;
048import javax.servlet.http.HttpServletRequest;
049import javax.servlet.http.HttpServletResponse;
050
051/**
052 * Loader for "pointers" to resources in the VFS or to external resources.<p>
053 *
054 * @since 6.0.0
055 */
056public class CmsPointerLoader extends CmsDumpLoader {
057
058    /**
059     * The configuration parameter for the OpenCms XML configuration to enable
060     * that the parameters in requests to pointer resources are appended to the
061     * pointer target link.
062     */
063    public static final String CONFIGURATION_REQUEST_PARAM_SUPPORT_ENABLED = "pointer.requestparamsupport.enabled";
064
065    /** The id of this loader. */
066    public static final int RESOURCE_POINTER_LOADER_ID = 4;
067
068    /**
069     * Flag that controls if parameters in requests to pointer resources are
070     * appended to the target link when redirecting.
071     */
072    protected static boolean m_requestParamSupportEnabled;
073
074    /** The html-code prefix for generating the export file for external links. */
075    private static String EXPORT_PREFIX = "<html>\n<head>\n<meta http-equiv="
076        + '"'
077        + "refresh"
078        + '"'
079        + " content="
080        + '"'
081        + "0; url=";
082
083    /** The html-code suffix for generating the export file for external links. */
084    private static String EXPORT_SUFFIX = '"' + ">\n</head>\n<body></body>\n</html>";
085
086    /**
087     * The constructor of the class is empty and does nothing.<p>
088     */
089    public CmsPointerLoader() {
090
091        // NOOP
092    }
093
094    /**
095     * Returns <code>true</code> if parameters in requests to pointer resources
096     * are appended to the target link when redirecting.
097     * <p>
098     * This is controlled by the configuration of this loader in
099     * <code>opencms-system.xml</code>.
100     * <p>
101     *
102     * @return <code>true</code> if parameters in requests to pointer resources
103     *         are appended to the target link when redirecting.
104     */
105    public static boolean isRequestParamSupportEnabled() {
106
107        return m_requestParamSupportEnabled;
108    }
109
110    /**
111     * Internal helper that is used by
112     * <code>{@link #load(CmsObject, CmsResource, HttpServletRequest, HttpServletResponse)}</code>
113     * and
114     * <code>{@link #export(CmsObject, CmsResource, HttpServletRequest, HttpServletResponse)}</code>
115     * to handle conditional request parameter support for links to pointer
116     * resources.
117     * <p>
118     *
119     * @param pointerLink
120     *            the link to append request parameters to
121     *
122     * @param req
123     *            the original request to the pointer
124     *
125     * @return the pointer with the parameters
126     */
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(
184            cms.readFile(resource).getContents(),
185            CmsLocaleManager.getResourceEncoding(cms, 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(
248                Messages.get().getBundle().key(Messages.INIT_LOADER_INITIALIZED_1, this.getClass().getName()));
249        }
250    }
251
252    /**
253     * Returns true if request parameter support is disabled. <p>
254     *
255     * @return
256     *      true if request parameter support is disabled
257     *
258     * @see org.opencms.loader.I_CmsResourceLoader#isStaticExportEnabled()
259     */
260    @Override
261    public boolean isStaticExportEnabled() {
262
263        return !m_requestParamSupportEnabled;
264    }
265
266    /**
267     * @see org.opencms.loader.I_CmsResourceLoader#isStaticExportProcessable()
268     */
269    @Override
270    public boolean isStaticExportProcessable() {
271
272        return false;
273    }
274
275    /**
276     * @see org.opencms.loader.I_CmsResourceLoader#isUsableForTemplates()
277     */
278    @Override
279    public boolean isUsableForTemplates() {
280
281        return false;
282    }
283
284    /**
285     * @see org.opencms.loader.I_CmsResourceLoader#isUsingUriWhenLoadingTemplate()
286     */
287    @Override
288    public boolean isUsingUriWhenLoadingTemplate() {
289
290        return false;
291    }
292
293    /**
294     * @see org.opencms.loader.I_CmsResourceLoader#load(org.opencms.file.CmsObject, org.opencms.file.CmsResource, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
295     */
296    @Override
297    public void load(CmsObject cms, CmsResource resource, HttpServletRequest req, HttpServletResponse res)
298    throws IOException, CmsException {
299
300        if ((res == null) || res.isCommitted()) {
301            // nothing we can do
302            return;
303        }
304
305        String pointer = new String(
306            cms.readFile(resource).getContents(),
307            CmsLocaleManager.getResourceEncoding(cms, resource));
308        if (CmsStringUtil.isEmptyOrWhitespaceOnly(pointer)) {
309            throw new CmsLoaderException(
310                Messages.get().container(Messages.ERR_INVALID_POINTER_FILE_1, resource.getName()));
311        }
312        if (pointer.indexOf(':') < 0) {
313            pointer = OpenCms.getLinkManager().substituteLink(cms, pointer);
314        } else {
315            pointer = CmsEncoder.convertHostToPunycode(pointer);
316        }
317
318        // conditionally append parameters of the current request:
319        pointer = appendLinkParams(pointer, req);
320
321        res.sendRedirect(pointer);
322    }
323
324    /**
325     * @see org.opencms.loader.I_CmsResourceLoader#service(org.opencms.file.CmsObject, org.opencms.file.CmsResource, javax.servlet.ServletRequest, javax.servlet.ServletResponse)
326     */
327    @Override
328    public void service(CmsObject cms, CmsResource file, ServletRequest req, ServletResponse res) {
329
330        throw new CmsRuntimeException(
331            Messages.get().container(Messages.ERR_SERVICE_UNSUPPORTED_1, getClass().getName()));
332    }
333}