* For a container node, this method return the URL to browse to the folder in the web-client */ public String getUrl() { if (getIsDocument() == true) { TemplateContentData content = (TemplateContentData)this.getProperties().get(ContentModel.PROP_CONTENT); return content != null ? content.getUrl() : ""; } else { return MessageFormat.format(FOLDER_BROWSE_URL, new Object[] { getNodeRef().getStoreRef().getProtocol(), getNodeRef().getStoreRef().getIdentifier(), getNodeRef().getId() } ); } } /** * @return For a content document, this method returns the download URL to the content for * the default content property (@see ContentModel.PROP_CONTENT) *
* For a container node, this method returns an empty string */ public String getDownloadUrl() { if (getIsDocument() == true) { TemplateContentData content = (TemplateContentData)this.getProperties().get(ContentModel.PROP_CONTENT); return content != null ? content.getDownloadUrl() : ""; } else { return ""; } } public String getServiceUrl() { if (getIsDocument() == true) { TemplateContentData content = (TemplateContentData)this.getProperties().get(ContentModel.PROP_CONTENT); return content != null ? content.getServiceUrl() : ""; } else { return ""; } } /** * @return The WebDav cm:name based path to the content for the default content property * (@see ContentModel.PROP_CONTENT) */ public String getWebdavUrl() { WebDavService webDavService = this.services.getWebDavService(); return webDavService.getWebdavUrl(getNodeRef()); } /** * @return The mimetype encoding for content attached to the node from the default content property * (@see ContentModel.PROP_CONTENT) */ public String getMimetype() { TemplateContentData content = (TemplateContentData)this.getProperties().get(ContentModel.PROP_CONTENT); return (content != null ? content.getMimetype() : null); } /** * @return The display label of the mimetype encoding for content attached to the node from the default * content property (@see ContentModel.PROP_CONTENT) */ public String getDisplayMimetype() { TemplateContentData content = (TemplateContentData)this.getProperties().get(ContentModel.PROP_CONTENT); return (content != null ? content.getDisplayMimetype() : null); } /** * @return The character encoding for content attached to the node from the default content property * (@see ContentModel.PROP_CONTENT) */ public String getEncoding() { TemplateContentData content = (TemplateContentData)this.getProperties().get(ContentModel.PROP_CONTENT); return (content != null ? content.getEncoding() : null); } /** * @return The size in bytes of the content attached to the node from the default content property * (@see ContentModel.PROP_CONTENT) */ public long getSize() { TemplateContentData content = (TemplateContentData)this.getProperties().get(ContentModel.PROP_CONTENT); return (content != null ? content.getSize() : 0L); } /** * Helper to return true if the supplied property value is a TemplateContentData object * * @param o Object to test * * @return true if instanceof TemplateContentData, false otherwise */ public boolean isTemplateContent(Object o) { return (o instanceof TemplateContentData); } /** * Helper to return true if the supplied property value is a TemplateNodeRef object * * @param o Object to test * * @return true if instanceof isTemplateNodeRef, false otherwise */ public boolean isTemplateNodeRef(Object o) { return (o instanceof TemplateNodeRef); } // ------------------------------------------------------------------------------ // Site methods /** * Returns the short name of the site this node is located within. If the * node is not located within a site null is returned. * * @return The short name of the site this node is located within, null * if the node is not located within a site. */ public String getSiteShortName() { if (!this.siteNameResolved) { this.siteNameResolved = true; Path path = this.services.getNodeService().getPath(getNodeRef()); for (int i = 0; i < path.size(); i++) { if ("st:sites".equals(path.get(i).getPrefixedString(this.services.getNamespaceService()))) { // we now know the node is in a site, find the next element in the array (if there is one) if ((i+1) < path.size()) { // get the site name Path.Element siteName = path.get(i+1); // remove the "cm:" prefix and add to result object this.siteName = ISO9075.decode(siteName.getPrefixedString( this.services.getNamespaceService()).substring(3)); } break; } } } return this.siteName; } // ------------------------------------------------------------------------------ // Inner classes /** * Inner class wrapping and providing access to a ContentData property */ public class TemplateContentData implements Serializable { /** * Constructor * * @param contentData The ContentData object this object wraps * @param property The property the ContentData is attached too */ public TemplateContentData(ContentData contentData, QName property) { this.contentData = contentData; this.property = property; } /** * @return the content stream */ public String getContent() { ContentService contentService = services.getContentService(); ContentReader reader = contentService.getReader(getNodeRef(), property); return (reader != null && reader.exists()) ? reader.getContentString() : ""; } /** * @return the content stream to the specified maximum length in characters */ public String getContentMaxLength(int length) { ContentService contentService = services.getContentService(); ContentReader reader = contentService.getReader(getNodeRef(), property); return (reader != null && reader.exists()) ? reader.getContentString(length) : ""; } /** * @param length Length of the character stream to return, or -1 for all * * @return the binary content stream converted to text using any available transformer * if fails to convert then null will be returned */ public String getContentAsText(int length) { String result = null; if (MimetypeMap.MIMETYPE_TEXT_PLAIN.equals(getMimetype())) { result = getContentMaxLength(length); } else { // get the content reader ContentService contentService = services.getContentService(); NodeRef nodeRef = getNodeRef(); ContentReader reader = contentService.getReader(nodeRef, property); if (reader == null) { return ""; // Caller of this method returns "" if there is an IOException } // get the writer and set it up for text convert ContentWriter writer = contentService.getTempWriter(); writer.setMimetype("text/plain"); writer.setEncoding(reader.getEncoding()); TransformationOptions options = new TransformationOptions(); options.setSourceNodeRef(nodeRef); // try and transform the content try { contentService.transform(reader, writer, options); ContentReader resultReader = writer.getReader(); if (resultReader != null && reader.exists()) { if (length != -1) { result = resultReader.getContentString(length); } else { result = resultReader.getContentString(); } } } catch (NoTransformerException e) { // ignore } } return result; } public String getUrl() { if (ContentModel.PROP_CONTENT.equals(property)) { return buildUrl(CONTENT_GET_URL); } else { return buildPropUrl(CONTENT_GET_PROP_URL); } } public String getDownloadUrl() { if (ContentModel.PROP_CONTENT.equals(property)) { return buildUrl(CONTENT_DOWNLOAD_URL); } else { return buildPropUrl(CONTENT_DOWNLOAD_PROP_URL); } } public String getServiceUrl() { if (ContentModel.PROP_CONTENT.equals(property)) { return buildUrl(CONTENT_SERVICE_GET_URL); } else { return buildPropUrl(CONTENT_SERVICE_GET_PROP_URL); } } private String buildUrl(String format) { return MessageFormat.format(format, new Object[] { getNodeRef().getStoreRef().getProtocol(), getNodeRef().getStoreRef().getIdentifier(), getNodeRef().getId(), URLEncoder.encode(getName()) } ); } private String buildPropUrl(String pformat) { return MessageFormat.format(pformat, new Object[] { getNodeRef().getStoreRef().getProtocol(), getNodeRef().getStoreRef().getIdentifier(), getNodeRef().getId(), URLEncoder.encode(getName()), URLEncoder.encode(property.toString()) } ); } public long getSize() { return contentData.getSize(); } public String getMimetype() { return contentData.getMimetype(); } public String getDisplayMimetype() { return services.getMimetypeService().getDisplaysByMimetype().get(getMimetype()); } public String getEncoding() { return contentData.getEncoding(); } private ContentData contentData; private QName property; } }