forked from alexo/wro4j
/
CssUrlRewritingProcessor.java
260 lines (247 loc) · 8.81 KB
/
CssUrlRewritingProcessor.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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
* Copyright (c) 2008. All rights reserved.
*/
package ro.isdc.wro.model.resource.processor.impl.css;
import static ro.isdc.wro.util.StringUtils.cleanPath;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ro.isdc.wro.WroRuntimeException;
import ro.isdc.wro.config.Context;
import ro.isdc.wro.model.group.Inject;
import ro.isdc.wro.model.resource.locator.ClasspathUriLocator;
import ro.isdc.wro.model.resource.locator.ServletContextUriLocator;
import ro.isdc.wro.model.resource.locator.UriLocator;
import ro.isdc.wro.model.resource.locator.UrlUriLocator;
/**
* Note: When used together with {@link CssImportPreProcessor}, the {@link CssUrlRewritingProcessor} should come first,
* otherwise it will produce wrong results.
* <p>
* Rewrites background images url of the provided css content. This implementation takes care of most common cases such
* as those described bellow:
* <p>
* <table border="1" cellpadding="5">
* <thead>
* <tr>
* <th>Css resource URI</th>
* <th>Image URL</th>
* <th>Computed Image URL</th>
* </tr>
* </thead> <tbody>
* <tr>
* <td>ANY</td>
* <td>[URL]/1.jpg</td>
* <td>[URL]/1.jpg</td>
* </tr>
* <tr>
* <td rowspan="4">/1.css</td>
* <td>/a/1.jpg</td>
* <td>/a/1.jpg</td>
* </tr>
* <tr>
* <td>/1.jpg</td>
* <td>/1.jpg</td>
* </tr>
* <tr>
* <td>1.jpg</td>
* <td>../1.jpg</td>
* </tr>
* <tr>
* <td>../1.jpg</td>
* <td>../../1.jpg</td>
* </tr>
* <tr>
* <td rowspan="4">/WEB-INF/1.css</td>
* <td>/a/1.jpg</td>
* <td>/a/1.jpg</td>
* </tr>
* <tr>
* <td>/1.jpg</td>
* <td>/1.jpg</td>
* </tr>
* <tr>
* <td>1.jpg</td>
* <td>[WRO-PREFIX]?id=/WEB-INF/1.jpg</td>
* </tr>
* <tr>
* <td>../1.jpg</td>
* <td>[WRO-PREFIX]?id=/WEB-INF/../1.jpg</td>
* </tr>
* <tr>
* <td rowspan="4">[X]/1.css <br/>
* <br/>
* where [X] is URL or a classpath resource<br/>
* where [WRO-PREFIX] is a servletContext prefix <br/>
* which will map WRO filter to the result url.</td>
* <td>/a/1.jpg</td>
* <td>[WRO-PREFIX]?id=[X]/a/1.jpg</td>
* </tr>
* <tr>
* <td>/1.jpg</td>
* <td>[WRO-PREFIX]?id=[X]/1.jpg</td>
* </tr>
* <tr>
* <td>1.jpg</td>
* <td>[WRO-PREFIX]?id=[X]/1.jpg</td>
* </tr>
* <tr>
* <td>../1.jpg</td>
* <td>[WRO-PREFIX]?id=[X]/../1.jpg</td>
* </tr>
* </tbody>
* </table>
* <p/>
* The algorithm requires two types of {@link UriLocator} objects, one for resolving url resources & one for classpath
* resources. Both need to be injected using IoC when creating the instance of {@link CssUrlRewritingProcessor} class.
*
* @author Alex Objelean
* @created Nov 19, 2008
*/
public class CssUrlRewritingProcessor
extends AbstractCssUrlRewritingProcessor {
private static final Logger LOG = LoggerFactory.getLogger(CssUrlRewritingProcessor.class);
public static final String ALIAS = "cssUrlRewriting";
/**
* A set of allowed url's.
*/
private final Set<String> allowedUrls = Collections.synchronizedSet(new HashSet<String>());
/**
* Prefix of the path to the overwritten image url. This will be of the following type: "../" or "../.." depending on
* the depth of the aggregatedFolderPath.
*/
private String aggregatedPathPrefix;
@Inject
private Context context;
/**
* The folder where the final css is located. This is important for computing image location after url rewriting.
*
* @param aggregatedFolderPath
* the aggregatedFolder to set
*/
private CssUrlRewritingProcessor setAggregatedFolderPath(final String aggregatedFolderPath) {
aggregatedPathPrefix = computeAggregationPathPrefix(aggregatedFolderPath);
LOG.debug("computed aggregatedPathPrefix {}", aggregatedPathPrefix);
return this;
}
/**
* {@inheritDoc}
*/
@Override
protected void onProcessCompleted() {
LOG.debug("allowed urls: {}", allowedUrls);
}
/**
* {@inheritDoc}
*/
@Override
protected void onUrlReplaced(final String replacedUrl) {
final String allowedUrl = StringUtils.removeStart(replacedUrl, getUrlPrefix());
LOG.debug("adding allowed url: {}", allowedUrl);
allowedUrls.add(allowedUrl);
}
/**
* Replace provided url with the new url if needed.
*
* @param cssUri
* Uri of the parsed css.
* @param imageUrl
* to replace.
* @return replaced url.
*/
@Override
protected String replaceImageUrl(final String cssUri, final String imageUrl) {
if (ServletContextUriLocator.isValid(cssUri)) {
if (ServletContextUriLocator.isValid(imageUrl)) {
return imageUrl;
}
// Treat WEB-INF special case
if (ServletContextUriLocator.isProtectedResource(cssUri)) {
return getUrlPrefix() + computeNewImageLocation(cssUri, imageUrl);
}
// ensure the folder path is set
setAggregatedFolderPath(context.getAggregatedFolderPath());
LOG.debug("aggregatedPathPrefix: {}", this.aggregatedPathPrefix);
return computeNewImageLocation(this.aggregatedPathPrefix + cssUri, imageUrl);
}
if (UrlUriLocator.isValid(cssUri)) {
return computeNewImageLocation(cssUri, imageUrl);
}
if (ClasspathUriLocator.isValid(cssUri)) {
return getUrlPrefix() + computeNewImageLocation(cssUri, imageUrl);
}
throw new WroRuntimeException("Could not replace imageUrl: " + imageUrl + ", contained at location: " + cssUri);
}
/**
* @return the path to be prefixed after css aggregation. This depends on the aggregated css destination folder. This
* is a fix for the following issue: {@link http://code.google.com/p/wro4j/issues/detail?id=259}
*/
private String computeAggregationPathPrefix(final String aggregatedFolderPath) {
LOG.debug("aggregatedFolderPath: {}", aggregatedFolderPath);
String computedPrefix = StringUtils.EMPTY;
if (aggregatedFolderPath != null) {
final String folderPrefix = "/..";
final StringBuffer result = new StringBuffer("");
final String[] depthFolders = aggregatedFolderPath.split("/");
LOG.debug("subfolders {}", Arrays.toString(depthFolders));
for (final String folder : depthFolders) {
if (!StringUtils.isEmpty(folder)) {
result.append(folderPrefix);
}
}
computedPrefix = result.toString().replaceFirst("/", "");
}
LOG.debug("computedPrefix: {}", computedPrefix);
return computedPrefix;
}
/**
* Concatenates cssUri and imageUrl after few changes are applied to both input parameters.
*
* @param cssUri
* the URI of css resource.
* @param imageUrl
* the URL of image referred in css.
* @return processed new location of image url.
*/
private String computeNewImageLocation(final String cssUri, final String imageUrl) {
LOG.debug("cssUri: {}, imageUrl {}", cssUri, imageUrl);
final String cleanImageUrl = cleanImageUrl(imageUrl);
// TODO move to ServletContextUriLocator as a helper method?
// for the following input: /a/b/c/1.css => /a/b/c/
int idxLastSeparator = cssUri.lastIndexOf(ServletContextUriLocator.PREFIX);
if (idxLastSeparator == -1) {
if (ClasspathUriLocator.isValid(cssUri)) {
idxLastSeparator = cssUri.lastIndexOf(ClasspathUriLocator.PREFIX);
// find the index of ':' character used by classpath prefix
if (idxLastSeparator >= 0) {
idxLastSeparator += ClasspathUriLocator.PREFIX.length() - 1;
}
}
if (idxLastSeparator < 0) {
throw new IllegalStateException("Invalid cssUri: " + cssUri + ". Should contain at least one '/' character!");
}
}
final String cssUriFolder = cssUri.substring(0, idxLastSeparator + 1);
// remove '/' from imageUrl if it starts with one.
final String processedImageUrl = cleanImageUrl.startsWith(ServletContextUriLocator.PREFIX) ? cleanImageUrl.substring(1)
: cleanImageUrl;
// remove redundant part of the path, but skip protected resources (ex: located inside /WEB-INF/ folder). -> Not
// sure if this is a problem yet.
// final String computedImageLocation = ServletContextUriLocator.isProtectedResource(cssUriFolder) ? cssUriFolder
// + processedImageUrl : cleanPath(cssUriFolder + processedImageUrl);
final String computedImageLocation = cleanPath(cssUriFolder + processedImageUrl);
LOG.debug("computedImageLocation: {}", computedImageLocation);
return computedImageLocation;
}
/**
* @param uri
* to check if is allowed.
* @return true if passed argument is contained in allowed list.
*/
public final boolean isUriAllowed(final String uri) {
return allowedUrls.contains(uri);
}
}