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.db;
029
030import org.opencms.file.CmsObject;
031import org.opencms.file.CmsResource;
032import org.opencms.main.CmsException;
033import org.opencms.main.CmsLog;
034import org.opencms.main.I_CmsResourceInit;
035import org.opencms.main.OpenCms;
036import org.opencms.util.CmsStringUtil;
037
038import javax.servlet.http.HttpServletRequest;
039import javax.servlet.http.HttpServletResponse;
040
041import org.apache.commons.logging.Log;
042
043/**
044 * This resource handler checks if a resource has to be marked as visited by the current user.
045 * It checks the value of the <code>usertracking.mark</code> property.<p>
046 *
047 * Possible values are:
048 * <ul>
049 * <li><code>online</code>: The resource is marked only in the online project
050 * <li><code>true</code>: The resource is marked in all projects
051 * <li><code>false</code>: The resource is not marked at all
052 * </ul>
053 *
054 * @since 8.0
055 */
056public class CmsUserTrackingResourceHandler implements I_CmsResourceInit {
057
058    /** Property that indicates if resources should be tracked,
059     *  value has to be <code>true</code>, <code>false</code> or <code>online</code>.
060     */
061    public static final String PROPERTY_USERTRACKING_MARK = "usertracking.mark";
062
063    /** Constant for the property value <code>online</code>. */
064    public static final String VALUE_ONLINE = "online";
065
066    /** The log object for this class. */
067    private static final Log LOG = CmsLog.getLog(CmsUserTrackingResourceHandler.class);
068
069    /**
070     * @see org.opencms.main.I_CmsResourceInit#initResource(org.opencms.file.CmsResource, org.opencms.file.CmsObject, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
071     */
072    public CmsResource initResource(
073        CmsResource resource,
074        CmsObject cms,
075        HttpServletRequest req,
076        HttpServletResponse res) {
077
078        if ((resource != null) && resource.isFile()) {
079            // only do something if the resource was found and is a file (that can be marked)
080            String mark = "";
081            try {
082                // read the property value
083                mark = cms.readPropertyObject(resource, PROPERTY_USERTRACKING_MARK, true).getValue(CmsStringUtil.FALSE);
084            } catch (CmsException e) {
085                // ignore, resource will not be marked at all
086            }
087            if (Boolean.valueOf(mark).booleanValue()
088                || (VALUE_ONLINE.equalsIgnoreCase(mark)
089                    && cms.getRequestContext().getCurrentProject().isOnlineProject())) {
090                // mark the resource as visited by the current user
091                try {
092                    OpenCms.getSubscriptionManager().markResourceAsVisitedBy(
093                        cms,
094                        resource,
095                        cms.getRequestContext().getCurrentUser());
096                } catch (CmsException e) {
097                    // error marking resource
098                    LOG.error(e.getLocalizedMessage(), e);
099                }
100            }
101        }
102
103        return resource;
104    }
105
106}