1 package org
.argeo
.util
.dav
;
3 import java
.io
.IOException
;
4 import java
.io
.InputStream
;
6 import java
.net
.http
.HttpClient
;
7 import java
.net
.http
.HttpRequest
;
8 import java
.net
.http
.HttpRequest
.BodyPublishers
;
9 import java
.net
.http
.HttpResponse
;
10 import java
.net
.http
.HttpResponse
.BodyHandler
;
11 import java
.net
.http
.HttpResponse
.BodyHandlers
;
12 import java
.util
.Iterator
;
14 import javax
.xml
.namespace
.QName
;
16 public class DavClient
{
18 private HttpClient httpClient
;
21 httpClient
= HttpClient
.newBuilder() //
22 // .sslContext(insecureContext()) //
23 .version(HttpClient
.Version
.HTTP_1_1
) //
27 public void setProperty(String url
, QName key
, String value
) {
30 <?xml version="1.0" encoding="utf
-8" ?>
31 <D:propertyupdate xmlns:D="DAV
:"
33 + "xmlns:" + key
.getPrefix() + "=\"" + key
.getNamespaceURI() + "\">" + //
38 + "<" + key
.getPrefix() + ":" + key
.getLocalPart() + ">" + value
+ "</" + key
.getPrefix() + ":"
39 + key
.getLocalPart() + ">" + //
45 System
.out
.println(body
);
46 HttpRequest request
= HttpRequest
.newBuilder().uri(URI
.create(url
)) //
47 .header("Depth", "1") //
48 .method(DavMethod
.PROPPATCH
.name(), BodyPublishers
.ofString(body
)) //
50 BodyHandler
<String
> bodyHandler
= BodyHandlers
.ofString();
51 HttpResponse
<String
> response
= httpClient
.send(request
, bodyHandler
);
52 System
.out
.println(response
.body());
53 } catch (IOException
| InterruptedException e
) {
54 // TODO Auto-generated catch block
59 public Iterator
<DavResponse
> listChildren(URI uri
) {
62 <?xml version="1.0" encoding="utf
-8" ?>
63 <D:propfind xmlns:D="DAV
:">
66 HttpRequest request
= HttpRequest
.newBuilder().uri(uri
) //
67 .header(DavHeader
.Depth
.name(), "1") //
68 .method(DavMethod
.PROPFIND
.name(), BodyPublishers
.ofString(body
)) //
71 // HttpResponse<String> responseStr = httpClient.send(request, BodyHandlers.ofString());
72 // System.out.println(responseStr.body());
74 HttpResponse
<InputStream
> response
= httpClient
.send(request
, BodyHandlers
.ofInputStream());
75 MultiStatusReader msReader
= new MultiStatusReader(response
.body(), uri
.getPath());
77 } catch (IOException
| InterruptedException e
) {
78 throw new IllegalStateException("Cannot list children of " + uri
, e
);
83 public boolean exists(URI uri
) {
85 HttpRequest request
= HttpRequest
.newBuilder().uri(uri
) //
86 .header(DavHeader
.Depth
.name(), "0") //
87 .method(DavMethod
.HEAD
.name(), BodyPublishers
.noBody()) //
89 BodyHandler
<String
> bodyHandler
= BodyHandlers
.ofString();
90 HttpResponse
<String
> response
= httpClient
.send(request
, bodyHandler
);
91 System
.out
.println(response
.body());
92 int responseStatusCode
= response
.statusCode();
93 if (responseStatusCode
== 404)
95 if (responseStatusCode
>= 200 && responseStatusCode
< 300)
97 throw new IllegalStateException(
98 "Cannot check whether " + uri
+ " exists: Unknown response status code " + responseStatusCode
);
99 } catch (IOException
| InterruptedException e
) {
100 throw new IllegalStateException("Cannot check whether " + uri
+ " exists", e
);
105 public DavResponse
get(URI uri
) {
108 <?xml version="1.0" encoding="utf
-8" ?>
109 <D:propfind xmlns:D="DAV
:">
112 HttpRequest request
= HttpRequest
.newBuilder().uri(uri
) //
113 .header(DavHeader
.Depth
.name(), "0") //
114 .method(DavMethod
.PROPFIND
.name(), BodyPublishers
.ofString(body
)) //
117 // HttpResponse<String> responseStr = httpClient.send(request, BodyHandlers.ofString());
118 // System.out.println(responseStr.body());
120 HttpResponse
<InputStream
> response
= httpClient
.send(request
, BodyHandlers
.ofInputStream());
121 MultiStatusReader msReader
= new MultiStatusReader(response
.body());
122 if (!msReader
.hasNext())
123 throw new IllegalArgumentException(uri
+ " does not exist");
124 return msReader
.next();
125 } catch (IOException
| InterruptedException e
) {
126 throw new IllegalStateException("Cannot list children of " + uri
, e
);
131 public static void main(String
[] args
) {
132 DavClient davClient
= new DavClient();
133 Iterator
<DavResponse
> responses
= davClient
134 .listChildren(URI
.create("http://localhost/unstable/a2/org.argeo.tp.sdk/"));
135 while (responses
.hasNext()) {
136 DavResponse response
= responses
.next();
137 System
.out
.println(response
.getHref() + (response
.isCollection() ?
" (collection)" : ""));
138 System
.out
.println(" " + response
.getPropertyNames());
141 // davClient.setProperty("http://localhost/unstable/a2/org.argeo.tp.sdk/org.opentest4j.1.2.jar",
142 // CrName.uuid.qName(), UUID.randomUUID().toString());