Skip to content

Commit f80a1cd

Browse files
committed
use constants
1 parent 7e732a0 commit f80a1cd

1 file changed

Lines changed: 16 additions & 14 deletions

File tree

src/main/java/edu/harvard/iq/dataverse/util/bagit/BagGenerator.java

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,11 @@
100100
public class BagGenerator {
101101

102102
private static final Logger logger = Logger.getLogger(BagGenerator.class.getCanonicalName());
103-
103+
104+
static final String CRLF = "\r\n";
105+
106+
protected static final int MAX_RETRIES = 5;
107+
104108
private ParallelScatterZipCreator scatterZipCreator = null;
105109
private ScatterZipOutputStream dirs = null;
106110

@@ -326,7 +330,7 @@ public boolean generateBag(OutputStream outputStream) throws Exception {
326330
boolean first = true;
327331
for (Entry<String, String> pidEntry : pidMap.entrySet()) {
328332
if (!first) {
329-
pidStringBuffer.append("\r\n");
333+
pidStringBuffer.append(CRLF);
330334
} else {
331335
first = false;
332336
}
@@ -341,7 +345,7 @@ public boolean generateBag(OutputStream outputStream) throws Exception {
341345
first = true;
342346
for (Entry<String, String> sha1Entry : checksumMap.entrySet()) {
343347
if (!first) {
344-
sha1StringBuffer.append("\r\n");
348+
sha1StringBuffer.append(CRLF);
345349
} else {
346350
first = false;
347351
}
@@ -784,7 +788,7 @@ private boolean addToZip(long fileSize) {
784788
// Method to append to fetch file content
785789
private void addToFetchFile(String url, long size, String filename) {
786790
// Format: URL size filename
787-
fetchFileContent.append(url).append(" ").append(Long.toString(size)).append(" ").append(filename).append("\r\n");
791+
fetchFileContent.append(url).append(" ").append(Long.toString(size)).append(" ").append(filename).append(CRLF);
788792
}
789793

790794
// Method to write fetch file to bag (call this before finalizing the bag)
@@ -910,8 +914,6 @@ public void writeTo(ZipArchiveOutputStream zipArchiveOutputStream)
910914
logger.fine("Files written");
911915
}
912916

913-
static final String CRLF = "\r\n";
914-
915917
private String generateInfoFile() {
916918
logger.fine("Generating info file");
917919
StringBuffer info = new StringBuffer();
@@ -1303,7 +1305,7 @@ public InputStream get() {
13031305
try {
13041306
URI uri = new URI(uriString);
13051307
int tries = 0;
1306-
while (tries < 5) {
1308+
while (tries < MAX_RETRIES) {
13071309

13081310
logger.finest("Get # " + tries + " for " + uriString);
13091311
HttpGet getFile = createNewGetRequest(uri, null);
@@ -1357,29 +1359,29 @@ public void close() throws IOException {
13571359
} catch (InterruptedException ie) {
13581360
logger.log(Level.SEVERE, "InterruptedException during retry delay for file: " + uriString, ie);
13591361
Thread.currentThread().interrupt(); // Restore interrupt status
1360-
tries += 5; // Skip remaining attempts
1362+
tries += MAX_RETRIES; // Skip remaining attempts
13611363
}
13621364
}
13631365
} catch (ClientProtocolException e) {
1364-
tries += 5;
1366+
tries += MAX_RETRIES;
13651367
logger.log(Level.SEVERE, "ClientProtocolException when retrieving file: " + uriString + " (attempt " + tries + ")", e);
13661368
} catch (SocketTimeoutException e) {
13671369
// Specific handling for timeout exceptions
13681370
tries++;
1369-
logger.log(Level.SEVERE, "SocketTimeoutException when retrieving file: " + uriString + " (attempt " + tries + " of 5) - Request exceeded timeout", e);
1370-
if (tries == 5) {
1371+
logger.log(Level.SEVERE, "SocketTimeoutException when retrieving file: " + uriString + " (attempt " + tries + " of " + MAX_RETRIES + ") - Request exceeded timeout", e);
1372+
if (tries == MAX_RETRIES) {
13711373
logger.log(Level.SEVERE, "FINAL FAILURE: File could not be retrieved after all retries due to timeouts: " + uriString, e);
13721374
}
13731375
} catch (InterruptedIOException e) {
13741376
// Catches interruptions during I/O operations
1375-
tries += 5;
1377+
tries += MAX_RETRIES;
13761378
logger.log(Level.SEVERE, "InterruptedIOException when retrieving file: " + uriString + " - Operation was interrupted", e);
13771379
Thread.currentThread().interrupt(); // Restore interrupt status
13781380
} catch (IOException e) {
13791381
// Retry if this is a potentially temporary error such as a timeout
13801382
tries++;
1381-
logger.log(Level.WARNING, "IOException when retrieving file: " + uriString + " (attempt " + tries + " of 5)", e);
1382-
if (tries == 5) {
1383+
logger.log(Level.WARNING, "IOException when retrieving file: " + uriString + " (attempt " + tries + " of " + MAX_RETRIES+ ")", e);
1384+
if (tries == MAX_RETRIES) {
13831385
logger.log(Level.SEVERE, "FINAL FAILURE: File could not be retrieved after all retries: " + uriString, e);
13841386
}
13851387
}

0 commit comments

Comments
 (0)