/** * @param cssFilePath - path of the cssFile * @param contextPath - web app context path or custom context path * @param inputStream - input stream * @param outputStream - output stream * @throws java.io.IOException - throws exception in case something woes wrong (IO read/write) */ private void processCSS( String contextPath, String cssFilePath, InputStream inputStream, OutputStream outputStream) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); String line; StringBuffer buffer = new StringBuffer(); while ((line = bufferedReader.readLine()) != null) { buffer.setLength(0); buffer.append(line); line = this.processCSSLine(context, contextPath, cssFilePath, buffer); outputStream.write((line + "\n").getBytes()); } }
/** * @param context - ServletContext * @param contextPath - context path or custom configured context path * @param cssFilePath - css file path * @param line - one single line in a css file * @return processed string with appropriate replacement of image URLs if any */ private String processCSSLine( ServletContext context, String contextPath, String cssFilePath, StringBuffer line) { Matcher matcher = CSS_IMG_URL_PATTERN.matcher(line); String cssRealPath = context.getRealPath(cssFilePath); while (matcher.find()) { String refImgPath = matcher.group(1); if (!Utils.isProtocolURL(refImgPath)) { // ignore absolute protocol paths String resolvedImgPath = refImgPath; if (!refImgPath.startsWith("/")) { resolvedImgPath = Utils.buildProperPath(Utils.getParentPath(cssFilePath), refImgPath); } String imgRealPath = context.getRealPath(resolvedImgPath); String fingerPrint = Utils.buildETagForResource(resolvedImgPath, context); int offset = line.indexOf(refImgPath); line.replace( offset, // from offset + refImgPath.length(), // to contextPath + Utils.addFingerPrint(fingerPrint, resolvedImgPath)); Utils.updateReferenceMap(cssRealPath, imgRealPath); } } return line.toString(); }