-
Notifications
You must be signed in to change notification settings - Fork 0
/
ComponentDecoratorProcessor.java
54 lines (42 loc) · 1.7 KB
/
ComponentDecoratorProcessor.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
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.apache.sling.api.wrappers.SlingHttpServletResponseWrapper;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
class ComponentDecoratorProcessor extends SlingHttpServletResponseWrapper {
private final SlingHttpServletRequest request;
private final SlingHttpServletResponse response;
private final FilterChain chain;
private final ConfigurableService service;
private StringWriter sw;
void process() throws IOException, ServletException {
chain.doFilter(request, this);
String html = sw != null ? sw.toString() : StringUtils.EMPTY;
for (String search : service.getReplacements().keySet()) {
if (html.contains(search)) {
html = StringUtils.replaceAll(html, search, service.getReplacements().get(search));
}
}
response.getWriter().write(html);
}
ComponentDecoratorProcessor(SlingHttpServletRequest request, SlingHttpServletResponse response, FilterChain chain, ConfigurableService service) {
super(response);
this.request = request;
this.response = response;
this.chain = chain;
this.service = service;
}
@Override
public PrintWriter getWriter() throws IOException {
if (sw == null) {
sw = new StringWriter();
}
return new PrintWriter(sw);
}
}