/
RestOperations.java
174 lines (140 loc) · 6.55 KB
/
RestOperations.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package es.aldago.dctm;
import java.io.IOException;
import java.math.BigInteger;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpRequest.BodyPublisher;
import java.net.http.HttpRequest.BodyPublishers;
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandlers;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
public class RestOperations {
public static void main(String[] args) throws URISyntaxException, IOException, InterruptedException, ExecutionException {
String pathCabinets = "http://192.168.94.130:8080/dctm-rest/repositories/dctm202/cabinets";
String pathObject = "http://192.168.94.130:8080/dctm-rest/repositories/dctm202/cabinets/0c00271080000107";
String pathContent = "http://192.168.94.130:8080/dctm-rest/repositories/dctm202/objects/0c00271080000107/contents/content";
String pathFolder = "http://192.168.94.130:8080/dctm-rest/repositories/dctm202/folders/0c00271080000107/folders";
String pathFolderImport = "http://192.168.94.130:8080/dctm-rest/repositories/dctm202/folders/0c00271080000107/documents";
String pathDocumentDelete = "http://192.168.94.130:8080/dctm-rest/repositories/dctm202/documents/090027108000415c";
String pathFolderDelete = "http://192.168.94.130:8080/dctm-rest/repositories/dctm202/folders/0b00271080004127";
HttpClient client = HttpClient.newHttpClient();
String basicAuthStr = basicAuth("dmadmin", "dmadmin");
System.out.println ("*****get Cabinet List *****");
HttpRequest request = HttpRequest.newBuilder()
.uri(new URI(pathCabinets))
.GET()
.header("Authorization", basicAuthStr)
.build();
client.sendAsync(request, BodyHandlers.ofString())
.thenApply(HttpResponse::body)
.thenAccept(System.out::println)
.join();
System.out.println ("*****get Object Properties *****");
request = HttpRequest.newBuilder()
.uri(new URI(pathObject))
.GET()
.header("Authorization", basicAuthStr)
.build();
client.sendAsync(request, BodyHandlers.ofString())
.thenApply(HttpResponse::body)
.thenAccept(System.out::println)
.join();
System.out.println ("*****get Object Content *****");
request = HttpRequest.newBuilder()
.uri(new URI(pathContent))
.GET()
.header("Authorization", basicAuthStr)
.build();
CompletableFuture<String> response=client.sendAsync(request, BodyHandlers.ofString())
.thenApply(HttpResponse::body);
String content=response.get().toString();
//GET Conent file URL in a very dirty way
String fileURL=content.substring(content.indexOf("\"ACS\",\"href\":"));
fileURL=fileURL.substring("\"ACS\",\"href\":\"".length(), fileURL.indexOf("\"},"));
request = HttpRequest.newBuilder()
.uri(new URI(fileURL))
.GET()
.header("Authorization", basicAuthStr)
.build();
client.send(request, BodyHandlers.ofFile(Paths.get("C:/Temp/pdf.pdf")));
System.out.println ("*****create Folder on /Temp *****");
String json = "{\"properties\" : {\"object_name\" : \"RESTFOLDER\", \"r_object_type\" :\"dm_folder\"}}";
request = HttpRequest.newBuilder()
.uri(new URI(pathFolder))
.POST(BodyPublishers.ofString(json))
.headers("Authorization", basicAuthStr,"content-type","application/vnd.emc.documentum+json")
.build();
client.sendAsync(request, BodyHandlers.ofString())
.thenApply(HttpResponse::body)
.thenAccept(System.out::println)
.join();
System.out.println ("*****create Documentum on /Temp *****");
json = "{\"properties\" : {\"object_name\" : \"TESTDOCUMENT\", \"r_object_type\" :\"dm_document\"}}";
String boundary = new BigInteger(256, new Random()).toString();
Map<Object, Object> data = new HashMap<>();
data.put("metadata", json);
data.put("content", Paths.get("d:/dctm202/Documentum_Server_20.2_Release_Notes.pdf"));
request = HttpRequest.newBuilder()
.uri(new URI(pathFolderImport))
.POST(ofMimeMultipartData(data, boundary))
.headers("Authorization", basicAuthStr,"content-type","multipart/form-data;boundary="+boundary,"Accept","application/vnd.emc.documentum+json")
.build();
client.sendAsync(request, BodyHandlers.ofString())
.thenApply(HttpResponse::body)
.thenAccept(System.out::println)
.join();
System.out.println ("*****Delete Folder/Document on /Temp *****");
request = HttpRequest.newBuilder()
.uri(new URI(pathFolderDelete))
.DELETE()
.header("Authorization", basicAuthStr)
.build();
client.sendAsync(request, BodyHandlers.ofString())
.thenApply(HttpResponse::body)
.thenAccept(System.out::println)
.join();
request = HttpRequest.newBuilder()
.uri(new URI(pathDocumentDelete))
.DELETE()
.header("Authorization", basicAuthStr)
.build();
client.sendAsync(request, BodyHandlers.ofString())
.thenApply(HttpResponse::body)
.thenAccept(System.out::println)
.join();
}
private static String basicAuth(String username, String password) {
return "Basic " + Base64.getEncoder().encodeToString((username + ":" + password).getBytes());
}
public static BodyPublisher ofMimeMultipartData(Map<Object, Object> data, String boundary) throws IOException {
var byteArrays = new ArrayList<byte[]>();
byte[] separator = ("--" + boundary + "\r\nContent-Disposition: form-data; name=").getBytes(StandardCharsets.UTF_8);
for (Map.Entry<Object, Object> entry : data.entrySet()) {
byteArrays.add(separator);
if (entry.getValue() instanceof Path) {
var path = (Path) entry.getValue();
String mimeType = Files.probeContentType(path);
byteArrays.add(("\"" + entry.getKey() + "\"; filename=\"" + path.getFileName()+ "\"\r\nContent-Type: " + mimeType + "\r\n\r\n").getBytes(StandardCharsets.UTF_8));
byteArrays.add(Files.readAllBytes(path));
byteArrays.add("\r\n".getBytes(StandardCharsets.UTF_8));
}
else {
byteArrays.add(("\"" + entry.getKey() + "\"\r\n\r\n" + entry.getValue() + "\r\n").getBytes(StandardCharsets.UTF_8));
}
}
byteArrays.add(("--" + boundary + "--").getBytes(StandardCharsets.UTF_8));
return BodyPublishers.ofByteArrays(byteArrays);
}
}