]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/dav/DavClient.java
FS utils throws IOException
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / dav / DavClient.java
1 package org.argeo.cms.dav;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.net.Authenticator;
6 import java.net.PasswordAuthentication;
7 import java.net.URI;
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;
15
16 import javax.xml.namespace.QName;
17
18 import org.argeo.cms.http.HttpHeader;
19 import org.argeo.cms.http.HttpMethod;
20 import org.argeo.cms.http.HttpStatus;
21
22 public class DavClient {
23
24 private HttpClient httpClient;
25
26 public DavClient() {
27 httpClient = HttpClient.newBuilder() //
28 // .sslContext(insecureContext()) //
29 .version(HttpClient.Version.HTTP_1_1) //
30 .authenticator(new Authenticator() {
31
32 @Override
33 protected PasswordAuthentication getPasswordAuthentication() {
34 return new PasswordAuthentication("root", "demo".toCharArray());
35 }
36
37 }) //
38 .build();
39 }
40
41 public void setProperty(String url, QName key, String value) {
42 try {
43 String body = """
44 <?xml version="1.0" encoding="utf-8" ?>
45 <D:propertyupdate xmlns:D="DAV:"
46 """ //
47 + "xmlns:" + key.getPrefix() + "=\"" + key.getNamespaceURI() + "\">" + //
48 """
49 <D:set>
50 <D:prop>
51 """ //
52 + "<" + key.getPrefix() + ":" + key.getLocalPart() + ">" + value + "</" + key.getPrefix() + ":"
53 + key.getLocalPart() + ">" + //
54 """
55 </D:prop>
56 </D:set>
57 </D:propertyupdate>
58 """;
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)) //
63 .build();
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
69 e.printStackTrace();
70 }
71 }
72
73 public Iterator<DavResponse> listChildren(URI uri) {
74 try {
75 String body = """
76 <?xml version="1.0" encoding="utf-8" ?>
77 <D:propfind xmlns:D="DAV:">
78 <D:propname/>
79 </D:propfind>""";
80 HttpRequest request = HttpRequest.newBuilder().uri(uri) //
81 .header(HttpHeader.DEPTH.getHeaderName(), DavDepth.DEPTH_1.getValue()) //
82 .method(HttpMethod.PROPFIND.name(), BodyPublishers.ofString(body)) //
83 .build();
84
85 HttpResponse<String> responseStr = httpClient.send(request, BodyHandlers.ofString());
86 System.out.println(responseStr.body());
87
88 HttpResponse<InputStream> response = httpClient.send(request, BodyHandlers.ofInputStream());
89 MultiStatusReader msReader = new MultiStatusReader(response.body(), uri.getPath());
90 return msReader;
91 } catch (IOException | InterruptedException e) {
92 throw new IllegalStateException("Cannot list children of " + uri, e);
93 }
94
95 }
96
97 public boolean exists(URI uri) {
98 try {
99 HttpRequest request = HttpRequest.newBuilder().uri(uri) //
100 .header(HttpHeader.DEPTH.getHeaderName(), DavDepth.DEPTH_0.getValue()) //
101 .method(HttpMethod.HEAD.name(), BodyPublishers.noBody()) //
102 .build();
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())
108 return false;
109 if (responseStatusCode >= 200 && responseStatusCode < 300)
110 return true;
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);
115 }
116
117 }
118
119 public DavResponse get(URI uri) {
120 try {
121 String body = """
122 <?xml version="1.0" encoding="utf-8" ?>
123 <D:propfind xmlns:D="DAV:">
124 <D:allprop/>
125 </D:propfind>""";
126 HttpRequest request = HttpRequest.newBuilder().uri(uri) //
127 .header(HttpHeader.DEPTH.getHeaderName(), DavDepth.DEPTH_0.getValue()) //
128 .method(HttpMethod.PROPFIND.name(), BodyPublishers.ofString(body)) //
129 .build();
130
131 // HttpResponse<String> responseStr = httpClient.send(request, BodyHandlers.ofString());
132 // System.out.println(responseStr.body());
133
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);
141 }
142
143 }
144
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));
155
156 }
157 // davClient.setProperty("http://localhost/unstable/a2/org.argeo.tp.sdk/org.opentest4j.1.2.jar",
158 // CrName.uuid.qName(), UUID.randomUUID().toString());
159
160 }
161
162 }