001/*
002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Anahide Tchertchian
018 */
019package org.nuxeo.ecm.web.resources.wro.factory;
020
021import static org.apache.commons.lang3.Validate.notNull;
022
023import java.util.Map;
024
025import javax.servlet.http.HttpServletRequest;
026
027import org.apache.commons.logging.Log;
028import org.apache.commons.logging.LogFactory;
029import org.nuxeo.common.utils.URIUtils;
030
031import ro.isdc.wro.cache.CacheKey;
032import ro.isdc.wro.cache.factory.DefaultCacheKeyFactory;
033import ro.isdc.wro.config.ReadOnlyContext;
034import ro.isdc.wro.model.group.GroupExtractor;
035import ro.isdc.wro.model.group.Inject;
036
037/**
038 * Cache key extractor generating a key depending on all request parameters.
039 *
040 * @since 7.3
041 */
042public class NuxeoWroCacheKeyFactory extends DefaultCacheKeyFactory {
043
044    private static final Log log = LogFactory.getLog(NuxeoWroCacheKeyFactory.class);
045
046    @Inject
047    private GroupExtractor groupExtractor;
048
049    @Inject
050    private ReadOnlyContext context;
051
052    @Override
053    public CacheKey create(HttpServletRequest request) {
054        notNull(request);
055        // this requires a type to be detected for now
056        CacheKey key = super.create(request);
057        if (key != null) {
058            // add additional attributes for request-related info
059            Map<String, String> params = URIUtils.getRequestParameters(request.getQueryString());
060            if (params != null) {
061                for (Map.Entry<String, String> entry : params.entrySet()) {
062                    key.addAttribute(entry.getKey(), entry.getValue());
063                }
064            }
065        }
066        if (log.isDebugEnabled()) {
067            log.debug(String.format("Cache key for request '%s' '%s': %s", request.getRequestURL(),
068                    request.getQueryString(), key));
069        }
070        return key;
071    }
072
073}