use of org.datatransferproject.types.common.StringPaginationToken in project data-transfer-project by google.
the class FacebookPhotosExporterTest method testSpecifiedAlbums.
@Test
public void testSpecifiedAlbums() throws CopyExceptionWithFailureReason {
ExportResult<PhotosContainerResource> result = facebookPhotosExporter.export(uuid, new TokensAndUrlAuthData("accessToken", null, null), Optional.of(new ExportInformation(new StringPaginationToken(PHOTO_TOKEN_PREFIX), new PhotosContainerResource(Lists.newArrayList(new PhotoAlbum(ALBUM_ID, ALBUM_NAME, ALBUM_DESCRIPTION)), new ArrayList<>()))));
assertEquals(ExportResult.ResultType.CONTINUE, result.getType());
PhotosContainerResource exportedData = result.getExportedData();
assertEquals(1, exportedData.getAlbums().size());
assertEquals(new PhotoAlbum(ALBUM_ID, ALBUM_NAME, ALBUM_DESCRIPTION), exportedData.getAlbums().toArray()[0]);
assertNull((result.getContinuationData().getPaginationData()));
assertThat(result.getContinuationData().getContainerResources()).contains(new IdOnlyContainerResource(ALBUM_ID));
}
use of org.datatransferproject.types.common.StringPaginationToken in project data-transfer-project by google.
the class GoogleCalendarExporter method export.
@Override
public ExportResult<CalendarContainerResource> export(UUID jobId, TokensAndUrlAuthData authData, Optional<ExportInformation> exportInformation) {
if (!exportInformation.isPresent()) {
return exportCalendars(authData, Optional.empty());
} else {
StringPaginationToken paginationToken = (StringPaginationToken) exportInformation.get().getPaginationData();
if (paginationToken != null && paginationToken.getToken().startsWith(CALENDAR_TOKEN_PREFIX)) {
// Next thing to export is more calendars
return exportCalendars(authData, Optional.of(paginationToken));
} else {
// Next thing to export is events
IdOnlyContainerResource idOnlyContainerResource = (IdOnlyContainerResource) exportInformation.get().getContainerResource();
Optional<PaginationData> pageData = Optional.ofNullable(paginationToken);
return getCalendarEvents(authData, idOnlyContainerResource.getId(), pageData);
}
}
}
use of org.datatransferproject.types.common.StringPaginationToken in project data-transfer-project by google.
the class GoogleCalendarExporter method exportCalendars.
private ExportResult<CalendarContainerResource> exportCalendars(TokensAndUrlAuthData authData, Optional<PaginationData> pageData) {
Calendar.CalendarList.List listRequest;
CalendarList listResult;
// Get calendar information
try {
listRequest = getOrCreateCalendarInterface(authData).calendarList().list();
if (pageData.isPresent()) {
StringPaginationToken paginationToken = (StringPaginationToken) pageData.get();
Preconditions.checkState(paginationToken.getToken().startsWith(CALENDAR_TOKEN_PREFIX), "Token is not applicable");
listRequest.setPageToken(((StringPaginationToken) pageData.get()).getToken().substring(CALENDAR_TOKEN_PREFIX.length()));
}
listResult = listRequest.execute();
} catch (IOException e) {
return new ExportResult<>(e);
}
// Set up continuation data
PaginationData nextPageData = null;
if (listResult.getNextPageToken() != null) {
nextPageData = new StringPaginationToken(CALENDAR_TOKEN_PREFIX + listResult.getNextPageToken());
}
ContinuationData continuationData = new ContinuationData(nextPageData);
// Process calendar list
List<CalendarModel> calendarModels = new ArrayList<>(listResult.getItems().size());
for (CalendarListEntry calendarData : listResult.getItems()) {
CalendarModel model = convertToCalendarModel(calendarData);
continuationData.addContainerResource(new IdOnlyContainerResource(calendarData.getId()));
calendarModels.add(model);
}
CalendarContainerResource calendarContainerResource = new CalendarContainerResource(calendarModels, null);
// Get result type
ExportResult.ResultType resultType = ResultType.CONTINUE;
if (calendarModels.isEmpty()) {
resultType = ResultType.END;
}
return new ExportResult<>(resultType, calendarContainerResource, continuationData);
}
use of org.datatransferproject.types.common.StringPaginationToken in project data-transfer-project by google.
the class GoogleMailExporter method export.
@Override
public ExportResult<MailContainerResource> export(UUID id, TokensAndUrlAuthData authData, Optional<ExportInformation> exportInformation) {
// Create a new gmail service for the authorized user
Gmail gmail = getOrCreateGmail(authData);
Messages.List request = null;
try {
request = gmail.users().messages().list(USER).setMaxResults(PAGE_SIZE);
} catch (IOException e) {
return new ExportResult<>(e);
}
if (exportInformation.isPresent() && exportInformation.get().getPaginationData() != null) {
request.setPageToken(((StringPaginationToken) exportInformation.get().getPaginationData()).getToken());
}
ListMessagesResponse response = null;
try {
response = request.execute();
} catch (IOException e) {
return new ExportResult<>(e);
}
List<MailMessageModel> results = new ArrayList<>(response.getMessages().size());
// as we can't store all the mail messages in memory at once.
for (Message listMessage : response.getMessages()) {
Message getResponse = null;
try {
getResponse = gmail.users().messages().get(USER, listMessage.getId()).setFormat("raw").execute();
} catch (IOException e) {
return new ExportResult<>(e);
}
// TODO: note this doesn't transfer things like labels
results.add(new MailMessageModel(getResponse.getRaw(), getResponse.getLabelIds()));
}
PaginationData newPage = null;
ResultType resultType = ResultType.END;
if (response.getNextPageToken() != null) {
newPage = new StringPaginationToken(response.getNextPageToken());
resultType = ResultType.CONTINUE;
}
MailContainerResource mailContainerResource = new MailContainerResource(null, results);
return new ExportResult<>(resultType, mailContainerResource, new ContinuationData(newPage));
}
use of org.datatransferproject.types.common.StringPaginationToken in project data-transfer-project by google.
the class GooglePhotosExporter method exportAlbums.
/**
* Note: not all accounts have albums to return. In that case, we just return an empty list of
* albums instead of trying to iterate through a null list.
*/
@VisibleForTesting
ExportResult<PhotosContainerResource> exportAlbums(TokensAndUrlAuthData authData, Optional<PaginationData> paginationData, UUID jobId) throws IOException, InvalidTokenException, PermissionDeniedException {
Optional<String> paginationToken = Optional.empty();
if (paginationData.isPresent()) {
String token = ((StringPaginationToken) paginationData.get()).getToken();
Preconditions.checkArgument(token.startsWith(ALBUM_TOKEN_PREFIX), "Invalid pagination token " + token);
paginationToken = Optional.of(token.substring(ALBUM_TOKEN_PREFIX.length()));
}
AlbumListResponse albumListResponse;
albumListResponse = getOrCreatePhotosInterface(authData).listAlbums(paginationToken);
PaginationData nextPageData;
String token = albumListResponse.getNextPageToken();
List<PhotoAlbum> albums = new ArrayList<>();
GoogleAlbum[] googleAlbums = albumListResponse.getAlbums();
if (Strings.isNullOrEmpty(token)) {
nextPageData = new StringPaginationToken(PHOTO_TOKEN_PREFIX);
} else {
nextPageData = new StringPaginationToken(ALBUM_TOKEN_PREFIX + token);
}
ContinuationData continuationData = new ContinuationData(nextPageData);
if (googleAlbums != null && googleAlbums.length > 0) {
for (GoogleAlbum googleAlbum : googleAlbums) {
// Add album info to list so album can be recreated later
PhotoAlbum photoAlbum = new PhotoAlbum(googleAlbum.getId(), googleAlbum.getTitle(), null);
albums.add(photoAlbum);
monitor.debug(() -> String.format("%s: Google Photos exporting album: %s", jobId, photoAlbum.getId()));
// Add album id to continuation data
continuationData.addContainerResource(new IdOnlyContainerResource(googleAlbum.getId()));
}
}
ResultType resultType = ResultType.CONTINUE;
PhotosContainerResource containerResource = new PhotosContainerResource(albums, null);
return new ExportResult<>(resultType, containerResource, continuationData);
}
Aggregations