use of org.datatransferproject.spi.transfer.types.InvalidTokenException in project data-transfer-project by google.
the class GooglePhotosExporterTest method exportAlbumFirstSet.
@Test
public void exportAlbumFirstSet() throws IOException, InvalidTokenException, PermissionDeniedException {
setUpSingleAlbum();
when(albumListResponse.getNextPageToken()).thenReturn(ALBUM_TOKEN);
// Run test
ExportResult<PhotosContainerResource> result = googlePhotosExporter.exportAlbums(null, Optional.empty(), uuid);
// Check results
// Verify correct methods were called
verify(photosInterface).listAlbums(Optional.empty());
verify(albumListResponse).getAlbums();
// Check pagination token
ContinuationData continuationData = result.getContinuationData();
StringPaginationToken paginationToken = (StringPaginationToken) continuationData.getPaginationData();
assertThat(paginationToken.getToken()).isEqualTo(ALBUM_TOKEN_PREFIX + ALBUM_TOKEN);
// Check albums field of container
Collection<PhotoAlbum> actualAlbums = result.getExportedData().getAlbums();
assertThat(actualAlbums.stream().map(PhotoAlbum::getId).collect(Collectors.toList())).containsExactly(ALBUM_ID);
// Check photos field of container (should be empty, even though there is a photo in the
// original album)
Collection<PhotoModel> actualPhotos = result.getExportedData().getPhotos();
assertThat(actualPhotos).isEmpty();
// Should be one container in the resource list
List<ContainerResource> actualResources = continuationData.getContainerResources();
assertThat(actualResources.stream().map(a -> ((IdOnlyContainerResource) a).getId()).collect(Collectors.toList())).containsExactly(ALBUM_ID);
}
use of org.datatransferproject.spi.transfer.types.InvalidTokenException in project data-transfer-project by google.
the class KoofrPhotosImporter method importSinglePhoto.
private String importSinglePhoto(PhotoModel photo, UUID jobId, IdempotentImportExecutor idempotentImportExecutor, KoofrClient koofrClient) throws IOException, InvalidTokenException, DestinationMemoryFullException {
monitor.debug(() -> String.format("Import single photo %s", photo.getTitle()));
BufferedInputStream inputStream = null;
try {
if (photo.isInTempStore()) {
inputStream = new BufferedInputStream(jobStore.getStream(jobId, photo.getFetchableUrl()).getStream());
} else if (photo.getFetchableUrl() != null) {
HttpURLConnection conn = imageStreamProvider.getConnection(photo.getFetchableUrl());
inputStream = new BufferedInputStream(conn.getInputStream());
} else {
throw new IllegalStateException("Don't know how to get the inputStream for " + photo.getTitle());
}
final byte[] bytes = IOUtils.toByteArray(inputStream);
Date dateCreated = getDateCreated(photo, bytes);
String title = buildPhotoTitle(jobId, photo.getTitle(), dateCreated);
String description = KoofrClient.trimDescription(photo.getDescription());
String parentPath = idempotentImportExecutor.getCachedValue(photo.getAlbumId());
String fullPath = parentPath + "/" + title;
if (koofrClient.fileExists(fullPath)) {
monitor.debug(() -> String.format("Photo already exists %s", photo.getTitle()));
return fullPath;
}
final ByteArrayInputStream inMemoryInputStream = new ByteArrayInputStream(bytes);
String response = koofrClient.uploadFile(parentPath, title, inMemoryInputStream, photo.getMediaType(), dateCreated, description);
try {
if (photo.isInTempStore()) {
jobStore.removeData(jobId, photo.getFetchableUrl());
}
} catch (Exception e) {
// Swallow the exception caused by Remove data so that existing flows continue
monitor.info(() -> format("Exception swallowed while removing data for jobId %s, localPath %s", jobId, photo.getFetchableUrl()), e);
}
return response;
} finally {
if (inputStream != null) {
inputStream.close();
}
}
}
use of org.datatransferproject.spi.transfer.types.InvalidTokenException in project data-transfer-project by google.
the class KoofrClient method uploadFile.
@SuppressWarnings("unchecked")
public String uploadFile(String parentPath, String name, InputStream inputStream, String mediaType, Date modified, String description) throws IOException, InvalidTokenException, DestinationMemoryFullException {
String url;
try {
URIBuilder builder = getUriBuilder().setPath(CONTENT_API_PATH_PREFIX + "/mounts/primary/files/put").setParameter("path", parentPath).setParameter("filename", name).setParameter("autorename", "true").setParameter("info", "true");
if (description != null && description.length() > 0) {
builder.setParameter("tags", "description=" + description);
}
if (modified != null) {
builder.setParameter("modified", Long.toString(modified.getTime()));
}
url = builder.build().toString();
} catch (URISyntaxException e) {
throw new IllegalStateException("Could not produce url.", e);
}
Request.Builder requestBuilder = getRequestBuilder(url);
RequestBody uploadBody = new InputStreamRequestBody(MediaType.parse(mediaType), inputStream);
requestBuilder.post(uploadBody);
// We need to reset the input stream because the request could already read some data
try (Response response = getResponse(fileUploadClient, requestBuilder, () -> inputStream.reset())) {
int code = response.code();
ResponseBody body = response.body();
if (code == 413) {
throw new DestinationMemoryFullException("Koofr quota exceeded", new Exception("Koofr file upload response code " + code));
}
if (code < 200 || code > 299) {
throw new IOException("Got error code: " + code + " message: " + response.message() + " body: " + body.string());
}
Map<String, Object> responseData = objectMapper.readValue(body.bytes(), Map.class);
String newName = (String) responseData.get("name");
Preconditions.checkState(!Strings.isNullOrEmpty(newName), "Expected name value to be present in %s", responseData);
return parentPath + "/" + newName;
}
}
use of org.datatransferproject.spi.transfer.types.InvalidTokenException in project data-transfer-project by google.
the class KoofrClientTest method testFileExistsRefreshTokenNotFound.
@Test
public void testFileExistsRefreshTokenNotFound() throws Exception {
when(credentialFactory.refreshCredential(credential)).then((InvocationOnMock invocation) -> {
throw new InvalidTokenException("Unable to refresh token.", null);
});
server.enqueue(new MockResponse().setResponseCode(401));
InvalidTokenException caughtExc = null;
try {
client.fileExists("/path/to/file");
} catch (InvalidTokenException exc) {
caughtExc = exc;
}
Assert.assertNotNull(caughtExc);
Assert.assertEquals("Unable to refresh token.", caughtExc.getMessage());
Assert.assertEquals(1, server.getRequestCount());
RecordedRequest recordedRequest = server.takeRequest();
Assert.assertEquals("GET", recordedRequest.getMethod());
Assert.assertEquals("/api/v2/mounts/primary/files/info?path=%2Fpath%2Fto%2Ffile", recordedRequest.getPath());
Assert.assertEquals("Bearer acc", recordedRequest.getHeader("Authorization"));
Assert.assertEquals("2.1", recordedRequest.getHeader("X-Koofr-Version"));
}
Aggregations