diff --git a/config/alfresco/remote-api-context.xml b/config/alfresco/remote-api-context.xml index 5249f71b13..4514eeb3a2 100644 --- a/config/alfresco/remote-api-context.xml +++ b/config/alfresco/remote-api-context.xml @@ -10,6 +10,7 @@ + diff --git a/source/java/org/alfresco/repo/webdav/WebDAV.java b/source/java/org/alfresco/repo/webdav/WebDAV.java index 7dcddc789e..1a79c83ddd 100644 --- a/source/java/org/alfresco/repo/webdav/WebDAV.java +++ b/source/java/org/alfresco/repo/webdav/WebDAV.java @@ -424,39 +424,29 @@ public class WebDAV * Returns a URL that could be used to access the given path. * * @param request HttpServletRequest + * @param urlPathPrefix * @param path String * @param isCollection boolean * @return String */ - public static String getURLForPath(HttpServletRequest request, String path, boolean isCollection) + public static String getURLForPath(HttpServletRequest request, String urlPathPrefix, String path, boolean isCollection) { - return getURLForPath(request, path, isCollection, null); + return getURLForPath(request, urlPathPrefix, path, isCollection, null); } /** * Returns a URL that could be used to access the given path. * * @param request HttpServletRequest + * @param urlPathPrefix * @param path String * @param isCollection boolean * @param userAgent String * @return String */ - public static String getURLForPath(HttpServletRequest request, String path, boolean isCollection, String userAgent) + public static String getURLForPath(HttpServletRequest request, String urlPathPrefix, String path, boolean isCollection, String userAgent) { - StringBuilder urlStr = new StringBuilder(request.getRequestURI()); - String servletPath = request.getServletPath(); - - int rootPos = urlStr.indexOf(servletPath); - if (rootPos != -1) - { - urlStr.setLength(rootPos + servletPath.length()); - } - - if (urlStr.charAt(urlStr.length() - 1) != PathSeperatorChar) - { - urlStr.append(PathSeperator); - } + StringBuilder urlStr = new StringBuilder(urlPathPrefix); if (path.equals(RootPath) == false) { @@ -477,6 +467,8 @@ public class WebDAV urlStr.append( PathSeperator); } + logger.debug("getURLForPath() path:" + path + " => url:" + urlStr); + // Return the URL string return urlStr.toString(); } diff --git a/source/java/org/alfresco/repo/webdav/WebDAVHelper.java b/source/java/org/alfresco/repo/webdav/WebDAVHelper.java index a58b285268..988efe1848 100644 --- a/source/java/org/alfresco/repo/webdav/WebDAVHelper.java +++ b/source/java/org/alfresco/repo/webdav/WebDAVHelper.java @@ -57,6 +57,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.extensions.surf.util.URLDecoder; import org.springframework.extensions.surf.util.URLEncoder; +import org.springframework.util.StringUtils; import org.xml.sax.helpers.AttributesImpl; /** @@ -99,11 +100,12 @@ public class WebDAVHelper // Empty XML attribute list private AttributesImpl m_nullAttribs = new AttributesImpl(); + private String m_urlPathPrefix; /** * Class constructor */ - protected WebDAVHelper(ServiceRegistry serviceRegistry, AuthenticationService authService, TenantService tenantService) + protected WebDAVHelper(String urlPathPrefix, ServiceRegistry serviceRegistry, AuthenticationService authService, TenantService tenantService) { m_serviceRegistry = serviceRegistry; @@ -118,6 +120,7 @@ public class WebDAVHelper m_permissionService = m_serviceRegistry.getPermissionService(); m_tenantService = tenantService; m_authService = authService; + m_urlPathPrefix = urlPathPrefix; } /** @@ -279,6 +282,11 @@ public class WebDAVHelper return results; } + protected String getURLForPath(HttpServletRequest request, String path, boolean isFolder, String userAgent) + { + return WebDAV.getURLForPath(request, getUrlPathPrefix(request), path, isFolder, userAgent); + } + /** * Get the file info for the given paths * @@ -817,7 +825,7 @@ public class WebDAVHelper logger.debug(" URL host=" + url.getHost() + ", ServerName=" + request.getServerName() + ", localAddr=" + request.getLocalAddr()); } } - else if (url.getPath().indexOf(request.getServletPath()) == -1) + else if (!url.getPath().startsWith(getUrlPathPrefix(request))) { // Debug @@ -843,4 +851,29 @@ public class WebDAVHelper throw new WebDAVServerException(HttpServletResponse.SC_BAD_GATEWAY); } } + + + public String getUrlPathPrefix(HttpServletRequest request) + { + if (StringUtils.hasText(m_urlPathPrefix)) + { + return m_urlPathPrefix; + } + + StringBuilder urlStr = new StringBuilder(request.getRequestURI()); + String servletPath = request.getServletPath(); + + int rootPos = urlStr.indexOf(servletPath); + if (rootPos != -1) + { + urlStr.setLength(rootPos + servletPath.length()); + } + + if (urlStr.charAt(urlStr.length() - 1) != PathSeperatorChar) + { + urlStr.append(PathSeperator); + } + + return urlStr.toString(); + } } diff --git a/source/java/org/alfresco/repo/webdav/WebDAVHelperTest.java b/source/java/org/alfresco/repo/webdav/WebDAVHelperTest.java index 1494d0131b..603d3414eb 100644 --- a/source/java/org/alfresco/repo/webdav/WebDAVHelperTest.java +++ b/source/java/org/alfresco/repo/webdav/WebDAVHelperTest.java @@ -46,7 +46,7 @@ public class WebDAVHelperTest @Before public void setUp() throws Exception { - davHelper = new WebDAVHelper(serviceRegistry, authService, tenantService); + davHelper = new WebDAVHelper("", serviceRegistry, authService, tenantService); } @Test diff --git a/source/java/org/alfresco/repo/webdav/WebDAVMethod.java b/source/java/org/alfresco/repo/webdav/WebDAVMethod.java index 894989890d..e15d30585d 100644 --- a/source/java/org/alfresco/repo/webdav/WebDAVMethod.java +++ b/source/java/org/alfresco/repo/webdav/WebDAVMethod.java @@ -1440,7 +1440,7 @@ public abstract class WebDAVMethod */ protected String getURLForPath(HttpServletRequest request, String path, boolean isFolder) { - return WebDAV.getURLForPath(request, path, isFolder, m_userAgent); + return getDAVHelper().getURLForPath(request, path, isFolder, m_userAgent); } /** diff --git a/source/java/org/alfresco/repo/webdav/WebDAVServlet.java b/source/java/org/alfresco/repo/webdav/WebDAVServlet.java index 06cf81dac5..4df6e1a7f3 100644 --- a/source/java/org/alfresco/repo/webdav/WebDAVServlet.java +++ b/source/java/org/alfresco/repo/webdav/WebDAVServlet.java @@ -347,8 +347,12 @@ public class WebDAVServlet extends HttpServlet m_davMethods.put(WebDAV.METHOD_PUT, PutMethod.class); m_davMethods.put(WebDAV.METHOD_UNLOCK, UnlockMethod.class); } + + protected WebDAVHelper getDAVHelper() + { + return m_davHelper; + } - /** * @param storeValue * @param rootPath @@ -437,6 +441,8 @@ public class WebDAVServlet extends HttpServlet private boolean enabled = false; private String storeName; private String rootPath; + private String urlPathPrefix; + public boolean getEnabled() { return enabled; @@ -477,5 +483,39 @@ public class WebDAVServlet extends HttpServlet { this.rootPath = rootPath; } + + /** + * Get the path prefix that generated URLs should exhibit, e.g. + *
+         *   http://server.name<prefix>/path/to/file.txt
+         * 
+ * In the default set up this would be of the form /context-path/servlet-name e.g. /alfresco/webdav: + *
+         *   http://server.name/alfresco/webdav/path/to/file.txt
+         * 
+ * however if using URL rewriting rules or a reverse proxy in front of the webdav server + * you may choose to use, for example / for shorter URLs. + *
+         *   http://server.name/path/to/file.txt
+         * 
+ *

+ * Leaving this property blank will cause the prefix used to be /context-path/servlet-name + * + * @return the urlPathPrefix + */ + public String getUrlPathPrefix() + { + return urlPathPrefix; + } + + /** + * See {@link #getUrlPathPrefix()} + * + * @param urlPathPrefix + */ + public void setUrlPathPrefix(String urlPathPrefix) + { + this.urlPathPrefix = urlPathPrefix; + } } }