forked from tus/tus-java-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,39 @@ | ||
package io.tus.java.client; | ||
|
||
import java.io.IOException; | ||
import java.net.HttpURLConnection; | ||
|
||
/** | ||
* This exception is thrown if the server sends a request with an unexpected status code or | ||
* missing/invalid headers. | ||
*/ | ||
public class ProtocolException extends Exception { | ||
private HttpURLConnection connection; | ||
|
||
public ProtocolException(String message) { | ||
super(message); | ||
} | ||
|
||
public ProtocolException(String message, HttpURLConnection connection) { | ||
super(message); | ||
this.connection = connection; | ||
} | ||
|
||
public HttpURLConnection getCausingConnection() { | ||
return connection; | ||
} | ||
|
||
public boolean shouldRetry() { | ||
if(connection == null) { | ||
return false; | ||
} | ||
|
||
try { | ||
int responseCode = connection.getResponseCode(); | ||
|
||
return responseCode >= 500 && responseCode < 600; | ||
} catch(IOException e) { | ||
return false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package io.tus.java.client; | ||
|
||
|
||
import java.io.IOException; | ||
|
||
public class TusRetryingClient extends TusClient { | ||
private int maxRetries = 5; | ||
|
||
public void setMaxRetries(int retries) { | ||
maxRetries = retries; | ||
} | ||
|
||
@Override | ||
public TusUploader createUpload(TusUpload upload) throws ProtocolException, IOException { | ||
int attempt = 0; | ||
while(true) { | ||
attempt++; | ||
|
||
try { | ||
return super.createUpload(upload); | ||
} catch(IOException e) { | ||
if(attempt < maxRetries) { | ||
continue; | ||
} | ||
|
||
throw e; | ||
} catch(ProtocolException e) { | ||
if(e.shouldRetry()) { | ||
continue; | ||
} | ||
|
||
if(attempt < maxRetries) { | ||
continue; | ||
} | ||
|
||
throw e; | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public TusUploader resumeUpload(TusUpload upload) throws FingerprintNotFoundException, ResumingNotEnabledException, ProtocolException, IOException { | ||
int attempt = 0; | ||
while(true) { | ||
attempt++; | ||
|
||
try { | ||
return super.resumeUpload(upload); | ||
} catch(IOException e) { | ||
if(attempt < maxRetries) { | ||
continue; | ||
} | ||
|
||
throw e; | ||
} catch(ProtocolException e) { | ||
if(e.shouldRetry()) { | ||
continue; | ||
} | ||
|
||
if(attempt < maxRetries) { | ||
continue; | ||
} | ||
|
||
throw e; | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public TusUploader resumeOrCreateUpload(TusUpload upload) throws IOException, ProtocolException { | ||
int attempt = 0; | ||
while(true) { | ||
attempt++; | ||
|
||
try { | ||
return super.resumeOrCreateUpload(upload); | ||
} catch(IOException e) { | ||
if(attempt < maxRetries) { | ||
continue; | ||
} | ||
|
||
break; | ||
} catch(ProtocolException e) { | ||
if(e.shouldRetry()) { | ||
continue; | ||
} | ||
|
||
if(attempt < maxRetries) { | ||
continue; | ||
} | ||
|
||
break; | ||
} | ||
} | ||
|
||
// Fall back to creating an upload if resuming failed for too long. | ||
return createUpload(upload); | ||
} | ||
} |