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

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 a IPackageResourceGuard. Please see ResourceSettings.getPackageResourceGuard() as well.
Author:
Jonathan Locke, Eelco Hillenius, Juergen Donnerstag, Matej Knopp, Tobias Soloschenko
See Also:
  • Constructor Details

    • 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 in
      name - The relative path to the resource
      locale - The locale of the resource
      style - The style of the resource
      variation - The component's variation (of the style)
  • Method Details

    • isCachingEnabled

      public boolean isCachingEnabled()
      Returns true if the caching for this resource is enabled
      Specified by:
      isCachingEnabled in interface IStaticCacheableResource
      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

      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 or null to use default
    • getCacheKey

      Description copied from interface: IStaticCacheableResource
      get unique caching key for the resource stream produced by IStaticCacheableResource.getResourceStream()
      Specified by:
      getCacheKey in interface IStaticCacheableResource
      Returns:
      serializable key which properly supports Object.equals(Object) and Object.hashCode()
    • getScope

      public final Class<?> getScope()
      Gets the scoping class, used for class loading and to determine the package.
      Returns:
      the scoping class
    • getName

      public final String getName()
    • getStyle

      public final String getStyle()
      Gets the style.
      Returns:
      the style
    • newResourceResponse

      creates a new resource response based on the request attributes
      Specified by:
      newResourceResponse in class AbstractResource
      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 client
      original - 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

      Returns:
      The charset to use to read the resource
    • getCompressor

      Gets the IJavaScriptCompressor 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

      locate resource stream for current resource
      Specified by:
      getResourceStream in interface IStaticCacheableResource
      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 method
      path - The path to the resource
      locale - The locale of the resource
      style - The style of the resource (see Session)
      variation - The component's variation (of the style)
      Returns:
      true if a resource could be loaded, false otherwise
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class 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