Skip to content

Commit

Permalink
Merge pull request #322 from Ilia-poliakov/feature/versioning
Browse files Browse the repository at this point in the history
implemented webdav resource versioning
  • Loading branch information
dkocher authored Jan 10, 2025
2 parents 0aca217 + a17b054 commit 1aa3ec1
Show file tree
Hide file tree
Showing 9 changed files with 324 additions and 85 deletions.
69 changes: 69 additions & 0 deletions src/main/java/com/github/sardine/Sardine.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,37 @@ public interface Sardine
*/
List<DavResource> list(String url, int depth, boolean allProp) throws IOException;

/**
* Gets versions listing of resource.
*
* @param url Path to the resource including protocol and hostname
* @throws IOException I/O error or HTTP response validation failure
*/
List<DavResource> versionsList(String url) throws IOException;

/**
* Gets versions listing of resource.
*
* @param url Path to the resource including protocol and hostname
* @param depth The depth to look at (use 0 for single resource, 1 for directory listing,
* -1 for infinite recursion)
* @throws IOException I/O error or HTTP response validation failure
*
*/
List<DavResource> versionsList(String url, int depth) throws IOException;

/**
* Gets versions listing of resource.
*
* @param url Path to the resource including protocol and hostname
* @param depth The depth to look at (use 0 for single resource, 1 for directory listing,
* -1 for infinite recursion)
* @param props Set of properties to be requested
* @throws IOException I/O error or HTTP response validation failure
*
*/
List<DavResource> versionsList(String url, int depth, Set<QName> props) throws IOException;

/**
* Fetches a resource using WebDAV <code>PROPFIND</code>. Only the specified properties
* are retrieved.
Expand Down Expand Up @@ -182,6 +213,17 @@ public interface Sardine
*/
InputStream get(String url) throws IOException;

/**
* Uses HTTP <code>GET</code> to download specific version of data from a server.
* The stream must be closed after reading.
*
* @param url Path to the resource including protocol and hostname
* @param version version of resource
* @return Data stream to read from
* @throws IOException I/O error or HTTP response validation failure
*/
InputStream get(String url, String version) throws IOException;

/**
* Uses HTTP <code>GET</code> to download data from a server. The stream must be closed after reading.
*
Expand Down Expand Up @@ -434,6 +476,33 @@ public interface Sardine
*/
void unlock(String url, String token) throws IOException;

/**
* Put the resource under version control.
*
* @param url Path to the resource including protocol and hostname
* @throws IOException I/O error or HTTP response validation failure
*/
void addToVersionControl(String url) throws IOException;

/**
* CHECKOUT request can be applied only to a checked-in version-controlled resource
* to allow modifications to the content and properties of that version-controlled resource.
*
* @param url Path to the <b>checked-in, version-controlled</b> resource including protocol and hostname
* @throws IOException I/O error or HTTP response validation failure
*/
void checkout(String url) throws IOException;

/**
* CHECKIN request can be applied to a checked-out version-controlled
* resource to produce a new version whose content and properties
* are copied from the checked-out resource.
*
* @param url Path to the <b>checked-out, version-controlled</b> resource including protocol and hostname
* @throws IOException I/O error or HTTP response validation failure
*/
void checkin(String url) throws IOException;

/**
* Read access control list for resource
*
Expand Down
153 changes: 68 additions & 85 deletions src/main/java/com/github/sardine/impl/SardineImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,19 @@

package com.github.sardine.impl;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.ProxySelector;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.logging.Logger;

import javax.xml.namespace.QName;

import org.apache.http.Consts;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import com.github.sardine.*;
import com.github.sardine.impl.handler.ExistsResponseHandler;
import com.github.sardine.impl.handler.LockResponseHandler;
import com.github.sardine.impl.handler.MultiStatusResponseHandler;
import com.github.sardine.impl.handler.VoidResponseHandler;
import com.github.sardine.impl.io.ContentLengthInputStream;
import com.github.sardine.impl.io.HttpMethodReleaseInputStream;
import com.github.sardine.impl.methods.*;
import com.github.sardine.model.*;
import com.github.sardine.report.SardineReport;
import com.github.sardine.report.VersionTreeReport;
import com.github.sardine.util.SardineUtil;
import org.apache.http.*;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.AuthState;
import org.apache.http.auth.NTCredentials;
Expand All @@ -49,12 +39,7 @@
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.config.AuthSchemes;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpHead;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.methods.*;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.client.protocol.RequestAcceptEncoding;
import org.apache.http.client.protocol.ResponseContentEncoding;
Expand All @@ -73,11 +58,7 @@
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.BasicAuthCache;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.client.*;
import org.apache.http.impl.conn.DefaultSchemePortResolver;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.impl.conn.SystemDefaultRoutePlanner;
Expand All @@ -89,57 +70,22 @@
import org.apache.http.util.VersionInfo;
import org.w3c.dom.Element;

import com.github.sardine.DavAce;
import com.github.sardine.DavAcl;
import com.github.sardine.DavPrincipal;
import com.github.sardine.DavQuota;
import com.github.sardine.DavResource;
import com.github.sardine.Sardine;
import com.github.sardine.Version;
import com.github.sardine.impl.handler.ExistsResponseHandler;
import com.github.sardine.impl.handler.LockResponseHandler;
import com.github.sardine.impl.handler.MultiStatusResponseHandler;
import com.github.sardine.impl.handler.VoidResponseHandler;
import com.github.sardine.impl.io.ContentLengthInputStream;
import com.github.sardine.impl.io.HttpMethodReleaseInputStream;
import com.github.sardine.impl.methods.HttpAcl;
import com.github.sardine.impl.methods.HttpCopy;
import com.github.sardine.impl.methods.HttpLock;
import com.github.sardine.impl.methods.HttpMkCol;
import com.github.sardine.impl.methods.HttpMove;
import com.github.sardine.impl.methods.HttpPropFind;
import com.github.sardine.impl.methods.HttpPropPatch;
import com.github.sardine.impl.methods.HttpReport;
import com.github.sardine.impl.methods.HttpSearch;
import com.github.sardine.impl.methods.HttpUnlock;
import com.github.sardine.model.Ace;
import com.github.sardine.model.Acl;
import com.github.sardine.model.Allprop;
import com.github.sardine.model.Displayname;
import com.github.sardine.model.Exclusive;
import com.github.sardine.model.Group;
import com.github.sardine.model.Lockinfo;
import com.github.sardine.model.Lockscope;
import com.github.sardine.model.Locktype;
import com.github.sardine.model.Multistatus;
import com.github.sardine.model.ObjectFactory;
import com.github.sardine.model.Owner;
import com.github.sardine.model.PrincipalCollectionSet;
import com.github.sardine.model.PrincipalURL;
import com.github.sardine.model.Prop;
import com.github.sardine.model.Propertyupdate;
import com.github.sardine.model.Propfind;
import com.github.sardine.model.Propstat;
import com.github.sardine.model.QuotaAvailableBytes;
import com.github.sardine.model.QuotaUsedBytes;
import com.github.sardine.model.Remove;
import com.github.sardine.model.Resourcetype;
import com.github.sardine.model.Response;
import com.github.sardine.model.SearchRequest;
import com.github.sardine.model.Set;
import com.github.sardine.model.Write;
import com.github.sardine.report.SardineReport;
import com.github.sardine.util.SardineUtil;
import javax.xml.namespace.QName;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.ProxySelector;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.logging.Logger;

import static com.github.sardine.util.SardineUtil.createQNameWithDefaultNamespace;

/**
* Implementation of the Sardine interface. This is where the meat of the Sardine library lives.
Expand Down Expand Up @@ -416,6 +362,21 @@ public List<DavResource> list(String url, int depth, boolean allProp) throws IOE
}
}

@Override
public List<DavResource> versionsList(String url) throws IOException {
return versionsList(url, 0);
}

@Override
public List<DavResource> versionsList(String url, int depth) throws IOException {
return versionsList(url, depth, Collections.<QName>emptySet());
}

@Override
public List<DavResource> versionsList(String url, int depth, java.util.Set<QName> props) throws IOException {
return report(url, depth, new VersionTreeReport(props));
}

@Override
public List<DavResource> list(String url, int depth, java.util.Set<QName> props) throws IOException
{
Expand Down Expand Up @@ -645,6 +606,21 @@ public void unlock(String url, String token) throws IOException
this.execute(entity, new VoidResponseHandler());
}

@Override
public void addToVersionControl(String url) throws IOException {
this.execute(new HttpVersionControl(url), new VoidResponseHandler());
}

@Override
public void checkout(String url) throws IOException {
this.execute(new HttpCheckout(url), new VoidResponseHandler());
}

@Override
public void checkin(String url) throws IOException {
this.execute(new HttpCheckin(url), new VoidResponseHandler());
}

@Override
public void setAcl(String url, List<DavAce> aces) throws IOException
{
Expand Down Expand Up @@ -808,6 +784,13 @@ public ContentLengthInputStream get(String url) throws IOException
return this.get(url, Collections.<String, String>emptyMap());
}

@Override
public ContentLengthInputStream get(String url, String version) throws IOException {
List<DavResource> versionHistory = propfind(url, 0, Collections.singleton(createQNameWithDefaultNamespace("version-history")));
String storageUrl = versionHistory.get(0).getCustomProps().get("version-history");
return this.get(storageUrl + version);
}

@Override
public ContentLengthInputStream get(String url, Map<String, String> headers) throws IOException
{
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/com/github/sardine/impl/methods/HttpCheckin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.github.sardine.impl.methods;

import org.apache.http.client.methods.HttpRequestBase;

import java.net.URI;

/**
* Simple class for making WebDAV <code>CHECKIN</code> requests.
*/
public class HttpCheckin extends HttpRequestBase {

public static final String METHOD_NAME = "CHECKIN";

public HttpCheckin(String uri) {
this(URI.create(uri));
}

public HttpCheckin(URI uri) {
this.setURI(uri);
}

@Override
public String getMethod() {
return METHOD_NAME;
}
}
26 changes: 26 additions & 0 deletions src/main/java/com/github/sardine/impl/methods/HttpCheckout.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.github.sardine.impl.methods;

import org.apache.http.client.methods.HttpRequestBase;

import java.net.URI;

/**
* Simple class for making WebDAV <code>CHECKOUT</code> requests.
*/
public class HttpCheckout extends HttpRequestBase {

public static final String METHOD_NAME = "CHECKOUT";

public HttpCheckout(String uri) {
this(URI.create(uri));
}

public HttpCheckout(URI uri) {
this.setURI(uri);
}

@Override
public String getMethod() {
return METHOD_NAME;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.github.sardine.impl.methods;

import org.apache.http.client.methods.HttpRequestBase;

import java.net.URI;

/**
* Simple class for making WebDAV <code>VERSION-CONTROL</code> requests.
*/
public class HttpVersionControl extends HttpRequestBase {

public static final String METHOD_NAME = "VERSION-CONTROL";

public HttpVersionControl(String uri) {
this(URI.create(uri));
}

public HttpVersionControl(URI uri) {
this.setURI(uri);
}

@Override
public String getMethod() {
return METHOD_NAME;
}
}
4 changes: 4 additions & 0 deletions src/main/java/com/github/sardine/model/ObjectFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,10 @@ public SyncCollection createSyncCollection() {
return new SyncCollection();
}

public VersionTree createVersionTree() {
return new VersionTree();
}

/**
* Create an instance of {@link Limit }
*
Expand Down
Loading

0 comments on commit 1aa3ec1

Please sign in to comment.