You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by np...@apache.org on 2018/09/11 12:14:07 UTC

[sling-site] branch asf-site updated: fixing filter annotation code sample

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

npeltier pushed a commit to branch asf-site
in repository https://gitbox.apache.org/repos/asf/sling-site.git


The following commit(s) were added to refs/heads/asf-site by this push:
     new d0839fb  fixing filter annotation code sample
d0839fb is described below

commit d0839fb3a12c062af11bd4fe20efe033095feee9
Author: Nicolas Peltier <np...@adobe.com>
AuthorDate: Tue Sep 11 14:13:16 2018 +0200

    fixing filter annotation code sample
---
 documentation/the-sling-engine/filters.html | 46 +++++++++++++++++++----------
 1 file changed, 30 insertions(+), 16 deletions(-)

diff --git a/documentation/the-sling-engine/filters.html b/documentation/the-sling-engine/filters.html
index cccd13f..e8251ec 100644
--- a/documentation/the-sling-engine/filters.html
+++ b/documentation/the-sling-engine/filters.html
@@ -174,28 +174,42 @@ from our integration tests shows an example Sling Filter.
 </table>
 <h2><a href="#slingservletfilter-annotation" name="slingservletfilter-annotation">SlingServletFilter Annotation</a></h2>
 <p>Coding the above being a bit tedious, <code>Apache Sling Servlets Annotations 1.1.0</code> provides handy `SlingServletFilter annotation to set those values:</p>
-<p>``` ...  import org.apache.sling.servlets.annotations.SlingServletFilter;  import org.apache.sling.servlets.annotations.SlingServletFilterScope;  import org.osgi.service.component.annotations.Component; ...<br/> @Component  @SlingServletFilter(scope = {SlingServletFilterScope.REQUEST},  suffix_pattern = "/suffix/foo",  resourceTypes = {"foo/bar"},  pattern = "/content/.*",  extensions = {"txt","json"},  selectors = {"foo","bar"},  methods = {"GET","HEAD"})  public class FooBarFilter i [...]
-<pre><code>   @Override
-   public void init(FilterConfig filterConfig) throws ServletException {
+<pre><code>    [...]
+   import org.apache.sling.servlets.annotations.SlingServletFilter;
+   import org.apache.sling.servlets.annotations.SlingServletFilterScope;
+   import org.osgi.service.component.annotations.Component;
+    [...]   
+   @Component
+   @SlingServletFilter(scope = {SlingServletFilterScope.REQUEST},
+                       suffix_pattern = &quot;/suffix/foo&quot;,
+                       resourceTypes = {&quot;foo/bar&quot;},
+                       pattern = &quot;/content/.*&quot;,
+                       extensions = {&quot;txt&quot;,&quot;json&quot;},
+                       selectors = {&quot;foo&quot;,&quot;bar&quot;},
+                       methods = {&quot;GET&quot;,&quot;HEAD&quot;})
+   public class FooBarFilter implements Filter {
 
-   }
+       @Override
+       public void init(FilterConfig filterConfig) throws ServletException {
 
-   @Override
-   public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
-       SlingHttpServletResponse slingResponse = (SlingHttpServletResponse)response;
-       //will only be run on (GET|HEAD) /content/.*.foo|bar.txt|json/suffix/foo requests
-       //code here can be reduced to what should actually be done in that case
-       //for other requests, this filter will not be in the call stack 
-       slingResponse.addHeader(&quot;foobared&quot;, &quot;true&quot;);
-       chain.doFilter(request, slingResponse);
-   }
+       }
+
+       @Override
+       public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
+           SlingHttpServletResponse slingResponse = (SlingHttpServletResponse)response;
+           //will only be run on (GET|HEAD) /content/.*.foo|bar.txt|json/suffix/foo requests
+           //code here can be reduced to what should actually be done in that case
+           //for other requests, this filter will not be in the call stack 
+           slingResponse.addHeader(&quot;foobared&quot;, &quot;true&quot;);
+           chain.doFilter(request, slingResponse);
+       }
 
-   @Override
-   public void destroy() {
+       @Override
+       public void destroy() {
 
+       }
    }
 </code></pre>
-<p>} ```</p>
 <h2><a href="#filter-chains" name="filter-chains">Filter Chains</a></h2>
 <p>Sling maintains five filter chains: request level, component level, include filters, forward filters and error filters. Except for the component level filter these filter chains correspond to the filter <code>&lt;dispatcher&gt;</code> configurations as defined for Servlet API 2.5 web applications (see section SRV.6.2.5 Filters and the RequestDispatcher).</p>
 <p>The following table summarizes when each of the filter chains is called and what value must be defined in the <code>sling.filter.scope</code> property to have a filter added to the respective chain:</p>