Class PackageResource
- java.lang.Object
-
- org.apache.wicket.request.resource.AbstractResource
-
- org.apache.wicket.request.resource.PackageResource
-
- All Implemented Interfaces:
Serializable
,IStaticCacheableResource
,IResource
,IClusterable
- Direct Known Subclasses:
CssPackageResource
,JavaScriptPackageResource
public class PackageResource extends AbstractResource implements IStaticCacheableResource
Represents a localizable static resource.Use like eg:
MyPackageResource IMG_UNKNOWN = new MyPackageResource(EditPage.class, "questionmark.gif");
where the static resource references image 'questionmark.gif' from the the package that EditPage is in to get a package resource. Access to resources can be granted or denied via aIPackageResourceGuard
. Please seeResourceSettings.getPackageResourceGuard()
as well.- Author:
- Jonathan Locke, Eelco Hillenius, Juergen Donnerstag, Matej Knopp, Tobias Soloschenko
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
PackageResource.PackageResourceBlockedException
Exception thrown when the creation of a package resource is not allowed.-
Nested classes/interfaces inherited from class org.apache.wicket.request.resource.AbstractResource
AbstractResource.ContentRangeType, AbstractResource.ResourceResponse, AbstractResource.WriteCallback
-
Nested classes/interfaces inherited from interface org.apache.wicket.request.resource.IResource
IResource.Attributes
-
-
Field Summary
-
Fields inherited from class org.apache.wicket.request.resource.AbstractResource
CONTENT_DISPOSITION_HEADER_NAME, CONTENT_RANGE_ENDBYTE, CONTENT_RANGE_STARTBYTE, INTERNAL_HEADERS
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected boolean
accept(String path)
Checks whether access is granted for this resource.protected byte[]
compressResponse(IResource.Attributes attributes, byte[] original)
Compresses the response if its is eligible and there is a configured compressorboolean
equals(Object obj)
static boolean
exists(Class<?> scope, String path, Locale locale, String style, String variation)
Checks whether a resource for a given set of criteria exists.static boolean
exists(ResourceReference.Key key)
Checks whether a resource for a given set of criteria exists.Serializable
getCacheKey()
get unique caching key for the resource stream produced byIStaticCacheableResource.getResourceStream()
boolean
getCompress()
protected ITextResourceCompressor
getCompressor()
Gets theIJavaScriptCompressor
to be used.String
getName()
protected Charset
getProcessingEncoding()
IResourceStream
getResourceStream()
locate resource stream for current resourceClass<?>
getScope()
Gets the scoping class, used for class loading and to determine the package.String
getStyle()
Gets the style.String
getTextEncoding()
get text encoding (intented for character-based resources)int
hashCode()
boolean
isCachingEnabled()
Returns true if the caching for this resource is enabledprotected AbstractResource.ResourceResponse
newResourceResponse(IResource.Attributes attributes)
creates a new resource response based on the request attributesprotected byte[]
processResponse(IResource.Attributes attributes, byte[] original)
Gives a chance to modify the resource going to be written in the responsePackageResource
readBuffered(boolean readBuffered)
If the package resource should be read buffered.
WARNING - if the stream is not read buffered compressors will not work, because they require the whole content to be read into memory.
(IJavaScriptCompressor
,
ICssCompressor
,
IScopeAwareTextResourceProcessor
)void
setCachingEnabled(boolean enabled)
Sets the caching for this resource to be enabledvoid
setCompress(boolean compress)
void
setTextEncoding(String textEncoding)
set text encoding (intented for character-based resources)String
toString()
-
Methods inherited from class org.apache.wicket.request.resource.AbstractResource
configureCache, getCachingStrategy, respond, setRequestMetaData, setRequestRangeMetaData, setResponseContentRangeHeaderFields, setResponseHeaders
-
-
-
-
Constructor Detail
-
PackageResource
protected PackageResource(Class<?> scope, String name, Locale locale, String style, String variation)
Hidden constructor.- Parameters:
scope
- This argument will be used to get the class loader for loading the package resource, and to determine what package it is inname
- The relative path to the resourcelocale
- The locale of the resourcestyle
- The style of the resourcevariation
- The component's variation (of the style)
-
-
Method Detail
-
isCachingEnabled
public boolean isCachingEnabled()
Returns true if the caching for this resource is enabled- Specified by:
isCachingEnabled
in interfaceIStaticCacheableResource
- Returns:
- if the caching is enabled
-
setCachingEnabled
public void setCachingEnabled(boolean enabled)
Sets the caching for this resource to be enabled- Parameters:
enabled
- if the cacheing should be enabled
-
getTextEncoding
public String getTextEncoding()
get text encoding (intented for character-based resources)- Returns:
- custom encoding or
null
to use default
-
setTextEncoding
public void setTextEncoding(String textEncoding)
set text encoding (intented for character-based resources)- Parameters:
textEncoding
- custom encoding ornull
to use default
-
getCacheKey
public Serializable getCacheKey()
Description copied from interface:IStaticCacheableResource
get unique caching key for the resource stream produced byIStaticCacheableResource.getResourceStream()
- Specified by:
getCacheKey
in interfaceIStaticCacheableResource
- Returns:
- serializable key which properly supports
Object.equals(Object)
andObject.hashCode()
-
getScope
public final Class<?> getScope()
Gets the scoping class, used for class loading and to determine the package.- Returns:
- the scoping class
-
newResourceResponse
protected AbstractResource.ResourceResponse newResourceResponse(IResource.Attributes attributes)
creates a new resource response based on the request attributes- Specified by:
newResourceResponse
in classAbstractResource
- Parameters:
attributes
- current request attributes from client- Returns:
- resource response for answering request
-
processResponse
protected byte[] processResponse(IResource.Attributes attributes, byte[] original)
Gives a chance to modify the resource going to be written in the response- Parameters:
attributes
- current request attributes from clientoriginal
- the original response- Returns:
- the processed response
-
compressResponse
protected byte[] compressResponse(IResource.Attributes attributes, byte[] original)
Compresses the response if its is eligible and there is a configured compressor- Parameters:
attributes
- * current request attributes from client * @param original * the original response * @return the compressed response
-
getProcessingEncoding
protected Charset getProcessingEncoding()
- Returns:
- The charset to use to read the resource
-
getCompressor
protected ITextResourceCompressor getCompressor()
Gets theIJavaScriptCompressor
to be used. By default returns the configured compressor on application level, but can be overriden by the user application to provide compressor specific to the resource.- Returns:
- the configured application level JavaScript compressor. May be
null
.
-
getResourceStream
public IResourceStream getResourceStream()
locate resource stream for current resource- Specified by:
getResourceStream
in interfaceIStaticCacheableResource
- Returns:
- resource stream or
null
if not found
-
getCompress
public boolean getCompress()
- Returns:
- whether
ITextResourceCompressor
can be used to compress the resource.
-
setCompress
public void setCompress(boolean compress)
- Parameters:
compress
- A flag indicating whether the resource should be compressed.
-
accept
protected boolean accept(String path)
Checks whether access is granted for this resource. By default IPackageResourceGuard is used to check the permissions but the resource itself can also make the check.- Parameters:
path
- resource path- Returns:
true
if resource access is granted
-
exists
public static boolean exists(ResourceReference.Key key)
Checks whether a resource for a given set of criteria exists.- Parameters:
key
- The key that contains all attributes about the requested resource- Returns:
true
if there is a package resource with the given attributes
-
exists
public static boolean exists(Class<?> scope, String path, Locale locale, String style, String variation)
Checks whether a resource for a given set of criteria exists.- Parameters:
scope
- This argument will be used to get the class loader for loading the package resource, and to determine what package it is in. Typically this is the class in which you call this methodpath
- The path to the resourcelocale
- The locale of the resourcestyle
- The style of the resource (seeSession
)variation
- The component's variation (of the style)- Returns:
true
if a resource could be loaded,false
otherwise
-
readBuffered
public PackageResource readBuffered(boolean readBuffered)
If the package resource should be read buffered.
WARNING - if the stream is not read buffered compressors will not work, because they require the whole content to be read into memory.
(IJavaScriptCompressor
,
ICssCompressor
,
IScopeAwareTextResourceProcessor
)- Parameters:
readBuffered
- if the package resource should be read buffered- Returns:
- the current package resource
-
-