Skip to content

Commit 101ee03

Browse files
committed
Add TUS request lifecycle devdock proof
1 parent aa20322 commit 101ee03

2 files changed

Lines changed: 199 additions & 0 deletions

File tree

example/build.gradle

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,9 @@ tasks.register('api2DevdockTusRetryOffsetRecovery', JavaExec) {
2323
mainClass = 'io.tus.java.example.Api2DevdockTusRetryOffsetRecovery'
2424
workingDir = rootProject.projectDir
2525
}
26+
27+
tasks.register('api2DevdockTusRequestLifecycleHooks', JavaExec) {
28+
classpath = sourceSets.main.runtimeClasspath
29+
mainClass = 'io.tus.java.example.Api2DevdockTusRequestLifecycleHooks'
30+
workingDir = rootProject.projectDir
31+
}
Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
package io.tus.java.example;
2+
3+
import io.tus.java.client.ProtocolException;
4+
import io.tus.java.client.TusClient;
5+
import io.tus.java.client.TusRequestLifecycleHooks;
6+
import io.tus.java.client.TusURLMemoryStore;
7+
import io.tus.java.client.TusUpload;
8+
import io.tus.java.client.TusUploader;
9+
import org.json.JSONArray;
10+
import org.json.JSONObject;
11+
12+
import java.io.ByteArrayInputStream;
13+
import java.io.IOException;
14+
import java.net.URL;
15+
import java.util.ArrayList;
16+
import java.util.List;
17+
18+
public final class Api2DevdockTusRequestLifecycleHooks {
19+
/**
20+
* Run the API2 devdock TUS request lifecycle hooks example.
21+
*
22+
* @param args ignored
23+
*/
24+
public static void main(String[] args) {
25+
try {
26+
System.setProperty("http.strictPostRedirect", "true");
27+
28+
final JSONObject scenario = Api2DevdockScenario.loadScenario();
29+
final JSONObject createResponse = Api2DevdockScenario.createResponse(scenario);
30+
final JSONObject result = uploadWithRequestLifecycleHooks(scenario, createResponse);
31+
Api2DevdockScenario.writeResult(result);
32+
33+
System.out.println(
34+
"Java TUS SDK devdock scenario "
35+
+ scenario.getString("scenarioId")
36+
+ " observed lifecycle hooks for "
37+
+ result.getString("uploadUrl")
38+
);
39+
} catch (Exception e) {
40+
e.printStackTrace();
41+
System.exit(1);
42+
}
43+
}
44+
45+
private static JSONObject uploadWithRequestLifecycleHooks(
46+
JSONObject scenario,
47+
JSONObject createResponse
48+
) throws IOException, ProtocolException {
49+
final JSONObject uploadConfig = scenario.getJSONObject("upload");
50+
final JSONObject hooksConfig = uploadConfig.getJSONObject("requestLifecycleHooks");
51+
final byte[] content = Api2DevdockScenario.scenarioBytes(uploadConfig);
52+
final List<String> beforeRequestMethods = new ArrayList<String>();
53+
final List<String> afterResponseMethods = new ArrayList<String>();
54+
final List<Integer> afterResponseStatusCodes = new ArrayList<Integer>();
55+
Api2DevdockScenario.requireFullFileChunkSize(uploadConfig);
56+
57+
final TusClient client = new TusClient();
58+
client.setUploadCreationURL(
59+
new URL(Api2DevdockScenario.tusUrl(uploadConfig, scenario, createResponse))
60+
);
61+
client.enableResuming(new TusURLMemoryStore());
62+
client.setRequestLifecycleHooks(new TusRequestLifecycleHooks(
63+
new TusRequestLifecycleHooks.BeforeRequest() {
64+
@Override
65+
public void beforeRequest(TusRequestLifecycleHooks.RequestContext context) {
66+
beforeRequestMethods.add(context.getMethod());
67+
}
68+
},
69+
new TusRequestLifecycleHooks.AfterResponse() {
70+
@Override
71+
public void afterResponse(
72+
TusRequestLifecycleHooks.RequestContext context
73+
) throws IOException {
74+
afterResponseMethods.add(context.getMethod());
75+
afterResponseStatusCodes.add(context.getConnection().getResponseCode());
76+
}
77+
}
78+
));
79+
80+
final TusUpload upload = new TusUpload();
81+
upload.setInputStream(new ByteArrayInputStream(content));
82+
upload.setSize(content.length);
83+
upload.setFingerprint(scenario.getString("scenarioId") + "-java-request-hooks");
84+
upload.setMetadata(
85+
Api2DevdockScenario.uploadMetadata(uploadConfig, scenario, createResponse)
86+
);
87+
88+
final TusUploader uploader = client.resumeOrCreateUpload(upload);
89+
uploader.setChunkSize(content.length);
90+
int uploadedChunkSize;
91+
do {
92+
uploadedChunkSize = uploader.uploadChunk();
93+
} while (uploadedChunkSize > -1);
94+
uploader.finish();
95+
96+
if (uploader.getOffset() != content.length) {
97+
throw new IllegalStateException(
98+
"request lifecycle hooks upload offset "
99+
+ uploader.getOffset()
100+
+ ", expected "
101+
+ content.length
102+
);
103+
}
104+
if (uploader.getUploadURL() == null) {
105+
throw new IllegalStateException("request lifecycle hooks upload did not return a URL");
106+
}
107+
assertStringArray(
108+
beforeRequestMethods,
109+
hooksConfig.getJSONArray("expectedBeforeRequestMethods"),
110+
"before request methods"
111+
);
112+
assertStringArray(
113+
afterResponseMethods,
114+
hooksConfig.getJSONArray("expectedAfterResponseMethods"),
115+
"after response methods"
116+
);
117+
assertIntegerArray(
118+
afterResponseStatusCodes,
119+
hooksConfig.getJSONArray("expectedAfterResponseStatusCodes"),
120+
"after response status codes"
121+
);
122+
123+
return new JSONObject()
124+
.put("afterResponseMethods", new JSONArray(afterResponseMethods))
125+
.put("afterResponseStatusCodes", new JSONArray(afterResponseStatusCodes))
126+
.put("beforeRequestMethods", new JSONArray(beforeRequestMethods))
127+
.put("uploadUrl", uploader.getUploadURL().toString());
128+
}
129+
130+
private static void assertStringArray(List<String> actual, JSONArray expected, String label) {
131+
if (actual.size() != expected.length()) {
132+
throw new IllegalStateException(
133+
"request lifecycle hooks expected "
134+
+ label
135+
+ " "
136+
+ expected
137+
+ ", got "
138+
+ actual
139+
);
140+
}
141+
142+
for (int index = 0; index < expected.length(); index++) {
143+
if (!actual.get(index).equals(expected.getString(index))) {
144+
throw new IllegalStateException(
145+
"request lifecycle hooks expected "
146+
+ label
147+
+ " "
148+
+ expected.getString(index)
149+
+ " at index "
150+
+ index
151+
+ ", got "
152+
+ actual.get(index)
153+
);
154+
}
155+
}
156+
}
157+
158+
private static void assertIntegerArray(
159+
List<Integer> actual,
160+
JSONArray expected,
161+
String label
162+
) {
163+
if (actual.size() != expected.length()) {
164+
throw new IllegalStateException(
165+
"request lifecycle hooks expected "
166+
+ label
167+
+ " "
168+
+ expected
169+
+ ", got "
170+
+ actual
171+
);
172+
}
173+
174+
for (int index = 0; index < expected.length(); index++) {
175+
if (actual.get(index) != expected.getInt(index)) {
176+
throw new IllegalStateException(
177+
"request lifecycle hooks expected "
178+
+ label
179+
+ " "
180+
+ expected.getInt(index)
181+
+ " at index "
182+
+ index
183+
+ ", got "
184+
+ actual.get(index)
185+
);
186+
}
187+
}
188+
}
189+
190+
private Api2DevdockTusRequestLifecycleHooks() {
191+
throw new IllegalStateException("Utility class");
192+
}
193+
}

0 commit comments

Comments
 (0)