@@ -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 }
0 commit comments