|
| 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.TusDetailedError; |
| 6 | +import io.tus.java.client.TusUpload; |
| 7 | + |
| 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.Map; |
| 16 | + |
| 17 | +public final class Api2DevdockTusDetailedError { |
| 18 | + public static void main(String[] args) throws Exception { |
| 19 | + final JSONObject scenario = Api2DevdockScenario.loadScenario(); |
| 20 | + final JSONObject result = uploadExpectingDetailedError( |
| 21 | + Api2DevdockScenario.conformanceScenario(scenario) |
| 22 | + ); |
| 23 | + |
| 24 | + Api2DevdockScenario.writeResult(result); |
| 25 | + System.out.println( |
| 26 | + "Java TUS SDK devdock scenario " |
| 27 | + + scenario.getString("scenarioId") |
| 28 | + + " observed detailed error " |
| 29 | + + result.getString("errorMessage") |
| 30 | + ); |
| 31 | + } |
| 32 | + |
| 33 | + private static JSONObject uploadExpectingDetailedError(JSONObject conformanceScenario) |
| 34 | + throws Exception { |
| 35 | + final JSONObject requestPlan = conformanceScenario |
| 36 | + .getJSONArray("requests") |
| 37 | + .getJSONObject(0); |
| 38 | + if (!requestPlan.isNull("errorMessage")) { |
| 39 | + return uploadExpectingRequestError(conformanceScenario, requestPlan); |
| 40 | + } |
| 41 | + |
| 42 | + return uploadExpectingResponseError(conformanceScenario); |
| 43 | + } |
| 44 | + |
| 45 | + private static JSONObject uploadExpectingResponseError(JSONObject conformanceScenario) |
| 46 | + throws Exception { |
| 47 | + final URL endpointUrl = new URL( |
| 48 | + Api2DevdockScenario.conformanceInputStringOption(conformanceScenario, "endpointUrl") |
| 49 | + ); |
| 50 | + try (Api2DevdockTusConformanceServer conformanceServer = |
| 51 | + new Api2DevdockTusConformanceServer(conformanceScenario, endpointUrl)) { |
| 52 | + final TusClient client = clientFor(conformanceScenario); |
| 53 | + client.setUploadCreationURL(conformanceServer.endpointUrl()); |
| 54 | + |
| 55 | + final Throwable error = createUploadExpectingError(client, conformanceScenario); |
| 56 | + conformanceServer.assertExhausted(); |
| 57 | + |
| 58 | + final JSONObject serverResult = conformanceServer.result(); |
| 59 | + return detailedResult( |
| 60 | + error, |
| 61 | + canonicalize(serverResult, conformanceServer), |
| 62 | + conformanceServer |
| 63 | + ); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + private static JSONObject uploadExpectingRequestError( |
| 68 | + JSONObject conformanceScenario, |
| 69 | + JSONObject requestPlan |
| 70 | + ) throws Exception { |
| 71 | + final URL endpointUrl = new URL( |
| 72 | + Api2DevdockScenario.conformanceInputStringOption(conformanceScenario, "endpointUrl") |
| 73 | + ); |
| 74 | + final FailingTusClient client = new FailingTusClient(requestPlan.getString("errorMessage")); |
| 75 | + configureClient(client, conformanceScenario); |
| 76 | + client.setUploadCreationURL(endpointUrl); |
| 77 | + |
| 78 | + final Throwable error = createUploadExpectingError(client, conformanceScenario); |
| 79 | + final JSONObject requestResult = new JSONObject() |
| 80 | + .put("requestMethods", new JSONArray().put(requestPlan.getString("effectiveMethod"))) |
| 81 | + .put("requestUrls", new JSONArray().put(requestPlan.getString("expectedUrl"))); |
| 82 | + |
| 83 | + return detailedResult(error, requestResult, null); |
| 84 | + } |
| 85 | + |
| 86 | + private static TusClient clientFor(JSONObject conformanceScenario) { |
| 87 | + final TusClient client = new TusClient(); |
| 88 | + configureClient(client, conformanceScenario); |
| 89 | + return client; |
| 90 | + } |
| 91 | + |
| 92 | + private static void configureClient(TusClient client, JSONObject conformanceScenario) { |
| 93 | + final Map<String, String> headers = Api2DevdockScenario.conformanceInputStringMapOption( |
| 94 | + conformanceScenario, |
| 95 | + "headers" |
| 96 | + ); |
| 97 | + client.setHeaders(headers); |
| 98 | + } |
| 99 | + |
| 100 | + private static Throwable createUploadExpectingError( |
| 101 | + TusClient client, |
| 102 | + JSONObject conformanceScenario |
| 103 | + ) throws Exception { |
| 104 | + final TusUpload upload = uploadFor(conformanceScenario); |
| 105 | + try { |
| 106 | + client.createUpload(upload); |
| 107 | + } catch (IOException | ProtocolException error) { |
| 108 | + return error; |
| 109 | + } |
| 110 | + |
| 111 | + throw new IllegalStateException("detailed error scenario unexpectedly created an upload"); |
| 112 | + } |
| 113 | + |
| 114 | + private static TusUpload uploadFor(JSONObject conformanceScenario) { |
| 115 | + final byte[] content = Api2DevdockScenario.conformanceInputSourceBytes(conformanceScenario); |
| 116 | + final Map<String, String> metadata = Api2DevdockScenario.conformanceInputStringMapOption( |
| 117 | + conformanceScenario, |
| 118 | + "metadata" |
| 119 | + ); |
| 120 | + final TusUpload upload = new TusUpload(); |
| 121 | + upload.setInputStream(new ByteArrayInputStream(content)); |
| 122 | + upload.setMetadata(metadata); |
| 123 | + upload.setSize(content.length); |
| 124 | + return upload; |
| 125 | + } |
| 126 | + |
| 127 | + private static JSONObject detailedResult( |
| 128 | + Throwable error, |
| 129 | + JSONObject requestResult, |
| 130 | + Api2DevdockTusConformanceServer conformanceServer |
| 131 | + ) { |
| 132 | + final JSONObject result = new JSONObject() |
| 133 | + .put("errorCaught", true) |
| 134 | + .put("errorIsDetailed", error instanceof TusDetailedError) |
| 135 | + .put("errorMessage", canonicalValue(error.getMessage(), conformanceServer)) |
| 136 | + .put("requestCount", requestResult.getJSONArray("requestMethods").length()) |
| 137 | + .put("requestMethods", requestResult.getJSONArray("requestMethods")) |
| 138 | + .put("requestUrls", requestResult.getJSONArray("requestUrls")); |
| 139 | + |
| 140 | + if (!(error instanceof TusDetailedError)) { |
| 141 | + return result; |
| 142 | + } |
| 143 | + |
| 144 | + final TusDetailedError detailedError = (TusDetailedError) error; |
| 145 | + result.put("causingErrorPresent", detailedError.getCausingError() != null); |
| 146 | + if (detailedError.getCausingError() != null) { |
| 147 | + result.put("causingErrorMessage", detailedError.getCausingError().getMessage()); |
| 148 | + } |
| 149 | + result.put("originalRequestMethod", detailedError.getOriginalRequestMethod()); |
| 150 | + result.put("originalRequestRequestId", detailedError.getOriginalRequestId()); |
| 151 | + result.put( |
| 152 | + "originalRequestUrl", |
| 153 | + canonicalValue(detailedError.getOriginalRequestURL().toString(), conformanceServer) |
| 154 | + ); |
| 155 | + result.put("originalResponsePresent", detailedError.hasOriginalResponse()); |
| 156 | + if (detailedError.hasOriginalResponse()) { |
| 157 | + result.put("originalResponseBody", detailedError.getOriginalResponseBody()); |
| 158 | + result.put("originalResponseStatus", detailedError.getOriginalResponseStatus()); |
| 159 | + } |
| 160 | + |
| 161 | + return result; |
| 162 | + } |
| 163 | + |
| 164 | + private static JSONObject canonicalize( |
| 165 | + JSONObject requestResult, |
| 166 | + Api2DevdockTusConformanceServer conformanceServer |
| 167 | + ) { |
| 168 | + final JSONArray requestUrls = requestResult.getJSONArray("requestUrls"); |
| 169 | + final JSONArray canonicalUrls = new JSONArray(); |
| 170 | + for (int index = 0; index < requestUrls.length(); index++) { |
| 171 | + canonicalUrls.put(conformanceServer.canonicalUrl(requestUrls.getString(index))); |
| 172 | + } |
| 173 | + |
| 174 | + return new JSONObject(requestResult.toString()).put("requestUrls", canonicalUrls); |
| 175 | + } |
| 176 | + |
| 177 | + private static String canonicalValue( |
| 178 | + String value, |
| 179 | + Api2DevdockTusConformanceServer conformanceServer |
| 180 | + ) { |
| 181 | + if (conformanceServer == null) { |
| 182 | + return value; |
| 183 | + } |
| 184 | + |
| 185 | + return conformanceServer.canonicalUrl(value); |
| 186 | + } |
| 187 | + |
| 188 | + private static final class FailingTusClient extends TusClient { |
| 189 | + private final String errorMessage; |
| 190 | + |
| 191 | + FailingTusClient(String errorMessage) { |
| 192 | + this.errorMessage = errorMessage; |
| 193 | + } |
| 194 | + |
| 195 | + @Override |
| 196 | + protected HttpURLConnection openConnection(URL uploadUrl) { |
| 197 | + return new FailingHttpURLConnection(uploadUrl, errorMessage); |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + private static final class FailingHttpURLConnection extends HttpURLConnection { |
| 202 | + private final String errorMessage; |
| 203 | + |
| 204 | + FailingHttpURLConnection(URL url, String errorMessage) { |
| 205 | + super(url); |
| 206 | + this.errorMessage = errorMessage; |
| 207 | + } |
| 208 | + |
| 209 | + @Override |
| 210 | + public void connect() throws IOException { |
| 211 | + throw new IOException(errorMessage); |
| 212 | + } |
| 213 | + |
| 214 | + @Override |
| 215 | + public void disconnect() { |
| 216 | + } |
| 217 | + |
| 218 | + @Override |
| 219 | + public boolean usingProxy() { |
| 220 | + return false; |
| 221 | + } |
| 222 | + } |
| 223 | + |
| 224 | + private Api2DevdockTusDetailedError() { |
| 225 | + throw new IllegalStateException("Utility class"); |
| 226 | + } |
| 227 | +} |
0 commit comments