PathRewriterTransformer.java
10.6 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.commons.Externalizer
* com.day.cq.rewriter.linkchecker.LinkChecker
* com.day.text.Text
* org.apache.cocoon.xml.sax.AbstractSAXPipe
* org.apache.commons.lang.StringUtils
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.rewriter.ProcessingComponentConfiguration
* org.apache.sling.rewriter.ProcessingContext
* org.apache.sling.rewriter.Transformer
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.wcm.contentsync.impl.rewriter;
import com.day.cq.commons.Externalizer;
import com.day.cq.rewriter.linkchecker.LinkChecker;
import com.day.cq.wcm.contentsync.PathRewriterOptions;
import com.day.cq.wcm.contentsync.impl.rewriter.PathRewriterTransformerConfig;
import com.day.text.Text;
import java.io.IOException;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.cocoon.xml.sax.AbstractSAXPipe;
import org.apache.commons.lang.StringUtils;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.ResourceResolver;
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 PathRewriterTransformer
extends AbstractSAXPipe
implements Transformer {
private static final Logger log = LoggerFactory.getLogger(PathRewriterTransformer.class);
private static final String SCRIPT_TAG = "script";
private final LinkChecker checker;
private final Externalizer externalizer;
private boolean enabled = false;
private SlingHttpServletRequest request;
private PathRewriterOptions options;
private String relativePrefix;
private PathRewriterTransformerConfig config;
private boolean insideScript;
private StringBuffer scriptBuffer;
public PathRewriterTransformer(LinkChecker checker, Externalizer externalizer, PathRewriterTransformerConfig config) {
this.checker = checker;
this.externalizer = externalizer;
this.config = config;
this.insideScript = false;
}
public void init(ProcessingContext context, ProcessingComponentConfiguration config) throws IOException {
this.request = context.getRequest();
this.options = (PathRewriterOptions)this.request.getAttribute("pathRewritingOptions");
this.enabled = this.options != null;
this.relativePrefix = this.getRelativePathPrefix(this.request);
if (this.enabled) {
log.debug("Rewriting paths for links ({}), clientlibs ({}) and images ({})", new Object[]{this.options.getRewriteMode("links").name(), this.options.getRewriteMode("clientlibs").name(), this.options.getRewriteMode("images").name()});
}
}
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;
result |= this.internalRewrite(this.config.linksMapping, "links", name, attrs);
result |= this.internalRewrite(this.config.clientlibsMapping, "clientlibs", name, attrs);
if (result |= this.internalRewrite(this.config.imagesMapping, "images", name, attrs)) {
super.startElement(uri, name, raw, (Attributes)attrs);
return;
}
if (name.equals("script") && attrs.getValue("type") != null && attrs.getValue("type").equals("text/javascript")) {
this.insideScript = true;
this.scriptBuffer = new StringBuffer();
}
}
super.startElement(uri, name, raw, a);
}
private boolean internalRewrite(Map<String, String> mapping, String rewriteOption, 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 attrPath = this.findPath(attrName, attrs);
if (!this.isTargetOfInterest(attrPath)) continue;
if (this.options.isExternal(rewriteOption)) {
this.makeExternal(attrName, attrs);
} else if (this.options.isRelative(rewriteOption)) {
this.makeRelative(attrName, attrs);
}
targetFound = true;
}
return targetFound;
}
return false;
}
public void characters(char[] chars, int start, int length) throws SAXException {
if (this.insideScript) {
this.scriptBuffer.append(chars, start, length);
} else {
super.characters(chars, start, length);
}
}
public void endElement(String uri, String loc, String raw) throws SAXException {
if (loc.equals("script") && this.insideScript) {
StringBuffer result = new StringBuffer();
if (Pattern.compile(".*CQClientLibraryManager.*", 32).matcher(this.scriptBuffer).matches()) {
Pattern pattern = Pattern.compile(this.config.clientLibraryPattern);
Matcher matcher = pattern.matcher(this.scriptBuffer);
while (matcher.find()) {
String group = matcher.group(1);
if (this.options.isExternal("clientlibs")) {
matcher.appendReplacement(result, this.config.clientLibraryReplace.replace("$1", this.externalizer.externalLink(null, "local", group)));
continue;
}
matcher.appendReplacement(result, this.config.clientLibraryReplace.replace("$1", this.relativePrefix + group));
}
matcher.appendTail(result);
} else {
result = this.scriptBuffer;
}
super.characters(result.toString().toCharArray(), 0, result.length());
this.insideScript = false;
}
super.endElement(uri, loc, raw);
}
private boolean isTargetOfInterest(String target) {
return target != null && !this.checker.isSpecial(target) && target.startsWith("/");
}
private String getRelativePathPrefix(SlingHttpServletRequest request) {
return this.getRelativePathPrefix(request.getRequestURI());
}
private String getRelativePathPrefix(String uri) {
String prefix = "";
int depth = Text.explode((String)uri, (int)47).length;
prefix = StringUtils.repeat((String)"../", (int)(depth - 1));
prefix = StringUtils.removeEnd((String)prefix, (String)"/");
return prefix;
}
private String getPathRelativeToParent(String parentPath, String absolutePath) {
if (StringUtils.isNotBlank((String)absolutePath) && StringUtils.startsWith((String)absolutePath, (String)parentPath)) {
return Text.getName((String)parentPath) + absolutePath.substring(parentPath.length());
}
String prefix = this.relativePrefix;
if (StringUtils.isNotBlank((String)parentPath)) {
String[] parentParts = Text.explode((String)parentPath, (int)47);
String[] absoluteParts = Text.explode((String)absolutePath, (int)47);
for (int i = 0; i < parentParts.length; ++i) {
if (i >= absoluteParts.length || parentParts[i].equals(absoluteParts[i])) continue;
prefix = this.getRelativePathPrefix(this.joinArray(parentParts, '/', i));
absolutePath = "/" + this.joinArray(absoluteParts, '/', i);
break;
}
}
return prefix + absolutePath;
}
private void makeExternal(String name, AttributesImpl attributes) {
int index = attributes.getIndex(name);
String value = this.findPath(name, attributes);
value = this.externalizer.externalLink(null, "local", value);
value = this.replacePath(name, attributes, value);
attributes.setAttribute(index, "", name, name, "String", value);
log.debug("Attribute \"{}\" rewritten to: {}", (Object)name, (Object)value);
}
private void makeRelative(String name, AttributesImpl attributes) {
int index = attributes.getIndex(name);
String value = this.findPath(name, attributes);
value = this.getPathRelativeToParent(this.options.getRelativeParentPath(), value);
value = this.replacePath(name, attributes, value);
attributes.setAttribute(index, "", name, name, "String", value);
log.debug("Attribute \"{}\" rewritten to: {}", (Object)name, (Object)value);
}
private String joinArray(String[] array, char separator, int startIndex) {
StringBuilder value = new StringBuilder();
for (int j = startIndex; j < array.length; ++j) {
value.append(array[j]);
if (j == array.length - 1) continue;
value.append(separator);
}
return value.toString();
}
private String findPath(String name, AttributesImpl attrs) {
String pattern;
Matcher m;
Pattern patt;
String attrValue = attrs.getValue(name);
if (StringUtils.isNotBlank((String)attrValue) && this.config.patternMapping.containsKey(name) && (m = (patt = Pattern.compile(pattern = this.config.patternMapping.get(name))).matcher(attrValue)).find()) {
if (m.groupCount() > 0) {
return m.group(1);
}
return m.group(0);
}
return attrValue;
}
private String replacePath(String name, AttributesImpl attrs, String newPath) {
if (this.config.patternMapping.containsKey(name)) {
String attrValue = attrs.getValue(name);
String pattern = this.config.patternMapping.get(name);
Pattern patt = Pattern.compile(pattern);
Matcher m = patt.matcher(attrValue);
StringBuffer sb = new StringBuffer(attrValue.length());
if (m.find()) {
if (m.groupCount() > 0) {
m.appendReplacement(sb, Matcher.quoteReplacement(m.group(0).replace(m.group(1), newPath)));
} else {
m.appendReplacement(sb, Matcher.quoteReplacement(newPath));
}
}
m.appendTail(sb);
return sb.toString();
}
return newPath;
}
}