001/*
002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Anahide Tchertchian
016 */
017package org.nuxeo.ecm.web.resources.wro.factory;
018
019import static org.apache.commons.lang3.Validate.notNull;
020
021import java.util.Map;
022
023import javax.servlet.http.HttpServletRequest;
024
025import org.apache.commons.logging.Log;
026import org.apache.commons.logging.LogFactory;
027import org.nuxeo.common.utils.URIUtils;
028
029import ro.isdc.wro.cache.CacheKey;
030import ro.isdc.wro.cache.factory.DefaultCacheKeyFactory;
031import ro.isdc.wro.config.ReadOnlyContext;
032import ro.isdc.wro.model.group.GroupExtractor;
033import ro.isdc.wro.model.group.Inject;
034
035/**
036 * Cache key extractor generating a key depending on all request parameters.
037 *
038 * @since 7.3
039 */
040public class NuxeoWroCacheKeyFactory extends DefaultCacheKeyFactory {
041
042    private static final Log log = LogFactory.getLog(NuxeoWroCacheKeyFactory.class);
043
044    @Inject
045    private GroupExtractor groupExtractor;
046
047    @Inject
048    private ReadOnlyContext context;
049
050    @Override
051    public CacheKey create(HttpServletRequest request) {
052        notNull(request);
053        // this requires a type to be detected for now
054        CacheKey key = super.create(request);
055        if (key != null) {
056            // add additional attributes for request-related info
057            Map<String, String> params = URIUtils.getRequestParameters(request.getQueryString());
058            if (params != null) {
059                for (Map.Entry<String, String> entry : params.entrySet()) {
060                    key.addAttribute(entry.getKey(), entry.getValue());
061                }
062            }
063        }
064        if (log.isDebugEnabled()) {
065            log.debug(String.format("Cache key for request '%s' '%s': %s", request.getRequestURL(),
066                    request.getQueryString(), key));
067        }
068        return key;
069    }
070
071}