diff --git a/src/main/java/com/github/sardine/DavResource.java b/src/main/java/com/github/sardine/DavResource.java index baeb3d7d..aad24262 100644 --- a/src/main/java/com/github/sardine/DavResource.java +++ b/src/main/java/com/github/sardine/DavResource.java @@ -8,6 +8,8 @@ package com.github.sardine; import com.github.sardine.model.Creationdate; +import com.github.sardine.model.Displayname; +import com.github.sardine.model.Getcontentlanguage; import com.github.sardine.model.Getcontentlength; import com.github.sardine.model.Getcontenttype; import com.github.sardine.model.Getetag; @@ -65,6 +67,8 @@ public class DavResource private final Date modified; private final String contentType; private final String etag; + private final String displayName; + private final String contentLanguage; private final Long contentLength; private final Map customProps; @@ -75,7 +79,8 @@ public class DavResource * @throws java.net.URISyntaxException If parsing the href from the response element fails */ protected DavResource(String href, Date creation, Date modified, String contentType, - Long contentLength, String etag, Map customProps) throws URISyntaxException + Long contentLength, String etag, String displayName, String contentLanguage, + Map customProps) throws URISyntaxException { this.href = new URI(href); this.creation = creation; @@ -83,6 +88,8 @@ protected DavResource(String href, Date creation, Date modified, String contentT this.contentType = contentType; this.contentLength = contentLength; this.etag = etag; + this.displayName = displayName; + this.contentLanguage = contentLanguage; this.customProps = customProps; } @@ -100,6 +107,8 @@ public DavResource(Response response) throws URISyntaxException this.contentType = this.getContentType(response); this.contentLength = this.getContentLength(response); this.etag = this.getEtag(response); + this.displayName = this.getDisplayName(response); + this.contentLanguage = this.getContentLanguage(response); this.customProps = this.getCustomProps(response); } @@ -251,6 +260,68 @@ private String getEtag(Response response) return null; } + /** + * Retrieves the content-language from prop. + * + * @param response The response complex type of the multistatus + * @return the content language; {@code null} if it is not avaialble + */ + private String getContentLanguage(Response response) + { + // Make sure that directories have the correct content type. + List list = response.getPropstat(); + if (list.isEmpty()) + { + return null; + } + for (Propstat propstat : list) + { + if (propstat.getProp() != null) { + Resourcetype resourcetype = propstat.getProp().getResourcetype(); + if ((resourcetype != null) && (resourcetype.getCollection() != null)) + { + // Need to correct the contentType to identify as a directory. + return HTTPD_UNIX_DIRECTORY_CONTENT_TYPE; + } + else + { + Getcontentlanguage gtl = propstat.getProp().getGetcontentlanguage(); + if ((gtl != null) && (gtl.getContent().size() == 1)) + { + return gtl.getContent().get(0); + } + } + } + } + return null; + } + + /** + * Retrieves displayName from props. + * + * @param response The response complex type of the multistatus + * @return the display name; {@code null} if it is not available + */ + private String getDisplayName(Response response) + { + List list = response.getPropstat(); + if (list.isEmpty()) + { + return null; + } + for (Propstat propstat : list) + { + if (propstat.getProp() != null) { + Displayname dn = propstat.getProp().getDisplayname(); + if ((dn != null) && (dn.getContent().size() == 1)) + { + return dn.getContent().get(0); + } + } + } + return null; + } + /** * Creates a simple complex Map from the given custom properties of a response. * This implementation does take namespaces into account. @@ -343,6 +414,22 @@ public String getEtag() return this.etag; } + /** + * @return Content language + */ + public String getContentLanguage() + { + return this.contentLanguage; + } + + /** + * @return Display name + */ + public String getDisplayName() + { + return this.displayName; + } + /** * Implementation assumes that every resource with a content type of httpd/unix-directory is a directory. * @@ -424,4 +511,4 @@ public String toString() { return this.getPath(); } -} \ No newline at end of file +} diff --git a/src/test/java/com/github/sardine/DavResourceTest.java b/src/test/java/com/github/sardine/DavResourceTest.java index 2e95e252..3b309ca5 100644 --- a/src/test/java/com/github/sardine/DavResourceTest.java +++ b/src/test/java/com/github/sardine/DavResourceTest.java @@ -34,7 +34,7 @@ public void testGetCreation() throws Exception { final Date creation = new Date(); DavResource folder = new DavResource("/test/path/", creation, null, null, -1L, null, - Collections.emptyMap()); + null, null, Collections.emptyMap()); assertEquals(creation, folder.getCreation()); } @@ -43,7 +43,7 @@ public void testGetModified() throws Exception { final Date modified = new Date(); DavResource folder = new DavResource("/test/path/", null, modified, null, -1L, null, - Collections.emptyMap()); + null, null, Collections.emptyMap()); assertEquals(modified, folder.getModified()); } @@ -51,7 +51,7 @@ public void testGetModified() throws Exception public void testGetContentType() throws Exception { DavResource folder = new DavResource("/test/path/", null, null, "httpd/unix-directory", new Long(-1), null, - Collections.emptyMap()); + null, null, Collections.emptyMap()); assertEquals("httpd/unix-directory", folder.getContentType()); } @@ -59,15 +59,31 @@ public void testGetContentType() throws Exception public void testGetContentLength() throws Exception { DavResource folder = new DavResource("/test/path/", null, null, null, 3423L, null, - Collections.emptyMap()); + null, null, Collections.emptyMap()); assertEquals(new Long(3423), folder.getContentLength()); } + @Test + public void testGetContentLanguage() throws Exception + { + DavResource folder = new DavResource("/test/path/", null, null, null, -1L, null, + null, "en_us", Collections.emptyMap()); + assertEquals("en_us", folder.getContentLanguage()); + } + + @Test + public void testDisplayname() throws Exception + { + DavResource folder = new DavResource("/test/path/", null, null, null, -1L, null, + "My path", null, Collections.emptyMap()); + assertEquals("My path", folder.getDisplayName()); + } + @Test public void testIsDirectory() throws Exception { DavResource folder = new DavResource("/test/path/", null, null, "httpd/unix-directory", new Long(-1), null, - Collections.emptyMap()); + null, null, Collections.emptyMap()); assertTrue(folder.isDirectory()); } @@ -76,12 +92,12 @@ public void testGetCustomProps() throws Exception { { DavResource file = new DavResource("/test/path/file.html", null, null, null, 6587L, null, - Collections.emptyMap()); + null, null, Collections.emptyMap()); assertNotNull(file.getCustomProps()); } { DavResource file = new DavResource("/test/path/file.html", null, null, null, 6587L, null, - Collections.singletonMap( + null, null, Collections.singletonMap( new QName("http://mynamespace", "property", "my"), "custom")); assertNotNull(file.getCustomProps()); assertEquals(file.getCustomProps(), Collections.singletonMap("property", "custom")); @@ -94,10 +110,10 @@ public void testGetCustomProps() throws Exception public void testGetName() throws Exception { DavResource folder = new DavResource("/test/path/", null, null, null, -1L, null, - Collections.emptyMap()); + null, null, Collections.emptyMap()); assertEquals("path", folder.getName()); DavResource file = new DavResource("/test/path/file.html", null, null, null, 6587L, null, - Collections.emptyMap()); + null, null, Collections.emptyMap()); assertEquals("file.html", file.getName()); } @@ -105,10 +121,10 @@ public void testGetName() throws Exception public void testGetPath() throws Exception { DavResource folder = new DavResource("/test/path/", null, null, null, -1L, null, - Collections.emptyMap()); + null, null, Collections.emptyMap()); assertEquals("/test/path/", folder.getPath()); DavResource file = new DavResource("/test/path/file.html", null, null, null, 6587L, null, - Collections.emptyMap()); + null, null, Collections.emptyMap()); assertEquals("/test/path/file.html", file.getPath()); } @@ -117,13 +133,13 @@ public void testFullyQualifiedHref() throws Exception { { DavResource folder = new DavResource("/test/path/", null, null, "httpd/unix-directory", 3423L, null, - Collections.emptyMap()); + null, null, Collections.emptyMap()); assertEquals("/test/path/", folder.getPath()); } { DavResource folder = new DavResource("http://example.net/test/path/", null, null, "httpd/unix-directory", 3423L, null, - Collections.emptyMap()); + null, null, Collections.emptyMap()); assertEquals("/test/path/", folder.getPath()); } } @@ -134,13 +150,13 @@ public void testUriEncoding() throws Exception { DavResource resource = new DavResource("http://example.net/path/%C3%A4%C3%B6%C3%BC/", null, null, "httpd/unix-directory", 3423L, null, - Collections.emptyMap()); + null, null, Collections.emptyMap()); assertEquals("/path/äöü/", resource.getPath()); assertEquals("/path/%C3%A4%C3%B6%C3%BC/", resource.getHref().getRawPath()); } { DavResource resource = new DavResource("/Meine%20Anlagen", null, null, "httpd/unix-directory", 0L, null, - Collections.emptyMap()); + null, null, Collections.emptyMap()); assertEquals("/Meine Anlagen", resource.getPath()); assertEquals("/Meine%20Anlagen", resource.getHref().getRawPath()); }