/
SyncCollectionReport.java
120 lines (102 loc) · 2.72 KB
/
SyncCollectionReport.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package com.github.sardine.report;
import java.math.BigInteger;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.logging.Logger;
import javax.xml.namespace.QName;
import org.w3c.dom.Element;
import com.github.sardine.DavResource;
import com.github.sardine.model.Limit;
import com.github.sardine.model.Multistatus;
import com.github.sardine.model.Prop;
import com.github.sardine.model.Response;
import com.github.sardine.model.SyncCollection;
import com.github.sardine.util.SardineUtil;
public class SyncCollectionReport extends SardineReport<SyncCollectionReport.Result>
{
private static final Logger log = Logger.getLogger(SardineReport.class.getName());
public enum SyncLevel
{
LEVEL_1("1"),
LEVEL_INFINITY("infinite");
private final String value;
SyncLevel(String value)
{
this.value = value;
}
public String toString()
{
return value;
}
}
private final String syncToken;
private final SyncLevel syncLevel;
private final Set<QName> properties;
private final Integer limit;
public SyncCollectionReport(String syncToken, SyncLevel syncLevel, Set<QName> properties, Integer limit)
{
this.syncToken = syncToken;
this.syncLevel = syncLevel;
this.properties = properties;
this.limit = limit;
}
@Override
public Object toJaxb()
{
Prop prop = new Prop();
List<Element> any = prop.getAny();
for (QName entry : properties)
{
any.add(SardineUtil.createElement(entry));
}
SyncCollection syncCollection = new SyncCollection();
syncCollection.setSyncToken(syncToken == null ? "" : syncToken);
syncCollection.setSyncLevel(syncLevel.toString());
syncCollection.setProp(prop);
if (limit != null && limit > 0)
{
Limit l = new Limit();
l.setNresults(BigInteger.valueOf(limit));
syncCollection.setLimit(l);
}
return syncCollection;
}
@Override
public Result 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 new Result(resources, multistatus.getSyncToken());
}
public static class Result
{
private final List<DavResource> resources;
private final String syncToken;
Result(List<DavResource> resources, String syncToken)
{
this.resources = resources;
this.syncToken = syncToken;
}
public List<DavResource> getResources()
{
return resources;
}
public String getSyncToken()
{
return syncToken;
}
}
}