Skip to content

Commit

Permalink
fix: wrap invalid token, destination full exceptions from DriveImport…
Browse files Browse the repository at this point in the history
…er (#1292)

* fix: wrap invalid token, destination full exceptions from DriveImporter

* formatting
  • Loading branch information
alexeyqu-fb authored Nov 13, 2023
1 parent cc16303 commit 56d5590
Showing 1 changed file with 38 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import static com.google.common.base.Preconditions.checkNotNull;

import com.google.api.client.auth.oauth2.TokenErrorResponse;
import com.google.api.client.auth.oauth2.TokenResponseException;
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
import com.google.api.client.http.InputStreamContent;
import com.google.api.client.util.DateTime;
import com.google.api.services.drive.Drive;
Expand All @@ -16,6 +19,8 @@
import org.datatransferproject.spi.transfer.idempotentexecutor.IdempotentImportExecutor;
import org.datatransferproject.spi.transfer.provider.ImportResult;
import org.datatransferproject.spi.transfer.provider.Importer;
import org.datatransferproject.spi.transfer.types.DestinationMemoryFullException;
import org.datatransferproject.spi.transfer.types.InvalidTokenException;
import org.datatransferproject.types.common.models.blob.BlobbyStorageContainerResource;
import org.datatransferproject.types.common.models.blob.DigitalDocumentWrapper;
import org.datatransferproject.types.common.models.blob.DtpDigitalDocument;
Expand Down Expand Up @@ -83,18 +88,32 @@ public ImportResult importItem(
}

private String importSingleFolder(Drive driveInterface, String folderName, String parentId)
throws IOException {
throws IOException, InvalidTokenException, DestinationMemoryFullException {
File newFolder = new File().setName(folderName).setMimeType(DriveExporter.FOLDER_MIME_TYPE);
if (!Strings.isNullOrEmpty(parentId)) {
newFolder.setParents(ImmutableList.of(parentId));
}
File resultFolder = driveInterface.files().create(newFolder).execute();
return resultFolder.getId();

try {
return driveInterface.files().create(newFolder).execute().getId();
} catch (TokenResponseException e) {
TokenErrorResponse details = e.getDetails();
if (details != null && details.getError().equals("invalid_grant")) {
throw new InvalidTokenException("Unable to refresh token.", e);
}
throw e;
} catch (GoogleJsonResponseException e) {
if (e.getStatusCode() == 403
&& e.getMessage().contains("storage quota has been exceeded")) {
throw new DestinationMemoryFullException("Destination Google Drive storage was full", e);
}
throw e;
}
}

private String importSingleFile(
UUID jobId, Drive driveInterface, DigitalDocumentWrapper file, String parentId)
throws IOException {
throws IOException, InvalidTokenException, DestinationMemoryFullException {
InputStreamContent content =
new InputStreamContent(
null, jobStore.getStream(jobId, file.getCachedContentId()).getStream());
Expand All @@ -110,7 +129,21 @@ private String importSingleFile(
&& file.getOriginalEncodingFormat().startsWith("application/vnd.google-apps.")) {
driveFile.setMimeType(file.getOriginalEncodingFormat());
}
return driveInterface.files().create(driveFile, content).execute().getId();
try {
return driveInterface.files().create(driveFile, content).execute().getId();
} catch (TokenResponseException e) {
TokenErrorResponse details = e.getDetails();
if (details != null && details.getError().equals("invalid_grant")) {
throw new InvalidTokenException("Unable to refresh token.", e);
}
throw e;
} catch (GoogleJsonResponseException e) {
if (e.getStatusCode() == 403
&& e.getMessage().contains("storage quota has been exceeded")) {
throw new DestinationMemoryFullException("Destination Google Drive storage was full", e);
}
throw e;
}
}

private synchronized Drive getDriveInterface(TokensAndUrlAuthData authData) {
Expand Down

0 comments on commit 56d5590

Please sign in to comment.