1 package org
.argeo
.cms
.dav
;
3 import java
.io
.IOException
;
4 import java
.io
.InputStream
;
5 import java
.net
.Authenticator
;
6 import java
.net
.PasswordAuthentication
;
8 import java
.net
.http
.HttpClient
;
9 import java
.net
.http
.HttpRequest
;
10 import java
.net
.http
.HttpRequest
.BodyPublishers
;
11 import java
.net
.http
.HttpResponse
;
12 import java
.net
.http
.HttpResponse
.BodyHandler
;
13 import java
.net
.http
.HttpResponse
.BodyHandlers
;
14 import java
.util
.Iterator
;
16 import javax
.xml
.namespace
.QName
;
18 import org
.argeo
.cms
.http
.HttpHeader
;
19 import org
.argeo
.cms
.http
.HttpMethod
;
20 import org
.argeo
.cms
.http
.HttpStatus
;
22 public class DavClient
{
24 private HttpClient httpClient
;
27 httpClient
= HttpClient
.newBuilder() //
28 // .sslContext(insecureContext()) //
29 .version(HttpClient
.Version
.HTTP_1_1
) //
30 .authenticator(new Authenticator() {
33 protected PasswordAuthentication
getPasswordAuthentication() {
34 return new PasswordAuthentication("root", "demo".toCharArray());
41 public void setProperty(String url
, QName key
, String value
) {
44 <?xml version="1.0" encoding="utf
-8" ?>
45 <D:propertyupdate xmlns:D="DAV
:"
47 + "xmlns:" + key
.getPrefix() + "=\"" + key
.getNamespaceURI() + "\">" + //
52 + "<" + key
.getPrefix() + ":" + key
.getLocalPart() + ">" + value
+ "</" + key
.getPrefix() + ":"
53 + key
.getLocalPart() + ">" + //
59 System
.out
.println(body
);
60 HttpRequest request
= HttpRequest
.newBuilder().uri(URI
.create(url
)) //
61 .header(HttpHeader
.DEPTH
.getHeaderName(), DavDepth
.DEPTH_1
.getValue()) //
62 .method(HttpMethod
.PROPPATCH
.name(), BodyPublishers
.ofString(body
)) //
64 BodyHandler
<String
> bodyHandler
= BodyHandlers
.ofString();
65 HttpResponse
<String
> response
= httpClient
.send(request
, bodyHandler
);
66 System
.out
.println(response
.body());
67 } catch (IOException
| InterruptedException e
) {
68 // TODO Auto-generated catch block
73 public Iterator
<DavResponse
> listChildren(URI uri
) {
76 <?xml version="1.0" encoding="utf
-8" ?>
77 <D:propfind xmlns:D="DAV
:">
80 HttpRequest request
= HttpRequest
.newBuilder().uri(uri
) //
81 .header(HttpHeader
.DEPTH
.getHeaderName(), DavDepth
.DEPTH_1
.getValue()) //
82 .method(HttpMethod
.PROPFIND
.name(), BodyPublishers
.ofString(body
)) //
85 HttpResponse
<String
> responseStr
= httpClient
.send(request
, BodyHandlers
.ofString());
86 System
.out
.println(responseStr
.body());
88 HttpResponse
<InputStream
> response
= httpClient
.send(request
, BodyHandlers
.ofInputStream());
89 MultiStatusReader msReader
= new MultiStatusReader(response
.body(), uri
.getPath());
91 } catch (IOException
| InterruptedException e
) {
92 throw new IllegalStateException("Cannot list children of " + uri
, e
);
97 public boolean exists(URI uri
) {
99 HttpRequest request
= HttpRequest
.newBuilder().uri(uri
) //
100 .header(HttpHeader
.DEPTH
.getHeaderName(), DavDepth
.DEPTH_0
.getValue()) //
101 .method(HttpMethod
.HEAD
.name(), BodyPublishers
.noBody()) //
103 BodyHandler
<String
> bodyHandler
= BodyHandlers
.ofString();
104 HttpResponse
<String
> response
= httpClient
.send(request
, bodyHandler
);
105 System
.out
.println(response
.body());
106 int responseStatusCode
= response
.statusCode();
107 if (responseStatusCode
== HttpStatus
.NOT_FOUND
.getCode())
109 if (responseStatusCode
>= 200 && responseStatusCode
< 300)
111 throw new IllegalStateException(
112 "Cannot check whether " + uri
+ " exists: Unknown response status code " + responseStatusCode
);
113 } catch (IOException
| InterruptedException e
) {
114 throw new IllegalStateException("Cannot check whether " + uri
+ " exists", e
);
119 public DavResponse
get(URI uri
) {
122 <?xml version="1.0" encoding="utf
-8" ?>
123 <D:propfind xmlns:D="DAV
:">
126 HttpRequest request
= HttpRequest
.newBuilder().uri(uri
) //
127 .header(HttpHeader
.DEPTH
.getHeaderName(), DavDepth
.DEPTH_0
.getValue()) //
128 .method(HttpMethod
.PROPFIND
.name(), BodyPublishers
.ofString(body
)) //
131 // HttpResponse<String> responseStr = httpClient.send(request, BodyHandlers.ofString());
132 // System.out.println(responseStr.body());
134 HttpResponse
<InputStream
> response
= httpClient
.send(request
, BodyHandlers
.ofInputStream());
135 MultiStatusReader msReader
= new MultiStatusReader(response
.body());
136 if (!msReader
.hasNext())
137 throw new IllegalArgumentException(uri
+ " does not exist");
138 return msReader
.next();
139 } catch (IOException
| InterruptedException e
) {
140 throw new IllegalStateException("Cannot list children of " + uri
, e
);
145 public static void main(String
[] args
) {
146 DavClient davClient
= new DavClient();
147 // Iterator<DavResponse> responses = davClient
148 // .listChildren(URI.create("http://localhost/unstable/a2/org.argeo.tp.sdk/"));
149 Iterator
<DavResponse
> responses
= davClient
150 .listChildren(URI
.create("http://root:demo@localhost:7070/api/acr/srv/example"));
151 while (responses
.hasNext()) {
152 DavResponse response
= responses
.next();
153 System
.out
.println(response
.getHref() + (response
.isCollection() ?
" (collection)" : ""));
154 //System.out.println(" " + response.getPropertyNames(HttpStatus.OK));
157 // davClient.setProperty("http://localhost/unstable/a2/org.argeo.tp.sdk/org.opentest4j.1.2.jar",
158 // CrName.uuid.qName(), UUID.randomUUID().toString());