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.xml.xml2json.handler;
029
030import org.opencms.main.CmsLog;
031import org.opencms.main.I_CmsEventListener;
032import org.opencms.main.OpenCms;
033import org.opencms.xml.xml2json.CmsJsonResult;
034
035import java.util.concurrent.ExecutionException;
036
037import javax.servlet.http.HttpServletResponse;
038
039import org.apache.commons.logging.Log;
040
041import com.google.common.cache.CacheBuilder;
042import com.google.common.cache.CacheLoader;
043import com.google.common.cache.LoadingCache;
044
045/**
046 * Wrapper for JSON handlers that caches online project requests.
047 */
048public class CmsJsonHandlerOnlineCachingWrapper implements I_CmsJsonHandler {
049
050    /** Logger instance for this class. */
051    private static final Log LOG = CmsLog.getLog(CmsJsonHandlerOnlineCachingWrapper.class);
052
053    /** The wrapped handler. */
054    private I_CmsJsonHandler m_handler;
055
056    /** The cache. */
057    private LoadingCache<CmsJsonHandlerContext.Key, CmsJsonResult> m_cache;
058
059    /**
060     * Creates a new instance.
061     *
062     * @param handler the handler to wrap
063     * @param spec the CacheBuilder specification to use for the cache
064     */
065    public CmsJsonHandlerOnlineCachingWrapper(I_CmsJsonHandler handler, String spec) {
066
067        m_handler = handler;
068        CacheBuilder<Object, Object> cacheBuilder = CacheBuilder.from(spec);
069        m_cache = cacheBuilder.build(new CacheLoader<CmsJsonHandlerContext.Key, CmsJsonResult>() {
070
071            @Override
072            @SuppressWarnings("synthetic-access")
073            public CmsJsonResult load(CmsJsonHandlerContext.Key key) throws Exception {
074
075                CmsJsonResult result = m_handler.renderJson(key.getContext());
076                return result;
077
078            }
079        });
080        OpenCms.addCmsEventListener(
081            evt -> m_cache.invalidateAll(),
082            new int[] {I_CmsEventListener.EVENT_CLEAR_CACHES, I_CmsEventListener.EVENT_PUBLISH_PROJECT});
083
084    }
085
086    /**
087     * @see org.opencms.xml.xml2json.handler.I_CmsJsonHandler#getOrder()
088     */
089    public double getOrder() {
090
091        return m_handler.getOrder();
092    }
093
094    /**
095     * @see org.opencms.xml.xml2json.handler.I_CmsJsonHandler#matches(org.opencms.xml.xml2json.handler.CmsJsonHandlerContext)
096     */
097    public boolean matches(CmsJsonHandlerContext context) {
098
099        return m_handler.matches(context);
100    }
101
102    /**
103     * @see org.opencms.xml.xml2json.handler.I_CmsJsonHandler#renderJson(org.opencms.xml.xml2json.handler.CmsJsonHandlerContext)
104     */
105    public CmsJsonResult renderJson(CmsJsonHandlerContext context) {
106
107        if (!context.getCms().getRequestContext().getCurrentProject().isOnlineProject()) {
108            // never cache offline requests
109            return m_handler.renderJson(context);
110        } else {
111            try {
112                return m_cache.get(context.getKey());
113            } catch (ExecutionException e) {
114                LOG.error(e.getLocalizedMessage(), e);
115                return new CmsJsonResult(e.getLocalizedMessage(), HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
116            }
117        }
118    }
119
120    /**
121     * @see java.lang.Object#toString()
122     */
123    @Override
124    public String toString() {
125
126        return m_handler.toString();
127    }
128
129}