]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.util/src/org/argeo/util/dav/DavClient.java
Improve ACR, introduce migration from JCR.
[lgpl/argeo-commons.git] / org.argeo.util / src / org / argeo / util / dav / DavClient.java
1 package org.argeo.util.dav;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.net.URI;
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;
13
14 import javax.xml.namespace.QName;
15
16 public class DavClient {
17
18 private HttpClient httpClient;
19
20 public DavClient() {
21 httpClient = HttpClient.newBuilder() //
22 // .sslContext(insecureContext()) //
23 .version(HttpClient.Version.HTTP_1_1) //
24 .build();
25 }
26
27 public void setProperty(String url, QName key, String value) {
28 try {
29 String body = """
30 <?xml version="1.0" encoding="utf-8" ?>
31 <D:propertyupdate xmlns:D="DAV:"
32 """ //
33 + "xmlns:" + key.getPrefix() + "=\"" + key.getNamespaceURI() + "\">" + //
34 """
35 <D:set>
36 <D:prop>
37 """ //
38 + "<" + key.getPrefix() + ":" + key.getLocalPart() + ">" + value + "</" + key.getPrefix() + ":"
39 + key.getLocalPart() + ">" + //
40 """
41 </D:prop>
42 </D:set>
43 </D:propertyupdate>
44 """;
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)) //
49 .build();
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
55 e.printStackTrace();
56 }
57 }
58
59 public Iterator<DavResponse> listChildren(URI uri) {
60 try {
61 String body = """
62 <?xml version="1.0" encoding="utf-8" ?>
63 <D:propfind xmlns:D="DAV:">
64 <D:propname/>
65 </D:propfind>""";
66 HttpRequest request = HttpRequest.newBuilder().uri(uri) //
67 .header(DavHeader.DEPTH.name(), "1") //
68 .method(DavMethod.PROPFIND.name(), BodyPublishers.ofString(body)) //
69 .build();
70
71 // HttpResponse<String> responseStr = httpClient.send(request, BodyHandlers.ofString());
72 // System.out.println(responseStr.body());
73
74 HttpResponse<InputStream> response = httpClient.send(request, BodyHandlers.ofInputStream());
75 MultiStatusReader msReader = new MultiStatusReader(response.body(), uri.getPath());
76 return msReader;
77 } catch (IOException | InterruptedException e) {
78 throw new IllegalStateException("Cannot list children of " + uri, e);
79 }
80
81 }
82
83 public boolean exists(URI uri) {
84 try {
85 HttpRequest request = HttpRequest.newBuilder().uri(uri) //
86 .header(DavHeader.DEPTH.name(), "0") //
87 .method(DavMethod.HEAD.name(), BodyPublishers.noBody()) //
88 .build();
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)
94 return false;
95 if (responseStatusCode >= 200 && responseStatusCode < 300)
96 return true;
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);
101 }
102
103 }
104
105 public DavResponse get(URI uri) {
106 try {
107 String body = """
108 <?xml version="1.0" encoding="utf-8" ?>
109 <D:propfind xmlns:D="DAV:">
110 <D:allprop/>
111 </D:propfind>""";
112 HttpRequest request = HttpRequest.newBuilder().uri(uri) //
113 .header(DavHeader.DEPTH.name(), "0") //
114 .method(DavMethod.PROPFIND.name(), BodyPublishers.ofString(body)) //
115 .build();
116
117 // HttpResponse<String> responseStr = httpClient.send(request, BodyHandlers.ofString());
118 // System.out.println(responseStr.body());
119
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);
127 }
128
129 }
130
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());
139
140 }
141 // davClient.setProperty("http://localhost/unstable/a2/org.argeo.tp.sdk/org.opentest4j.1.2.jar",
142 // CrName.uuid.qName(), UUID.randomUUID().toString());
143
144 }
145
146 }