Skip to content

Commit 5c2a70e

Browse files
authored
Linted codebase (#44)
* Linted the Code of the TUS-Java-SDK to prepare for the Linter in the CI Workflow * Requested changes
1 parent 2eaf0d8 commit 5c2a70e

12 files changed

Lines changed: 188 additions & 77 deletions

example/src/main/java/io/tus/java/example/Main.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,14 @@
1111
import io.tus.java.client.TusUpload;
1212
import io.tus.java.client.TusUploader;
1313

14-
public class Main {
14+
/**
15+
* A representative Example class to show an usual usecase.
16+
*/
17+
public final class Main {
18+
/**
19+
* Main method to run a standard upload task.
20+
* @param args
21+
*/
1522
public static void main(String[] args) {
1623
try {
1724
// When Java's HTTP client follows a redirect for a POST request, it will change the
@@ -71,7 +78,7 @@ protected void makeAttempt() throws ProtocolException, IOException {
7178
double progress = (double) bytesUploaded / totalBytes * 100;
7279

7380
System.out.printf("Upload at %06.2f%%.\n", progress);
74-
} while(uploader.uploadChunk() > -1);
81+
} while (uploader.uploadChunk() > -1);
7582

7683
// Allow the HTTP connection to be closed and cleaned up
7784
uploader.finish();
@@ -81,9 +88,12 @@ protected void makeAttempt() throws ProtocolException, IOException {
8188
}
8289
};
8390
executor.makeAttempts();
84-
} catch(Exception e) {
91+
} catch (Exception e) {
8592
e.printStackTrace();
8693
}
8794

8895
}
96+
private Main() {
97+
throw new IllegalStateException("Utility class");
98+
}
8999
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/**
2+
* This package contains example classes for TUS-Client usage.
3+
*/
4+
package io.tus.java.example;

src/main/java/io/tus/java/client/FingerprintNotFoundException.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
* has been stored in the {@link TusURLStore}.
66
*/
77
public class FingerprintNotFoundException extends Exception {
8+
/**
9+
* Instantiates a new Object of type {@link FingerprintNotFoundException}.
10+
* @param fingerprint
11+
*/
812
public FingerprintNotFoundException(String fingerprint) {
913
super("fingerprint not in storage found: " + fingerprint);
1014
}

src/main/java/io/tus/java/client/ProtocolException.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,38 @@
1010
public class ProtocolException extends Exception {
1111
private HttpURLConnection connection;
1212

13+
/**
14+
* Instantiates a new Object of type {@link ProtocolException}.
15+
* @param message Message to be thrown with the exception.
16+
*/
1317
public ProtocolException(String message) {
1418
super(message);
1519
}
1620

21+
/**
22+
Instantiates a new Object of type {@link ProtocolException}.
23+
* @param message Message to be thrown with the exception.
24+
* @param connection {@link HttpURLConnection}, where the error occurred.
25+
*/
1726
public ProtocolException(String message, HttpURLConnection connection) {
1827
super(message);
1928
this.connection = connection;
2029
}
2130

31+
/**
32+
* Returns the {@link HttpURLConnection} instances, which caused the error.
33+
* @return {@link HttpURLConnection}
34+
*/
2235
public HttpURLConnection getCausingConnection() {
2336
return connection;
2437
}
2538

39+
/**
40+
* Determines whether a retry attempt should be made after a {@link ProtocolException} or not.
41+
* @return {@code true} if there should be a retry attempt.
42+
*/
2643
public boolean shouldRetry() {
27-
if(connection == null) {
44+
if (connection == null) {
2845
return false;
2946
}
3047

@@ -33,7 +50,7 @@ public boolean shouldRetry() {
3350

3451
// 5XX and 423 Resource Locked status codes should be retried.
3552
return (responseCode >= 500 && responseCode < 600) || responseCode == 423;
36-
} catch(IOException e) {
53+
} catch (IOException e) {
3754
return false;
3855
}
3956
}

src/main/java/io/tus/java/client/ResumingNotEnabledException.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
* {@link TusClient#resumeUpload(TusUpload)} without enabling it first.
66
*/
77
public class ResumingNotEnabledException extends Exception {
8+
/**
9+
* Instantiates a new Object of Type {@link ResumingNotEnabledException}.
10+
*/
811
public ResumingNotEnabledException() {
912
super("resuming not enabled for this client. use enableResuming() to do so");
1013
}

src/main/java/io/tus/java/client/TusClient.java

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class TusClient {
1616
* Version of the tus protocol used by the client. The remote server needs to support this
1717
* version, too.
1818
*/
19-
public final static String TUS_VERSION = "1.0.0";
19+
public static final String TUS_VERSION = "1.0.0";
2020

2121
private URL uploadCreationURL;
2222
private boolean resumingEnabled;
@@ -44,7 +44,7 @@ public void setUploadCreationURL(URL uploadCreationURL) {
4444
}
4545

4646
/**
47-
* Get the current upload creation URL
47+
* Get the current upload creation URL.
4848
*
4949
* @return Current upload creation URL
5050
*/
@@ -144,10 +144,18 @@ public Map<String, String> getHeaders() {
144144
return headers;
145145
}
146146

147+
/**
148+
* Sets the timeout for a Connection.
149+
* @param timeout in milliseconds
150+
*/
147151
public void setConnectTimeout(int timeout) {
148152
connectTimeout = timeout;
149153
}
150154

155+
/**
156+
* Returns the Connection Timeout.
157+
* @return Timeout in milliseconds.
158+
*/
151159
public int getConnectTimeout() {
152160
return connectTimeout;
153161
}
@@ -171,20 +179,21 @@ public TusUploader createUpload(@NotNull TusUpload upload) throws ProtocolExcept
171179
prepareConnection(connection);
172180

173181
String encodedMetadata = upload.getEncodedMetadata();
174-
if(encodedMetadata.length() > 0) {
182+
if (encodedMetadata.length() > 0) {
175183
connection.setRequestProperty("Upload-Metadata", encodedMetadata);
176184
}
177185

178186
connection.addRequestProperty("Upload-Length", Long.toString(upload.getSize()));
179187
connection.connect();
180188

181189
int responseCode = connection.getResponseCode();
182-
if(!(responseCode >= 200 && responseCode < 300)) {
183-
throw new ProtocolException("unexpected status code (" + responseCode + ") while creating upload", connection);
190+
if (!(responseCode >= 200 && responseCode < 300)) {
191+
throw new ProtocolException(
192+
"unexpected status code (" + responseCode + ") while creating upload", connection);
184193
}
185194

186195
String urlStr = connection.getHeaderField("Location");
187-
if(urlStr == null || urlStr.length() == 0) {
196+
if (urlStr == null || urlStr.length() == 0) {
188197
throw new ProtocolException("missing upload URL in response for creating upload", connection);
189198
}
190199

@@ -193,7 +202,7 @@ public TusUploader createUpload(@NotNull TusUpload upload) throws ProtocolExcept
193202
// but there may be cases in which the POST request is redirected.
194203
URL uploadURL = new URL(connection.getURL(), urlStr);
195204

196-
if(resumingEnabled) {
205+
if (resumingEnabled) {
197206
urlStore.set(upload.getFingerprint(), uploadURL);
198207
}
199208

@@ -217,7 +226,8 @@ public TusUploader createUpload(@NotNull TusUpload upload) throws ProtocolExcept
217226
* wrong status codes or missing/invalid headers.
218227
* @throws IOException Thrown if an exception occurs while issuing the HTTP request.
219228
*/
220-
public TusUploader resumeUpload(@NotNull TusUpload upload) throws FingerprintNotFoundException, ResumingNotEnabledException, ProtocolException, IOException {
229+
public TusUploader resumeUpload(@NotNull TusUpload upload) throws
230+
FingerprintNotFoundException, ResumingNotEnabledException, ProtocolException, IOException {
221231
if (!resumingEnabled) {
222232
throw new ResumingNotEnabledException();
223233
}
@@ -247,20 +257,22 @@ public TusUploader resumeUpload(@NotNull TusUpload upload) throws FingerprintNot
247257
* wrong status codes or missing/invalid headers.
248258
* @throws IOException Thrown if an exception occurs while issuing the HTTP request.
249259
*/
250-
public TusUploader beginOrResumeUploadFromURL(@NotNull TusUpload upload, @NotNull URL uploadURL) throws ProtocolException, IOException {
260+
public TusUploader beginOrResumeUploadFromURL(@NotNull TusUpload upload, @NotNull URL uploadURL) throws
261+
ProtocolException, IOException {
251262
HttpURLConnection connection = (HttpURLConnection) uploadURL.openConnection();
252263
connection.setRequestMethod("HEAD");
253264
prepareConnection(connection);
254265

255266
connection.connect();
256267

257268
int responseCode = connection.getResponseCode();
258-
if(!(responseCode >= 200 && responseCode < 300)) {
259-
throw new ProtocolException("unexpected status code (" + responseCode + ") while resuming upload", connection);
269+
if (!(responseCode >= 200 && responseCode < 300)) {
270+
throw new ProtocolException(
271+
"unexpected status code (" + responseCode + ") while resuming upload", connection);
260272
}
261273

262274
String offsetStr = connection.getHeaderField("Upload-Offset");
263-
if(offsetStr == null || offsetStr.length() == 0) {
275+
if (offsetStr == null || offsetStr.length() == 0) {
264276
throw new ProtocolException("missing upload offset in response for resuming upload", connection);
265277
}
266278
long offset = Long.parseLong(offsetStr);
@@ -277,19 +289,20 @@ public TusUploader beginOrResumeUploadFromURL(@NotNull TusUpload upload, @NotNul
277289
* @throws ProtocolException Thrown if the remote server sent an unexpected response, e.g.
278290
* wrong status codes or missing/invalid headers.
279291
* @throws IOException Thrown if an exception occurs while issuing the HTTP request.
292+
* @return {@link TusUploader} instance.
280293
*/
281294
public TusUploader resumeOrCreateUpload(@NotNull TusUpload upload) throws ProtocolException, IOException {
282295
try {
283296
return resumeUpload(upload);
284-
} catch(FingerprintNotFoundException e) {
297+
} catch (FingerprintNotFoundException e) {
285298
return createUpload(upload);
286-
} catch(ResumingNotEnabledException e) {
299+
} catch (ResumingNotEnabledException e) {
287300
return createUpload(upload);
288-
} catch(ProtocolException e) {
301+
} catch (ProtocolException e) {
289302
// If the attempt to resume returned a 404 Not Found, we immediately try to create a new
290303
// one since TusExectuor would not retry this operation.
291304
HttpURLConnection connection = e.getCausingConnection();
292-
if(connection != null && connection.getResponseCode() == 404) {
305+
if (connection != null && connection.getResponseCode() == 404) {
293306
return createUpload(upload);
294307
}
295308

@@ -306,13 +319,16 @@ public TusUploader resumeOrCreateUpload(@NotNull TusUpload upload) throws Protoc
306319
public void prepareConnection(@NotNull HttpURLConnection connection) {
307320
// Only follow redirects, if the POST methods is preserved. If http.strictPostRedirect is
308321
// disabled, a POST request will be transformed into a GET request which is not wanted by us.
309-
// See: http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/7u40-b43/sun/net/www/protocol/http/HttpURLConnection.java#2372
322+
// CHECKSTYLE:OFF
323+
// Necessary because of length of the link
324+
// See:https://github.com/openjdk/jdk/blob/jdk7-b43/jdk/src/share/classes/sun/net/www/protocol/http/HttpURLConnection.java#L2020-L2035
325+
// CHECKSTYLE:ON
310326
connection.setInstanceFollowRedirects(Boolean.getBoolean("http.strictPostRedirect"));
311327

312328
connection.setConnectTimeout(connectTimeout);
313329
connection.addRequestProperty("Tus-Resumable", TUS_VERSION);
314330

315-
if(headers != null) {
331+
if (headers != null) {
316332
for (Map.Entry<String, String> entry : headers.entrySet()) {
317333
connection.addRequestProperty(entry.getKey(), entry.getValue());
318334
}

src/main/java/io/tus/java/client/TusExecutor.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,27 +78,27 @@ public int[] getDelays() {
7878
*/
7979
public boolean makeAttempts() throws ProtocolException, IOException {
8080
int attempt = -1;
81-
while(true) {
81+
while (true) {
8282
attempt++;
8383

8484
try {
8585
makeAttempt();
8686
// Returning true is the signal that the makeAttempt() function exited without
8787
// throwing an error.
8888
return true;
89-
} catch(ProtocolException e) {
89+
} catch (ProtocolException e) {
9090
// Do not attempt a retry, if the Exception suggests so.
91-
if(!e.shouldRetry()) {
91+
if (!e.shouldRetry()) {
9292
throw e;
9393
}
9494

95-
if(attempt >= delays.length) {
95+
if (attempt >= delays.length) {
9696
// We exceeds the number of maximum retries. In this case the latest exception
9797
// is thrown.
9898
throw e;
9999
}
100-
} catch(IOException e) {
101-
if(attempt >= delays.length) {
100+
} catch (IOException e) {
101+
if (attempt >= delays.length) {
102102
// We exceeds the number of maximum retries. In this case the latest exception
103103
// is thrown.
104104
throw e;
@@ -108,7 +108,7 @@ public boolean makeAttempts() throws ProtocolException, IOException {
108108
try {
109109
// Sleep for the specified delay before attempting the next retry.
110110
Thread.sleep(delays[attempt]);
111-
} catch(InterruptedException e) {
111+
} catch (InterruptedException e) {
112112
// If we get interrupted while waiting for the next retry, the user has cancelled
113113
// the upload willingly and we return false as a signal.
114114
return false;

src/main/java/io/tus/java/client/TusInputStream.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ class TusInputStream {
1919
*
2020
* @param stream The stream to read from
2121
*/
22-
public TusInputStream(InputStream stream) {
23-
if(!stream.markSupported()) {
22+
TusInputStream(InputStream stream) {
23+
if (!stream.markSupported()) {
2424
stream = new BufferedInputStream(stream);
2525
}
2626

@@ -50,7 +50,7 @@ public int read(byte[] buffer, int length) throws IOException {
5050
* @throws IOException
5151
*/
5252
public void seekTo(long position) throws IOException {
53-
if(lastMark != -1) {
53+
if (lastMark != -1) {
5454
stream.reset();
5555
stream.skip(position - lastMark);
5656
lastMark = -1;

src/main/java/io/tus/java/client/TusURLMemoryStore.java

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,34 @@
1313
* keep the values after your application crashes or restarts.
1414
*/
1515
public class TusURLMemoryStore implements TusURLStore {
16+
private Map<String, URL> store = new HashMap<String, URL>();
1617

17-
private Map<String, URL> store = new HashMap<String, URL>();
18+
/**
19+
* Stores the upload's fingerprint and url.
20+
* @param fingerprint An upload's fingerprint.
21+
* @param url The corresponding upload URL.
22+
*/
23+
@Override
24+
public void set(String fingerprint, URL url) {
25+
store.put(fingerprint, url);
26+
}
1827

19-
@Override
20-
public void set(String fingerprint, URL url) {
21-
store.put(fingerprint, url);
22-
}
28+
/**
29+
* Returns the corresponding Upload URL to a given fingerprint.
30+
* @param fingerprint An upload's fingerprint.
31+
* @return The corresponding upload URL.
32+
*/
33+
@Override
34+
public URL get(String fingerprint) {
35+
return store.get(fingerprint);
36+
}
2337

24-
@Override
25-
public URL get(String fingerprint) {
26-
return store.get(fingerprint);
27-
}
28-
29-
@Override
30-
public void remove(String fingerprint) {
31-
store.remove(fingerprint);
32-
}
38+
/**
39+
* Removes the corresponding entry to a fingerprint from the {@link TusURLMemoryStore}.
40+
* @param fingerprint An upload's fingerprint.
41+
*/
42+
@Override
43+
public void remove(String fingerprint) {
44+
store.remove(fingerprint);
45+
}
3346
}

0 commit comments

Comments
 (0)