Skip to content

Commit

Permalink
Add support to get active lock token from lock discovery. Resolves lo…
Browse files Browse the repository at this point in the history
  • Loading branch information
dkocher committed Jun 16, 2023
1 parent 18dcfa0 commit 628f7b5
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 4 deletions.
60 changes: 57 additions & 3 deletions src/main/java/com/github/sardine/DavResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

package com.github.sardine;

import com.github.sardine.model.Activelock;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
Expand All @@ -31,6 +32,8 @@
import com.github.sardine.model.Getcontenttype;
import com.github.sardine.model.Getetag;
import com.github.sardine.model.Getlastmodified;
import com.github.sardine.model.Lockdiscovery;
import com.github.sardine.model.Locktoken;
import com.github.sardine.model.Propstat;
import com.github.sardine.model.Report;
import com.github.sardine.model.Resourcetype;
Expand Down Expand Up @@ -87,14 +90,15 @@ private class DavProperties
final String contentType;
final String etag;
final String displayName;
final String lockToken;
final List<QName> resourceTypes;
final String contentLanguage;
final Long contentLength;
final List<QName> supportedReports;
final Map<QName, String> customProps;

DavProperties(Date creation, Date modified, String contentType,
Long contentLength, String etag, String displayName, List<QName> resourceTypes,
Long contentLength, String etag, String displayName, String lockToken, List<QName> resourceTypes,
String contentLanguage, List<QName> supportedReports, Map<QName, String> customProps)
{
this.creation = creation;
Expand All @@ -103,6 +107,7 @@ private class DavProperties
this.contentLength = contentLength;
this.etag = etag;
this.displayName = displayName;
this.lockToken = lockToken;
this.resourceTypes = resourceTypes;
this.contentLanguage = contentLanguage;
this.supportedReports = supportedReports;
Expand All @@ -116,6 +121,7 @@ private class DavProperties
this.contentLength = getContentLength(response);
this.etag = getEtag(response);
this.displayName = getDisplayName(response);
this.lockToken = getLockToken(response);
this.resourceTypes = getResourceTypes(response);
this.contentLanguage = getContentLanguage(response);
this.supportedReports = getSupportedReports(response);
Expand All @@ -130,13 +136,13 @@ private class DavProperties
* @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, String displayName, List<QName> resourceTypes,
Long contentLength, String etag, String displayName, String lockToken, List<QName> resourceTypes,
String contentLanguage, List<QName> supportedReports, Map<QName, String> customProps)
throws URISyntaxException
{
this.href = new URI(href);
this.status = DEFAULT_STATUS_CODE;
this.props = new DavProperties(creation, modified, contentType, contentLength, etag, displayName,
this.props = new DavProperties(creation, modified, contentType, contentLength, etag, displayName, lockToken,
resourceTypes, contentLanguage, supportedReports, customProps);
}

Expand Down Expand Up @@ -204,6 +210,46 @@ private String getModifiedDate(Response response)
return null;
}

/**
* Retrieves locktocken from props. If it is not available return null.
*
* @param response The response complex type of the multistatus
* @return Null if not found in props
*/
private String getLockToken(Response response)
{
List<Propstat> list = response.getPropstat();
if (list.isEmpty())
{
return null;
}
for (Propstat propstat : list)
{
if (propstat.getProp() != null) {
Lockdiscovery ld = propstat.getProp().getLockdiscovery();
if (ld != null)
{
if (ld.getActivelock().size() == 1)
{
final Activelock al = ld.getActivelock().get(0);
if (al != null)
{
final Locktoken lt = al.getLocktoken();
if (lt != null)
{
if (lt.getHref().size() == 1)
{
return lt.getHref().get(0);
}
}
}
}
}
}
}
return null;
}

/**
* Retrieves creationdate from props. If it is not available return null.
*
Expand Down Expand Up @@ -552,6 +598,14 @@ public String getDisplayName()
return this.props.displayName;
}

/**
* @return Lock Token
*/
public String getLockToken()
{
return this.props.lockToken;
}

/**
* @return Resource types
*/
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/github/sardine/impl/SardineImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,7 @@ public List<DavResource> list(String url, int depth, java.util.Set<QName> props)
prop.setGetcontenttype(objectFactory.createGetcontenttype());
prop.setResourcetype(objectFactory.createResourcetype());
prop.setGetetag(objectFactory.createGetetag());
prop.setLockdiscovery(objectFactory.createLockdiscovery());
addCustomProperties(prop, props);
body.setProp(prop);
return propfind(url, depth, body);
Expand Down
8 changes: 7 additions & 1 deletion src/test/java/com/github/sardine/DavResourceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ private static class Builder
private String contentType;
private String etag;
private String displayName;
private String lockToken;
private List<QName> resourceTypes= Collections.<QName>emptyList();
private String contentLanguage;
private Long contentLength = -1L;
Expand Down Expand Up @@ -90,6 +91,11 @@ Builder withDisplayName(String displayName) {
return this;
}

Builder withLockToken(String lockToken) {
this.lockToken = lockToken;
return this;
}

Builder withResourceTypes(List<QName> resourceTypes) {
this.resourceTypes = resourceTypes;
return this;
Expand All @@ -114,7 +120,7 @@ Builder withCustomProps(Map<QName, String> customProps) {
DavResource build() throws URISyntaxException
{
return new DavResource(href, creation, modified, contentType, contentLength, etag,
displayName, resourceTypes, contentLanguage, supportedReports, customProps);
displayName, lockToken, resourceTypes, contentLanguage, supportedReports, customProps);
}
}

Expand Down

0 comments on commit 628f7b5

Please sign in to comment.