-
Notifications
You must be signed in to change notification settings - Fork 0
/
ComponentDecoratorFilter.java
60 lines (51 loc) · 1.99 KB
/
ComponentDecoratorFilter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package com.szokone.aem.gokotlin.java.filter;
import com.szokone.aem.gokotlin.java.service.ConfigurableService;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.osgi.framework.Constants;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import javax.servlet.*;
import java.io.IOException;
/**
* Sling Filter for decorating components with additional classes
*/
@Component(
service = Filter.class,
property = {
"sling.filter.scope=COMPONENT",
Constants.SERVICE_RANKING + ":Integer=" + (Integer.MAX_VALUE - 1)
}
)
/*
When using `org.apache.felix.scr.annotations` you can use Felix SCR equivalent:
@SlingFilter(
scope = SlingFilterScope.COMPONENT,
order = Integer.MAX_VALUE - 1
)
*/
public class ComponentDecoratorFilter implements Filter {
@Reference
private ConfigurableService service;
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
SlingHttpServletRequest req = (SlingHttpServletRequest) request;
SlingHttpServletResponse res = (SlingHttpServletResponse) response;
// NOTE: in real-life project make sure that your decorator touches only places that it should
// be aware of the performance drop when badly implemented use resource type as a filter
if (service.isEnabled() && StringUtils.startsWith(req.getResource().getPath(), "/content/experience-fragments/go-kotlin")) {
new ComponentDecoratorProcessor(req, res, chain, service).process();
} else {
chain.doFilter(request, response);
}
}
@Override
public void init(FilterConfig filterConfig) {
// nothing to init
}
@Override
public void destroy() {
// Searching, Seek and destroy
}
}