You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by kw...@apache.org on 2018/05/29 08:18:36 UTC

[sling-org-apache-sling-servlets-annotations] 04/09: SLING-7624 add annotation for Sling Servlet Filters

This is an automated email from the ASF dual-hosted git repository.

kwin pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-servlets-annotations.git

commit de6ddf01a0d9d6dd4735a74b65dfddde87e31f5e
Author: Konrad Windszus <kw...@apache.org>
AuthorDate: Sun May 13 18:57:06 2018 +0200

    SLING-7624 add annotation for Sling Servlet Filters
---
 pom.xml                                            | 13 +++-
 .../servlets/annotations/SlingServletByPath.java   |  3 +-
 .../annotations/SlingServletByResourceType.java    |  3 +-
 .../servlets/annotations/SlingServletFilter.java   | 60 ++++++++++++++++
 .../annotations/SlingServletFilterScope.java       | 82 ++++++++++++++++++++++
 5 files changed, 158 insertions(+), 3 deletions(-)

diff --git a/pom.xml b/pom.xml
index 8903fa0..58e2e98 100644
--- a/pom.xml
+++ b/pom.xml
@@ -46,12 +46,23 @@
             <artifactId>org.osgi.service.component.annotations</artifactId>
             <version>1.4.0</version><!-- for annotation @ComponentPropertyType -->
         </dependency>
-         <dependency>
+        <dependency>
             <groupId>org.apache.sling</groupId>
             <artifactId>org.apache.sling.api</artifactId>
             <!-- https://issues.apache.org/jira/browse/SLING-6249, only for link in javadoc -->
             <version>2.16.0</version>
         </dependency>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.api</artifactId>
+            <!-- https://issues.apache.org/jira/browse/SLING-6249, only for link in javadoc -->
+            <version>2.16.0</version>
+        </dependency>
+        <dependency>
+            <groupId>javax.servlet</groupId>
+            <artifactId>javax.servlet-api</artifactId>
+        </dependency>
+        
     </dependencies>
 
 </project>
diff --git a/src/main/java/org/apache/sling/servlets/annotations/SlingServletByPath.java b/src/main/java/org/apache/sling/servlets/annotations/SlingServletByPath.java
index d244457..9bbdc5c 100644
--- a/src/main/java/org/apache/sling/servlets/annotations/SlingServletByPath.java
+++ b/src/main/java/org/apache/sling/servlets/annotations/SlingServletByPath.java
@@ -26,8 +26,9 @@ import org.osgi.service.component.annotations.ComponentPropertyType;
  * for reasons outlined at <a href="https://sling.apache.org/documentation/the-sling-engine/servlets.html#caveats-when-binding-servlets-by-path">
  * Caveats when binding servlets by path</a>
  *
- * @see <a href="https://github.com/apache/felix/blob/trunk/tools/org.apache.felix.scr.annotations/src/main/java/org/apache/felix/scr/annotations/sling/SlingServlet.java">Felix SCR annotations</a>
+ * @see <a href="https://sling.apache.org/documentation/the-sling-engine/servlets.html">Sling Servlets</a>
  * @see ServletResolverConstants
+ * @see <a href="https://github.com/apache/felix/blob/trunk/tools/org.apache.felix.scr.annotations/src/main/java/org/apache/felix/scr/annotations/sling/SlingServlet.java">Felix SCR annotation</a>
  */
 @ComponentPropertyType
 public @interface SlingServletByPath {
diff --git a/src/main/java/org/apache/sling/servlets/annotations/SlingServletByResourceType.java b/src/main/java/org/apache/sling/servlets/annotations/SlingServletByResourceType.java
index c9dc6d6..096df75 100644
--- a/src/main/java/org/apache/sling/servlets/annotations/SlingServletByResourceType.java
+++ b/src/main/java/org/apache/sling/servlets/annotations/SlingServletByResourceType.java
@@ -23,8 +23,9 @@ import org.osgi.service.component.annotations.ComponentPropertyType;
  * Takes care of writing the relevant component properties as being used by the Sling Servlet Resolver ({@link ServletResolverConstants})
  * to register the annotated servlet component as Sling servlet for a specific resource type.
  * 
- * @see <a href="https://github.com/apache/felix/blob/trunk/tools/org.apache.felix.scr.annotations/src/main/java/org/apache/felix/scr/annotations/sling/SlingServlet.java">Felix SCR annotations</a>
+ * @see <a href="https://sling.apache.org/documentation/the-sling-engine/servlets.html">Sling Servlets</a>
  * @see ServletResolverConstants
+ * @see <a href="https://github.com/apache/felix/blob/trunk/tools/org.apache.felix.scr.annotations/src/main/java/org/apache/felix/scr/annotations/sling/SlingServlet.java">Felix SCR annotation</a>
  */
 @ComponentPropertyType
 public @interface SlingServletByResourceType {
diff --git a/src/main/java/org/apache/sling/servlets/annotations/SlingServletFilter.java b/src/main/java/org/apache/sling/servlets/annotations/SlingServletFilter.java
new file mode 100644
index 0000000..de6fbb6
--- /dev/null
+++ b/src/main/java/org/apache/sling/servlets/annotations/SlingServletFilter.java
@@ -0,0 +1,60 @@
+/*
+ * 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.servlets.annotations;
+
+import org.apache.sling.api.servlets.ServletResolverConstants;
+import org.osgi.service.component.annotations.ComponentPropertyType;
+
+/** 
+ * Component Property Type (as defined by OSGi DS 1.4) for Sling Servlet Filters. 
+ * Takes care of writing the relevant component properties as being used by the Sling Servlet Resolver 
+ * ({@link ServletResolverConstants}) to register the annotated servlet filter component as
+ * Sling servlet filter. 
+ * <br><br>
+ * The order of the filter is determined by the property {@code service.ranking}. Its value is used to sort the filters. 
+ * Filters with a higher order are executed before a filter with a lower order. If two filters have the same order, 
+ * the one with the lower service id is executed first.
+ * <br>
+ * <br>
+ * Please note that the ordering is actually depending on the used Apache Sling Engine bundle version. Version older than 2.3.4 of that
+ * bundle are sorting the filters in the wrong reverse order. Make sure to run a newer version of the Sling engine to get the correct
+ * ordering (see also <a href="https://issues.apache.org/jira/browse/SLING-2920">SLING-2920</a>).
+ *
+ * @see <a href=
+ *      "https://github.com/apache/felix/blob/trunk/tools/org.apache.felix.scr.annotations/src/main/java/org/apache/felix/scr/annotations/sling/SlingFilter.java">Felix
+ *      SCR annotation</a>
+ * @see <a href="https://sling.apache.org/documentation/the-sling-engine/filters.html">Sling Servlet Filter</a> 
+ */
+@ComponentPropertyType
+public @interface SlingServletFilter {
+
+    /** 
+     * Prefix for every property being generated from the annotations elements (as defined in OSGi 7 Compendium, 112.8.2.1) 
+     */
+    static final String PREFIX_ = "sling.filter.";
+
+    /** 
+     * Restrict the filter to paths that match the supplied regular expression. Requires Sling Engine 2.4.0.
+     */
+    String pattern() default "";
+
+    /** 
+     * The scopes of a filter. If the filter has request scope, it is run once for a request. If the filter has component scope, it is run
+     * once for every included component (rendering). 
+     */
+    SlingServletFilterScope[] scope() default SlingServletFilterScope.REQUEST;
+}
diff --git a/src/main/java/org/apache/sling/servlets/annotations/SlingServletFilterScope.java b/src/main/java/org/apache/sling/servlets/annotations/SlingServletFilterScope.java
new file mode 100644
index 0000000..ac92f02
--- /dev/null
+++ b/src/main/java/org/apache/sling/servlets/annotations/SlingServletFilterScope.java
@@ -0,0 +1,82 @@
+/*
+ * 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.servlets.annotations;
+
+import javax.servlet.RequestDispatcher;
+import javax.servlet.http.HttpServletResponse;
+/**
+ * The possible scopes for the {@link SlingServletFilter#scope()} annotation.
+ */
+public enum SlingServletFilterScope {
+
+    /**
+     * Filters are called once per request hitting Sling from the outside. 
+     * These filters are called after the resource addressed by the request URL and the Servlet or script to process the request has been resolved 
+     * before the COMPONENT filters (if any) and the Servlet or script are called.
+     * <p>Servlet API Correspondence: {@code REQUEST}</p>
+     */
+    REQUEST("REQUEST"),
+
+    /**
+     * Filters are called upon calling the {@link RequestDispatcher#include(javax.servlet.ServletRequest, javax.servlet.ServletResponse)} 
+     * method after the included resource and the Servlet or script to process the include have been resolved before the Servlet or script is called.
+     * <p>Servlet API Correspondence: {@code REQUEST},{@code INCLUDE},{@code FORWARD}</p>
+     */
+    COMPONENT("COMPONENT"),
+
+    /**
+     * Filters are called upon {@link HttpServletResponse#sendError(int)} or {@link HttpServletResponse#sendError(int, String)}
+     * or any uncaught Throwable before resolving the error handler Servlet or script.
+     * <p>Servlet API Correspondence: {@code ERROR}</
+     */
+    ERROR("ERROR"),
+
+    /**
+     * Filters are called upon calling the RequestDispatcher.include method after the included resource and the Servlet 
+     * or script to process the include have been resolved before the Servlet or script is called.
+     * <p>Servlet API Correspondence: {@code INCLUDE}</p>
+     */
+    INCLUDE("INCLUDE"),
+
+    /**
+     * Filters are called upon calling the RequestDispatcher.forward method after the included resource and the Servlet
+     * or script to process the include have been resolved before the Servlet or script is called.
+     * <p>Servlet API Correspondence: {@code FORWARD}</p>
+     */
+    FORWARD("FORWARD");
+
+    private final String scope;
+
+    private SlingServletFilterScope(final String scope) {
+        this.scope = scope;
+    }
+
+    /**
+     * @return String representation of the scope
+     */
+    public String getScope() {
+        return this.scope;
+    }
+
+    @Override
+    public String toString() {
+        return this.getScope();
+    }
+
+}
\ No newline at end of file

-- 
To stop receiving notification emails like this one, please contact
kwin@apache.org.