/
ImportTutorial.java
164 lines (138 loc) · 7.83 KB
/
ImportTutorial.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
import okhttp3.*;
import org.json.JSONObject;
import java.io.File;
import java.net.CookieManager;
import java.net.CookiePolicy;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Base64;
import java.util.concurrent.TimeUnit;
public class ImportTutorial {
public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
public static final MediaType PLAINTEXT = MediaType.parse("text/plain; charset=utf-8");
public static final MediaType OCTETSTREAM = MediaType.parse("application/octetstream; charset=utf-8");
public static final String username = "";
public static final String userpassword = "";
public static final String auth = "Basic "+Base64.getEncoder().encodeToString((username+":"+userpassword).getBytes());
public static final String tenant = "";
public static final String baseUrl = "";
public static void main(String[] args) {
try {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.cookieJar(new JavaNetCookieJar(new CookieManager(null, CookiePolicy.ACCEPT_ALL)));
OkHttpClient client = builder.build();
//Single Import
RequestBody singleImportRequestBody = new MultipartBody
.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("data",
"metaData.json",
RequestBody.create(JSON,
new File("./src/main/resources/metaData.json")))
.addFormDataPart("cid_63apple",
"test.txt",
RequestBody.create(PLAINTEXT,
new File("./src/main/resources/test.txt")))
.build();
Request singleImportRequest = new Request.Builder()
.header("Authorization", auth)
.header("X-ID-TENANT-NAME", tenant)
.url(baseUrl + "objects")
.post(singleImportRequestBody)
.build();
Response singleImportResponse = client.newCall(singleImportRequest).execute();
System.out.println(singleImportResponse.body().string());
//Batch Import
RequestBody batchImportRequestBody = new MultipartBody
.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("data",
"metaDataBatch.json",
RequestBody.create(JSON,
new File("./src/main/resources/metaDataBatch.json")))
.addFormDataPart("cid_63apple",
"test1.txt",
RequestBody.create(PLAINTEXT,
new File("./src/main/resources/test1.txt")))
.addFormDataPart("cid_64apple",
"test2.txt",
RequestBody.create(PLAINTEXT,
new File("./src/main/resources/test2.txt")))
.build();
Request batchImportRequest = new Request.Builder()
//.header("Authorization", auth)
.header("X-ID-TENANT-NAME", tenant)
.url(baseUrl + "objects")
.post(batchImportRequestBody)
.build();
Response batchImportResponse = client.newCall(batchImportRequest).execute();
System.out.println(batchImportResponse.body().string());
RequestBody compoundDocumentImportBody = new MultipartBody
.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("data", "metaDataCompound.json", RequestBody.create(JSON, new File("./src/main/resources/metaDataCompound.json")))
.addFormDataPart("cid_63apple", "compound-forward-document.bin", RequestBody.create(OCTETSTREAM, new File("./src/main/resources/compound-forward-document.bin")))
.build();
Request compoundDocumentImportRequest = new Request.Builder()
.header("X-ID-TENANT-NAME",tenant)
.url(baseUrl + "objects")
.post(compoundDocumentImportBody)
.build();
Response compoundDocumentImportResponse = client.newCall(compoundDocumentImportRequest).execute();
String compoundDocumentResponseString = compoundDocumentImportResponse.body().string();
System.out.println(compoundDocumentResponseString);
String contentStreamId = getContentStreamIdFromJsonResponse(compoundDocumentResponseString);
System.out.println(contentStreamId);
String repositoryId = getRepositoryIdFromJsonResponse(compoundDocumentResponseString);
System.out.println(repositoryId);
String archivePath = getArchivePathFromJsonResponse(compoundDocumentResponseString);
System.out.println(archivePath);
JSONObject afterCompoundDocumentJson = new JSONObject(new String (Files.readAllBytes(Paths.get("./src/main/resources/metaDataCompoundAfter.json"))));
afterCompoundDocumentJson.getJSONArray("objects").getJSONObject(0).getJSONArray("contentStreams").getJSONObject(0).put("contentStreamId", contentStreamId);
afterCompoundDocumentJson.getJSONArray("objects").getJSONObject(0).getJSONArray("contentStreams").getJSONObject(0).put("repositoryId", repositoryId);
// afterCompoundDocumentJson.getJSONArray("objects").getJSONObject(0).getJSONArray("contentStreams").getJSONObject(0).put("archivePath", archivePath);
String afterCompoundDocumentJsonString = afterCompoundDocumentJson.toString();
System.out.println(afterCompoundDocumentJsonString);
//wait for index service to catch up
TimeUnit.SECONDS.sleep(5);
RequestBody compoundDocumentPartImportBody = new MultipartBody
.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("data", "metaDataCompoundAfter.json", RequestBody.create(JSON, afterCompoundDocumentJsonString))
.build();
Request importCompoundPartDocumentRequest = new Request.Builder()
.header("X-ID-TENANT-NAME", tenant)
.url(baseUrl + "objects")
.post(compoundDocumentPartImportBody)
.build();
Response importCompoundPartDocumentResponse = client.newCall(importCompoundPartDocumentRequest).execute();
System.out.println(importCompoundPartDocumentResponse.body().string());
} catch (Exception e) {
e.printStackTrace();
}
}
private static String getContentStreamIdFromJsonResponse(String responseJson){
JSONObject jsonObject = new JSONObject(responseJson);
return jsonObject.getJSONArray("objects")
.getJSONObject(0)
.getJSONArray("contentStreams")
.getJSONObject(0)
.getString("contentStreamId");
}
private static String getRepositoryIdFromJsonResponse(String responseJson){
JSONObject jsonObject = new JSONObject(responseJson);
return jsonObject.getJSONArray("objects")
.getJSONObject(0)
.getJSONArray("contentStreams")
.getJSONObject(0)
.getString("repositoryId");
}
private static String getArchivePathFromJsonResponse(String responseJson){
JSONObject jsonObject = new JSONObject(responseJson);
return jsonObject.getJSONArray("objects")
.getJSONObject(0)
.getJSONArray("contentStreams")
.getJSONObject(0)
.getString("archivePath");
}
}