|
| 1 | +package io.tus.java.example; |
| 2 | + |
| 3 | +import io.tus.java.client.TusClient; |
| 4 | +import io.tus.java.client.TusURLMemoryStore; |
| 5 | +import io.tus.java.client.TusUpload; |
| 6 | +import io.tus.java.client.TusUploader; |
| 7 | +import org.json.JSONArray; |
| 8 | +import org.json.JSONObject; |
| 9 | + |
| 10 | +import java.io.ByteArrayInputStream; |
| 11 | +import java.net.URL; |
| 12 | +import java.util.ArrayList; |
| 13 | +import java.util.List; |
| 14 | +import java.util.Map; |
| 15 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 16 | +import java.util.concurrent.atomic.AtomicReference; |
| 17 | + |
| 18 | +public final class Api2DevdockTusAbortUpload { |
| 19 | + /** |
| 20 | + * Run the API2 devdock TUS abort-upload 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 result = uploadAndAbort(scenario); |
| 30 | + Api2DevdockScenario.writeResult(result); |
| 31 | + |
| 32 | + System.out.println( |
| 33 | + "Java TUS SDK devdock scenario " |
| 34 | + + scenario.getString("scenarioId") |
| 35 | + + " aborted the upload" |
| 36 | + ); |
| 37 | + } catch (Exception e) { |
| 38 | + e.printStackTrace(); |
| 39 | + System.exit(1); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + private static JSONObject uploadAndAbort(JSONObject scenario) throws Exception { |
| 44 | + final JSONObject conformanceScenario = Api2DevdockScenario.conformanceScenario(scenario); |
| 45 | + final byte[] content = Api2DevdockScenario.conformanceInputSourceBytes( |
| 46 | + conformanceScenario |
| 47 | + ); |
| 48 | + final URL endpointOrigin = new URL(Api2DevdockScenario.conformanceInputStringOption( |
| 49 | + conformanceScenario, |
| 50 | + "endpointUrl" |
| 51 | + )); |
| 52 | + final Map<String, String> metadata = Api2DevdockScenario.conformanceInputStringMapOption( |
| 53 | + conformanceScenario, |
| 54 | + "metadata" |
| 55 | + ); |
| 56 | + final Map<String, String> headers = Api2DevdockScenario.conformanceInputStringMapOptionOrEmpty( |
| 57 | + conformanceScenario, |
| 58 | + "headers" |
| 59 | + ); |
| 60 | + final boolean terminateUploadOnAbort = conformanceScenario |
| 61 | + .getJSONObject("runtimeSetup") |
| 62 | + .getJSONObject("abort") |
| 63 | + .getBoolean("terminateUpload"); |
| 64 | + final String fingerprint = runtimeFingerprint(conformanceScenario, scenario); |
| 65 | + final JSONObject completion = conformanceScenario.getJSONObject("completion"); |
| 66 | + |
| 67 | + final TusClient client = new TusClient(); |
| 68 | + final TusURLMemoryStore urlStore = new TusURLMemoryStore(); |
| 69 | + final List<JSONObject> events = new ArrayList<JSONObject>(); |
| 70 | + final AtomicReference<TusUploader> activeUploader = new AtomicReference<TusUploader>(); |
| 71 | + final AtomicBoolean aborted = new AtomicBoolean(false); |
| 72 | + final AtomicBoolean successCalled = new AtomicBoolean(false); |
| 73 | + final AtomicReference<Exception> uploadError = new AtomicReference<Exception>(); |
| 74 | + |
| 75 | + try (Api2DevdockTusConformanceServer conformanceServer = |
| 76 | + new Api2DevdockTusConformanceServer( |
| 77 | + conformanceScenario, |
| 78 | + endpointOrigin, |
| 79 | + new Api2DevdockTusConformanceServer.RequestAbortHandler() { |
| 80 | + @Override |
| 81 | + public void abortRequest( |
| 82 | + Api2DevdockTusConformanceServer.RequestAbortContext context |
| 83 | + ) throws Exception { |
| 84 | + aborted.set(true); |
| 85 | + events.add(new JSONObject() |
| 86 | + .put("kind", "request-abort") |
| 87 | + .put("method", context.method()) |
| 88 | + .put("requestIndex", context.requestIndex()) |
| 89 | + .put("url", context.url())); |
| 90 | + |
| 91 | + final TusUploader uploader = activeUploader.get(); |
| 92 | + if (uploader == null) { |
| 93 | + if (terminateUploadOnAbort) { |
| 94 | + throw new IllegalStateException( |
| 95 | + "abort scenario requested termination " |
| 96 | + + "before uploader was available" |
| 97 | + ); |
| 98 | + } |
| 99 | + client.abortUpload(); |
| 100 | + return; |
| 101 | + } |
| 102 | + |
| 103 | + client.abortUpload(uploader, false); |
| 104 | + } |
| 105 | + })) { |
| 106 | + client.setUploadCreationURL(conformanceServer.endpointUrl()); |
| 107 | + client.enableResuming(urlStore); |
| 108 | + client.setHeaders(headers); |
| 109 | + |
| 110 | + final TusUpload upload = uploadFor(content, fingerprint, metadata); |
| 111 | + final Thread uploadThread = new Thread(new Runnable() { |
| 112 | + @Override |
| 113 | + public void run() { |
| 114 | + try { |
| 115 | + final TusUploader uploader = client.resumeOrCreateUpload(upload); |
| 116 | + activeUploader.set(uploader); |
| 117 | + uploader.setChunkSize(content.length); |
| 118 | + uploader.setRequestPayloadSize(content.length); |
| 119 | + while (uploader.uploadChunk() > -1) { } |
| 120 | + uploader.finish(); |
| 121 | + successCalled.set(true); |
| 122 | + } catch (Exception error) { |
| 123 | + if (!aborted.get()) { |
| 124 | + uploadError.set(error); |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + }); |
| 129 | + uploadThread.start(); |
| 130 | + uploadThread.join(5000); |
| 131 | + if (uploadThread.isAlive()) { |
| 132 | + uploadThread.interrupt(); |
| 133 | + throw new IllegalStateException("timed out waiting for abort upload thread"); |
| 134 | + } |
| 135 | + if (uploadError.get() != null) { |
| 136 | + throw uploadError.get(); |
| 137 | + } |
| 138 | + if (!aborted.get()) { |
| 139 | + throw new IllegalStateException("abort scenario completed without aborting"); |
| 140 | + } |
| 141 | + if (terminateUploadOnAbort) { |
| 142 | + final TusUploader uploader = activeUploader.get(); |
| 143 | + if (uploader == null) { |
| 144 | + throw new IllegalStateException( |
| 145 | + "abort scenario requested termination before uploader was available" |
| 146 | + ); |
| 147 | + } |
| 148 | + client.abortUpload(uploader, true); |
| 149 | + } |
| 150 | + |
| 151 | + conformanceServer.assertExhausted(); |
| 152 | + final JSONObject result = conformanceServer.result(); |
| 153 | + result.put("completionKind", completion.getString("kind")); |
| 154 | + result.put("errorCalled", false); |
| 155 | + result.put("events", new JSONArray(events)); |
| 156 | + result.put("requestCount", result.getJSONArray("requestMethods").length()); |
| 157 | + result.put("successCalled", successCalled.get()); |
| 158 | + result.put("uploadUrl", uploadUrlResult(conformanceServer, activeUploader.get())); |
| 159 | + |
| 160 | + return result; |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + private static TusUpload uploadFor( |
| 165 | + byte[] content, |
| 166 | + String fingerprint, |
| 167 | + Map<String, String> metadata |
| 168 | + ) { |
| 169 | + final TusUpload upload = new TusUpload(); |
| 170 | + upload.setInputStream(new ByteArrayInputStream(content)); |
| 171 | + upload.setSize(content.length); |
| 172 | + upload.setFingerprint(fingerprint); |
| 173 | + upload.setMetadata(metadata); |
| 174 | + return upload; |
| 175 | + } |
| 176 | + |
| 177 | + private static Object uploadUrlResult( |
| 178 | + Api2DevdockTusConformanceServer conformanceServer, |
| 179 | + TusUploader uploader |
| 180 | + ) { |
| 181 | + if (uploader == null || uploader.getUploadURL() == null) { |
| 182 | + return JSONObject.NULL; |
| 183 | + } |
| 184 | + |
| 185 | + return conformanceServer.canonicalUrl(uploader.getUploadURL().toString()); |
| 186 | + } |
| 187 | + |
| 188 | + private static String runtimeFingerprint(JSONObject conformanceScenario, JSONObject scenario) { |
| 189 | + final JSONObject runtimeSetup = conformanceScenario.getJSONObject("runtimeSetup"); |
| 190 | + if (!runtimeSetup.has("fingerprint")) { |
| 191 | + return scenario.getString("scenarioId") + "-java-abort-upload"; |
| 192 | + } |
| 193 | + |
| 194 | + final JSONObject fingerprint = runtimeSetup.getJSONObject("fingerprint"); |
| 195 | + if (!fingerprint.optBoolean("install", false)) { |
| 196 | + return scenario.getString("scenarioId") + "-java-abort-upload"; |
| 197 | + } |
| 198 | + |
| 199 | + return fingerprint.getString("value"); |
| 200 | + } |
| 201 | + |
| 202 | + private Api2DevdockTusAbortUpload() { |
| 203 | + throw new IllegalStateException("Utility class"); |
| 204 | + } |
| 205 | +} |
0 commit comments