public class CssImageDataHandler extends Object implements CacheReporter
This class provides the capability to search through CSS resources and convert all URL references to be Base64 encoded data. This
can have performance benefits since as CSS files that include data rather than links will reduce the number of HTTP requests that the
browser needs to make. Processing CSS resources in this way will only be enabled if the Surf application configuration sets the
property <generate-css-data-images> to true (however, is will only have an effect if the property
use-checksum-dependencies> is also set to true.
| Modifier and Type | Field and Description |
|---|---|
static String |
DATA_BASE64_ENCODING
This is the second part of the CSS data image text.
|
static String |
DATA_IMAGE_PREFIX
This is the first part of the CSS data image text.
|
static String |
DOUBLE_FULL_STOP
Constant for double full stop ".." (or period as it is known in the US).
|
static String |
DOUBLE_FULL_STOP_SLASH |
static String |
DOUBLE_QUOTES
Constant for the double quote '"'.
|
static String |
FORWARD_SLASH
Constant for the forward slash "/"
|
static String |
FULL_STOP
Constant for the full stop "." (or period as it is known in the US).
|
static String |
MISSING_IMAGE_SENTINENEL
The empty String is used as the missing image sentinel.
|
static String |
SINGLE_QUOTE
Constant for the single quote "'"
|
protected ThreadLocal<byte[]> |
streamBuffer
Thread local stream byte buffer.
|
static String |
URL_CLOSE_TARGET_PATTERN
This is the String used to target the end of an image URL.
|
static String |
URL_OPEN_TARGET_PATTERN
This is the String used to target the start of image URL.
|
| Constructor and Description |
|---|
CssImageDataHandler() |
| Modifier and Type | Method and Description |
|---|---|
void |
clearCaches() |
protected String |
encodeImage(InputStream in)
Converts the image file in the supplied
InputStream into a Base64 encoded String. |
DependencyHandler |
getDependencyHandler() |
String |
getEncodedImage(String path)
Gets the encoded image for the supplied path.
|
List<String> |
getExcludeCssPaths() |
List<String> |
getExcludeMimeTypes() |
protected List<Pattern> |
getExcludePatterns() |
Integer |
getMaximumImageSize()
Gets the maximum image size to allow to be encoded
|
Map<String,String> |
getRemapMimetypes() |
int |
insert(StringBuilder toUpdate,
int offset,
String toInsert)
Helper method for inserting content into a
StringBuilder whilst maintaining a reference
to the offset that indicates the end of the inserted content. |
void |
processCssImages(String cssPath,
StringBuilder cssContents)
Updates the supplied
StringBuilder (which should contain the contents of a CSS file) so that
all image URLs are converted into Base64 encoded data. |
List<CacheReport> |
report() |
void |
setDependencyHandler(DependencyHandler dependencyHandler)
Setter provided to allow the Spring application context to set the
DependencyHandler. |
void |
setExcludeCssPaths(List<String> excludeCssPaths) |
void |
setExcludeMimeTypes(List<String> excludeMimeTypes) |
void |
setMaximumImageSize(Integer maximumImageSize)
This setter is provided to allow the Spring application context to set the maximum size of image to encode (in bytes).
|
void |
setRemapMimetypes(Map<String,String> remapMimetypes) |
public static final String DATA_IMAGE_PREFIX
This is the first part of the CSS data image text. This is the text that runs up until the MIME type of the image.
public static final String DATA_BASE64_ENCODING
This is the second part of the CSS data image text. It runs from the MIME type to Base64 encoded data.
public static final String URL_OPEN_TARGET_PATTERN
This is the String used to target the start of image URL. NOTE: At the moment this assumes URL only applies to "background-image" which is technically not guaranteed in the specification.
public static final String URL_CLOSE_TARGET_PATTERN
This is the String used to target the end of an image URL. The first occurrence of the closing bracket after the opening bracket should mark the end of the URL because it is not valid as part of a URL.
public static final String FORWARD_SLASH
Constant for the forward slash "/"
public static final String FULL_STOP
Constant for the full stop "." (or period as it is known in the US). In this context it is used to indicate the current location in paths.
public static final String DOUBLE_FULL_STOP
Constant for double full stop ".." (or period as it is known in the US). In this context it is used to indicate the part folder of the current location.
public static final String DOUBLE_QUOTES
Constant for the double quote '"'.
public static final String SINGLE_QUOTE
Constant for the single quote "'"
public static final String DOUBLE_FULL_STOP_SLASH
public static final String MISSING_IMAGE_SENTINENEL
The empty String is used as the missing image sentinel. We cannot use null as this will indicate that that the encoded image does not exist in the cache. However the empty String can be used as no image will ever be encoded to the empty String as it MUST contain some data.
protected ThreadLocal<byte[]> streamBuffer
Thread local stream byte buffer.
public Integer getMaximumImageSize()
Gets the maximum image size to allow to be encoded
public void setMaximumImageSize(Integer maximumImageSize)
This setter is provided to allow the Spring application context to set the maximum size of image to encode (in bytes).
maximumImageSize - The maximum size of an image to encode (in bytes)public DependencyHandler getDependencyHandler()
public void setDependencyHandler(DependencyHandler dependencyHandler)
Setter provided to allow the Spring application context to set the DependencyHandler.
dependencyHandler - DependencyHandlerpublic void processCssImages(String cssPath, StringBuilder cssContents) throws IOException
Updates the supplied StringBuilder (which should contain the contents of a CSS file) so that
all image URLs are converted into Base64 encoded data. This method uses the DependencyHandler to
load the image file and if the image cannot be loaded then it will not be encoded and will be left as
the original URL.
cssPath - The path the CSS file.cssContents - The contents of the CSS file.IOExceptionpublic int insert(StringBuilder toUpdate, int offset, String toInsert)
Helper method for inserting content into a StringBuilder whilst maintaining a reference
to the offset that indicates the end of the inserted content.
toUpdate - The StringBuilder to update.offset - The current offset (e.g. where to insert the supplied String)toInsert - The String to insert.StringBuilder.public String getEncodedImage(String path)
Gets the encoded image for the supplied path. This method will first check the private cache to see
if the image has previously been encoded and if it cannot be found then it will attempt to load the
image and encode it. If the image resource could not be loaded then this method will return null
path - The path to the image to get the encoded String representation.null if the image could not be loaded.protected String encodeImage(InputStream in) throws IOException
Converts the image file in the supplied InputStream into a Base64 encoded String.
in - An InputStream of the image to convert.IOExceptionpublic void clearCaches()
clearCaches in interface CacheReporterpublic List<CacheReport> report()
report in interface CacheReporterCopyright © 2005–2016 Alfresco Software. All rights reserved.