Package com.day.cq.commons
Class DownloadResource
- java.lang.Object
-
- org.apache.sling.api.resource.ResourceWrapper
-
- com.day.cq.commons.DownloadResource
-
- Direct Known Subclasses:
Download
,ImageResource
public class DownloadResource extends ResourceWrapper
Provides convenience methods for rendering download paragraphs.
-
-
Field Summary
Fields Modifier and Type Field Description static String
NN_FILE
name of the file node.static String
PN_DESCRIPTION
name of the description propertystatic String
PN_FILE_NAME
name of the 'file name' property.static String
PN_REFERENCE
name of the file reference property.static String
PN_TITLE
name of the title property.-
Fields inherited from interface org.apache.sling.api.resource.Resource
RESOURCE_TYPE_NON_EXISTING
-
-
Constructor Summary
Constructors Constructor Description DownloadResource(Resource resource)
Creates a new download based on the given resource.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
addAttribute(String name, String value)
Adds a tag attribute to this download.void
addCssClass(String name)
Adds a CSS class name to the respective attribute.void
addQueryParam(String name, String value)
Adds a query param to the 'query' attributevoid
draw(Writer w)
Writes this download as link tag to the given writerString
get(String name)
Get a property either from the overlaid map or the underlying properties.int
get(String name, int defaultValue)
Get a property and convert it to an integer.Map<String,String>
getAttributes()
Returns a map of attributes.Property
getData()
Returns the property that contains the binary data of this download.String
getDescription()
Returns the image description as defined by getItemName(PN_DESCRIPTION) or overridden bysetDescription(String)
.String
getDescription(boolean escape)
Returns the image description as defined by 'getItemName(PN_DESCRIPTION)' or overridden bysetDescription(String)
.String
getExtension()
Returns the extension.String
getFileName()
Returns the file name of this download as defined by the property with the name from 'getItemName(PN_FILE_NAME).String
getFileNodePath()
Returns the file path.String
getFileReference()
Returns the file reference.String
getHref()
Returns the href attribute of this download.String
getIconPath()
Deprecated.since 5.4 please use css classes for the icon, like "icon_xls.gif"String
getIconType()
Returns the icon type of this file.Object
getInnerHtml()
Returns the inner html object for the download link.String
getItemName(String name)
Returns the name of the given item which is either the default or can be redefined bysetItemName(String, String)
.Calendar
getLastModified()
Returns the last modified of this image.String
getMimeType()
Returns the mime type of this image.String
getQuery()
Gets the query attributeString
getSelector()
Returns the selector string.String
getString()
Returns a string representation as HTML tag of this image.String
getSuffix()
Returns the suffix.String
getTitle()
Returns the image title as defined by 'getItemName(PN_TITLE)' or overridden bysetTitle(String)
.String
getTitle(boolean escape)
Returns the image title as defined by 'getItemName(PN_TITLE)' or overridden bysetTitle(String)
.boolean
hasContent()
Checks if this download has content.void
init()
Calculates all default values if 'source' isnull
boolean
isTouchAuthoringUIMode()
Returns if page is in UI Touch mode?void
set(String name, String value)
Set a property to the overlaid map.void
setDescription(String description)
Sets the description.void
setExtension(String extension)
Sets the extension.void
setFileName(String fileName)
Sets the file name.void
setFileNodePath(String fileNodePath)
Sets the path to the file node.void
setFileReference(String fileReference)
Sets the file reference.void
setHref(String href)
Sets the href attributevoid
setInnerHtml(Object innerHtml)
Sets the inner html object for the download.void
setIsInUITouchMode(boolean inUITouchMode)
Page is in UI Touch mode?void
setItemName(String key, String name)
Defines the name of an item.void
setQuery(String query)
Sets the query attribute, overwrites any previously generated queries.void
setSelector(String selector)
Sets the selector string.void
setSuffix(String suffix)
Sets the suffix.void
setTitle(String title)
Sets the title.-
Methods inherited from class org.apache.sling.api.resource.ResourceWrapper
adaptTo, getChild, getChildren, getName, getParent, getPath, getResource, getResourceMetadata, getResourceResolver, getResourceSuperType, getResourceType, getValueMap, hasChildren, isResourceType, listChildren, toString
-
-
-
-
Field Detail
-
PN_REFERENCE
public static final String PN_REFERENCE
name of the file reference property. this can hold a path to a file or resource node, to a binary property or a uuid to a resource node.- See Also:
- Constant Field Values
-
NN_FILE
public static final String NN_FILE
name of the file node.- See Also:
- Constant Field Values
-
PN_FILE_NAME
public static final String PN_FILE_NAME
name of the 'file name' property.- See Also:
- Constant Field Values
-
PN_TITLE
public static final String PN_TITLE
name of the title property.- See Also:
- Constant Field Values
-
PN_DESCRIPTION
public static final String PN_DESCRIPTION
name of the description property- See Also:
- Constant Field Values
-
-
Constructor Detail
-
DownloadResource
public DownloadResource(Resource resource)
Creates a new download based on the given resource. the file properties are considered to be 'on' the given resource.- Parameters:
resource
- resource of the image- Throws:
IllegalArgumentException
- if the given resource is not adaptable to node.
-
-
Method Detail
-
isTouchAuthoringUIMode
public boolean isTouchAuthoringUIMode()
Returns if page is in UI Touch mode?- Returns:
- if page is in UI Touch mode
-
setIsInUITouchMode
public void setIsInUITouchMode(boolean inUITouchMode)
Page is in UI Touch mode?- Parameters:
inUITouchMode
- value to set
-
get
public String get(String name)
Get a property either from the overlaid map or the underlying properties.- Parameters:
name
- name of the property- Returns:
- string value of the property or an empty string
-
get
public int get(String name, int defaultValue)
Get a property and convert it to an integer. If any exception occurs, return the default value.- Parameters:
name
- name of the propertydefaultValue
- default value- Returns:
- integer value
-
set
public void set(String name, String value)
Set a property to the overlaid map.- Parameters:
name
- name of the propertyvalue
- value of the property
-
addAttribute
public void addAttribute(String name, String value)
Adds a tag attribute to this download. The attributes are included whendrawing
the tag.- Parameters:
name
- name of the attributevalue
- value of the attribute
-
addCssClass
public void addCssClass(String name)
Adds a CSS class name to the respective attribute. If the class name is already present, nothing is added.- Parameters:
name
- the class name
-
init
public void init()
Calculates all default values if 'source' isnull
-
getItemName
public String getItemName(String name)
Returns the name of the given item which is either the default or can be redefined bysetItemName(String, String)
. If the name is not defined, the given name is returned. Example: Download.getItemName(Download.PN_FILE_NAME)- Parameters:
name
- item name- Returns:
- defined item name
-
setItemName
public void setItemName(String key, String name)
Defines the name of an item.- Parameters:
key
- key. egPN_FILE_NAME
.name
- redefined name
-
getFileReference
public String getFileReference()
Returns the file reference.- Returns:
- the file reference.
-
setFileReference
public void setFileReference(String fileReference)
Sets the file reference.- Parameters:
fileReference
- the file reference.
-
getInnerHtml
public Object getInnerHtml()
Returns the inner html object for the download link.- Returns:
- the inner html or
null
if not defined.
-
setInnerHtml
public void setInnerHtml(Object innerHtml)
Sets the inner html object for the download. If not inner html is defined the file name is used when drawing the download link.- Parameters:
innerHtml
- the inner html object
-
getFileNodePath
public String getFileNodePath()
Returns the file path. This defaults to the path of the node addressed by 'getItemName(NN_FILE)' or an empty string if that node does not exist.- Returns:
- path of the file node.
-
setFileNodePath
public void setFileNodePath(String fileNodePath)
Sets the path to the file node. Set this to an empty string to disable fetching data from the respective node. the path can be relative to address a node relative to the image node.- Parameters:
fileNodePath
- path of the file node.
-
getFileName
public String getFileName()
Returns the file name of this download as defined by the property with the name from 'getItemName(PN_FILE_NAME). this is an informative property and is not used for any logic. the file name is added per default as suffix to the link path.- Returns:
- file name.
-
setFileName
public void setFileName(String fileName)
Sets the file name.- Parameters:
fileName
- the file name
-
getTitle
public String getTitle()
Returns the image title as defined by 'getItemName(PN_TITLE)' or overridden bysetTitle(String)
.- Returns:
- the title
-
getTitle
public String getTitle(boolean escape)
Returns the image title as defined by 'getItemName(PN_TITLE)' or overridden bysetTitle(String)
.- Parameters:
escape
- iftrue
the string is HTML escaped- Returns:
- the title
-
setTitle
public void setTitle(String title)
Sets the title.- Parameters:
title
- the title.
-
getDescription
public String getDescription()
Returns the image description as defined by getItemName(PN_DESCRIPTION) or overridden bysetDescription(String)
.- Returns:
- the description
-
getDescription
public String getDescription(boolean escape)
Returns the image description as defined by 'getItemName(PN_DESCRIPTION)' or overridden bysetDescription(String)
.- Parameters:
escape
- iftrue
the string is HTML escaped- Returns:
- the description
-
setDescription
public void setDescription(String description)
Sets the description.- Parameters:
description
- the description.
-
getHref
public String getHref()
Returns the href attribute of this download. the source is computed as follows:- if a selector is defined the path of the current resource concatenated with the selector, extension, and suffix is used.
- if a file node path is defined it is concatenated with the extension and suffix.
- if a file reference is defined it is concatenated with the suffix.
- Returns:
- the href attribute
-
setHref
public void setHref(String href)
Sets the href attribute- Parameters:
href
- the href attribute
-
getQuery
public String getQuery()
Gets the query attribute- Returns:
- the query
-
setQuery
public void setQuery(String query)
Sets the query attribute, overwrites any previously generated queries.- Parameters:
query
- the query
-
addQueryParam
public void addQueryParam(String name, String value)
Adds a query param to the 'query' attribute- Parameters:
name
- name of the paramvalue
- value of the param
-
getExtension
public String getExtension()
Returns the extension. defaults to.res
- Returns:
- the extension.
-
setExtension
public void setExtension(String extension)
Sets the extension.- Parameters:
extension
- the extension.
-
getIconType
public String getIconType()
Returns the icon type of this file. Note: currently the mime type of the file is not respected but only the extension of the file name is used.- Returns:
- the icon type.
-
getIconPath
@Deprecated public String getIconPath()
Deprecated.since 5.4 please use css classes for the icon, like "icon_xls.gif"Returns a path to an icon representing the file.- Returns:
- a path to an icon or
null
.
-
getSuffix
public String getSuffix()
Returns the suffix. defaults to""
- Returns:
- the suffix.
-
setSuffix
public void setSuffix(String suffix)
Sets the suffix.- Parameters:
suffix
- the suffix.
-
getSelector
public String getSelector()
Returns the selector string. defaults to an empty string. Note: in order to use a spool script, you need to defined the selector, otherwise the file will be addressed directly.- Returns:
- the selector string.
-
setSelector
public void setSelector(String selector)
Sets the selector string.- Parameters:
selector
- the selector string.
-
hasContent
public boolean hasContent()
Checks if this download has content. i.e. if there either an file or an file reference defined and they have binary data.- Returns:
true
if this download has content.
-
draw
public void draw(Writer w) throws IOException
Writes this download as link tag to the given writer- Parameters:
w
- the writer- Throws:
IOException
- if an I/O error occurs
-
getString
public String getString()
Returns a string representation as HTML tag of this image.- Returns:
- the HTML tag.
-
getMimeType
public String getMimeType() throws RepositoryException
Returns the mime type of this image. This is a convenience method that gets the "jcr:mimeType" sibling property of the data property returned bygetData()
.- Returns:
- the mime type of the image or
null
if the image has no content. - Throws:
RepositoryException
- if an error accessing the repository occurs.
-
getLastModified
public Calendar getLastModified() throws RepositoryException
Returns the last modified of this image. This is a convenience method that gets the "jcr:lastModified" sibling property of the data property returned bygetData()
.- Returns:
- the last modified of the image or
null
if the image has no content. - Throws:
RepositoryException
- if an error accessing the repository occurs.
-
getData
public Property getData() throws RepositoryException
Returns the property that contains the binary data of this download. This can either by a property addressed by the internal file resource or a property addressed by an external file reference.- Returns:
- binary property or
null
- Throws:
RepositoryException
- if an error accessing the repository occurs.
-
-