Skip to content

Commit

Permalink
implemented versioning
Browse files Browse the repository at this point in the history
  • Loading branch information
epam-java committed May 18, 2020
1 parent 74cccd9 commit 1318d8b
Show file tree
Hide file tree
Showing 9 changed files with 299 additions and 0 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 @@ -86,6 +86,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 @@ -168,6 +199,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 @@ -389,6 +431,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
43 changes: 43 additions & 0 deletions src/main/java/com/github/sardine/impl/SardineImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
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.HttpCheckin;
import com.github.sardine.impl.methods.HttpCheckout;
import com.github.sardine.impl.methods.HttpCopy;
import com.github.sardine.impl.methods.HttpLock;
import com.github.sardine.impl.methods.HttpMkCol;
Expand All @@ -39,6 +41,7 @@
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.impl.methods.HttpVersionControl;
import com.github.sardine.model.Ace;
import com.github.sardine.model.Acl;
import com.github.sardine.model.Allprop;
Expand Down Expand Up @@ -66,6 +69,7 @@
import com.github.sardine.model.Set;
import com.github.sardine.model.Write;
import com.github.sardine.report.SardineReport;
import com.github.sardine.report.VersionTreeReport;
import com.github.sardine.util.SardineUtil;
import org.apache.http.Consts;
import org.apache.http.Header;
Expand Down Expand Up @@ -139,6 +143,8 @@
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 @@ -414,6 +420,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 @@ -632,6 +653,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 @@ -787,6 +823,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 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 @@ -282,6 +282,10 @@ public SyncCollection createSyncCollection() {
return new SyncCollection();
}

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

/**
* Create an instance of {@link Limit }
*
Expand Down
38 changes: 38 additions & 0 deletions src/main/java/com/github/sardine/model/VersionTree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.github.sardine.model;

import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"prop"
})
@XmlRootElement(name = "version-tree")
public class VersionTree {

@XmlElement
protected Prop prop;

/**
* Gets the value of the prop property.
*
* @return
* possible object is
* {@link Prop }
*
*/
public Prop getProp() {
return prop;
}

/**
* Sets the value of the prop property.
*
* @param value
* allowed object is
* {@link Prop }
*
*/
public void setProp(Prop value) {
this.prop = value;
}
}
59 changes: 59 additions & 0 deletions src/main/java/com/github/sardine/report/VersionTreeReport.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.github.sardine.report;

import com.github.sardine.DavResource;
import com.github.sardine.model.*;
import com.github.sardine.util.SardineUtil;
import org.w3c.dom.Element;

import javax.xml.namespace.QName;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.logging.Logger;

public class VersionTreeReport extends SardineReport<List<DavResource>>
{

private static final Logger log = Logger.getLogger(SardineReport.class.getName());

private final Set<QName> properties;

public VersionTreeReport(Set<QName> properties)
{
this.properties = properties;
}

@Override
public Object toJaxb()
{
Prop prop = new Prop();
List<Element> any = prop.getAny();
for (QName entry : properties)
{
any.add(SardineUtil.createElement(entry));
}

VersionTree versionTree = new VersionTree();
versionTree.setProp(prop);
return versionTree;
}

@Override
public List<DavResource> fromMultistatus(Multistatus multistatus)
{
List<Response> responses = multistatus.getResponse();
List<DavResource> resources = new ArrayList<DavResource>(responses.size());
for (Response response : responses) {
try
{
resources.add(new DavResource(response));
}
catch (URISyntaxException e)
{
log.warning(String.format("Ignore resource with invalid URI %s", response.getHref().get(0)));
}
}
return resources;
}
}
8 changes: 8 additions & 0 deletions webdav.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,14 @@
</complexType>
</element>

<element name="version-tree">
<complexType>
<sequence>
<element ref="dav:prop" minOccurs="0"/>
</sequence>
</complexType>
</element>

<element name="sync-collection">
<complexType>
<sequence>
Expand Down

0 comments on commit 1318d8b

Please sign in to comment.