Skip to content

Commit

Permalink
Merge pull request lookfirst#146 from mcdee/master
Browse files Browse the repository at this point in the history
Pass content language and display name through to DavResource
  • Loading branch information
David Kocher committed Jul 21, 2013
2 parents b111b11 + baeb80f commit aa8dd13
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 17 deletions.
91 changes: 89 additions & 2 deletions src/main/java/com/github/sardine/DavResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<QName, String> customProps;

Expand All @@ -75,14 +79,17 @@ 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<QName, String> customProps) throws URISyntaxException
Long contentLength, String etag, String displayName, String contentLanguage,
Map<QName, String> customProps) throws URISyntaxException
{
this.href = new URI(href);
this.creation = creation;
this.modified = modified;
this.contentType = contentType;
this.contentLength = contentLength;
this.etag = etag;
this.displayName = displayName;
this.contentLanguage = contentLanguage;
this.customProps = customProps;
}

Expand All @@ -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);
}

Expand Down Expand Up @@ -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<Propstat> 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<Propstat> 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.
Expand Down Expand Up @@ -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 <code>httpd/unix-directory</code> is a directory.
*
Expand Down Expand Up @@ -424,4 +511,4 @@ public String toString()
{
return this.getPath();
}
}
}
46 changes: 31 additions & 15 deletions src/test/java/com/github/sardine/DavResourceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.<QName, String>emptyMap());
null, null, Collections.<QName, String>emptyMap());
assertEquals(creation, folder.getCreation());
}

Expand All @@ -43,31 +43,47 @@ public void testGetModified() throws Exception
{
final Date modified = new Date();
DavResource folder = new DavResource("/test/path/", null, modified, null, -1L, null,
Collections.<QName, String>emptyMap());
null, null, Collections.<QName, String>emptyMap());
assertEquals(modified, folder.getModified());
}

@Test
public void testGetContentType() throws Exception
{
DavResource folder = new DavResource("/test/path/", null, null, "httpd/unix-directory", new Long(-1), null,
Collections.<QName, String>emptyMap());
null, null, Collections.<QName, String>emptyMap());
assertEquals("httpd/unix-directory", folder.getContentType());
}

@Test
public void testGetContentLength() throws Exception
{
DavResource folder = new DavResource("/test/path/", null, null, null, 3423L, null,
Collections.<QName, String>emptyMap());
null, null, Collections.<QName, String>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.<QName, String>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.<QName, String>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.<QName, String>emptyMap());
null, null, Collections.<QName, String>emptyMap());
assertTrue(folder.isDirectory());
}

Expand All @@ -76,12 +92,12 @@ public void testGetCustomProps() throws Exception
{
{
DavResource file = new DavResource("/test/path/file.html", null, null, null, 6587L, null,
Collections.<QName, String>emptyMap());
null, null, Collections.<QName, String>emptyMap());
assertNotNull(file.getCustomProps());
}
{
DavResource file = new DavResource("/test/path/file.html", null, null, null, 6587L, null,
Collections.<QName, String>singletonMap(
null, null, Collections.<QName, String>singletonMap(
new QName("http://mynamespace", "property", "my"), "custom"));
assertNotNull(file.getCustomProps());
assertEquals(file.getCustomProps(), Collections.singletonMap("property", "custom"));
Expand All @@ -94,21 +110,21 @@ public void testGetCustomProps() throws Exception
public void testGetName() throws Exception
{
DavResource folder = new DavResource("/test/path/", null, null, null, -1L, null,
Collections.<QName, String>emptyMap());
null, null, Collections.<QName, String>emptyMap());
assertEquals("path", folder.getName());
DavResource file = new DavResource("/test/path/file.html", null, null, null, 6587L, null,
Collections.<QName, String>emptyMap());
null, null, Collections.<QName, String>emptyMap());
assertEquals("file.html", file.getName());
}

@Test
public void testGetPath() throws Exception
{
DavResource folder = new DavResource("/test/path/", null, null, null, -1L, null,
Collections.<QName, String>emptyMap());
null, null, Collections.<QName, String>emptyMap());
assertEquals("/test/path/", folder.getPath());
DavResource file = new DavResource("/test/path/file.html", null, null, null, 6587L, null,
Collections.<QName, String>emptyMap());
null, null, Collections.<QName, String>emptyMap());
assertEquals("/test/path/file.html", file.getPath());
}

Expand All @@ -117,13 +133,13 @@ public void testFullyQualifiedHref() throws Exception
{
{
DavResource folder = new DavResource("/test/path/", null, null, "httpd/unix-directory", 3423L, null,
Collections.<QName, String>emptyMap());
null, null, Collections.<QName, String>emptyMap());
assertEquals("/test/path/", folder.getPath());
}
{
DavResource folder = new DavResource("http://example.net/test/path/", null, null,
"httpd/unix-directory", 3423L, null,
Collections.<QName, String>emptyMap());
null, null, Collections.<QName, String>emptyMap());
assertEquals("/test/path/", folder.getPath());
}
}
Expand All @@ -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.<QName, String>emptyMap());
null, null, Collections.<QName, String>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.<QName, String>emptyMap());
null, null, Collections.<QName, String>emptyMap());
assertEquals("/Meine Anlagen", resource.getPath());
assertEquals("/Meine%20Anlagen", resource.getHref().getRawPath());
}
Expand Down

0 comments on commit aa8dd13

Please sign in to comment.