You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by fm...@apache.org on 2010/04/28 17:19:25 UTC

svn commit: r938991 - in /sling/whiteboard/fmeschbe/cachecontrol: ./ src/ src/main/ src/main/java/ src/main/java/org/ src/main/java/org/apache/ src/main/java/org/apache/sling/ src/main/java/org/apache/sling/extensions/ src/main/java/org/apache/sling/ex...

Author: fmeschbe
Date: Wed Apr 28 15:19:23 2010
New Revision: 938991

URL: http://svn.apache.org/viewvc?rev=938991&view=rev
Log:
Cache-Control header configuration prototype (see http://markmail.org/message/gvv5ugrchb4wzbif)

Added:
    sling/whiteboard/fmeschbe/cachecontrol/   (with props)
    sling/whiteboard/fmeschbe/cachecontrol/pom.xml   (with props)
    sling/whiteboard/fmeschbe/cachecontrol/src/
    sling/whiteboard/fmeschbe/cachecontrol/src/main/
    sling/whiteboard/fmeschbe/cachecontrol/src/main/java/
    sling/whiteboard/fmeschbe/cachecontrol/src/main/java/org/
    sling/whiteboard/fmeschbe/cachecontrol/src/main/java/org/apache/
    sling/whiteboard/fmeschbe/cachecontrol/src/main/java/org/apache/sling/
    sling/whiteboard/fmeschbe/cachecontrol/src/main/java/org/apache/sling/extensions/
    sling/whiteboard/fmeschbe/cachecontrol/src/main/java/org/apache/sling/extensions/cachecontrol/
    sling/whiteboard/fmeschbe/cachecontrol/src/main/java/org/apache/sling/extensions/cachecontrol/impl/
    sling/whiteboard/fmeschbe/cachecontrol/src/main/java/org/apache/sling/extensions/cachecontrol/impl/CacheControl.java   (with props)
    sling/whiteboard/fmeschbe/cachecontrol/src/main/java/org/apache/sling/extensions/cachecontrol/impl/CacheControlDirective.java   (with props)
    sling/whiteboard/fmeschbe/cachecontrol/src/main/java/org/apache/sling/extensions/cachecontrol/impl/CacheControlFilter.java   (with props)
    sling/whiteboard/fmeschbe/cachecontrol/src/main/java/org/apache/sling/extensions/cachecontrol/impl/CacheControlResponse.java   (with props)

Propchange: sling/whiteboard/fmeschbe/cachecontrol/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Wed Apr 28 15:19:23 2010
@@ -0,0 +1,4 @@
+.classpath
+.project
+.settings
+target

Added: sling/whiteboard/fmeschbe/cachecontrol/pom.xml
URL: http://svn.apache.org/viewvc/sling/whiteboard/fmeschbe/cachecontrol/pom.xml?rev=938991&view=auto
==============================================================================
--- sling/whiteboard/fmeschbe/cachecontrol/pom.xml (added)
+++ sling/whiteboard/fmeschbe/cachecontrol/pom.xml Wed Apr 28 15:19:23 2010
@@ -0,0 +1,109 @@
+<?xml version="1.0" encoding="ISO-8859-1"?>
+<!--
+    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.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+
+    <modelVersion>4.0.0</modelVersion>
+    <parent>
+        <groupId>org.apache.sling</groupId>
+        <artifactId>sling</artifactId>
+        <version>8</version>
+        <relativePath>../../parent/pom.xml</relativePath>
+    </parent>
+
+    <artifactId>org.apache.sling.extensions.cachecontrol</artifactId>
+    <packaging>bundle</packaging>
+    <version>0.0.1-SNAPSHOT</version>
+
+    <name>Apache Sling Cache-Control Header Support Filter</name>
+    <description>
+        This bundle provides a request level filter to help manage
+        the Cache-Control response header.
+    </description>
+
+    <scm>
+        <connection>scm:svn:http://svn.apache.org/repos/asf/incubator/sling/trunk/extensions/cachecontrol</connection>
+        <developerConnection>scm:svn:https://svn.apache.org/repos/asf/incubator/sling/trunk/extensions/cachecontrol</developerConnection>
+        <url>http://svn.apache.org/viewvc/incubator/sling/trunk/extensions/cachecontrol</url>
+    </scm>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.felix</groupId>
+                <artifactId>maven-scr-plugin</artifactId>
+            </plugin>
+            <plugin>
+                <groupId>org.apache.felix</groupId>
+                <artifactId>maven-bundle-plugin</artifactId>
+                <extensions>true</extensions>
+                <configuration>
+                    <instructions>
+                        <Bundle-Category>sling</Bundle-Category>
+                        <!-- No Export-Package -->
+                        <Private-Package>
+                            org.apache.sling.extensions.cachecontrol.impl.*,
+                        </Private-Package>
+                    </instructions>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
+    <reporting>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-javadoc-plugin</artifactId>
+                <configuration>
+                    <excludePackageNames>
+                        org.apache.sling.extensions.cachecontrol.impl
+                    </excludePackageNames>
+                </configuration>
+            </plugin>
+        </plugins>
+    </reporting>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.api</artifactId>
+            <version>2.0.6</version>
+        </dependency>
+
+        <dependency>
+            <groupId>javax.servlet</groupId>
+            <artifactId>servlet-api</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>org.slf4j</groupId>
+            <artifactId>slf4j-api</artifactId>
+        </dependency>
+
+        <!-- OSGi Libraries -->
+        <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>org.osgi.core</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>org.osgi.compendium</artifactId>
+        </dependency>
+    </dependencies>
+</project>

Propchange: sling/whiteboard/fmeschbe/cachecontrol/pom.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Added: sling/whiteboard/fmeschbe/cachecontrol/src/main/java/org/apache/sling/extensions/cachecontrol/impl/CacheControl.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/fmeschbe/cachecontrol/src/main/java/org/apache/sling/extensions/cachecontrol/impl/CacheControl.java?rev=938991&view=auto
==============================================================================
--- sling/whiteboard/fmeschbe/cachecontrol/src/main/java/org/apache/sling/extensions/cachecontrol/impl/CacheControl.java (added)
+++ sling/whiteboard/fmeschbe/cachecontrol/src/main/java/org/apache/sling/extensions/cachecontrol/impl/CacheControl.java Wed Apr 28 15:19:23 2010
@@ -0,0 +1,76 @@
+/*
+ * 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.extensions.cachecontrol.impl;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Map.Entry;
+
+public class CacheControl {
+
+    private static final String NIL = "<nil>";
+
+    private final Map<String, String> values = new HashMap<String, String>();
+
+    void clear() {
+        values.clear();
+    }
+
+    /**
+     * @param values Comma-separated list of Cache-Control header values
+     */
+    void put(final String value) {
+        // actually this neglects the case of private and no-cache directives
+        // which may take attributes in quotes, which themselves are
+        // comma-separated
+        String[] values = value.split(",");
+        for (String setting : values) {
+            int idx = setting.indexOf("=");
+            String settingValue;
+            if (idx < 0) {
+                setting = setting.trim().toLowerCase();
+                // setting = CacheControlDirective.valueOf(setting).toString();
+                settingValue = NIL;
+            } else {
+                settingValue = setting.substring(idx + 1).trim();
+                setting = setting.substring(0, idx).trim().toLowerCase();
+            }
+            this.values.put(setting, settingValue);
+        }
+    }
+
+    String toHeaderValue() {
+        StringBuilder builder = new StringBuilder();
+        for (Entry<String, String> entry : values.entrySet()) {
+            if (builder.length() > 0) {
+                builder.append(',');
+            }
+            builder.append(entry.getKey());
+            if (entry.getValue() != NIL) {
+                builder.append("=").append(entry.getValue());
+            }
+        }
+        return builder.toString();
+    }
+
+    @Override
+    public String toString() {
+        return getClass().getSimpleName() + values.toString();
+    }
+}

Propchange: sling/whiteboard/fmeschbe/cachecontrol/src/main/java/org/apache/sling/extensions/cachecontrol/impl/CacheControl.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/whiteboard/fmeschbe/cachecontrol/src/main/java/org/apache/sling/extensions/cachecontrol/impl/CacheControl.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev Url

Added: sling/whiteboard/fmeschbe/cachecontrol/src/main/java/org/apache/sling/extensions/cachecontrol/impl/CacheControlDirective.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/fmeschbe/cachecontrol/src/main/java/org/apache/sling/extensions/cachecontrol/impl/CacheControlDirective.java?rev=938991&view=auto
==============================================================================
--- sling/whiteboard/fmeschbe/cachecontrol/src/main/java/org/apache/sling/extensions/cachecontrol/impl/CacheControlDirective.java (added)
+++ sling/whiteboard/fmeschbe/cachecontrol/src/main/java/org/apache/sling/extensions/cachecontrol/impl/CacheControlDirective.java Wed Apr 28 15:19:23 2010
@@ -0,0 +1,55 @@
+/*
+ * 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.extensions.cachecontrol.impl;
+
+public enum CacheControlDirective {
+
+    PUBLIC("public"), // ; Section 14.9.1
+    PRIVATE("private"), // [ "=" <"> 1#field-name <"> ] ; Section 14.9.1
+    NO_CACHE("no-cache"), // [ "=" <"> 1#field-name <"> ]; Section 14.9.1
+    NO_STORE("no-store"), // ; Section 14.9.2
+    NO_TRANSFORM("no-transform"), // ; Section 14.9.5
+    MUST_REVALIDATE("must-revalidate"), // ; Section 14.9.4
+    PROXY_REVALIDATE("proxy-revalidate"), // ; Section 14.9.4
+    MAX_AGE("max-age"), // "=" delta-seconds ; Section 14.9.3
+    S_MAXAGE("s-maxage");// "=" delta-seconds ; Section 14.9.3
+
+    /**
+     * The user-friendly message returned by {@link #toString()}
+     */
+    private final String message;
+
+    /**
+     * Creates an instance of the reason conveying the given descriptive reason.
+     *
+     * @param message The descriptive reason.
+     */
+    private CacheControlDirective(String message) {
+        this.message = message;
+    }
+
+    /**
+     * Returns the message set when constructing this instance. To get the
+     * official name call the <code>name()</code> method.
+     */
+    @Override
+    public String toString() {
+        return message;
+    }
+}

Propchange: sling/whiteboard/fmeschbe/cachecontrol/src/main/java/org/apache/sling/extensions/cachecontrol/impl/CacheControlDirective.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/whiteboard/fmeschbe/cachecontrol/src/main/java/org/apache/sling/extensions/cachecontrol/impl/CacheControlDirective.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev Url

Added: sling/whiteboard/fmeschbe/cachecontrol/src/main/java/org/apache/sling/extensions/cachecontrol/impl/CacheControlFilter.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/fmeschbe/cachecontrol/src/main/java/org/apache/sling/extensions/cachecontrol/impl/CacheControlFilter.java?rev=938991&view=auto
==============================================================================
--- sling/whiteboard/fmeschbe/cachecontrol/src/main/java/org/apache/sling/extensions/cachecontrol/impl/CacheControlFilter.java (added)
+++ sling/whiteboard/fmeschbe/cachecontrol/src/main/java/org/apache/sling/extensions/cachecontrol/impl/CacheControlFilter.java Wed Apr 28 15:19:23 2010
@@ -0,0 +1,72 @@
+/*
+ * 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.extensions.cachecontrol.impl;
+
+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 javax.servlet.http.HttpServletRequest;
+
+import org.apache.sling.api.SlingHttpServletResponse;
+
+/**
+ * The <code>CacheControlFilter</code> ...
+ *
+ * @scr.component immediate="true"
+ * @scr.service interface="javax.servlet.Filter"
+ * @scr.property name="control" cardinality="-"
+ * @scr.property name="filter.scope" value="request"
+ * @scr.property name="filter.order" value="-1000000" type="Integer"
+ */
+public class CacheControlFilter implements Filter {
+
+    public void init(final FilterConfig filterConfig) {
+    }
+
+    public void doFilter(final ServletRequest request,
+            final ServletResponse response, FilterChain chain)
+            throws IOException, ServletException {
+        HttpServletRequest req = (HttpServletRequest) request;
+        if (canCache(req) && response instanceof SlingHttpServletResponse) {
+            CacheControlResponse res = new CacheControlResponse((SlingHttpServletResponse) response);
+            res.setCacheControlHeader(getDefaultCacheControl(req), true);
+            chain.doFilter(request, res);
+        } else {
+            chain.doFilter(req, response);
+        }
+    }
+
+    public void destroy() {
+    }
+
+    private boolean canCache(final HttpServletRequest request) {
+        return "GET".equals(request.getMethod())
+            && request.getQueryString() == null;
+    }
+
+    private String getDefaultCacheControl(final HttpServletRequest request) {
+        // TODO make configurable ...
+        return "max-age=86400";
+    }
+}

Propchange: sling/whiteboard/fmeschbe/cachecontrol/src/main/java/org/apache/sling/extensions/cachecontrol/impl/CacheControlFilter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/whiteboard/fmeschbe/cachecontrol/src/main/java/org/apache/sling/extensions/cachecontrol/impl/CacheControlFilter.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev Url

Added: sling/whiteboard/fmeschbe/cachecontrol/src/main/java/org/apache/sling/extensions/cachecontrol/impl/CacheControlResponse.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/fmeschbe/cachecontrol/src/main/java/org/apache/sling/extensions/cachecontrol/impl/CacheControlResponse.java?rev=938991&view=auto
==============================================================================
--- sling/whiteboard/fmeschbe/cachecontrol/src/main/java/org/apache/sling/extensions/cachecontrol/impl/CacheControlResponse.java (added)
+++ sling/whiteboard/fmeschbe/cachecontrol/src/main/java/org/apache/sling/extensions/cachecontrol/impl/CacheControlResponse.java Wed Apr 28 15:19:23 2010
@@ -0,0 +1,81 @@
+/*
+ * 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.extensions.cachecontrol.impl;
+
+import org.apache.sling.api.SlingHttpServletResponse;
+import org.apache.sling.api.wrappers.SlingHttpServletResponseWrapper;
+
+public class CacheControlResponse extends SlingHttpServletResponseWrapper {
+
+    private static final String LAST_MODIFIED = "Last-Modified";
+
+    private static final String CACHE_CONTROL = "Cache-Control";
+
+    private CacheControl cacheControl;
+
+    CacheControlResponse(final SlingHttpServletResponse wrapped) {
+        super(wrapped);
+        super.setDateHeader(LAST_MODIFIED, System.currentTimeMillis());
+    }
+
+    @Override
+    public void setDateHeader(String name, long date) {
+        if (!LAST_MODIFIED.equalsIgnoreCase(name)) {
+            super.setDateHeader(name, date);
+        }
+    }
+
+    @Override
+    public void setHeader(final String name, final String value) {
+        if (CACHE_CONTROL.equalsIgnoreCase(name)) {
+            setCacheControlHeader(value, true);
+        }
+        if (!LAST_MODIFIED.equalsIgnoreCase(name)) {
+            super.setHeader(name, value);
+        }
+    }
+
+    @Override
+    public void addDateHeader(String name, long date) {
+        if (!LAST_MODIFIED.equalsIgnoreCase(name)) {
+            super.addDateHeader(name, date);
+        }
+    }
+
+    @Override
+    public void addHeader(String name, String value) {
+        if (CACHE_CONTROL.equalsIgnoreCase(name)) {
+            setCacheControlHeader(value, false);
+        }
+        if (!LAST_MODIFIED.equalsIgnoreCase(name)) {
+            super.addHeader(name, value);
+        }
+    }
+
+    void setCacheControlHeader(final String value, final boolean replace) {
+        if (cacheControl == null) {
+            cacheControl = new CacheControl();
+        } else if (replace) {
+            cacheControl.clear();
+        }
+
+        cacheControl.put(value);
+        super.setHeader(CACHE_CONTROL, cacheControl.toHeaderValue());
+    }
+}

Propchange: sling/whiteboard/fmeschbe/cachecontrol/src/main/java/org/apache/sling/extensions/cachecontrol/impl/CacheControlResponse.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/whiteboard/fmeschbe/cachecontrol/src/main/java/org/apache/sling/extensions/cachecontrol/impl/CacheControlResponse.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev Url