sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From asa...@apache.org
Subject svn commit: r1655980 - in /sling/trunk/bundles/engine: ./ src/main/java/org/apache/sling/engine/ src/main/java/org/apache/sling/engine/impl/filter/ src/test/java/org/apache/sling/engine/impl/filter/
Date Fri, 30 Jan 2015 10:46:45 GMT
Author: asanso
Date: Fri Jan 30 10:46:45 2015
New Revision: 1655980

URL: http://svn.apache.org/r1655980
Log:
SLING-4294 - Servlet Filter Support adding sling.filter.pattern support

* applied patch with suggested changes

Modified:
    sling/trunk/bundles/engine/pom.xml
    sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/EngineConstants.java
    sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/impl/filter/AbstractSlingFilterChain.java
    sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/impl/filter/FilterHandle.java
    sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/impl/filter/ServletFilterManager.java
    sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/impl/filter/SlingFilterChainHelper.java
    sling/trunk/bundles/engine/src/test/java/org/apache/sling/engine/impl/filter/SlingFilterChainHelperTest.java

Modified: sling/trunk/bundles/engine/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/engine/pom.xml?rev=1655980&r1=1655979&r2=1655980&view=diff
==============================================================================
--- sling/trunk/bundles/engine/pom.xml (original)
+++ sling/trunk/bundles/engine/pom.xml Fri Jan 30 10:46:45 2015
@@ -105,7 +105,7 @@
                 <configuration>
                     <instructions>
                         <Export-Package>
-                            org.apache.sling.engine;version=2.1;provide:=true,
+                            org.apache.sling.engine;version=2.2;provide:=true,
                             org.apache.sling.engine.servlets;version=2.0.6;provide:=true,
                             org.apache.sling.engine.jmx;version=1.0;provide:=true
                         </Export-Package>

Modified: sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/EngineConstants.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/EngineConstants.java?rev=1655980&r1=1655979&r2=1655980&view=diff
==============================================================================
--- sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/EngineConstants.java
(original)
+++ sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/EngineConstants.java
Fri Jan 30 10:46:45 2015
@@ -154,6 +154,11 @@ public class EngineConstants {
      * @since 2.1, Sling Engine 2.2
      */
     public static final String SLING_FILTER_SCOPE = "sling.filter.scope";
+    
+    /**
+     *@since 2.2, Sling Engine 2.4
+     */
+    public static final String SLING_FILTER_PATTERN = "sling.filter.pattern";
 
     /**
      * Filter scope value identifying a component level filter.

Modified: sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/impl/filter/AbstractSlingFilterChain.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/impl/filter/AbstractSlingFilterChain.java?rev=1655980&r1=1655979&r2=1655980&view=diff
==============================================================================
--- sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/impl/filter/AbstractSlingFilterChain.java
(original)
+++ sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/impl/filter/AbstractSlingFilterChain.java
Fri Jan 30 10:46:45 2015
@@ -62,13 +62,15 @@ public abstract class AbstractSlingFilte
 
                 // continue filtering with the next filter
                 FilterHandle filter = this.filters[this.current];
-                trackFilter(slingRequest, filter);
-                filter.getFilter().doFilter(slingRequest, slingResponse, this);
-
+                
+                if (filter.select(slingRequest)) {
+                    trackFilter(slingRequest, filter);
+                    filter.getFilter().doFilter(slingRequest, slingResponse, this);
+                } else if (this.current == this.filters.length-1) {
+                    this.render(slingRequest, slingResponse);
+                }
             } else {
-
                 this.render(slingRequest, slingResponse);
-
             }
 
         } finally {

Modified: sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/impl/filter/FilterHandle.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/impl/filter/FilterHandle.java?rev=1655980&r1=1655979&r2=1655980&view=diff
==============================================================================
--- sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/impl/filter/FilterHandle.java
(original)
+++ sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/impl/filter/FilterHandle.java
Fri Jan 30 10:46:45 2015
@@ -19,12 +19,17 @@
 package org.apache.sling.engine.impl.filter;
 
 import java.util.concurrent.atomic.AtomicLong;
+import java.util.regex.Pattern;
 
 import javax.servlet.Filter;
 
+import org.apache.sling.api.SlingHttpServletRequest;
+
 public class FilterHandle implements Comparable<FilterHandle> {
 
     private final Filter filter;
+    
+    private final Pattern regex;
 
     private final Long filterId;
 
@@ -36,8 +41,14 @@ public class FilterHandle implements Com
 
     private AtomicLong time;
 
-    FilterHandle(Filter filter, Long filterId, int order, final String orderSource) {
+    FilterHandle(Filter filter, String pattern, Long filterId, int order, final String orderSource)
{
         this.filter = filter;
+        if (pattern != null && pattern.length() > 0) {
+            this.regex = Pattern.compile(pattern);
+        } else {
+            this.regex = null;
+        }
+        
         this.filterId = filterId;
         this.order = order;
         this.orderSource = orderSource;
@@ -60,6 +71,20 @@ public class FilterHandle implements Com
     public String getOrderSource() {
         return orderSource;
     }
+    
+    boolean select(SlingHttpServletRequest slingHttpServletRequest) {
+        boolean select = true;        
+        if (regex != null) {
+            String uri = slingHttpServletRequest.getPathInfo();
+            // assume root if uri is null
+            if (uri == null)
+            {
+                uri = "/";
+            }
+            select = this.regex.matcher(uri).matches();
+        }        
+        return select;
+    }
 
     public long getCalls() {
         return calls.get();

Modified: sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/impl/filter/ServletFilterManager.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/impl/filter/ServletFilterManager.java?rev=1655980&r1=1655979&r2=1655980&view=diff
==============================================================================
--- sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/impl/filter/ServletFilterManager.java
(original)
+++ sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/impl/filter/ServletFilterManager.java
Fri Jan 30 10:46:45 2015
@@ -214,6 +214,9 @@ public class ServletFilterManager extend
                 // register by scope
                 String[] scopes = OsgiUtil.toStringArray(
                         reference.getProperty(EngineConstants.SLING_FILTER_SCOPE), null);
+                
+                String pattern = OsgiUtil.toString(reference.getProperty(EngineConstants.SLING_FILTER_PATTERN),
"");
+                
                 if ( scopes == null ) {
                     scopes = OsgiUtil.toStringArray(
                         reference.getProperty(EngineConstants.FILTER_SCOPE), null);
@@ -223,14 +226,14 @@ public class ServletFilterManager extend
                         scope = scope.toUpperCase();
                         try {
                             FilterChainType type = FilterChainType.valueOf(scope.toString());
-                            getFilterChain(type).addFilter(filter, serviceId,
+                            getFilterChain(type).addFilter(filter, pattern, serviceId,
                                 order, orderSource);
 
                             if (type == FilterChainType.COMPONENT) {
                                 getFilterChain(FilterChainType.INCLUDE).addFilter(
-                                    filter, serviceId, order, orderSource);
+                                    filter, pattern, serviceId, order, orderSource);
                                 getFilterChain(FilterChainType.FORWARD).addFilter(
-                                    filter, serviceId, order, orderSource);
+                                    filter, pattern, serviceId, order, orderSource);
                             }
 
                         } catch (IllegalArgumentException iae) {
@@ -241,7 +244,7 @@ public class ServletFilterManager extend
                     log.warn(String.format(
                         "A Filter (Service ID %s) has been registered without a filter.scope
property.",
                         reference.getProperty(Constants.SERVICE_ID)));
-                    getFilterChain(FilterChainType.REQUEST).addFilter(filter,
+                    getFilterChain(FilterChainType.REQUEST).addFilter(filter, pattern,
                         serviceId, order, orderSource);
                 }
 

Modified: sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/impl/filter/SlingFilterChainHelper.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/impl/filter/SlingFilterChainHelper.java?rev=1655980&r1=1655979&r2=1655980&view=diff
==============================================================================
--- sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/impl/filter/SlingFilterChainHelper.java
(original)
+++ sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/impl/filter/SlingFilterChainHelper.java
Fri Jan 30 10:46:45 2015
@@ -42,12 +42,12 @@ public class SlingFilterChainHelper {
     SlingFilterChainHelper() {
     }
 
-    public synchronized Filter addFilter(final Filter filter,
+    public synchronized Filter addFilter(final Filter filter,  String pattern,
             final Long filterId, final int order, final String orderSource) {
         if (filterList == null) {
             filterList = new TreeSet<FilterHandle>();
         }
-        filterList.add(new FilterHandle(filter, filterId, order, orderSource));
+        filterList.add(new FilterHandle(filter, pattern, filterId, order, orderSource));
         filters = getFiltersInternal();
         return filter;
     }

Modified: sling/trunk/bundles/engine/src/test/java/org/apache/sling/engine/impl/filter/SlingFilterChainHelperTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/engine/src/test/java/org/apache/sling/engine/impl/filter/SlingFilterChainHelperTest.java?rev=1655980&r1=1655979&r2=1655980&view=diff
==============================================================================
--- sling/trunk/bundles/engine/src/test/java/org/apache/sling/engine/impl/filter/SlingFilterChainHelperTest.java
(original)
+++ sling/trunk/bundles/engine/src/test/java/org/apache/sling/engine/impl/filter/SlingFilterChainHelperTest.java
Fri Jan 30 10:46:45 2015
@@ -36,11 +36,11 @@ public class SlingFilterChainHelperTest
     @Test public void testOrdering() {
         final SlingFilterChainHelper chain = new SlingFilterChainHelper();
 
-        chain.addFilter(context.mock(Filter.class, "A"), 1L, 100, "1:100");
-        chain.addFilter(context.mock(Filter.class, "B"), 2L, 100, "2:100");
-        chain.addFilter(context.mock(Filter.class, "C"), 3L, -100, "3:-100");
-        chain.addFilter(context.mock(Filter.class, "D"), 4L, -1000, "4:-1000");
-        chain.addFilter(context.mock(Filter.class, "E"), 5L, 1000, "5:1000");
+        chain.addFilter(context.mock(Filter.class, "A"), null, 1L, 100, "1:100");
+        chain.addFilter(context.mock(Filter.class, "B"), null, 2L, 100, "2:100");
+        chain.addFilter(context.mock(Filter.class, "C"), null, 3L, -100, "3:-100");
+        chain.addFilter(context.mock(Filter.class, "D"), null, 4L, -1000, "4:-1000");
+        chain.addFilter(context.mock(Filter.class, "E"), null, 5L, 1000, "5:1000");
 
         final FilterHandle[] entries = chain.getFilters();
         assertEquals(5, entries.length);



Mime
View raw message