blob: c515815e7b42c0e32bc34693820076f6ecf3a0fd [file] [log] [blame]
/*-
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.sling.dynamicinclude;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.sling.SlingFilter;
import org.apache.felix.scr.annotations.sling.SlingFilterScope;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@SlingFilter(scope = {SlingFilterScope.REQUEST, SlingFilterScope.FORWARD}, order = 0)
public class CacheControlFilter implements Filter {
private static final String HEADER_DATE = "Date";
private static final String HEADER_CACHE_CONTROL = "Cache-Control";
private static final Logger LOG = LoggerFactory.getLogger(CacheControlFilter.class);
@Reference
private ConfigurationWhiteboard configurationWhiteboard;
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
ServletException {
final SlingHttpServletRequest slingRequest = (SlingHttpServletRequest) request;
final String resourceType = slingRequest.getResource().getResourceType();
final Configuration config = configurationWhiteboard.getConfiguration(slingRequest, resourceType);
if (config != null && config.hasTtlSet()) {
SlingHttpServletResponse slingResponse = (SlingHttpServletResponse) response;
slingResponse.setHeader(HEADER_CACHE_CONTROL, "max-age=" + config.getTtl());
LOG.debug("set \"{}: max-age={}\" to {}", HEADER_CACHE_CONTROL, config.getTtl(), resourceType);
if (!slingResponse.containsHeader(HEADER_DATE)) {
slingResponse.setDateHeader(HEADER_DATE, System.currentTimeMillis());
}
}
chain.doFilter(request, response);
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void destroy() {
}
}