HybridAppPathRewriterTransformer.java
4.17 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.rewriter.linkchecker.LinkChecker
* com.day.text.Text
* org.apache.cocoon.xml.sax.AbstractSAXPipe
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.rewriter.ProcessingComponentConfiguration
* org.apache.sling.rewriter.ProcessingContext
* org.apache.sling.rewriter.Transformer
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.mobile.platform.impl.rewriter;
import com.adobe.cq.mobile.platform.impl.rewriter.HybridAppPathRewriterOptions;
import com.adobe.cq.mobile.platform.impl.rewriter.HybridAppPathRewriterTransformerConfig;
import com.day.cq.rewriter.linkchecker.LinkChecker;
import com.day.text.Text;
import java.io.IOException;
import java.util.Map;
import org.apache.cocoon.xml.sax.AbstractSAXPipe;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.rewriter.ProcessingComponentConfiguration;
import org.apache.sling.rewriter.ProcessingContext;
import org.apache.sling.rewriter.Transformer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;
public class HybridAppPathRewriterTransformer
extends AbstractSAXPipe
implements Transformer {
private static final Logger log = LoggerFactory.getLogger(HybridAppPathRewriterTransformer.class);
private final LinkChecker checker;
private SlingHttpServletRequest request;
private HybridAppPathRewriterOptions options;
private boolean enabled = false;
private HybridAppPathRewriterTransformerConfig config;
public HybridAppPathRewriterTransformer(LinkChecker checker, HybridAppPathRewriterTransformerConfig config) {
this.checker = checker;
this.config = config;
}
public void init(ProcessingContext context, ProcessingComponentConfiguration config) throws IOException {
this.request = context.getRequest();
this.options = (HybridAppPathRewriterOptions)this.request.getAttribute("hybridAppPathRewritingOptions");
boolean bl = this.enabled = this.options != null;
if (this.enabled) {
log.debug("Rewriting paths for hybrid app");
}
}
public void dispose() {
}
public void startElement(String uri, String name, String raw, Attributes a) throws SAXException {
if (this.enabled) {
AttributesImpl attrs = new AttributesImpl(a);
boolean result = false;
if (result |= this.hybridRewrite(this.config.assetsMapping, name, attrs)) {
super.startElement(uri, name, raw, (Attributes)attrs);
return;
}
}
super.startElement(uri, name, raw, a);
}
private boolean hybridRewrite(Map<String, String> mapping, String name, AttributesImpl attrs) throws SAXException {
if (mapping.containsKey(name)) {
String[] attributesOfInterest = Text.explode((String)mapping.get(name), (int)58);
boolean targetFound = false;
for (String attrName : attributesOfInterest) {
String value = attrs.getValue(attrName);
if (!this.isTargetOfInterest(value)) continue;
targetFound = true;
int index = attrs.getIndex(attrName);
boolean replace = true;
if ("src".equals(attrName)) {
boolean bl = replace = !value.startsWith("http");
}
if (replace) {
value = this.replaceJcrContent(value);
}
attrs.setAttribute(index, "", attrName, attrName, "String", value);
log.debug("Attribute \"{}\" rewritten to: {}", (Object)name, (Object)value);
}
return targetFound;
}
return false;
}
private boolean isTargetOfInterest(String target) {
return target != null && !this.checker.isSpecial(target);
}
private String replaceJcrContent(String path) {
if (path == null) {
return null;
}
return path.replaceAll("/jcr:content/", "/jcr_content/").replaceAll("/_jcr_content/", "/jcr_content/");
}
}