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.util;
029
030import org.opencms.loader.CmsTemplateContext;
031import org.opencms.loader.CmsTemplateContextManager;
032import org.opencms.util.CmsRequestUtil;
033
034import java.util.Arrays;
035import java.util.HashSet;
036import java.util.List;
037import java.util.Set;
038
039import javax.servlet.http.HttpServletRequest;
040
041/**
042 * Device selector class which distinguishes between the cases "desktop", "mobile" and "tablet".<p>
043 */
044public class CmsJspDeviceSelectorDesktopMobileTablet implements I_CmsJspDeviceSelector {
045
046    /** Constant for desktop detection. */
047    public static final String C_DESKTOP = "desktop";
048
049    /** Constant for mobile detection. */
050    public static final String C_MOBILE = "mobile";
051
052    /** Constant for tablet. */
053    public static final String C_TABLET = "tablet";
054
055    /** The list of types supported by this device selector implementation. */
056    public static final List<String> TYPES = Arrays.asList(new String[] {C_MOBILE, C_DESKTOP, C_TABLET});
057
058    /** The user agent info. */
059    private UAgentInfo m_userAgentInfo;
060
061    /**
062     * @see org.opencms.jsp.util.I_CmsJspDeviceSelector#getDeviceType(javax.servlet.http.HttpServletRequest)
063     */
064    public String getDeviceType(HttpServletRequest req) {
065
066        CmsTemplateContext templateContext = (CmsTemplateContext)(req.getAttribute(
067            CmsTemplateContextManager.ATTR_TEMPLATE_CONTEXT));
068        if ((templateContext != null) && isTemplateContextCompatible(templateContext)) {
069            return templateContext.getKey();
070        }
071        m_userAgentInfo = new UAgentInfo(
072            req.getHeader(CmsRequestUtil.HEADER_USER_AGENT),
073            req.getHeader(CmsRequestUtil.HEADER_ACCEPT));
074        if (m_userAgentInfo.detectMobileQuick()) {
075            return C_MOBILE;
076        }
077
078        if (m_userAgentInfo.getIsTierTablet()) {
079            return C_TABLET;
080        }
081        return C_DESKTOP;
082    }
083
084    /**
085     * @see org.opencms.jsp.util.I_CmsJspDeviceSelector#getDeviceTypes()
086     */
087    public List<String> getDeviceTypes() {
088
089        return TYPES;
090    }
091
092    /**
093     * Returns the User Agent info.<p>
094     *
095     * @return the information about the user agent
096     */
097    public UAgentInfo getUserAgentInfo() {
098
099        return m_userAgentInfo;
100    }
101
102    /**
103     * Checks if a template context is compatible with this device selector.<p>
104     *
105     * @param templateContext the template context to check
106     *
107     * @return true if the template context is compatible
108     */
109    protected boolean isTemplateContextCompatible(CmsTemplateContext templateContext) {
110
111        Set<String> contextKeys = new HashSet<String>(templateContext.getProvider().getAllContexts().keySet());
112        return contextKeys.equals(new HashSet<String>(TYPES));
113    }
114
115}