protected String processCss(String previousOut, String serverUrl, HttpServletRequest request,
HttpServletResponse response) {
Source source = new Source(previousOut);
OutputDocument document = new OutputDocument(source);
StringBuilder sb = new StringBuilder();
List<StartTag> linkStartTags = source.getAllStartTags(HTMLElementName.LINK);
for (StartTag linkTag : linkStartTags) {
Attributes attributes = linkTag.getAttributes();
String rel = attributes.getValue("rel");
if (rel == null || !"stylesheet".equalsIgnoreCase(rel)) {
continue;
}
String href = attributes.getValue("href");
if (href == null) {
continue;
}
String styleSheetContent = null;
try {
if (useServletContextResources || request == null || response == null) {
if (request != null && StringUtils.startsWith(href, request.getContextPath())) {
href = StringUtils.substringAfter(href, request.getContextPath());
}
styleSheetContent = httpClientService.getResourceAsString(href);
} else {
styleSheetContent = httpClientService.getResourceAsString(href, request, response);
}
} catch (Exception e) {
logger.warn("Unable to retrieve resource content for " + href + ".Cause: " + e.getMessage(), e);
}
if (StringUtils.isNotEmpty(styleSheetContent)) {
sb.setLength(0);
sb.append("<style");
Attribute typeAttribute = attributes.get("type");
if (typeAttribute != null) {
sb.append(' ').append(typeAttribute);
}
if (rewriteUrlsInCss) {
String baseUrl = HttpClientService.isAbsoluteUrl(href) ? href : serverUrl + href;
baseUrl = StringUtils.substringBeforeLast(baseUrl, "/") + "/";
styleSheetContent = rewriteCssUrls(styleSheetContent, baseUrl);
}
sb.append(">\n");
if (request!=null && Boolean.valueOf(request.getParameter("debug"))) {
sb.append("/* ").append(href).append(" */\n");
}
sb.append(styleSheetContent)
.append("\n</style>");
document.replace(linkTag, sb.toString());
}
}
return document.toString();
}