Skip to content

Commit

Permalink
Merge pull request #42 from markus-s24/cleanup-entity-temp-files
Browse files Browse the repository at this point in the history
Take care that BulkServiceManager deletes its temp files
  • Loading branch information
shyTNT committed Sep 27, 2016
2 parents e457560 + c1447e1 commit d82b39c
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 69 deletions.
Original file line number Diff line number Diff line change
@@ -1,50 +1,41 @@
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;
import com.microsoft.bingads.AuthorizationData;
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.
Expand Down Expand Up @@ -83,10 +74,15 @@ public class BulkServiceManager {
*/
private int downloadHttpTimeoutInMilliseconds;

private final ServiceClient<IBulkService> serviceClient;
private final ServiceClient<IBulkService> 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}.
*
Expand Down Expand Up @@ -238,13 +234,20 @@ private void validateSubmitUploadParameters(SubmitUploadParameters parameters) {
}
}

private Future<BulkEntityIterable> uploadEntitiesAsyncImpl(FileUploadParameters parameters, Progress<BulkOperationProgressInfo> progress, AsyncCallback<BulkEntityIterable> callback) {
private Future<BulkEntityIterable> uploadEntitiesAsyncImpl(final FileUploadParameters parameters, Progress<BulkOperationProgressInfo> progress, AsyncCallback<BulkEntityIterable> callback) {
final ResultFuture<BulkEntityIterable> resultFuture = new ResultFuture<BulkEntityIterable>(callback);

uploadFileAsyncImpl(parameters, progress, new ParentCallback<File>(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());
}
});

Expand Down Expand Up @@ -290,7 +293,8 @@ private Future<BulkEntityIterable> 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());
}
Expand Down Expand Up @@ -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).
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

}
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
};
}

}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {

Expand Down Expand Up @@ -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");

Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit d82b39c

Please sign in to comment.