|
| 1 | +package com.transloadit.examples; |
| 2 | + |
| 3 | +import com.transloadit.sdk.Transloadit; |
| 4 | +import com.transloadit.sdk.response.AssemblyResponse; |
| 5 | +import org.json.JSONArray; |
| 6 | +import org.json.JSONObject; |
| 7 | + |
| 8 | +import java.net.HttpURLConnection; |
| 9 | +import java.net.URL; |
| 10 | +import java.nio.charset.StandardCharsets; |
| 11 | +import java.nio.file.Files; |
| 12 | +import java.nio.file.Paths; |
| 13 | +import java.util.ArrayList; |
| 14 | +import java.util.Base64; |
| 15 | +import java.util.HashMap; |
| 16 | +import java.util.LinkedHashMap; |
| 17 | +import java.util.List; |
| 18 | +import java.util.Map; |
| 19 | + |
| 20 | +/** |
| 21 | + * Runs API2's contract-owned TUS resume scenario against devdock. |
| 22 | + * |
| 23 | + * <p>This example is intentionally checked into the SDK repository: it reads the |
| 24 | + * API/TUS facts from API2's injected scenario JSON, interrupts an upload like an |
| 25 | + * unlucky user would, and resumes it through the public SDK method.</p> |
| 26 | + */ |
| 27 | +public final class Api2DevdockTusResumeUpload { |
| 28 | + /** |
| 29 | + * Runs the TUS resume scenario. |
| 30 | + * |
| 31 | + * @param args ignored |
| 32 | + * @throws Exception if the scenario cannot be completed |
| 33 | + */ |
| 34 | + public static void main(String[] args) throws Exception { |
| 35 | + JSONObject scenario = loadScenario(); |
| 36 | + Transloadit transloadit = new Transloadit( |
| 37 | + requiredEnv("TRANSLOADIT_KEY"), |
| 38 | + requiredEnv("TRANSLOADIT_SECRET"), |
| 39 | + requiredEnv("TRANSLOADIT_ENDPOINT")); |
| 40 | + |
| 41 | + JSONObject createResponse = scenario.getJSONObject("prepared").getJSONObject("createResponse"); |
| 42 | + JSONObject upload = scenario.getJSONObject("upload"); |
| 43 | + JSONObject resume = upload.getJSONObject("resume"); |
| 44 | + |
| 45 | + Map<String, Object> context = new HashMap<String, Object>(); |
| 46 | + context.put("createResponse", createResponse); |
| 47 | + context.put("scenario", scenario); |
| 48 | + |
| 49 | + byte[] content = scenarioBytes(upload); |
| 50 | + String tusUrl = String.valueOf(resolveValue(upload.get("tusUrl"), context, "upload.tusUrl")); |
| 51 | + Map<String, String> metadata = uploadMetadata(upload, context); |
| 52 | + |
| 53 | + String firstUploadUrl = createInterruptedUpload( |
| 54 | + tusUrl, |
| 55 | + content, |
| 56 | + metadata, |
| 57 | + resume.getInt("stopAfterAcceptedBytes")); |
| 58 | + |
| 59 | + // Remember the interrupted upload by fingerprint, like a TUS client URL storage would. |
| 60 | + Map<String, String> storedUploads = new HashMap<String, String>(); |
| 61 | + storedUploads.put(resume.getString("fingerprint"), firstUploadUrl); |
| 62 | + int previousUploadCount = storedUploads.size(); |
| 63 | + |
| 64 | + AssemblyResponse completedAssembly = transloadit.resumeTusUpload( |
| 65 | + storedUploads.get(resume.getString("fingerprint")), |
| 66 | + content, |
| 67 | + createResponse.getString("assembly_ssl_url")); |
| 68 | + JSONObject status = completedAssembly.json(); |
| 69 | + if (status.has("error") && !status.isNull("error")) { |
| 70 | + throw new IllegalStateException("resumeTusUpload returned " + status.getString("error") |
| 71 | + + ": " + status.optString("message")); |
| 72 | + } |
| 73 | + |
| 74 | + if (resume.getBoolean("removeFingerprintOnSuccess")) { |
| 75 | + storedUploads.remove(resume.getString("fingerprint")); |
| 76 | + } |
| 77 | + int remainingPreviousUploadCount = storedUploads.size(); |
| 78 | + |
| 79 | + JSONObject result = new JSONObject(); |
| 80 | + result.put("firstUploadUrl", firstUploadUrl); |
| 81 | + result.put("previousUploadCount", previousUploadCount); |
| 82 | + result.put("remainingPreviousUploadCount", remainingPreviousUploadCount); |
| 83 | + result.put("uploadUrl", firstUploadUrl); |
| 84 | + writeResult(result); |
| 85 | + |
| 86 | + System.out.println("Java SDK devdock scenario " |
| 87 | + + scenario.getJSONObject("exampleInput").getString("scenarioId") |
| 88 | + + " resumed " + firstUploadUrl); |
| 89 | + } |
| 90 | + |
| 91 | + private static JSONObject loadScenario() throws Exception { |
| 92 | + String scenarioPath = System.getenv("API2_SDK_EXAMPLE_SCENARIO"); |
| 93 | + if (scenarioPath == null || scenarioPath.isEmpty()) { |
| 94 | + scenarioPath = "examples/api2-devdock-tus-resume-upload/api2-scenario.json"; |
| 95 | + } |
| 96 | + |
| 97 | + byte[] contents = Files.readAllBytes(Paths.get(scenarioPath)); |
| 98 | + return new JSONObject(new String(contents, StandardCharsets.UTF_8)); |
| 99 | + } |
| 100 | + |
| 101 | + private static String requiredEnv(String name) { |
| 102 | + String value = System.getenv(name); |
| 103 | + if (value == null || value.isEmpty()) { |
| 104 | + throw new IllegalStateException(name + " must be set"); |
| 105 | + } |
| 106 | + |
| 107 | + return value; |
| 108 | + } |
| 109 | + |
| 110 | + private static Object resolveValue(Object valueSpec, Map<String, Object> context, String label) { |
| 111 | + if (!(valueSpec instanceof JSONObject)) { |
| 112 | + throw new IllegalStateException(label + " value spec must be an object"); |
| 113 | + } |
| 114 | + |
| 115 | + JSONObject spec = (JSONObject) valueSpec; |
| 116 | + if (spec.has("value")) { |
| 117 | + return spec.get("value"); |
| 118 | + } |
| 119 | + |
| 120 | + JSONObject source = spec.getJSONObject("source"); |
| 121 | + Object current = context.get(source.getString("root")); |
| 122 | + if (current == null) { |
| 123 | + throw new IllegalStateException(label + " value source root is unavailable"); |
| 124 | + } |
| 125 | + JSONArray pathParts = source.getJSONArray("path"); |
| 126 | + for (int index = 0; index < pathParts.length(); index += 1) { |
| 127 | + String part = pathParts.getString(index); |
| 128 | + if (!(current instanceof JSONObject) || !((JSONObject) current).has(part)) { |
| 129 | + throw new IllegalStateException(label + " value source cannot read " + part); |
| 130 | + } |
| 131 | + current = ((JSONObject) current).get(part); |
| 132 | + } |
| 133 | + |
| 134 | + return current; |
| 135 | + } |
| 136 | + |
| 137 | + private static byte[] scenarioBytes(JSONObject upload) { |
| 138 | + JSONObject source = upload.getJSONObject("source"); |
| 139 | + if (!"bytes".equals(source.getString("kind"))) { |
| 140 | + throw new IllegalStateException("upload.source.kind must be bytes"); |
| 141 | + } |
| 142 | + if (!"utf8".equals(source.getString("encoding"))) { |
| 143 | + throw new IllegalStateException("upload.source.encoding must be utf8"); |
| 144 | + } |
| 145 | + |
| 146 | + return source.getString("value").getBytes(StandardCharsets.UTF_8); |
| 147 | + } |
| 148 | + |
| 149 | + private static Map<String, String> uploadMetadata(JSONObject upload, Map<String, Object> context) { |
| 150 | + Map<String, String> metadata = new LinkedHashMap<String, String>(); |
| 151 | + JSONArray fields = upload.getJSONArray("metadata"); |
| 152 | + for (int index = 0; index < fields.length(); index += 1) { |
| 153 | + JSONObject field = fields.getJSONObject(index); |
| 154 | + String name = field.getString("name"); |
| 155 | + metadata.put(name, String.valueOf(resolveValue(field.get("value"), context, name))); |
| 156 | + } |
| 157 | + |
| 158 | + return metadata; |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * Creates a TUS upload and only sends the first chunk, leaving the upload |
| 163 | + * interrupted the way a dropped connection would. |
| 164 | + */ |
| 165 | + private static String createInterruptedUpload( |
| 166 | + String tusUrl, |
| 167 | + byte[] content, |
| 168 | + Map<String, String> metadata, |
| 169 | + int stopAfterAcceptedBytes) throws Exception { |
| 170 | + List<String> metadataParts = new ArrayList<String>(); |
| 171 | + for (Map.Entry<String, String> entry : metadata.entrySet()) { |
| 172 | + metadataParts.add(entry.getKey() + " " |
| 173 | + + Base64.getEncoder().encodeToString(entry.getValue().getBytes(StandardCharsets.UTF_8))); |
| 174 | + } |
| 175 | + |
| 176 | + URL createUrl = new URL(tusUrl); |
| 177 | + HttpURLConnection createConnection = (HttpURLConnection) createUrl.openConnection(); |
| 178 | + createConnection.setRequestMethod("POST"); |
| 179 | + createConnection.setRequestProperty("Tus-Resumable", "1.0.0"); |
| 180 | + createConnection.setRequestProperty("Upload-Length", String.valueOf(content.length)); |
| 181 | + createConnection.setRequestProperty("Upload-Metadata", String.join(",", metadataParts)); |
| 182 | + createConnection.setDoOutput(true); |
| 183 | + createConnection.getOutputStream().close(); |
| 184 | + if (createConnection.getResponseCode() != 201) { |
| 185 | + throw new IllegalStateException("TUS create returned HTTP " |
| 186 | + + createConnection.getResponseCode() + ", expected 201"); |
| 187 | + } |
| 188 | + String location = createConnection.getHeaderField("Location"); |
| 189 | + createConnection.disconnect(); |
| 190 | + if (location == null || location.isEmpty()) { |
| 191 | + throw new IllegalStateException("TUS create did not return a Location header"); |
| 192 | + } |
| 193 | + String uploadUrl = new URL(createUrl, location).toString(); |
| 194 | + |
| 195 | + HttpURLConnection patchConnection = (HttpURLConnection) new URL(uploadUrl).openConnection(); |
| 196 | + // HttpURLConnection rejects PATCH, so use the same POST + override header TUS supports. |
| 197 | + patchConnection.setRequestMethod("POST"); |
| 198 | + patchConnection.setRequestProperty("X-HTTP-Method-Override", "PATCH"); |
| 199 | + patchConnection.setRequestProperty("Tus-Resumable", "1.0.0"); |
| 200 | + patchConnection.setRequestProperty("Upload-Offset", "0"); |
| 201 | + patchConnection.setRequestProperty("Content-Type", "application/offset+octet-stream"); |
| 202 | + patchConnection.setDoOutput(true); |
| 203 | + patchConnection.getOutputStream().write(content, 0, stopAfterAcceptedBytes); |
| 204 | + patchConnection.getOutputStream().close(); |
| 205 | + if (patchConnection.getResponseCode() != 204) { |
| 206 | + throw new IllegalStateException("TUS first chunk returned HTTP " |
| 207 | + + patchConnection.getResponseCode() + ", expected 204"); |
| 208 | + } |
| 209 | + String acceptedBytesHeader = patchConnection.getHeaderField("Upload-Offset"); |
| 210 | + patchConnection.disconnect(); |
| 211 | + int acceptedBytes = acceptedBytesHeader == null ? -1 : Integer.parseInt(acceptedBytesHeader); |
| 212 | + if (acceptedBytes != stopAfterAcceptedBytes) { |
| 213 | + throw new IllegalStateException("TUS first chunk accepted " + acceptedBytes |
| 214 | + + " bytes, expected " + stopAfterAcceptedBytes); |
| 215 | + } |
| 216 | + |
| 217 | + return uploadUrl; |
| 218 | + } |
| 219 | + |
| 220 | + private static void writeResult(JSONObject result) throws Exception { |
| 221 | + String resultPath = System.getenv("API2_SDK_EXAMPLE_RESULT"); |
| 222 | + if (resultPath == null || resultPath.isEmpty()) { |
| 223 | + return; |
| 224 | + } |
| 225 | + |
| 226 | + Files.write( |
| 227 | + Paths.get(resultPath), |
| 228 | + (result.toString(2) + "\n").getBytes(StandardCharsets.UTF_8)); |
| 229 | + } |
| 230 | + |
| 231 | + private Api2DevdockTusResumeUpload() { |
| 232 | + throw new IllegalStateException("Utility class"); |
| 233 | + } |
| 234 | +} |
0 commit comments