Merged BRANCHES/DEV/CLOUD1_SP to HEAD:

38996: WebDAV: added getUrlPathPrefix tests for WebDAVHelper.
   38997: WebDAV: added setter for urlPathPrefix property (to aid in spring config), ensures path prefix terminated in slash.


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@39106 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Matt Ward
2012-07-11 17:17:57 +00:00
parent 91f9df2b23
commit 771502842d
2 changed files with 48 additions and 10 deletions

View File

@@ -53,6 +53,8 @@ import org.alfresco.service.cmr.site.SiteService;
import org.alfresco.service.namespace.NamespaceService;
import org.alfresco.service.namespace.QName;
import org.alfresco.util.EqualsHelper;
import org.alfresco.util.Pair;
import org.apache.commons.lang.NotImplementedException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.extensions.surf.util.URLDecoder;
@@ -853,14 +855,26 @@ public class WebDAVHelper
}
public String getUrlPathPrefix(HttpServletRequest request)
public void setUrlPathPrefix(String urlPathPrefix)
{
if (StringUtils.hasText(m_urlPathPrefix))
{
return m_urlPathPrefix;
m_urlPathPrefix = urlPathPrefix;
}
StringBuilder urlStr = new StringBuilder(request.getRequestURI());
public String getUrlPathPrefix(HttpServletRequest request)
{
StringBuilder urlStr = null;
if (StringUtils.hasText(m_urlPathPrefix))
{
// A specific prefix has been configured in, so use it.
urlStr = new StringBuilder(m_urlPathPrefix);
}
else
{
// Extract the path prefix from the request, using the servlet path as a guide.
// e.g. "/preamble/servlet-mapping/folder/file.txt"
// with a servlet path of "/servlet-mapping"
// would result in a path prefix of "/preamble/servlet-mapping" being discovered.
urlStr = new StringBuilder(request.getRequestURI());
String servletPath = request.getServletPath();
int rootPos = urlStr.indexOf(servletPath);
@@ -868,7 +882,9 @@ public class WebDAVHelper
{
urlStr.setLength(rootPos + servletPath.length());
}
}
// Ensure the prefix ends in the path separator.
if (urlStr.charAt(urlStr.length() - 1) != PathSeperatorChar)
{
urlStr.append(PathSeperator);

View File

@@ -18,17 +18,17 @@
*/
package org.alfresco.repo.webdav;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import org.alfresco.repo.tenant.TenantService;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.cmr.security.AuthenticationService;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.mock.web.MockHttpServletRequest;
/**
* Tests for the WebDAVHelper class.
@@ -49,6 +49,28 @@ public class WebDAVHelperTest
davHelper = new WebDAVHelper("", serviceRegistry, authService, tenantService);
}
@Test
public void canGetUrlPathPrefixWhenExplicitlySet()
{
// Path prefix explicitly set on helper.
davHelper = new WebDAVHelper("/my/prefix", serviceRegistry, authService, tenantService);
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/my/prefix/folder/filename.txt");
String prefix = davHelper.getUrlPathPrefix(request);
assertEquals("/my/prefix/", prefix);
}
@Test
public void canGetUrlPathPrefixFromServletPath()
{
// Path prefix not explicitly set on helper.
davHelper = new WebDAVHelper("", serviceRegistry, authService, tenantService);
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/before/the-servlet/folder/filename.txt");
// Servlet path will be used to determine path prefix.
request.setServletPath("/the-servlet");
String prefix = davHelper.getUrlPathPrefix(request);
assertEquals("/before/the-servlet/", prefix);
}
@Test
public void canGetDestinationPathWhenNoServletName()
{