|
| 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.TusUpload; |
| 7 | +import io.tus.java.client.TusUploader; |
| 8 | +import org.json.JSONArray; |
| 9 | +import org.json.JSONObject; |
| 10 | + |
| 11 | +import java.io.ByteArrayInputStream; |
| 12 | +import java.io.IOException; |
| 13 | +import java.net.HttpURLConnection; |
| 14 | +import java.net.URL; |
| 15 | +import java.util.ArrayList; |
| 16 | +import java.util.List; |
| 17 | + |
| 18 | +public final class Api2DevdockTusTerminateUpload { |
| 19 | + /** |
| 20 | + * Run the API2 devdock TUS terminate-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 createResponse = Api2DevdockScenario.createResponse(scenario); |
| 30 | + final JSONObject result = uploadAndTerminate(scenario, createResponse); |
| 31 | + Api2DevdockScenario.writeResult(result); |
| 32 | + |
| 33 | + System.out.println( |
| 34 | + "Java TUS SDK devdock scenario " |
| 35 | + + scenario.getString("scenarioId") |
| 36 | + + " terminated " |
| 37 | + + result.getString("uploadUrl") |
| 38 | + ); |
| 39 | + } catch (Exception e) { |
| 40 | + e.printStackTrace(); |
| 41 | + System.exit(1); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + private static JSONObject uploadAndTerminate( |
| 46 | + JSONObject scenario, |
| 47 | + JSONObject createResponse |
| 48 | + ) throws IOException, ProtocolException { |
| 49 | + final JSONObject uploadConfig = scenario.getJSONObject("upload"); |
| 50 | + final Api2DevdockScenario.TerminationPlan termination = |
| 51 | + Api2DevdockScenario.termination(uploadConfig); |
| 52 | + final byte[] content = Api2DevdockScenario.scenarioBytes(uploadConfig); |
| 53 | + final int chunkSize = Api2DevdockScenario.fixedChunkSizeBytes(uploadConfig); |
| 54 | + final List<String> requestMethods = new ArrayList<String>(); |
| 55 | + |
| 56 | + if (termination.stopAfterAcceptedBytes > content.length) { |
| 57 | + throw new IllegalStateException( |
| 58 | + "terminate upload stop-after bytes " |
| 59 | + + termination.stopAfterAcceptedBytes |
| 60 | + + " exceeds content length " |
| 61 | + + content.length |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + final TusClient client = new TusClient(); |
| 66 | + client.setUploadCreationURL( |
| 67 | + new URL(Api2DevdockScenario.tusUrl(uploadConfig, scenario, createResponse)) |
| 68 | + ); |
| 69 | + client.setRequestLifecycleHooks(new TusRequestLifecycleHooks( |
| 70 | + new TusRequestLifecycleHooks.BeforeRequest() { |
| 71 | + @Override |
| 72 | + public void beforeRequest(TusRequestLifecycleHooks.RequestContext context) { |
| 73 | + requestMethods.add(context.getMethod()); |
| 74 | + } |
| 75 | + }, |
| 76 | + null |
| 77 | + )); |
| 78 | + |
| 79 | + final TusUpload upload = new TusUpload(); |
| 80 | + upload.setInputStream(new ByteArrayInputStream(content)); |
| 81 | + upload.setSize(content.length); |
| 82 | + upload.setFingerprint(scenario.getString("scenarioId") + "-java-terminate-upload"); |
| 83 | + upload.setMetadata( |
| 84 | + Api2DevdockScenario.uploadMetadata(uploadConfig, scenario, createResponse) |
| 85 | + ); |
| 86 | + |
| 87 | + final TusUploader uploader = client.createUpload(upload); |
| 88 | + uploader.setChunkSize(chunkSize); |
| 89 | + uploader.setRequestPayloadSize(termination.stopAfterAcceptedBytes); |
| 90 | + final int uploadedChunkSize = uploader.uploadChunk(); |
| 91 | + uploader.finish(); |
| 92 | + |
| 93 | + if (uploadedChunkSize != termination.stopAfterAcceptedBytes) { |
| 94 | + throw new IllegalStateException( |
| 95 | + "terminate upload wrote " |
| 96 | + + uploadedChunkSize |
| 97 | + + " bytes, expected " |
| 98 | + + termination.stopAfterAcceptedBytes |
| 99 | + ); |
| 100 | + } |
| 101 | + if (uploader.getOffset() != termination.stopAfterAcceptedBytes) { |
| 102 | + throw new IllegalStateException( |
| 103 | + "terminate upload accepted " |
| 104 | + + uploader.getOffset() |
| 105 | + + " bytes, expected " |
| 106 | + + termination.stopAfterAcceptedBytes |
| 107 | + ); |
| 108 | + } |
| 109 | + if (uploader.getUploadURL() == null) { |
| 110 | + throw new IllegalStateException("terminate upload did not expose a URL"); |
| 111 | + } |
| 112 | + |
| 113 | + final URL uploadUrl = uploader.getUploadURL(); |
| 114 | + client.terminateUpload(uploadUrl).disconnect(); |
| 115 | + final int verificationStatus = verifyTerminatedUpload( |
| 116 | + client, |
| 117 | + termination.verificationMethod, |
| 118 | + uploadUrl |
| 119 | + ); |
| 120 | + if (verificationStatus != termination.expectedVerificationStatus) { |
| 121 | + throw new IllegalStateException( |
| 122 | + "terminate upload verification status " |
| 123 | + + verificationStatus |
| 124 | + + ", expected " |
| 125 | + + termination.expectedVerificationStatus |
| 126 | + ); |
| 127 | + } |
| 128 | + |
| 129 | + return new JSONObject() |
| 130 | + .put("acceptedBytes", (int) uploader.getOffset()) |
| 131 | + .put("deleteRequestCount", countMethod(requestMethods, termination.method)) |
| 132 | + .put("requestMethods", new JSONArray(requestMethods)) |
| 133 | + .put("terminated", true) |
| 134 | + .put("uploadUrl", uploadUrl.toString()) |
| 135 | + .put("verificationStatus", verificationStatus); |
| 136 | + } |
| 137 | + |
| 138 | + private static int verifyTerminatedUpload( |
| 139 | + TusClient client, |
| 140 | + String method, |
| 141 | + URL uploadUrl |
| 142 | + ) throws IOException { |
| 143 | + final HttpURLConnection connection = (HttpURLConnection) uploadUrl.openConnection(); |
| 144 | + try { |
| 145 | + connection.setRequestMethod(method); |
| 146 | + client.prepareConnection(connection); |
| 147 | + connection.connect(); |
| 148 | + return connection.getResponseCode(); |
| 149 | + } finally { |
| 150 | + connection.disconnect(); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + private static int countMethod(List<String> methods, String expectedMethod) { |
| 155 | + int count = 0; |
| 156 | + for (String method : methods) { |
| 157 | + if (method.equals(expectedMethod)) { |
| 158 | + count += 1; |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + return count; |
| 163 | + } |
| 164 | + |
| 165 | + private Api2DevdockTusTerminateUpload() { |
| 166 | + throw new IllegalStateException("Utility class"); |
| 167 | + } |
| 168 | +} |
0 commit comments