/** * Try to resume an upload using {@link #resumeUpload(TusUpload)}. If the method call throws * an {@link ResumingNotEnabledException} or {@link FingerprintNotFoundException}, a new upload * will be created using {@link #createUpload(TusUpload)}. * * @param upload The file for which an upload will be resumed * @throws ProtocolException Thrown if the remote server sent an unexpected response, e.g. * wrong status codes or missing/invalid headers. * @throws IOException Thrown if an exception occurs while issuing the HTTP request. */ public TusUploader resumeOrCreateUpload(@NotNull TusUpload upload) throws ProtocolException, IOException { try { return resumeUpload(upload); } catch(FingerprintNotFoundException e) { return createUpload(upload); } catch(ResumingNotEnabledException e) { return createUpload(upload); } catch(ProtocolException e) { // If the attempt to resume returned a 404 Not Found, we immediately try to create a new // one since TusExectuor would not retry this operation. HttpURLConnection connection = e.getCausingConnection(); if(connection != null && connection.getResponseCode() == 404) { return createUpload(upload); } throw e; } }
@Test public void testCreateUpload() throws IOException, ProtocolException { mockServer.when(new HttpRequest() .withMethod("POST") .withPath("/files") .withHeader("Tus-Resumable", TusClient.TUS_VERSION) .withHeader("Upload-Metadata", "foo aGVsbG8=,bar d29ybGQ=") .withHeader("Upload-Length", "10")) .respond(new HttpResponse() .withStatusCode(201) .withHeader("Tus-Resumable", TusClient.TUS_VERSION) .withHeader("Location", mockServerURL + "/foo")); Map<String, String> metadata = new LinkedHashMap<String, String>(); metadata.put("foo", "hello"); metadata.put("bar", "world"); TusClient client = new TusClient(); client.setUploadCreationURL(mockServerURL); TusUpload upload = new TusUpload(); upload.setSize(10); upload.setInputStream(new ByteArrayInputStream(new byte[10])); upload.setMetadata(metadata); TusUploader uploader = client.createUpload(upload); assertEquals(uploader.getUploadURL(), new URL(mockServerURL + "/foo")); }
@Test public void testCreateUploadWithMissingLocationHeader() throws IOException, Exception { mockServer.when(new HttpRequest() .withMethod("POST") .withPath("/files") .withHeader("Tus-Resumable", TusClient.TUS_VERSION) .withHeader("Upload-Length", "10")) .respond(new HttpResponse() .withStatusCode(201) .withHeader("Tus-Resumable", TusClient.TUS_VERSION)); TusClient client = new TusClient(); client.setUploadCreationURL(mockServerURL); TusUpload upload = new TusUpload(); upload.setSize(10); upload.setInputStream(new ByteArrayInputStream(new byte[10])); try { TusUploader uploader = client.createUpload(upload); throw new Exception("unreachable code reached"); } catch(ProtocolException e) { assertEquals(e.getMessage(), "missing upload URL in response for creating upload"); } }
upload.setSize(10); upload.setInputStream(new ByteArrayInputStream(new byte[10])); TusUploader uploader = client.createUpload(upload);
upload.setSize(10); upload.setInputStream(new ByteArrayInputStream(new byte[10])); TusUploader uploader = client.createUpload(upload);