/* * Copyright (C) 2005-2007 Alfresco Software Limited. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * As a special exception to the terms and conditions of version 2.0 of * the GPL, you may redistribute this Program in connection with Free/Libre * and Open Source Software ("FLOSS") applications as described in Alfresco's * FLOSS exception. You should have recieved a copy of the text describing * the FLOSS exception, and it is also available here: * http://www.alfresco.com/legal/licensing */ package org.alfresco.repo.template; import java.io.Serializable; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.text.MessageFormat; import java.util.ArrayList; import java.util.List; import java.util.Set; import org.alfresco.model.ApplicationModel; import org.alfresco.model.ContentModel; import org.alfresco.repo.content.MimetypeMap; import org.alfresco.service.ServiceRegistry; import org.alfresco.service.cmr.dictionary.DictionaryService; import org.alfresco.service.cmr.model.FileInfo; import org.alfresco.service.cmr.model.FileNotFoundException; import org.alfresco.service.cmr.repository.ChildAssociationRef; import org.alfresco.service.cmr.repository.ContentData; import org.alfresco.service.cmr.repository.ContentReader; import org.alfresco.service.cmr.repository.ContentService; import org.alfresco.service.cmr.repository.ContentWriter; import org.alfresco.service.cmr.repository.FileTypeImageSize; import org.alfresco.service.cmr.repository.TemplateException; import org.alfresco.service.cmr.repository.TemplateImageResolver; import org.alfresco.service.namespace.QName; import org.springframework.util.StringUtils; /** * Base class for Template API objects that supply content functionality. * * @author Kevin Roast */ public abstract class BaseContentNode implements TemplateContent { protected final static String CONTENT_GET_URL = "/d/d/{0}/{1}/{2}/{3}"; protected final static String CONTENT_DOWNLOAD_URL = "/d/a/{0}/{1}/{2}/{3}"; protected final static String CONTENT_GET_PROP_URL = "/d/d/{0}/{1}/{2}/{3}?property={4}"; protected final static String CONTENT_DOWNLOAD_PROP_URL = "/d/a/{0}/{1}/{2}/{3}?property={4}"; protected final static String FOLDER_BROWSE_URL = "/n/browse/{0}/{1}/{2}"; protected final static String NAMESPACE_BEGIN = "" + QName.NAMESPACE_BEGIN; /** The children of this node */ protected List children = null; protected ServiceRegistry services = null; protected TemplateImageResolver imageResolver = null; protected Set aspects = null; private String displayPath = null; private Boolean isDocument = null; private Boolean isContainer = null; private Boolean isLinkToDocument = null; private Boolean isLinkToContainer = null; /** * @return true if this Node is a container (i.e. a folder) */ public boolean getIsContainer() { if (isContainer == null) { DictionaryService dd = this.services.getDictionaryService(); isContainer = Boolean.valueOf( (dd.isSubClass(getType(), ContentModel.TYPE_FOLDER) == true && dd.isSubClass(getType(), ContentModel.TYPE_SYSTEM_FOLDER) == false) ); } return isContainer.booleanValue(); } /** * @return true if this Node is a Document (i.e. with content) */ public boolean getIsDocument() { if (isDocument == null) { DictionaryService dd = this.services.getDictionaryService(); isDocument = Boolean.valueOf(dd.isSubClass(getType(), ContentModel.TYPE_CONTENT)); } return isDocument.booleanValue(); } /** * @return true if this Node is a Link to a Container (i.e. a folderlink) */ public boolean getIsLinkToContainer() { if (isLinkToContainer == null) { DictionaryService dd = this.services.getDictionaryService(); isLinkToContainer = Boolean.valueOf(dd.isSubClass(getType(), ApplicationModel.TYPE_FOLDERLINK)); } return isLinkToContainer.booleanValue(); } /** * @return true if this Node is a Link to a Document (i.e. a filelink) */ public boolean getIsLinkToDocument() { if (isLinkToDocument == null) { DictionaryService dd = this.services.getDictionaryService(); isLinkToDocument = Boolean.valueOf(dd.isSubClass(getType(), ApplicationModel.TYPE_FILELINK)); } return isLinkToDocument.booleanValue(); } /** * Override Object.toString() to provide useful debug output */ public String toString() { if (this.services.getNodeService().exists(getNodeRef())) { return "Node Type: " + getType() + "\tNode Ref: " + getNodeRef().toString(); } else { return "Node no longer exists: " + getNodeRef(); } } // ------------------------------------------------------------------------------ // Content display API /** * @return QName path to this node. This can be used for Lucene PATH: style queries */ public String getQnamePath() { return this.services.getNodeService().getPath(getNodeRef()).toPrefixString(this.services.getNamespaceService()); } /** * @return the small icon image for this node */ public String getIcon16() { if (this.imageResolver != null) { if (getIsDocument()) { return this.imageResolver.resolveImagePathForName(getName(), FileTypeImageSize.Small); } else { String icon = (String)getProperties().get("app:icon"); if (icon != null) { return "/images/icons/" + icon + "-16.gif"; } else { return "/images/icons/space-icon-default-16.gif"; } } } else { return "/images/filetypes/_default.gif"; } } /** * @return the medium icon image for this node */ public String getIcon32() { if (this.imageResolver != null) { if (getIsDocument()) { return this.imageResolver.resolveImagePathForName(getName(), FileTypeImageSize.Medium); } else { String icon = (String)getProperties().get("app:icon"); if (icon != null) { return "/images/icons/" + icon + ".gif"; } else { return "/images/icons/space-icon-default.gif"; } } } else { return "/images/filetypes32/_default.gif"; } } /** * @return the large icon image for this node */ public String getIcon64() { if (this.imageResolver != null) { if (getIsDocument()) { return this.imageResolver.resolveImagePathForName(getName(), FileTypeImageSize.Large); } else { String icon = (String)getProperties().get("app:icon"); if (icon != null) { return "/images/icons/" + icon + "-64.png"; } else { return "/images/icons/space-icon-default-64.png"; } } } else { return "/images/filetypes64/_default.gif"; } } /** * @return Display path to this node - the path built of 'cm:name' attribute values. */ public String getDisplayPath() { if (displayPath == null) { displayPath = this.services.getNodeService().getPath(getNodeRef()).toDisplayPath( services.getNodeService(), services.getPermissionService()); } return displayPath; } // ------------------------------------------------------------------------------ // TemplateProperties contract impl /** * @return The children of this Node as objects that support the TemplateProperties contract. */ public List getChildren() { if (this.children == null) { List childRefs = this.services.getNodeService().getChildAssocs(getNodeRef()); this.children = new ArrayList(childRefs.size()); for (ChildAssociationRef ref : childRefs) { // create our Node representation from the NodeRef TemplateNode child = new TemplateNode(ref.getChildRef(), this.services, this.imageResolver); this.children.add(child); } } return this.children; } /** * @return The list of aspects applied to this node */ public Set getAspects() { if (this.aspects == null) { this.aspects = this.services.getNodeService().getAspects(getNodeRef()); } return this.aspects; } /** * @param aspect The aspect name to test for * * @return true if the node has the aspect false otherwise */ public boolean hasAspect(String aspect) { if (this.aspects == null) { getAspects(); } if (aspect.startsWith(NAMESPACE_BEGIN)) { return this.aspects.contains((QName.createQName(aspect))); } else { boolean found = false; for (QName qname : this.aspects) { if (qname.toPrefixString(this.services.getNamespaceService()).equals(aspect)) { found = true; break; } } return found; } } // ------------------------------------------------------------------------------ // Content API /** * @return the content String for this node from the default content property * (@see ContentModel.PROP_CONTENT) */ public String getContent() { TemplateContentData content = (TemplateContentData)this.getProperties().get(ContentModel.PROP_CONTENT); return content != null ? content.getContent() : ""; } /** * @return For a content document, this method returns the URL to the content stream for * the default content property (@see ContentModel.PROP_CONTENT) *

* For a container node, this method return the URL to browse to the folder in the web-client */ public String getUrl() { if (getIsDocument() == true) { try { return MessageFormat.format(CONTENT_GET_URL, new Object[] { getNodeRef().getStoreRef().getProtocol(), getNodeRef().getStoreRef().getIdentifier(), getNodeRef().getId(), StringUtils.replace(URLEncoder.encode(getName(), "UTF-8"), "+", "%20") } ); } catch (UnsupportedEncodingException err) { throw new TemplateException("Failed to encode content URL for node: " + getNodeRef(), err); } } 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) { try { return MessageFormat.format(CONTENT_DOWNLOAD_URL, new Object[] { getNodeRef().getStoreRef().getProtocol(), getNodeRef().getStoreRef().getIdentifier(), getNodeRef().getId(), StringUtils.replace(URLEncoder.encode(getName(), "UTF-8"), "+", "%20") }); } catch (UnsupportedEncodingException err) { throw new TemplateException("Failed to encode content download URL for node: " + getNodeRef(), err); } } else { return ""; } } /** * @return The WebDav cm:name based path to the content for the default content property * (@see ContentModel.PROP_CONTENT) */ public String getWebdavUrl() { try { List paths = this.services.getFileFolderService().getNamePath(null, getNodeRef()); // build up the webdav url StringBuilder path = new StringBuilder(128); path.append("/webdav"); // build up the path skipping the first path as it is the root folder for (int i=1; i