incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bdelacre...@apache.org
Subject svn commit: r1482606 - in /sling/trunk/contrib/extensions/healthcheck: hc-core/src/main/java/org/apache/sling/hc/util/ hc-sling/src/main/java/org/apache/sling/hc/sling/api/ hc-sling/src/main/java/org/apache/sling/hc/sling/impl/ hc-sling/src/test/java/o...
Date Tue, 14 May 2013 21:44:34 GMT
Author: bdelacretaz
Date: Tue May 14 21:44:34 2013
New Revision: 1482606

URL: http://svn.apache.org/r1482606
Log:
SLING-2822 - Sling support for tagged rules, including demo content

Added:
    sling/trunk/contrib/extensions/healthcheck/hc-sling/src/test/java/org/apache/sling/hc/sling/impl/ServletTagsSelectionTest.java
  (with props)
Modified:
    sling/trunk/contrib/extensions/healthcheck/hc-core/src/main/java/org/apache/sling/hc/util/TaggedRuleFilter.java
    sling/trunk/contrib/extensions/healthcheck/hc-sling/src/main/java/org/apache/sling/hc/sling/api/RulesResourceParser.java
    sling/trunk/contrib/extensions/healthcheck/hc-sling/src/main/java/org/apache/sling/hc/sling/impl/RulesResourceParserImpl.java
    sling/trunk/contrib/extensions/healthcheck/hc-sling/src/main/java/org/apache/sling/hc/sling/impl/SlingHealthCheckServlet.java
    sling/trunk/contrib/extensions/healthcheck/hc-sling/src/test/java/org/apache/sling/hc/sling/MockResource.java
    sling/trunk/contrib/extensions/healthcheck/hc-sling/src/test/java/org/apache/sling/hc/sling/RulesResourceParserTest.java
    sling/trunk/contrib/extensions/healthcheck/sling-demo/src/main/resources/SLING-CONTENT/apps/hc/demo/admin-foo.json
    sling/trunk/contrib/extensions/healthcheck/sling-demo/src/main/resources/SLING-CONTENT/apps/hc/demo/admin-login.json
    sling/trunk/contrib/extensions/healthcheck/sling-demo/src/main/resources/SLING-CONTENT/apps/hc/demo/groovy-bundle.json
    sling/trunk/contrib/extensions/healthcheck/sling-demo/src/main/resources/SLING-CONTENT/apps/hc/demo/non-existent-bundle.json
    sling/trunk/contrib/extensions/healthcheck/sling-demo/src/main/resources/SLING-CONTENT/apps/hc/demo/sling-api-bundle.json

Modified: sling/trunk/contrib/extensions/healthcheck/hc-core/src/main/java/org/apache/sling/hc/util/TaggedRuleFilter.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/healthcheck/hc-core/src/main/java/org/apache/sling/hc/util/TaggedRuleFilter.java?rev=1482606&r1=1482605&r2=1482606&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/healthcheck/hc-core/src/main/java/org/apache/sling/hc/util/TaggedRuleFilter.java
(original)
+++ sling/trunk/contrib/extensions/healthcheck/hc-core/src/main/java/org/apache/sling/hc/util/TaggedRuleFilter.java
Tue May 14 21:44:34 2013
@@ -17,6 +17,8 @@
  */
 package org.apache.sling.hc.util;
 
+import java.util.Arrays;
+
 import org.apache.sling.hc.api.Rule;
 import org.apache.sling.hc.api.RuleFilter;
 
@@ -42,4 +44,9 @@ public class TaggedRuleFilter implements
         }
         return true;
     }
+    
+    @Override
+    public String toString() {
+        return getClass().getSimpleName() + ", tags=" + Arrays.asList(tags);
+    }
 }

Modified: sling/trunk/contrib/extensions/healthcheck/hc-sling/src/main/java/org/apache/sling/hc/sling/api/RulesResourceParser.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/healthcheck/hc-sling/src/main/java/org/apache/sling/hc/sling/api/RulesResourceParser.java?rev=1482606&r1=1482605&r2=1482606&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/healthcheck/hc-sling/src/main/java/org/apache/sling/hc/sling/api/RulesResourceParser.java
(original)
+++ sling/trunk/contrib/extensions/healthcheck/hc-sling/src/main/java/org/apache/sling/hc/sling/api/RulesResourceParser.java
Tue May 14 21:44:34 2013
@@ -28,6 +28,7 @@ public interface RulesResourceParser {
     String RULE_NAME = "ruleName";
     String QUALIFIER = "qualifier";
     String EXPRESSION = "expression";
+    String TAGS = "tags";
     
     List<Rule> parseResource(Resource r);
 }

Modified: sling/trunk/contrib/extensions/healthcheck/hc-sling/src/main/java/org/apache/sling/hc/sling/impl/RulesResourceParserImpl.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/healthcheck/hc-sling/src/main/java/org/apache/sling/hc/sling/impl/RulesResourceParserImpl.java?rev=1482606&r1=1482605&r2=1482606&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/healthcheck/hc-sling/src/main/java/org/apache/sling/hc/sling/impl/RulesResourceParserImpl.java
(original)
+++ sling/trunk/contrib/extensions/healthcheck/hc-sling/src/main/java/org/apache/sling/hc/sling/impl/RulesResourceParserImpl.java
Tue May 14 21:44:34 2013
@@ -78,12 +78,22 @@ public class RulesResourceParserImpl imp
         final ValueMap props = r.adaptTo(ValueMap.class);
         if(props.containsKey(NAMESPACE) && props.containsKey(RULE_NAME)) {
             for(RuleBuilder b : healthcheck.getRuleBuilders()) {
+                // basic properties
                 final Rule rule = b.buildRule(
                     props.get(NAMESPACE, String.class), 
                     props.get(RULE_NAME, String.class), 
                     props.get(QUALIFIER, String.class), 
                     props.get(EXPRESSION, String.class)
                 );
+                
+                // tags, if any
+                if(rule != null && props.containsKey(TAGS)) {
+                    final String [] tags = props.get(TAGS, String[].class);
+                    if(tags != null && tags.length > 0) {
+                        rule.setTags(tags);
+                    }
+                }
+                
                 if(rule != null) {
                     return rule;
                 }

Modified: sling/trunk/contrib/extensions/healthcheck/hc-sling/src/main/java/org/apache/sling/hc/sling/impl/SlingHealthCheckServlet.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/healthcheck/hc-sling/src/main/java/org/apache/sling/hc/sling/impl/SlingHealthCheckServlet.java?rev=1482606&r1=1482605&r2=1482606&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/healthcheck/hc-sling/src/main/java/org/apache/sling/hc/sling/impl/SlingHealthCheckServlet.java
(original)
+++ sling/trunk/contrib/extensions/healthcheck/hc-sling/src/main/java/org/apache/sling/hc/sling/impl/SlingHealthCheckServlet.java
Tue May 14 21:44:34 2013
@@ -18,6 +18,8 @@
 package org.apache.sling.hc.sling.impl;
 
 import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
 
 import javax.servlet.ServletException;
 
@@ -30,6 +32,9 @@ import org.apache.sling.hc.api.HealthChe
 import org.apache.sling.hc.api.RulesEngine;
 import org.apache.sling.hc.sling.api.JsonResultRenderer;
 import org.apache.sling.hc.sling.api.RulesResourceParser;
+import org.apache.sling.hc.util.TaggedRuleFilter;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /** Sling Servlet that renders a Resource that contains health check rules 
  *  definitions, after evaluating the rules.
@@ -37,9 +42,17 @@ import org.apache.sling.hc.sling.api.Rul
  *  defines the output format. 
  */
 @SuppressWarnings("serial")
-@SlingServlet(extensions="json",resourceTypes="sling/healthcheck/rules",methods="GET",selectors="healthcheck")
+@SlingServlet(
+        extensions="json",
+        resourceTypes="sling/healthcheck/rules",
+        methods="GET",
+        selectors=SlingHealthCheckServlet.HC_SELECTOR)
 public class SlingHealthCheckServlet extends SlingSafeMethodsServlet {
 
+    public static final String HC_SELECTOR = "healthcheck";
+    
+    private final Logger log = LoggerFactory.getLogger(getClass());
+    
     @Reference
     private HealthCheckFacade healthcheck;
     
@@ -60,7 +73,20 @@ public class SlingHealthCheckServlet ext
         engine.addRules(parser.parseResource(request.getResource()));
         response.setContentType("application/json");
         response.setCharacterEncoding("UTF-8");
-        renderer.render(engine.evaluateRules(), response.getWriter());
+        final TaggedRuleFilter filter = new TaggedRuleFilter(getRuleTagsFromRequest(request));
+        log.info("Executing rules found under {} with {}", request.getResource().getPath(),
filter);
+        renderer.render(engine.evaluateRules(filter), response.getWriter());
         response.getWriter().flush();
     }
+    
+    static String [] getRuleTagsFromRequest(SlingHttpServletRequest r) {
+        // Get request selectors and remove this servlet's selector
+        final List<String> result = new ArrayList<String>();
+        for(String tag : r.getRequestPathInfo().getSelectors()) {
+            if(!HC_SELECTOR.equals(tag)) {
+                result.add(tag);
+            }
+        }
+        return result.toArray(new String[] {});
+    }
 }

Modified: sling/trunk/contrib/extensions/healthcheck/hc-sling/src/test/java/org/apache/sling/hc/sling/MockResource.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/healthcheck/hc-sling/src/test/java/org/apache/sling/hc/sling/MockResource.java?rev=1482606&r1=1482605&r2=1482606&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/healthcheck/hc-sling/src/test/java/org/apache/sling/hc/sling/MockResource.java
(original)
+++ sling/trunk/contrib/extensions/healthcheck/hc-sling/src/test/java/org/apache/sling/hc/sling/MockResource.java
Tue May 14 21:44:34 2013
@@ -14,6 +14,7 @@ import static org.apache.sling.hc.sling.
 import static org.apache.sling.hc.sling.api.RulesResourceParser.RULE_NAME;
 import static org.apache.sling.hc.sling.api.RulesResourceParser.QUALIFIER;
 import static org.apache.sling.hc.sling.api.RulesResourceParser.EXPRESSION;
+import static org.apache.sling.hc.sling.api.RulesResourceParser.TAGS;
 
 class MockResource implements Resource {
     private final ResourceResolver resolver;
@@ -29,7 +30,7 @@ class MockResource implements Resource {
         public <T> T get(String key, Class<T> type) {
             // we only need Strings in our tests
             final Object value = get(key);
-            return value == null ? null : (T)value.toString();
+            return value == null ? null : (T)value;
         }
 
         @SuppressWarnings("unchecked")
@@ -62,6 +63,10 @@ class MockResource implements Resource {
         this.scriptCode = scriptCode;
     }
     
+    void setTags(String [] tags) {
+        valueMap.put(TAGS, tags);
+    }
+    
     @SuppressWarnings("unchecked")
     @Override
     public <AdapterType> AdapterType adaptTo(Class<AdapterType> target) {

Modified: sling/trunk/contrib/extensions/healthcheck/hc-sling/src/test/java/org/apache/sling/hc/sling/RulesResourceParserTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/healthcheck/hc-sling/src/test/java/org/apache/sling/hc/sling/RulesResourceParserTest.java?rev=1482606&r1=1482605&r2=1482606&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/healthcheck/hc-sling/src/test/java/org/apache/sling/hc/sling/RulesResourceParserTest.java
(original)
+++ sling/trunk/contrib/extensions/healthcheck/hc-sling/src/test/java/org/apache/sling/hc/sling/RulesResourceParserTest.java
Tue May 14 21:44:34 2013
@@ -92,6 +92,19 @@ public class RulesResourceParserTest {
     }
     
     @Test
+    public void testResourceWithTags() {
+        final MockResource r = new MockResource(resolver, "/", "test", "constant", "5", ">
3");
+        r.setTags(new String [] { "foo", "bar" });
+        final List<Rule> rules = parser.parseResource(r); 
+        assertEquals(1, rules.size());
+        final Rule rule = rules.get(0);
+        assertEquals("Rule: test:constant:5 > 3", rule.toString());
+        assertEquals(2, rule.getTags().size());
+        assertTrue(rule.hasTag("foo"));
+        assertTrue(rule.hasTag("bar"));
+    }
+    
+    @Test
     public void testScriptResource() {
         final Resource root = new MockResource(resolver, "/foo", "test", "constant", "5",
"> 3");
         new MockResource(resolver, "/foo/script1", "some script");

Added: sling/trunk/contrib/extensions/healthcheck/hc-sling/src/test/java/org/apache/sling/hc/sling/impl/ServletTagsSelectionTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/healthcheck/hc-sling/src/test/java/org/apache/sling/hc/sling/impl/ServletTagsSelectionTest.java?rev=1482606&view=auto
==============================================================================
--- sling/trunk/contrib/extensions/healthcheck/hc-sling/src/test/java/org/apache/sling/hc/sling/impl/ServletTagsSelectionTest.java
(added)
+++ sling/trunk/contrib/extensions/healthcheck/hc-sling/src/test/java/org/apache/sling/hc/sling/impl/ServletTagsSelectionTest.java
Tue May 14 21:44:34 2013
@@ -0,0 +1,77 @@
+/*
+ * 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 SF 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.hc.sling.impl;
+
+import java.util.Arrays;
+import java.util.List;
+
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.api.request.RequestPathInfo;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.MockitoAnnotations;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+public class ServletTagsSelectionTest {
+    
+    @Mock
+    private SlingHttpServletRequest request;
+    
+    @Mock
+    private RequestPathInfo requestPathInfo;
+    
+    @Before
+    public void setup() {
+        MockitoAnnotations.initMocks(this);
+        Mockito.when(request.getRequestPathInfo()).thenReturn(requestPathInfo);
+    }
+
+    private void assertTags(String [] actual, String ... expected) {
+        assertEquals("Expecting " + expected.length + " tags", expected.length, actual.length);
+        final List<String> actualList = Arrays.asList(actual);
+        for(String tag : expected) {
+            assertTrue("Expecting " + actualList + " to contain " + tag, actualList.contains(tag));
+        }
+    }
+    
+    @Test
+    public void testNoSelectors() {
+        Mockito.when(requestPathInfo.getSelectors()).thenReturn(new String [] {});
+        assertTags(SlingHealthCheckServlet.getRuleTagsFromRequest(request), new String[]
{});
+    }
+    
+    @Test
+    public void testNoHcSelector() {
+        final String [] selectors = new String [] { "foo", "bar" };
+        Mockito.when(requestPathInfo.getSelectors()).thenReturn(selectors);
+        assertTags(SlingHealthCheckServlet.getRuleTagsFromRequest(request), selectors);
+    }
+    
+    @Test
+    public void testWithHcSelector() {
+        final String [] selectors = new String [] { "foo", "bar", SlingHealthCheckServlet.HC_SELECTOR
};
+        Mockito.when(requestPathInfo.getSelectors()).thenReturn(selectors);
+        final String [] expected = new String [] { "foo", "bar" };
+        assertTags(SlingHealthCheckServlet.getRuleTagsFromRequest(request), expected);
+    }
+    
+}

Propchange: sling/trunk/contrib/extensions/healthcheck/hc-sling/src/test/java/org/apache/sling/hc/sling/impl/ServletTagsSelectionTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/trunk/contrib/extensions/healthcheck/hc-sling/src/test/java/org/apache/sling/hc/sling/impl/ServletTagsSelectionTest.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Modified: sling/trunk/contrib/extensions/healthcheck/sling-demo/src/main/resources/SLING-CONTENT/apps/hc/demo/admin-foo.json
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/healthcheck/sling-demo/src/main/resources/SLING-CONTENT/apps/hc/demo/admin-foo.json?rev=1482606&r1=1482605&r2=1482606&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/healthcheck/sling-demo/src/main/resources/SLING-CONTENT/apps/hc/demo/admin-foo.json
(original)
+++ sling/trunk/contrib/extensions/healthcheck/sling-demo/src/main/resources/SLING-CONTENT/apps/hc/demo/admin-foo.json
Tue May 14 21:44:34 2013
@@ -3,5 +3,6 @@
   "ruleName": "loginfails",
   "namespace": "sling",
   "jcr:primaryType": "nt:unstructured",
-  "qualifier": "admin#foo"
+  "qualifier": "admin#foo",
+  "tags" : [ "admin", "security" ]
 }

Modified: sling/trunk/contrib/extensions/healthcheck/sling-demo/src/main/resources/SLING-CONTENT/apps/hc/demo/admin-login.json
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/healthcheck/sling-demo/src/main/resources/SLING-CONTENT/apps/hc/demo/admin-login.json?rev=1482606&r1=1482605&r2=1482606&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/healthcheck/sling-demo/src/main/resources/SLING-CONTENT/apps/hc/demo/admin-login.json
(original)
+++ sling/trunk/contrib/extensions/healthcheck/sling-demo/src/main/resources/SLING-CONTENT/apps/hc/demo/admin-login.json
Tue May 14 21:44:34 2013
@@ -3,5 +3,6 @@
   "ruleName": "loginfails",
   "namespace": "sling",
   "jcr:primaryType": "nt:unstructured",
-  "qualifier": "admin#admin"
+  "qualifier": "admin#admin",
+  "tags" : [ "admin", "security" ]
 }

Modified: sling/trunk/contrib/extensions/healthcheck/sling-demo/src/main/resources/SLING-CONTENT/apps/hc/demo/groovy-bundle.json
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/healthcheck/sling-demo/src/main/resources/SLING-CONTENT/apps/hc/demo/groovy-bundle.json?rev=1482606&r1=1482605&r2=1482606&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/healthcheck/sling-demo/src/main/resources/SLING-CONTENT/apps/hc/demo/groovy-bundle.json
(original)
+++ sling/trunk/contrib/extensions/healthcheck/sling-demo/src/main/resources/SLING-CONTENT/apps/hc/demo/groovy-bundle.json
Tue May 14 21:44:34 2013
@@ -4,5 +4,6 @@
   "ruleName": "bundle.state",
   "namespace": "osgi",
   "jcr:primaryType": "nt:unstructured",
-  "qualifier": "groovy-all"
+  "qualifier": "groovy-all",
+  "tags" : ["groovy","bundles"]
 }

Modified: sling/trunk/contrib/extensions/healthcheck/sling-demo/src/main/resources/SLING-CONTENT/apps/hc/demo/non-existent-bundle.json
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/healthcheck/sling-demo/src/main/resources/SLING-CONTENT/apps/hc/demo/non-existent-bundle.json?rev=1482606&r1=1482605&r2=1482606&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/healthcheck/sling-demo/src/main/resources/SLING-CONTENT/apps/hc/demo/non-existent-bundle.json
(original)
+++ sling/trunk/contrib/extensions/healthcheck/sling-demo/src/main/resources/SLING-CONTENT/apps/hc/demo/non-existent-bundle.json
Tue May 14 21:44:34 2013
@@ -4,5 +4,6 @@
   "ruleName": "bundle.state",
   "namespace": "osgi",
   "jcr:primaryType": "nt:unstructured",
-  "qualifier": "non.existent.bundle"
+  "qualifier": "non.existent.bundle",
+  "tags" : "bundles"
 }

Modified: sling/trunk/contrib/extensions/healthcheck/sling-demo/src/main/resources/SLING-CONTENT/apps/hc/demo/sling-api-bundle.json
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/healthcheck/sling-demo/src/main/resources/SLING-CONTENT/apps/hc/demo/sling-api-bundle.json?rev=1482606&r1=1482605&r2=1482606&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/healthcheck/sling-demo/src/main/resources/SLING-CONTENT/apps/hc/demo/sling-api-bundle.json
(original)
+++ sling/trunk/contrib/extensions/healthcheck/sling-demo/src/main/resources/SLING-CONTENT/apps/hc/demo/sling-api-bundle.json
Tue May 14 21:44:34 2013
@@ -4,5 +4,6 @@
   "ruleName": "bundle.state",
   "namespace": "osgi",
   "jcr:primaryType": "nt:unstructured",
-  "qualifier": "org.apache.sling.api"
+  "qualifier": "org.apache.sling.api",
+  "tags" : ["bundles","sling"]
 }



Mime
View raw message