sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fmesc...@apache.org
Subject svn commit: r1561617 - in /sling/whiteboard/fmeschbe/featureflags/feature-flags/src/main/java/org/apache/sling/featureflags/impl: CurrentClientContextFilter.java FeatureManager.java
Date Mon, 27 Jan 2014 09:08:59 GMT
Author: fmeschbe
Date: Mon Jan 27 09:08:58 2014
New Revision: 1561617

URL: http://svn.apache.org/r1561617
Log:
Fix ClientContext request filter registration:

- Allow stacking and replacement
- Global ClientContext on Http Service Filter level
- Sling ClientContext on Sling Filter level

Modified:
    sling/whiteboard/fmeschbe/featureflags/feature-flags/src/main/java/org/apache/sling/featureflags/impl/CurrentClientContextFilter.java
    sling/whiteboard/fmeschbe/featureflags/feature-flags/src/main/java/org/apache/sling/featureflags/impl/FeatureManager.java

Modified: sling/whiteboard/fmeschbe/featureflags/feature-flags/src/main/java/org/apache/sling/featureflags/impl/CurrentClientContextFilter.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/fmeschbe/featureflags/feature-flags/src/main/java/org/apache/sling/featureflags/impl/CurrentClientContextFilter.java?rev=1561617&r1=1561616&r2=1561617&view=diff
==============================================================================
--- sling/whiteboard/fmeschbe/featureflags/feature-flags/src/main/java/org/apache/sling/featureflags/impl/CurrentClientContextFilter.java
(original)
+++ sling/whiteboard/fmeschbe/featureflags/feature-flags/src/main/java/org/apache/sling/featureflags/impl/CurrentClientContextFilter.java
Mon Jan 27 09:08:58 2014
@@ -26,45 +26,38 @@ import javax.servlet.FilterConfig;
 import javax.servlet.ServletException;
 import javax.servlet.ServletRequest;
 import javax.servlet.ServletResponse;
-
-import org.apache.felix.scr.annotations.Component;
-import org.apache.felix.scr.annotations.Property;
-import org.apache.felix.scr.annotations.Reference;
-import org.apache.felix.scr.annotations.Service;
-import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.featureflags.ClientContext;
 
 /**
- * This general servlet filter sets the current client context to the
- * current request.
+ * This general servlet filter sets the current client context to the current
+ * request.
  */
-@Component
-@Service(value=Filter.class)
-@Property(name="pattern", value="/.*")
 public class CurrentClientContextFilter implements Filter {
 
-    @Reference
-    private FeatureManager manager;
+    private final FeatureManager featureManager;
+
+    public CurrentClientContextFilter(final FeatureManager featureManager) {
+        this.featureManager = featureManager;
+    }
 
     @Override
-    public void doFilter(final ServletRequest req, final ServletResponse res,
-            final FilterChain chain)
-    throws IOException, ServletException {
-        if ( req instanceof SlingHttpServletRequest ) {
-            manager.setCurrentClientContext((SlingHttpServletRequest)req);
-        }
+    public void init(final FilterConfig config) {
+        // nothing to do
+    }
+
+    @Override
+    public void doFilter(final ServletRequest req, final ServletResponse res, final FilterChain
chain)
+            throws IOException, ServletException {
+
+        ClientContext current = this.featureManager.setCurrentClientContext(req);
         try {
             chain.doFilter(req, res);
         } finally {
-            manager.unsetCurrentClientContext();
+            this.featureManager.unsetCurrentClientContext(current);
         }
     }
 
     @Override
-    public void init(final FilterConfig config) throws ServletException {
-        // nothing to do
-    }
-
-    @Override
     public void destroy() {
         // nothing to do
     }

Modified: sling/whiteboard/fmeschbe/featureflags/feature-flags/src/main/java/org/apache/sling/featureflags/impl/FeatureManager.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/fmeschbe/featureflags/feature-flags/src/main/java/org/apache/sling/featureflags/impl/FeatureManager.java?rev=1561617&r1=1561616&r2=1561617&view=diff
==============================================================================
--- sling/whiteboard/fmeschbe/featureflags/feature-flags/src/main/java/org/apache/sling/featureflags/impl/FeatureManager.java
(original)
+++ sling/whiteboard/fmeschbe/featureflags/feature-flags/src/main/java/org/apache/sling/featureflags/impl/FeatureManager.java
Mon Jan 27 09:08:58 2014
@@ -27,7 +27,9 @@ import java.util.List;
 import java.util.Map;
 import java.util.TreeMap;
 
+import javax.servlet.Filter;
 import javax.servlet.Servlet;
+import javax.servlet.ServletRequest;
 import javax.servlet.http.HttpServletRequest;
 
 import org.apache.felix.scr.annotations.Activate;
@@ -71,6 +73,8 @@ public class FeatureManager {
     private ServiceRegistration resourceDecorator;
 
     private ServiceRegistration featureWebConsolePlugin;
+    private ServiceRegistration featureGlobalFilter;
+    private ServiceRegistration featureSlingFilter;
 
     @SuppressWarnings("serial")
     @Activate
@@ -86,6 +90,20 @@ public class FeatureManager {
                     put("felix.webconsole.category", "Sling");
                 }
             });
+        this.featureGlobalFilter = bundleContext.registerService(Filter.class.getName(),
+            new CurrentClientContextFilter(this), new Hashtable<String, Object>() {
+            {
+                put("pattern", "/.*");
+                put("service.ranking", Integer.MAX_VALUE);
+            }
+        });
+        this.featureGlobalFilter = bundleContext.registerService(Filter.class.getName(),
+            new CurrentClientContextFilter(this), new Hashtable<String, Object>() {
+            {
+                put("sling.filter.scope", "REQUEST");
+                put("service.ranking", Integer.MAX_VALUE);
+            }
+        });
     }
 
     @Deactivate
@@ -95,6 +113,16 @@ public class FeatureManager {
             this.featureWebConsolePlugin = null;
         }
 
+        if (this.featureSlingFilter != null) {
+            this.featureSlingFilter.unregister();
+            this.featureSlingFilter = null;
+        }
+
+        if (this.featureGlobalFilter != null) {
+            this.featureGlobalFilter.unregister();
+            this.featureGlobalFilter = null;
+        }
+
         if (this.featuresService != null) {
             this.featuresService.unregister();
             this.featuresService = null;
@@ -152,7 +180,7 @@ public class FeatureManager {
         this.activeFeatures = activeMap;
     }
 
-    private final ThreadLocal<ClientContextImpl> perThreadClientContext = new ThreadLocal<ClientContextImpl>();
+    private final ThreadLocal<ClientContext> perThreadClientContext = new ThreadLocal<ClientContext>();
 
     private final ClientContext defaultClientContext = new ClientContext() {
 
@@ -175,14 +203,22 @@ public class FeatureManager {
         return result;
     }
 
-    public void setCurrentClientContext(final HttpServletRequest request) {
-        final ExecutionContext providerContext = new ExecutionContextImpl(request);
-        final ClientContextImpl ctx = this.createClientContext(providerContext);
-        perThreadClientContext.set(ctx);
+    public ClientContext setCurrentClientContext(final ServletRequest request) {
+        final ClientContext current = perThreadClientContext.get();
+        if (request instanceof HttpServletRequest) {
+            final ExecutionContext providerContext = new ExecutionContextImpl((HttpServletRequest)
request);
+            final ClientContextImpl ctx = this.createClientContext(providerContext);
+            perThreadClientContext.set(ctx);
+        }
+        return current;
     }
 
-    public void unsetCurrentClientContext() {
-        perThreadClientContext.remove();
+    public void unsetCurrentClientContext(final ClientContext previous) {
+        if (previous != null) {
+            perThreadClientContext.set(previous);
+        } else {
+            perThreadClientContext.remove();
+        }
     }
 
     public ClientContext createClientContext(final ResourceResolver resolver) {



Mime
View raw message