Removing code smells
[dmaap/datarouter.git] / datarouter-node / src / main / java / org / onap / dmaap / datarouter / node / DeliveryTask.java
index bbd1391..eb79b56 100644 (file)
@@ -39,6 +39,7 @@ import java.io.OutputStream;
 import java.net.HttpURLConnection;
 import java.net.ProtocolException;
 import java.net.URL;
+import java.nio.file.Files;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.UUID;
@@ -93,9 +94,9 @@ public class DeliveryTask implements Runnable, Comparable<DeliveryTask> {
         this.followRedirects = destInfo.isFollowRedirects();
         feedid = destInfo.getLogData();
         spool = destInfo.getSpool();
-        String dfn = spool + "/" + pubid;
+        String dfn = spool + File.separator + pubid;
         String mfn = dfn + ".M";
-        datafile = new File(spool + "/" + pubid);
+        datafile = new File(spool + File.separator + pubid);
         metafile = new File(mfn);
         boolean monly = destInfo.isMetaDataOnly();
         date = Long.parseLong(pubid.substring(0, pubid.indexOf('.')));
@@ -221,6 +222,7 @@ public class DeliveryTask implements Runnable, Comparable<DeliveryTask> {
             byte[] buf = new byte[4096];
             if (is != null) {
                 while (is.read(buf) > 0) {
+                    //flush the buffer
                 }
                 is.close();
             }
@@ -241,7 +243,7 @@ public class DeliveryTask implements Runnable, Comparable<DeliveryTask> {
         httpURLConnection.setRequestProperty(DECOMPRESSION_STATUS, "SUCCESS");
         OutputStream outputStream = getOutputStream(httpURLConnection);
         if (outputStream != null) {
-            int bytesRead = 0;
+            int bytesRead;
             try (InputStream gzipInputStream = new GZIPInputStream(new FileInputStream(datafile))) {
                 int bufferLength = buffer.length;
                 while ((bytesRead = gzipInputStream.read(buffer, 0, bufferLength)) > 0) {
@@ -295,7 +297,7 @@ public class DeliveryTask implements Runnable, Comparable<DeliveryTask> {
      * @param httpURLConnection connection used to make request
      * @return AN Outpustream that can be used to send your data.
      */
-    private OutputStream getOutputStream(HttpURLConnection httpURLConnection) throws IOException {
+    OutputStream getOutputStream(HttpURLConnection httpURLConnection) throws IOException {
         OutputStream outputStream = null;
         try {
             outputStream = httpURLConnection.getOutputStream();
@@ -355,13 +357,28 @@ public class DeliveryTask implements Runnable, Comparable<DeliveryTask> {
      * Remove meta and data files.
      */
     void clean() {
-        datafile.delete();
-        metafile.delete();
+        deleteWithRetry(datafile);
+        deleteWithRetry(metafile);
         eelfLogger.info(EelfMsgs.INVOKE, newInvocationId);
         eelfLogger.info(EelfMsgs.EXIT);
         hdrs = null;
     }
 
+    private void deleteWithRetry(File file) {
+        int maxTries = 3;
+        int tryCount = 1;
+        while (tryCount <= maxTries) {
+            try {
+                Files.deleteIfExists(file.toPath());
+                break;
+            } catch (IOException e) {
+                eelfLogger.error("IOException : Failed to delete file :"
+                                         + file.getName() + " on attempt " + tryCount, e);
+            }
+            tryCount++;
+        }
+    }
+
     /**
      * Get the resume time for a delivery task.
      */
@@ -449,7 +466,7 @@ public class DeliveryTask implements Runnable, Comparable<DeliveryTask> {
     /**
      * Get the followRedirects for this delivery task.
      */
-    public boolean getFollowRedirects() {
+    boolean getFollowRedirects() {
         return (followRedirects);
     }
 }