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 java.lang.String
NN_FILE
name of the file node.static java.lang.String
PN_DESCRIPTION
name of the description propertystatic java.lang.String
PN_FILE_NAME
name of the 'file name' property.static java.lang.String
PN_REFERENCE
name of the file reference property.static java.lang.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(java.lang.String name, java.lang.String value)
Adds a tag attribute to this download.void
addCssClass(java.lang.String name)
Adds a CSS class name to the respective attribute.void
addQueryParam(java.lang.String name, java.lang.String value)
Adds a query param to the 'query' attributevoid
draw(java.io.Writer w)
Writes this download as link tag to the given writerjava.lang.String
get(java.lang.String name)
Get a property either from the overlaid map or the underlying properties.int
get(java.lang.String name, int defaultValue)
Get a property and convert it to an integer.java.util.Map<java.lang.String,java.lang.String>
getAttributes()
Returns a map of attributes.Property
getData()
Returns the property that contains the binary data of this download.java.lang.String
getDescription()
Returns the image description as defined by getItemName(PN_DESCRIPTION) or overridden bysetDescription(String)
.java.lang.String
getDescription(boolean escape)
Returns the image description as defined by 'getItemName(PN_DESCRIPTION)' or overridden bysetDescription(String)
.java.lang.String
getExtension()
Returns the extension.java.lang.String
getFileName()
Returns the file name of this download as defined by the property with the name from 'getItemName(PN_FILE_NAME).java.lang.String
getFileNodePath()
Returns the file path.java.lang.String
getFileReference()
Returns the file reference.java.lang.String
getHref()
Returns the href attribute of this download.java.lang.String
getIconPath()
Deprecated.since 5.4 please use css classes for the icon, like "icon_xls.gif"java.lang.String
getIconType()
Returns the icon type of this file.java.lang.Object
getInnerHtml()
Returns the inner html object for the download link.java.lang.String
getItemName(java.lang.String name)
Returns the name of the given item which is either the default or can be redefined bysetItemName(String, String)
.java.util.Calendar
getLastModified()
Returns the last modified of this image.java.lang.String
getMimeType()
Returns the mime type of this image.java.lang.String
getQuery()
Gets the query attributejava.lang.String
getSelector()
Returns the selector string.java.lang.String
getString()
Returns a string representation as HTML tag of this image.java.lang.String
getSuffix()
Returns the suffix.java.lang.String
getTitle()
Returns the image title as defined by 'getItemName(PN_TITLE)' or overridden bysetTitle(String)
.java.lang.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(java.lang.String name, java.lang.String value)
Set a property to the overlaid map.void
setDescription(java.lang.String description)
Sets the description.void
setExtension(java.lang.String extension)
Sets the extension.void
setFileName(java.lang.String fileName)
Sets the file name.void
setFileNodePath(java.lang.String fileNodePath)
Sets the path to the file node.void
setFileReference(java.lang.String fileReference)
Sets the file reference.void
setHref(java.lang.String href)
Sets the href attributevoid
setInnerHtml(java.lang.Object innerHtml)
Sets the inner html object for the download.void
setIsInUITouchMode(boolean inUITouchMode)
Page is in UI Touch mode?void
setItemName(java.lang.String key, java.lang.String name)
Defines the name of an item.void
setQuery(java.lang.String query)
Sets the query attribute, overwrites any previously generated queries.void
setSelector(java.lang.String selector)
Sets the selector string.void
setSuffix(java.lang.String suffix)
Sets the suffix.void
setTitle(java.lang.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 java.lang.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 java.lang.String NN_FILE
name of the file node.- See Also:
- Constant Field Values
-
PN_FILE_NAME
public static final java.lang.String PN_FILE_NAME
name of the 'file name' property.- See Also:
- Constant Field Values
-
PN_TITLE
public static final java.lang.String PN_TITLE
name of the title property.- See Also:
- Constant Field Values
-
PN_DESCRIPTION
public static final java.lang.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:
java.lang.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 java.lang.String get(java.lang.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(java.lang.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(java.lang.String name, java.lang.String value)
Set a property to the overlaid map.- Parameters:
name
- name of the propertyvalue
- value of the property
-
addAttribute
public void addAttribute(java.lang.String name, java.lang.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(java.lang.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 java.lang.String getItemName(java.lang.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(java.lang.String key, java.lang.String name)
Defines the name of an item.- Parameters:
key
- key. egPN_FILE_NAME
.name
- redefined name
-
getFileReference
public java.lang.String getFileReference()
Returns the file reference.- Returns:
- the file reference.
-
setFileReference
public void setFileReference(java.lang.String fileReference)
Sets the file reference.- Parameters:
fileReference
- the file reference.
-
getInnerHtml
public java.lang.Object getInnerHtml()
Returns the inner html object for the download link.- Returns:
- the inner html or
null
if not defined.
-
setInnerHtml
public void setInnerHtml(java.lang.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 java.lang.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(java.lang.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 java.lang.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(java.lang.String fileName)
Sets the file name.- Parameters:
fileName
- the file name
-
getTitle
public java.lang.String getTitle()
Returns the image title as defined by 'getItemName(PN_TITLE)' or overridden bysetTitle(String)
.- Returns:
- the title
-
getTitle
public java.lang.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(java.lang.String title)
Sets the title.- Parameters:
title
- the title.
-
getDescription
public java.lang.String getDescription()
Returns the image description as defined by getItemName(PN_DESCRIPTION) or overridden bysetDescription(String)
.- Returns:
- the description
-
getDescription
public java.lang.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(java.lang.String description)
Sets the description.- Parameters:
description
- the description.
-
getHref
public java.lang.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(java.lang.String href)
Sets the href attribute- Parameters:
href
- the href attribute
-
getQuery
public java.lang.String getQuery()
Gets the query attribute- Returns:
- the query
-
setQuery
public void setQuery(java.lang.String query)
Sets the query attribute, overwrites any previously generated queries.- Parameters:
query
- the query
-
addQueryParam
public void addQueryParam(java.lang.String name, java.lang.String value)
Adds a query param to the 'query' attribute- Parameters:
name
- name of the paramvalue
- value of the param
-
getExtension
public java.lang.String getExtension()
Returns the extension. defaults to.res
- Returns:
- the extension.
-
setExtension
public void setExtension(java.lang.String extension)
Sets the extension.- Parameters:
extension
- the extension.
-
getIconType
public java.lang.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 java.lang.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 java.lang.String getSuffix()
Returns the suffix. defaults to""
- Returns:
- the suffix.
-
setSuffix
public void setSuffix(java.lang.String suffix)
Sets the suffix.- Parameters:
suffix
- the suffix.
-
getSelector
public java.lang.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(java.lang.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(java.io.Writer w) throws java.io.IOException
Writes this download as link tag to the given writer- Parameters:
w
- the writer- Throws:
java.io.IOException
- if an I/O error occurs
-
getString
public java.lang.String getString()
Returns a string representation as HTML tag of this image.- Returns:
- the HTML tag.
-
getMimeType
public java.lang.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 java.util.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.
-
getAttributes
public java.util.Map<java.lang.String,java.lang.String> getAttributes()
Returns a map of attributes.- Returns:
- the attributes map.
-
-