/* * Copyright (C) 2005 Alfresco, Inc. * * Licensed under the Mozilla Public License version 1.1 * with a permitted attribution clause. You may obtain a * copy of the License at * * http://www.alfresco.org/legal/license.txt * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, * either express or implied. See the License for the specific * language governing permissions and limitations under the * License. */ package org.alfresco.repo.jscript; import java.io.Serializable; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.text.MessageFormat; import java.util.List; import java.util.Map; import java.util.Set; import org.alfresco.error.AlfrescoRuntimeException; import org.alfresco.model.ContentModel; import org.alfresco.repo.security.permissions.AccessDeniedException; import org.alfresco.service.ServiceRegistry; import org.alfresco.service.cmr.dictionary.DictionaryService; import org.alfresco.service.cmr.lock.LockStatus; import org.alfresco.service.cmr.repository.AssociationRef; 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.NodeRef; import org.alfresco.service.cmr.repository.TemplateImageResolver; import org.alfresco.service.namespace.QName; import org.alfresco.service.namespace.RegexQNamePattern; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.util.StringUtils; /** * Node class specific for use by Template pages that support Bean objects as part of the model. * The default template engine FreeMarker can use these objects and they are provided to support it. * A single method is completely freemarker specific - getXmlNodeModel() *
* The class exposes Node properties, children as dynamically populated maps and lists. *
* Various helper methods are provided to access common and useful node variables such
* as the content url and type information.
*
* @author Kevin Roast
*/
public final class Node implements Serializable
{
private static Log logger = LogFactory.getLog(Node.class);
private final static String NAMESPACE_BEGIN = "" + QName.NAMESPACE_BEGIN;
private final static String CONTENT_DEFAULT_URL = "/download/direct/{0}/{1}/{2}/{3}";
private final static String CONTENT_PROP_URL = "/download/direct/{0}/{1}/{2}/{3}?property={4}";
private final static String FOLDER_BROWSE_URL = "/navigate/browse/{0}/{1}/{2}";
/** The children of this node */
private Node[] children = null;
/** The associations from this node */
private ScriptableQNameMap
* For a container node, this method return the URL to browse to the folder in the web-client
*/
public String getUrl()
{
if (isDocument() == true)
{
try
{
return MessageFormat.format(CONTENT_DEFAULT_URL, new Object[] {
nodeRef.getStoreRef().getProtocol(),
nodeRef.getStoreRef().getIdentifier(),
nodeRef.getId(),
StringUtils.replace(URLEncoder.encode(getName(), "UTF-8"), "+", "%20") } );
}
catch (UnsupportedEncodingException err)
{
throw new AlfrescoRuntimeException("Failed to encode content URL for node: " + nodeRef, err);
}
}
else
{
return MessageFormat.format(FOLDER_BROWSE_URL, new Object[] {
nodeRef.getStoreRef().getProtocol(),
nodeRef.getStoreRef().getIdentifier(),
nodeRef.getId() } );
}
}
/**
* @return The mimetype encoding for content attached to the node from the default content property
* (@see ContentModel.PROP_CONTENT)
*/
public String getMimetype()
{
if (mimetype == null)
{
ScriptContentData content = (ScriptContentData)this.getProperties().get(ContentModel.PROP_CONTENT);
if (content != null)
{
mimetype = content.getMimetype();
}
}
return mimetype;
}
/**
* @return The size in bytes of the content attached to the node from the default content property
* (@see ContentModel.PROP_CONTENT)
*/
public long getSize()
{
if (size == null)
{
ScriptContentData content = (ScriptContentData)this.getProperties().get(ContentModel.PROP_CONTENT);
if (content != null)
{
size = content.getSize();
}
}
return size != null ? size.longValue() : 0L;
}
/**
* @return the image resolver instance used by this node
*/
public TemplateImageResolver getImageResolver()
{
return this.imageResolver;
}
/**
* Override Object.toString() to provide useful debug output
*/
public String toString()
{
if (this.services.getNodeService().exists(nodeRef))
{
return "Node Type: " + getType() +
"\nNode Properties: " + this.getProperties().toString() +
"\nNode Aspects: " + this.getAspects().toString();
}
else
{
return "Node no longer exists: " + nodeRef;
}
}
/**
* Inner class wrapping and providing access to a ContentData property
*/
public class ScriptContentData implements Serializable
{
/**
* Constructor
*
* @param contentData The ContentData object this object wraps
* @param property The property the ContentData is attached too
*/
public ScriptContentData(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(nodeRef, property);
return (reader != null && reader.exists()) ? reader.getContentString() : "";
}
/**
* @return
*/
public String getUrl()
{
try
{
return MessageFormat.format(CONTENT_PROP_URL, new Object[] {
nodeRef.getStoreRef().getProtocol(),
nodeRef.getStoreRef().getIdentifier(),
nodeRef.getId(),
StringUtils.replace(URLEncoder.encode(getName(), "UTF-8"), "+", "%20"),
StringUtils.replace(URLEncoder.encode(property.toString(), "UTF-8"), "+", "%20") } );
}
catch (UnsupportedEncodingException err)
{
throw new AlfrescoRuntimeException("Failed to encode content URL for node: " + nodeRef, err);
}
}
public long getSize()
{
return contentData.getSize();
}
public String getMimetype()
{
return contentData.getMimetype();
}
private ContentData contentData;
private QName property;
}
}node.getProperties()["name"]
*
* @return Map of properties for this Node.
*/
public Map