diff --git a/src/main/java/com/microsoft/bingads/v10/bulk/BulkServiceManager.java b/src/main/java/com/microsoft/bingads/v10/bulk/BulkServiceManager.java index e20193459b..54458a503a 100644 --- a/src/main/java/com/microsoft/bingads/v10/bulk/BulkServiceManager.java +++ b/src/main/java/com/microsoft/bingads/v10/bulk/BulkServiceManager.java @@ -1,5 +1,19 @@ package com.microsoft.bingads.v10.bulk; +import java.io.File; +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; +import java.util.List; +import java.util.UUID; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Future; + +import javax.xml.ws.AsyncHandler; +import javax.xml.ws.Response; + +import org.apache.http.HttpRequest; + import com.microsoft.bingads.ApiEnvironment; import com.microsoft.bingads.AsyncCallback; import com.microsoft.bingads.Authentication; @@ -7,44 +21,21 @@ import com.microsoft.bingads.CouldNotUploadFileException; import com.microsoft.bingads.HeadersImpl; import com.microsoft.bingads.InternalException; -import com.microsoft.bingads.internal.ParentCallback; import com.microsoft.bingads.ServiceClient; -import com.microsoft.bingads.internal.ServiceUtils; -import com.microsoft.bingads.v10.bulk.entities.BulkEntity; import com.microsoft.bingads.internal.HttpHeaders; +import com.microsoft.bingads.internal.ParentCallback; import com.microsoft.bingads.internal.ResultFuture; -import com.microsoft.bingads.v10.internal.bulk.StringExtensions; -import com.microsoft.bingads.v10.internal.bulk.Config; +import com.microsoft.bingads.internal.ServiceUtils; import com.microsoft.bingads.internal.functionalinterfaces.Consumer; -import com.microsoft.bingads.v10.internal.bulk.BulkFileReaderFactory; -import com.microsoft.bingads.v10.internal.bulk.CSVBulkFileReaderFactory; import com.microsoft.bingads.internal.utilities.HttpClientHttpFileService; import com.microsoft.bingads.internal.utilities.HttpFileService; import com.microsoft.bingads.internal.utilities.SimpleZipExtractor; import com.microsoft.bingads.internal.utilities.ZipExtractor; -import com.microsoft.bingads.v10.bulk.ArrayOfCampaignScope; -import com.microsoft.bingads.v10.bulk.ArrayOflong; -import com.microsoft.bingads.v10.bulk.CampaignScope; -import com.microsoft.bingads.v10.bulk.DownloadCampaignsByAccountIdsRequest; -import com.microsoft.bingads.v10.bulk.DownloadCampaignsByAccountIdsResponse; -import com.microsoft.bingads.v10.bulk.DownloadCampaignsByCampaignIdsRequest; -import com.microsoft.bingads.v10.bulk.DownloadCampaignsByCampaignIdsResponse; -import com.microsoft.bingads.v10.bulk.DownloadFileType; -import com.microsoft.bingads.v10.bulk.GetBulkUploadUrlRequest; -import com.microsoft.bingads.v10.bulk.GetBulkUploadUrlResponse; -import com.microsoft.bingads.v10.bulk.IBulkService; - -import java.io.File; -import java.io.IOException; -import java.net.URI; -import java.net.URISyntaxException; -import java.util.List; -import java.util.UUID; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.Future; -import javax.xml.ws.AsyncHandler; -import javax.xml.ws.Response; -import org.apache.http.HttpRequest; +import com.microsoft.bingads.v10.bulk.entities.BulkEntity; +import com.microsoft.bingads.v10.internal.bulk.BulkFileReaderFactory; +import com.microsoft.bingads.v10.internal.bulk.CSVBulkFileReaderFactory; +import com.microsoft.bingads.v10.internal.bulk.Config; +import com.microsoft.bingads.v10.internal.bulk.StringExtensions; /** * Provides high level methods for uploading and downloading entities using the Bulk API functionality. Also provides methods for submitting upload or download operations. @@ -83,10 +74,15 @@ public class BulkServiceManager { */ private int downloadHttpTimeoutInMilliseconds; - private final ServiceClient serviceClient; + private final ServiceClient serviceClient; private File workingDirectory; + /** + * Delete internally created temp files automatically?. + */ + private boolean cleanupTempFilesAutomatically = false; + /** * Initializes a new instance of this class with the specified {@link AuthorizationData}. * @@ -238,13 +234,20 @@ private void validateSubmitUploadParameters(SubmitUploadParameters parameters) { } } - private Future uploadEntitiesAsyncImpl(FileUploadParameters parameters, Progress progress, AsyncCallback callback) { + private Future uploadEntitiesAsyncImpl(final FileUploadParameters parameters, Progress progress, AsyncCallback callback) { final ResultFuture resultFuture = new ResultFuture(callback); uploadFileAsyncImpl(parameters, progress, new ParentCallback(resultFuture) { @Override public void onSuccess(File resultFile) throws IOException { - resultFuture.setResult(bulkFileReaderFactory.createBulkFileReader(resultFile, ResultFileType.UPLOAD, DownloadFileType.CSV).getEntities()); + if (cleanupTempFilesAutomatically) { + parameters.getUploadFilePath().delete(); + } + + BulkFileReader reader = bulkFileReaderFactory.createBulkFileReader( + resultFile, ResultFileType.UPLOAD, DownloadFileType.CSV, cleanupTempFilesAutomatically); + + resultFuture.setResult(reader.getEntities()); } }); @@ -290,7 +293,8 @@ private Future downloadEntitiesAsyncImpl(final DownloadParam public void onSuccess(File result) throws IOException { ResultFileType resultFileType = parameters.getLastSyncTimeInUTC() != null ? ResultFileType.PARTIAL_DOWNLOAD : ResultFileType.FULL_DOWNLOAD; - BulkFileReader reader = bulkFileReaderFactory.createBulkFileReader(result, resultFileType, parameters.getFileType()); + BulkFileReader reader = bulkFileReaderFactory.createBulkFileReader( + result, resultFileType, parameters.getFileType(), cleanupTempFilesAutomatically); resultFuture.setResult(reader.getEntities()); } @@ -709,6 +713,20 @@ public void setWorkingDirectory(File value) { this.workingDirectory = value; } + /** + * Delete internally created temp files automatically?. + */ + public boolean isCleanupTempFilesAutomatically() { + return cleanupTempFilesAutomatically; + } + + /** + * Set, if to delete internally created temp files automatically. + */ + public void setCleanupTempFilesAutomatically(boolean cleanupTempFilesAutomatically) { + this.cleanupTempFilesAutomatically = cleanupTempFilesAutomatically; + } + /** * Gets the time interval in milliseconds between two status polling attempts. The default value is 1000 (1 second). */ diff --git a/src/main/java/com/microsoft/bingads/v10/internal/bulk/BulkFileReaderFactory.java b/src/main/java/com/microsoft/bingads/v10/internal/bulk/BulkFileReaderFactory.java index 8a32beeaf7..ec0af21f25 100644 --- a/src/main/java/com/microsoft/bingads/v10/internal/bulk/BulkFileReaderFactory.java +++ b/src/main/java/com/microsoft/bingads/v10/internal/bulk/BulkFileReaderFactory.java @@ -1,13 +1,16 @@ package com.microsoft.bingads.v10.internal.bulk; +import java.io.File; +import java.io.IOException; + import com.microsoft.bingads.v10.bulk.BulkFileReader; import com.microsoft.bingads.v10.bulk.DownloadFileType; import com.microsoft.bingads.v10.bulk.ResultFileType; -import java.io.File; -import java.io.IOException; public interface BulkFileReaderFactory { - BulkFileReader createBulkFileReader(File localFile, ResultFileType fileType, DownloadFileType downloadFileType) throws IOException; + BulkFileReader createBulkFileReader( + File localFile, ResultFileType fileType, DownloadFileType downloadFileType, boolean deleteFileOnClose) + throws IOException; } diff --git a/src/main/java/com/microsoft/bingads/v10/internal/bulk/CSVBulkFileReaderFactory.java b/src/main/java/com/microsoft/bingads/v10/internal/bulk/CSVBulkFileReaderFactory.java index b5a5fdf89e..7e45926217 100644 --- a/src/main/java/com/microsoft/bingads/v10/internal/bulk/CSVBulkFileReaderFactory.java +++ b/src/main/java/com/microsoft/bingads/v10/internal/bulk/CSVBulkFileReaderFactory.java @@ -1,17 +1,28 @@ package com.microsoft.bingads.v10.internal.bulk; +import java.io.File; +import java.io.IOException; + import com.microsoft.bingads.v10.bulk.BulkFileReader; import com.microsoft.bingads.v10.bulk.DownloadFileType; import com.microsoft.bingads.v10.bulk.ResultFileType; -import java.io.File; -import java.io.IOException; public class CSVBulkFileReaderFactory implements BulkFileReaderFactory { - @Override - public BulkFileReader createBulkFileReader(File localFile, - ResultFileType resultType, DownloadFileType downloadFileType) throws IOException { - return new BulkFileReader(localFile, resultType, downloadFileType); + public BulkFileReader createBulkFileReader( + final File localFile, ResultFileType resultType, DownloadFileType downloadFileType, final boolean deleteFileOnClose) + throws IOException { + return new BulkFileReader(localFile, resultType, downloadFileType) { + @Override + public void close() throws IOException { + try { + super.close(); + } finally { + if (deleteFileOnClose) { + localFile.delete(); + } + } + } + }; } - } diff --git a/src/test/java/com/microsoft/bingads/v10/api/test/operations/BulkServiceTest.java b/src/test/java/com/microsoft/bingads/v10/api/test/operations/BulkServiceTest.java index b465056a2f..03b86f1f30 100644 --- a/src/test/java/com/microsoft/bingads/v10/api/test/operations/BulkServiceTest.java +++ b/src/test/java/com/microsoft/bingads/v10/api/test/operations/BulkServiceTest.java @@ -1,7 +1,29 @@ package com.microsoft.bingads.v10.api.test.operations; +import static org.easymock.EasyMock.createMock; +import static org.easymock.EasyMock.expect; +import static org.easymock.EasyMock.replay; +import static org.easymock.EasyMock.verify; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.io.File; +import java.io.IOException; +import java.net.URISyntaxException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.GregorianCalendar; +import java.util.List; +import java.util.concurrent.ExecutionException; + +import org.junit.Test; + import com.microsoft.bingads.AuthorizationData; import com.microsoft.bingads.PasswordAuthentication; +import com.microsoft.bingads.internal.functionalinterfaces.BiConsumer; +import com.microsoft.bingads.internal.functionalinterfaces.Consumer; +import com.microsoft.bingads.internal.functionalinterfaces.Supplier; +import com.microsoft.bingads.internal.utilities.ZipExtractor; import com.microsoft.bingads.v10.bulk.ArrayOfKeyValuePairOfstringstring; import com.microsoft.bingads.v10.bulk.BulkDownloadEntity; import com.microsoft.bingads.v10.bulk.BulkDownloadOperation; @@ -18,34 +40,14 @@ import com.microsoft.bingads.v10.bulk.DownloadParameters; import com.microsoft.bingads.v10.bulk.GetBulkDownloadStatusRequest; import com.microsoft.bingads.v10.bulk.GetBulkDownloadStatusResponse; -//import com.microsoft.bingads.v10.bulk.GetDetailedBulkDownloadStatusRequest; -//import com.microsoft.bingads.v10.bulk.GetDetailedBulkDownloadStatusResponse; import com.microsoft.bingads.v10.bulk.PerformanceStatsDateRange; import com.microsoft.bingads.v10.bulk.ResultFileType; import com.microsoft.bingads.v10.bulk.SubmitDownloadParameters; import com.microsoft.bingads.v10.bulk.entities.BulkEntity; -import com.microsoft.bingads.internal.functionalinterfaces.BiConsumer; -import com.microsoft.bingads.internal.functionalinterfaces.Consumer; -import com.microsoft.bingads.internal.functionalinterfaces.Supplier; import com.microsoft.bingads.v10.internal.bulk.BulkFileReaderFactory; -import com.microsoft.bingads.internal.utilities.ZipExtractor; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.UnsupportedEncodingException; -import java.net.URISyntaxException; -import java.util.ArrayList; -import java.util.Collection; -import java.util.GregorianCalendar; -import java.util.List; -import java.util.concurrent.ExecutionException; -import static org.easymock.EasyMock.createMock; -import static org.easymock.EasyMock.expect; -import static org.easymock.EasyMock.replay; -import static org.easymock.EasyMock.verify; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import org.junit.Test; + +//import com.microsoft.bingads.v10.bulk.GetDetailedBulkDownloadStatusRequest; +//import com.microsoft.bingads.v10.bulk.GetDetailedBulkDownloadStatusResponse; public class BulkServiceTest extends FakeApiTest { @@ -167,7 +169,7 @@ public void accept(DownloadCampaignsByCampaignIdsRequest request) { } @Test - public void BulkService_DownloadFile_CallsApiDownloadsAndExtractsFile() throws FileNotFoundException, UnsupportedEncodingException, IOException, URISyntaxException, InterruptedException, ExecutionException { + public void BulkService_DownloadFile_CallsApiDownloadsAndExtractsFile() throws IOException, URISyntaxException, InterruptedException, ExecutionException { final DownloadCampaignsByAccountIdsResponse apiResponse = new DownloadCampaignsByAccountIdsResponse(); apiResponse.setDownloadRequestId("req456"); @@ -347,7 +349,7 @@ public void accept(String url, File file) { expect(expectedReader.getEntities()).andReturn(bulkEntities); - expect(factory.createBulkFileReader(new File("file path"), ResultFileType.PARTIAL_DOWNLOAD, DownloadFileType.TSV)).andReturn(expectedReader); + expect(factory.createBulkFileReader(new File("file path"), ResultFileType.PARTIAL_DOWNLOAD, DownloadFileType.TSV, false)).andReturn(expectedReader); replay(zipExtractor, expectedReader, factory);