/** * 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 testResumeUpload() throws ResumingNotEnabledException, FingerprintNotFoundException, IOException, ProtocolException { mockServer.when(new HttpRequest() .withMethod("HEAD") .withPath("/files/foo") .withHeader("Tus-Resumable", TusClient.TUS_VERSION)) .respond(new HttpResponse() .withStatusCode(204) .withHeader("Tus-Resumable", TusClient.TUS_VERSION) .withHeader("Upload-Offset", "3")); TusClient client = new TusClient(); client.setUploadCreationURL(mockServerURL); client.enableResuming(new TestResumeUploadStore()); TusUpload upload = new TusUpload(); upload.setSize(10); upload.setInputStream(new ByteArrayInputStream(new byte[10])); upload.setFingerprint("test-fingerprint"); TusUploader uploader = client.resumeUpload(upload); assertEquals(uploader.getUploadURL(), new URL(mockServerURL.toString() + "/foo")); assertEquals(uploader.getOffset(), 3); }