Utils.java
5.64 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.jcr.Binary
* javax.jcr.Node
* javax.jcr.Property
* javax.jcr.Session
* org.apache.jackrabbit.util.Base64
* org.apache.jackrabbit.util.Text
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.granite.ui.clientlibs.script;
import java.io.InputStream;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.jcr.Binary;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.Session;
import org.apache.jackrabbit.util.Base64;
import org.apache.jackrabbit.util.Text;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public final class Utils {
private static final Logger log = LoggerFactory.getLogger(Utils.class);
private static Pattern SCHEME_START = Pattern.compile("^[^:/]+:[^/]*/.*");
private static Pattern URL_PATTERN = Pattern.compile("url\\(\\s*(['\"]?)([^'\")]*)(['\"]?\\s*)\\)");
private Utils() {
}
public static String rewriteUrlsInCss(String libPath, String filePath, String css) {
return Utils.rewriteUrlsInCss(Text.explode((String)libPath, (int)47), Text.explode((String)filePath, (int)47), css, null, 0);
}
public static String rewriteUrlsInCss(String libPath, String filePath, String css, Session session, long maxDataUriSize) {
return Utils.rewriteUrlsInCss(Text.explode((String)libPath, (int)47), Text.explode((String)filePath, (int)47), css, session, maxDataUriSize);
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public static String rewriteUrlsInCss(String[] libPathSegs, String[] filePathSegs, String css, Session session, long maxDataUriSize) {
Matcher m = URL_PATTERN.matcher(css);
StringBuffer result = new StringBuffer();
while (m.find()) {
String url;
url = m.group(2);
if (url.startsWith("absolute:")) {
url = url.substring(9);
} else if (!url.endsWith(".htc") && !url.startsWith("//")) {
url = Utils.resolveUrl(libPathSegs, filePathSegs, url);
if (maxDataUriSize > 0 && session != null && !SCHEME_START.matcher(url).matches()) {
Binary bin = null;
String path = "/" + Text.implode((String[])libPathSegs, (String)"/") + "/../" + url + "/jcr:content/jcr:data";
try {
Property p;
if (session.propertyExists(path) && (p = session.getProperty(path)).getLength() < maxDataUriSize) {
bin = p.getBinary();
StringWriter w = new StringWriter();
Base64.encode((InputStream)bin.getStream(), (Writer)w);
url = "data:" + p.getParent().getProperty("{http://www.jcp.org/jcr/1.0}mimeType").getString() + ";base64," + w.toString();
}
}
catch (Exception e) {
log.warn("Error while encoding data uri of {}: {}", (Object)path, (Object)e.toString());
}
finally {
if (bin != null) {
bin.dispose();
}
}
}
}
m.appendReplacement(result, "url($1" + url + "$3)");
}
m.appendTail(result);
return result.toString();
}
public static String resolveUrl(String libPath, String filePath, String url) {
return Utils.resolveUrl(Text.explode((String)libPath, (int)47), Text.explode((String)filePath, (int)47), url);
}
private static String resolveUrl(String[] libPath, String[] filePath, String url) {
int i;
if (url.length() == 0 || SCHEME_START.matcher(url).matches()) {
if (log.isDebugEnabled()) {
log.debug("resolving lib=/{}, file=/{}, url={} (ignored)", new Object[]{Text.implode((String[])libPath, (String)"/"), Text.implode((String[])filePath, (String)"/"), url});
}
return url;
}
LinkedList<String> file = new LinkedList<String>();
if (!url.startsWith("/")) {
file.addAll(Arrays.asList(filePath));
file.removeLast();
}
boolean warned = false;
for (String seg : Text.explode((String)url, (int)47)) {
if ("..".equals(seg)) {
if (file.isEmpty()) {
if (warned) continue;
log.warn("/{}: url('{}') invalid. too many '..'", (Object)Text.implode((String[])filePath, (String)"/"), (Object)url);
warned = true;
continue;
}
file.removeLast();
continue;
}
if (".".equals(seg)) continue;
file.add(seg);
}
for (i = 0; i < libPath.length - 1 && libPath[i].matches((String)file.getFirst()); ++i) {
file.removeFirst();
}
while (i++ < libPath.length - 1) {
file.addFirst("..");
}
StringBuilder ret = new StringBuilder();
String delim = "";
for (String seg2 : file) {
ret.append(delim).append(seg2);
delim = "/";
}
if (log.isDebugEnabled()) {
log.debug("resolving lib=/{}, file=/{}, url={} -> {}", new Object[]{Text.implode((String[])libPath, (String)"/"), Text.implode((String[])filePath, (String)"/"), url, ret});
}
return ret.toString();
}
}