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, 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.jsp;
029
030import org.opencms.flex.CmsFlexController;
031import org.opencms.jsp.util.I_CmsJspDeviceSelector;
032import org.opencms.main.CmsLog;
033import org.opencms.main.OpenCms;
034import org.opencms.util.CmsStringUtil;
035
036import java.util.List;
037
038import javax.servlet.http.HttpServletRequest;
039import javax.servlet.jsp.tagext.BodyTagSupport;
040
041import org.apache.commons.logging.Log;
042
043/**
044 * This class provides a <code>&lt;cms:device type="..."&gt;</code>-Tag
045 * with the attribute <code>type</code> to specify the device.<p>
046 *
047 * @since 8.0.0
048 */
049public class CmsJspTagDevice extends BodyTagSupport {
050
051    /** The log object for this class. */
052    private static final Log LOG = CmsLog.getLog(CmsJspTagDevice.class);
053
054    /** Serial version UID required for safe serialization. */
055    private static final long serialVersionUID = 9175484824140856283L;
056
057    /** Device for output. */
058    protected String m_type;
059
060    /**
061     * Close the device tag.<p>
062     *
063     * @return {@link #EVAL_PAGE}
064     */
065    @Override
066    public int doEndTag() {
067
068        if (OpenCms.getSystemInfo().getServletContainerSettings().isReleaseTagsAfterEnd()) {
069            // need to release manually, JSP container may not call release as required (happens with Tomcat)
070            release();
071        }
072
073        return EVAL_PAGE;
074    }
075
076    /**
077     * Decides on the base of the device selector interface whether the user's device is in
078     * the selected device types or not.<p>
079     *
080     * If the user's device is in the list of possible devices the content inside the tag is printed out
081     * and otherwise the content won't be printed out.<p>
082     *
083     * @return {@link #EVAL_BODY_INCLUDE}<br/>{@link #SKIP_BODY}
084     */
085    @Override
086    public int doStartTag() {
087
088        // get the flex controller
089        CmsFlexController controller = CmsFlexController.getController(pageContext.getRequest());
090
091        // get the device selector
092        I_CmsJspDeviceSelector selector = OpenCms.getSystemInfo().getDeviceSelector();
093
094        List<String> supportedDevices = selector.getDeviceTypes();
095        List<String> selectedDevices = CmsStringUtil.splitAsList(m_type, ",", true);
096
097        // check if the selected device is in the list of supported devices
098        for (String selectedDevice : selectedDevices) {
099            if (supportedDevices.contains(selectedDevice)) {
100                // get the current device from the request
101                HttpServletRequest req = controller.getTopRequest();
102                String device = (String)req.getAttribute(I_CmsJspDeviceSelector.REQUEST_ATTRIBUTE_DEVICE);
103
104                if (CmsStringUtil.isEmptyOrWhitespaceOnly(device)) {
105                    // device not found in request
106                    device = selector.getDeviceType(req);
107                    if (CmsStringUtil.isNotEmpty(device)) {
108                        // put the detected device into the request
109                        req.setAttribute(I_CmsJspDeviceSelector.REQUEST_ATTRIBUTE_DEVICE, device);
110                    }
111                }
112
113                // check if the detected device is in the list of given types
114                if (selectedDevices.contains(device)) {
115                    return EVAL_BODY_INCLUDE;
116                } else {
117                    return SKIP_BODY;
118                }
119            } else {
120                LOG.error(
121                    Messages.get().getBundle().key(
122                        Messages.LOG_WRONG_DEVICE_TYPE_2,
123                        selectedDevice,
124                        controller.getCurrentRequest().getElementUri()));
125            }
126        }
127        return SKIP_BODY;
128    }
129
130    /**
131     * Returns the device type.<p>
132     *
133     * @return the device type
134     */
135    public String getType() {
136
137        return m_type != null ? m_type : "";
138    }
139
140    /**
141     * Releases any resources we may have (or inherit).<p>
142     */
143    @Override
144    public void release() {
145
146        super.release();
147        m_type = null;
148    }
149
150    /**
151     * Sets the type for the device.<p>
152     *
153     * @param type the device type
154     */
155    public void setType(String type) {
156
157        if (type != null) {
158            m_type = type;
159        }
160    }
161}