Sanitising of all java.net.URLEncoder class usage to use a proper UTF-8 URL encoder.

Since URLEncoder is actually an HTML form encoder - not really for URI encoding - and it requires an extra step (converting '+' to %20)
Replaced with w3 Consortium algorithm for fast UTF-8 URL encoding in a single step (rather than using the cludgy and slow URI core java classes)
Addition of Template API for generate URLs to node content download webscript API: node.serviceUrl

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@7316 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Kevin Roast
2007-11-09 14:59:59 +00:00
parent 689772c5a1
commit d0b5272b91
4 changed files with 88 additions and 130 deletions

View File

@@ -28,8 +28,6 @@ import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.Serializable; import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.MessageFormat; import java.text.MessageFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
@@ -74,6 +72,7 @@ import org.alfresco.service.namespace.QName;
import org.alfresco.service.namespace.RegexQNamePattern; import org.alfresco.service.namespace.RegexQNamePattern;
import org.alfresco.util.GUID; import org.alfresco.util.GUID;
import org.alfresco.util.ParameterCheck; import org.alfresco.util.ParameterCheck;
import org.alfresco.util.URLEncoder;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.mozilla.javascript.Context; import org.mozilla.javascript.Context;
@@ -82,7 +81,6 @@ import org.mozilla.javascript.ScriptableObject;
import org.mozilla.javascript.UniqueTag; import org.mozilla.javascript.UniqueTag;
import org.mozilla.javascript.Wrapper; import org.mozilla.javascript.Wrapper;
import org.springframework.util.FileCopyUtils; import org.springframework.util.FileCopyUtils;
import org.springframework.util.StringUtils;
/** /**
* Node class implementation, specific for use by ScriptService as part of the object model. * Node class implementation, specific for use by ScriptService as part of the object model.
@@ -861,16 +859,9 @@ public class ScriptNode implements Serializable, Scopeable
{ {
if (getIsDocument() == true) if (getIsDocument() == true)
{ {
try return MessageFormat.format(CONTENT_DEFAULT_URL, new Object[] { nodeRef.getStoreRef().getProtocol(),
{ nodeRef.getStoreRef().getIdentifier(), nodeRef.getId(),
return MessageFormat.format(CONTENT_DEFAULT_URL, new Object[] { nodeRef.getStoreRef().getProtocol(), URLEncoder.encode(getName())});
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 else
{ {
@@ -894,18 +885,11 @@ public class ScriptNode implements Serializable, Scopeable
{ {
if (getIsDocument() == true) if (getIsDocument() == true)
{ {
try return MessageFormat.format(CONTENT_DOWNLOAD_URL, new Object[] {
{ nodeRef.getStoreRef().getProtocol(),
return MessageFormat.format(CONTENT_DOWNLOAD_URL, new Object[] { nodeRef.getStoreRef().getIdentifier(),
nodeRef.getStoreRef().getProtocol(), nodeRef.getId(),
nodeRef.getStoreRef().getIdentifier(), URLEncoder.encode(getName()) });
nodeRef.getId(),
StringUtils.replace(URLEncoder.encode(getName(), "UTF-8"), "+", "%20") });
}
catch (UnsupportedEncodingException err)
{
throw new AlfrescoRuntimeException("Failed to encode content download URL for node: " + nodeRef, err);
}
} }
else else
{ {
@@ -2308,17 +2292,10 @@ public class ScriptNode implements Serializable, Scopeable
*/ */
public String getUrl() public String getUrl()
{ {
try return MessageFormat.format(CONTENT_PROP_URL, new Object[] { nodeRef.getStoreRef().getProtocol(),
{ nodeRef.getStoreRef().getIdentifier(), nodeRef.getId(),
return MessageFormat.format(CONTENT_PROP_URL, new Object[] { nodeRef.getStoreRef().getProtocol(), URLEncoder.encode(getName()),
nodeRef.getStoreRef().getIdentifier(), nodeRef.getId(), URLEncoder.encode(property.toString()) });
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 String jsGet_url() public String jsGet_url()
@@ -2333,19 +2310,12 @@ public class ScriptNode implements Serializable, Scopeable
{ {
if (getIsDocument() == true) if (getIsDocument() == true)
{ {
try return MessageFormat.format(CONTENT_DOWNLOAD_PROP_URL, new Object[] {
{ nodeRef.getStoreRef().getProtocol(),
return MessageFormat.format(CONTENT_DOWNLOAD_PROP_URL, new Object[] { nodeRef.getStoreRef().getIdentifier(),
nodeRef.getStoreRef().getProtocol(), nodeRef.getId(),
nodeRef.getStoreRef().getIdentifier(), URLEncoder.encode(getName()),
nodeRef.getId(), URLEncoder.encode(property.toString()) });
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 download URL for node: " + nodeRef, err);
}
} }
else else
{ {

View File

@@ -25,8 +25,6 @@
package org.alfresco.repo.template; package org.alfresco.repo.template;
import java.io.Serializable; import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.MessageFormat; import java.text.MessageFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@@ -45,10 +43,9 @@ import org.alfresco.service.cmr.repository.ContentReader;
import org.alfresco.service.cmr.repository.ContentService; import org.alfresco.service.cmr.repository.ContentService;
import org.alfresco.service.cmr.repository.ContentWriter; import org.alfresco.service.cmr.repository.ContentWriter;
import org.alfresco.service.cmr.repository.FileTypeImageSize; 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.cmr.repository.TemplateImageResolver;
import org.alfresco.service.namespace.QName; import org.alfresco.service.namespace.QName;
import org.springframework.util.StringUtils; import org.alfresco.util.URLEncoder;
/** /**
* Base class for Template API objects that supply content functionality. * Base class for Template API objects that supply content functionality.
@@ -58,9 +55,11 @@ import org.springframework.util.StringUtils;
public abstract class BaseContentNode implements TemplateContent public abstract class BaseContentNode implements TemplateContent
{ {
protected final static String CONTENT_GET_URL = "/d/d/{0}/{1}/{2}/{3}"; 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_GET_PROP_URL = "/d/d/{0}/{1}/{2}/{3}?property={4}";
protected final static String CONTENT_DOWNLOAD_URL = "/d/a/{0}/{1}/{2}/{3}";
protected final static String CONTENT_DOWNLOAD_PROP_URL = "/d/a/{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 CONTENT_SERVICE_GET_URL = "/api/node/content/{0}/{1}/{2}/{3}";
protected final static String CONTENT_SERVICE_GET_PROP_URL = "/api/node/content;{4}/{0}/{1}/{2}/{3}";
protected final static String FOLDER_BROWSE_URL = "/n/browse/{0}/{1}/{2}"; protected final static String FOLDER_BROWSE_URL = "/n/browse/{0}/{1}/{2}";
protected final static String NAMESPACE_BEGIN = "" + QName.NAMESPACE_BEGIN; protected final static String NAMESPACE_BEGIN = "" + QName.NAMESPACE_BEGIN;
@@ -359,18 +358,8 @@ public abstract class BaseContentNode implements TemplateContent
{ {
if (getIsDocument() == true) if (getIsDocument() == true)
{ {
try TemplateContentData content = (TemplateContentData)this.getProperties().get(ContentModel.PROP_CONTENT);
{ return content != null ? content.getUrl() : "";
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 else
{ {
@@ -391,18 +380,21 @@ public abstract class BaseContentNode implements TemplateContent
{ {
if (getIsDocument() == true) if (getIsDocument() == true)
{ {
try TemplateContentData content = (TemplateContentData)this.getProperties().get(ContentModel.PROP_CONTENT);
{ return content != null ? content.getDownloadUrl() : "";
return MessageFormat.format(CONTENT_DOWNLOAD_URL, new Object[] { }
getNodeRef().getStoreRef().getProtocol(), else
getNodeRef().getStoreRef().getIdentifier(), {
getNodeRef().getId(), return "";
StringUtils.replace(URLEncoder.encode(getName(), "UTF-8"), "+", "%20") }); }
} }
catch (UnsupportedEncodingException err)
{ public String getServiceUrl()
throw new TemplateException("Failed to encode content download URL for node: " + getNodeRef(), err); {
} if (getIsDocument() == true)
{
TemplateContentData content = (TemplateContentData)this.getProperties().get(ContentModel.PROP_CONTENT);
return content != null ? content.getServiceUrl() : "";
} }
else else
{ {
@@ -428,7 +420,7 @@ public abstract class BaseContentNode implements TemplateContent
for (int i=1; i<paths.size(); i++) for (int i=1; i<paths.size(); i++)
{ {
path.append("/") path.append("/")
.append(StringUtils.replace(URLEncoder.encode(paths.get(i).getName(), "UTF-8"), "+", "%20")); .append(URLEncoder.encode(paths.get(i).getName()));
} }
return path.toString(); return path.toString();
} }
@@ -437,10 +429,6 @@ public abstract class BaseContentNode implements TemplateContent
// cannot build path if file no longer exists // cannot build path if file no longer exists
return ""; return "";
} }
catch (UnsupportedEncodingException err)
{
throw new TemplateException("Failed to encode content WebDav URL for node: " + getNodeRef(), err);
}
} }
/** /**
@@ -575,38 +563,59 @@ public abstract class BaseContentNode implements TemplateContent
public String getUrl() public String getUrl()
{ {
try if (ContentModel.PROP_CONTENT.equals(property))
{ {
return MessageFormat.format(CONTENT_GET_PROP_URL, new Object[] { return buildUrl(CONTENT_GET_URL);
getNodeRef().getStoreRef().getProtocol(),
getNodeRef().getStoreRef().getIdentifier(),
getNodeRef().getId(),
StringUtils.replace(URLEncoder.encode(getName(), "UTF-8"), "+", "%20"),
StringUtils.replace(URLEncoder.encode(property.toString(), "UTF-8"), "+", "%20") } );
} }
catch (UnsupportedEncodingException err) else
{ {
throw new TemplateException("Failed to encode content URL for node: " + getNodeRef(), err); return buildPropUrl(CONTENT_GET_PROP_URL);
} }
} }
public String getDownloadUrl() public String getDownloadUrl()
{ {
try if (ContentModel.PROP_CONTENT.equals(property))
{ {
return MessageFormat.format(CONTENT_DOWNLOAD_PROP_URL, new Object[] { return buildUrl(CONTENT_DOWNLOAD_URL);
getNodeRef().getStoreRef().getProtocol(),
getNodeRef().getStoreRef().getIdentifier(),
getNodeRef().getId(),
StringUtils.replace(URLEncoder.encode(getName(), "UTF-8"), "+", "%20"),
StringUtils.replace(URLEncoder.encode(property.toString(), "UTF-8"), "+", "%20") });
} }
catch (UnsupportedEncodingException err) else
{ {
throw new TemplateException("Failed to encode content download URL for node: " + getNodeRef(), err); 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() public long getSize()
{ {
return contentData.getSize(); return contentData.getSize();

View File

@@ -24,10 +24,10 @@
*/ */
package org.alfresco.repo.template; package org.alfresco.repo.template;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List; import java.util.List;
import org.alfresco.util.URLEncoder;
import freemarker.template.TemplateMethodModelEx; import freemarker.template.TemplateMethodModelEx;
import freemarker.template.TemplateModelException; import freemarker.template.TemplateModelException;
import freemarker.template.TemplateScalarModel; import freemarker.template.TemplateScalarModel;
@@ -55,18 +55,7 @@ public class UrlEncodeMethod extends BaseTemplateProcessorExtension implements T
Object arg0 = args.get(0); Object arg0 = args.get(0);
if (arg0 instanceof TemplateScalarModel) if (arg0 instanceof TemplateScalarModel)
{ {
try result = URLEncoder.encode(((TemplateScalarModel)arg0).getAsString());
{
// TODO: Support other encodings, although
// http://java.sun.com/j2se/1.4.2/docs/api/java/net/URLEncoder.html
// recommends UTF-8
result = URLEncoder.encode(((TemplateScalarModel)arg0).getAsString(), "UTF-8");
}
catch (UnsupportedEncodingException e)
{
// fallback to original url
result = ((TemplateScalarModel)arg0).getAsString();
}
} }
} }

View File

@@ -25,7 +25,6 @@
package org.alfresco.repo.template; package org.alfresco.repo.template;
import java.io.Serializable; import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.text.MessageFormat; import java.text.MessageFormat;
import java.util.Date; import java.util.Date;
@@ -37,12 +36,10 @@ import org.alfresco.model.ContentModel;
import org.alfresco.service.ServiceRegistry; import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.cmr.repository.ContentData; import org.alfresco.service.cmr.repository.ContentData;
import org.alfresco.service.cmr.repository.NodeRef; import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.TemplateException;
import org.alfresco.service.cmr.version.Version; import org.alfresco.service.cmr.version.Version;
import org.alfresco.service.cmr.version.VersionType; import org.alfresco.service.cmr.version.VersionType;
import org.alfresco.service.namespace.QName; import org.alfresco.service.namespace.QName;
import org.alfresco.service.namespace.QNameMap; import org.alfresco.service.namespace.QNameMap;
import org.springframework.util.StringUtils;
/** /**
* Template Node wrapper representing a record in the version history of a node. * Template Node wrapper representing a record in the version history of a node.
@@ -275,17 +272,10 @@ public class VersionHistoryNode extends BaseContentNode
public String getUrl() public String getUrl()
{ {
NodeRef nodeRef = this.version.getFrozenStateNodeRef(); NodeRef nodeRef = this.version.getFrozenStateNodeRef();
try return MessageFormat.format(parent.CONTENT_GET_URL, new Object[] {
{ nodeRef.getStoreRef().getProtocol(),
return MessageFormat.format(parent.CONTENT_GET_URL, new Object[] { nodeRef.getStoreRef().getIdentifier(),
nodeRef.getStoreRef().getProtocol(), nodeRef.getId(),
nodeRef.getStoreRef().getIdentifier(), URLEncoder.encode(getName()) } );
nodeRef.getId(),
StringUtils.replace(URLEncoder.encode(getName(), "UTF-8"), "+", "%20") } );
}
catch (UnsupportedEncodingException err)
{
throw new TemplateException("Failed to encode content URL for node: " + nodeRef, err);
}
} }
} }