Class PackageResource
- java.lang.Object
-
- org.apache.wicket.request.resource.AbstractResource
-
- org.apache.wicket.request.resource.PackageResource
-
- All Implemented Interfaces:
java.io.Serializable,IStaticCacheableResource,IResource,org.apache.wicket.util.io.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 classPackageResource.PackageResourceBlockedExceptionException 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
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedPackageResource(java.lang.Class<?> scope, java.lang.String name, java.util.Locale locale, java.lang.String style, java.lang.String variation)Hidden constructor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected booleanaccept(java.lang.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 compressorbooleanequals(java.lang.Object obj)static booleanexists(java.lang.Class<?> scope, java.lang.String path, java.util.Locale locale, java.lang.String style, java.lang.String variation)Checks whether a resource for a given set of criteria exists.static booleanexists(ResourceReference.Key key)Checks whether a resource for a given set of criteria exists.java.io.SerializablegetCacheKey()get unique caching key for the resource stream produced byIStaticCacheableResource.getResourceStream()booleangetCompress()protected ITextResourceCompressorgetCompressor()Gets theIJavaScriptCompressorto be used.java.lang.StringgetName()protected java.nio.charset.CharsetgetProcessingEncoding()org.apache.wicket.util.resource.IResourceStreamgetResourceStream()locate resource stream for current resourcejava.lang.Class<?>getScope()Gets the scoping class, used for class loading and to determine the package.java.lang.StringgetStyle()Gets the style.java.lang.StringgetTextEncoding()get text encoding (intented for character-based resources)inthashCode()booleanisCachingEnabled()Returns true if the caching for this resource is enabledprotected AbstractResource.ResourceResponsenewResourceResponse(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 responsePackageResourcereadBuffered(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)voidsetCachingEnabled(boolean enabled)Sets the caching for this resource to be enabledvoidsetCompress(boolean compress)voidsetTextEncoding(java.lang.String textEncoding)set text encoding (intented for character-based resources)java.lang.StringtoString()-
Methods inherited from class org.apache.wicket.request.resource.AbstractResource
configureCache, getCachingStrategy, respond, setRequestMetaData, setRequestRangeMetaData, setResponseContentRangeHeaderFields, setResponseHeaders
-
-
-
-
Constructor Detail
-
PackageResource
protected PackageResource(java.lang.Class<?> scope, java.lang.String name, java.util.Locale locale, java.lang.String style, java.lang.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:
isCachingEnabledin 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 java.lang.String getTextEncoding()
get text encoding (intented for character-based resources)- Returns:
- custom encoding or
nullto use default
-
setTextEncoding
public void setTextEncoding(java.lang.String textEncoding)
set text encoding (intented for character-based resources)- Parameters:
textEncoding- custom encoding ornullto use default
-
getCacheKey
public java.io.Serializable getCacheKey()
Description copied from interface:IStaticCacheableResourceget unique caching key for the resource stream produced byIStaticCacheableResource.getResourceStream()- Specified by:
getCacheKeyin interfaceIStaticCacheableResource- Returns:
- serializable key with properly supports
#equals(Object)and#hashCode()
-
getScope
public final java.lang.Class<?> getScope()
Gets the scoping class, used for class loading and to determine the package.- Returns:
- the scoping class
-
getName
public final java.lang.String getName()
-
getStyle
public final java.lang.String getStyle()
Gets the style.- Returns:
- the style
-
newResourceResponse
protected AbstractResource.ResourceResponse newResourceResponse(IResource.Attributes attributes)
creates a new resource response based on the request attributes- Specified by:
newResourceResponsein 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 java.nio.charset.Charset getProcessingEncoding()
- Returns:
- The charset to use to read the resource
-
getCompressor
protected ITextResourceCompressor getCompressor()
Gets theIJavaScriptCompressorto 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 org.apache.wicket.util.resource.IResourceStream getResourceStream()
locate resource stream for current resource- Specified by:
getResourceStreamin interfaceIStaticCacheableResource- Returns:
- resource stream or
nullif not found
-
getCompress
public boolean getCompress()
- Returns:
- whether
ITextResourceCompressorcan 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(java.lang.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:
trueif 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:
trueif there is a package resource with the given attributes
-
exists
public static boolean exists(java.lang.Class<?> scope, java.lang.String path, java.util.Locale locale, java.lang.String style, java.lang.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:
trueif a resource could be loaded,falseotherwise
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
equals
public boolean equals(java.lang.Object obj)
- Overrides:
equalsin classjava.lang.Object
-
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
-
-