Search in sources :

Example 1 with TokensAndUrlAuthData

use of org.datatransferproject.types.transfer.auth.TokensAndUrlAuthData in project data-transfer-project by google.

the class GoogleBloggerImporter method insertActivity.

private void insertActivity(IdempotentImportExecutor idempotentExecutor, SocialActivityActor actor, SocialActivityModel activity, String blogId, TokensAndUrlAuthData authData) throws Exception {
    String content = activity.getContent() == null ? "" : activity.getContent();
    Collection<SocialActivityAttachment> linkAttachments = activity.getAttachments().stream().filter(attachment -> attachment.getType() == SocialActivityAttachmentType.LINK).collect(Collectors.toList());
    Collection<SocialActivityAttachment> imageAttachments = activity.getAttachments().stream().filter(attachment -> attachment.getType() == SocialActivityAttachmentType.IMAGE).collect(Collectors.toList());
    // don't know how they were laid out in the originating service.
    for (SocialActivityAttachment attachment : linkAttachments) {
        content = "<a href=\"" + attachment.getUrl() + "\">" + attachment.getName() + "</a>\n</hr>\n" + content;
    }
    if (!imageAttachments.isEmpty()) {
        // Store any attached images in Drive in a new folder.
        Drive driveInterface = getOrCreateDriveService(authData);
        String folderId = idempotentExecutor.executeOrThrowException("MainAlbum", "Photo Album", () -> createAlbumFolder(driveInterface));
        for (SocialActivityAttachment image : imageAttachments) {
            try {
                String newImgSrc = idempotentExecutor.executeAndSwallowIOExceptions(image.toString(), "Image", () -> uploadImage(image, driveInterface, folderId));
                content += "\n<hr/><img src=\"" + newImgSrc + "\">";
            } catch (RuntimeException e) {
                throw new IOException("Couldn't import: " + imageAttachments, e);
            }
        }
    }
    String title = "";
    if (activity.getTitle() != null && !Strings.isNullOrEmpty(activity.getTitle())) {
        title = activity.getTitle();
    }
    Post post = new Post().setTitle("Imported post: " + title).setContent(content);
    if (actor != null) {
        Post.Author author = new Post.Author();
        if (!Strings.isNullOrEmpty(actor.getName())) {
            author.setDisplayName(actor.getName());
        }
        if (!Strings.isNullOrEmpty(actor.getUrl())) {
            author.setUrl(actor.getUrl());
        }
        post.setAuthor(author);
    }
    if (activity.getPublished() != null) {
        post.setPublished(new DateTime(activity.getPublished().toEpochMilli()));
    }
    idempotentExecutor.executeAndSwallowIOExceptions(title, title, () -> getOrCreateBloggerService(authData).posts().insert(blogId, post).setIsDraft(true).execute().getId());
}
Also used : HttpURLConnection(java.net.HttpURLConnection) Blogger(com.google.api.services.blogger.Blogger) ImportResult(org.datatransferproject.spi.transfer.provider.ImportResult) SocialActivityAttachment(org.datatransferproject.types.common.models.social.SocialActivityAttachment) Drive(com.google.api.services.drive.Drive) SocialActivityContainerResource(org.datatransferproject.types.common.models.social.SocialActivityContainerResource) Permission(com.google.api.services.drive.model.Permission) GoogleStaticObjects(org.datatransferproject.datatransfer.google.common.GoogleStaticObjects) Strings(com.google.common.base.Strings) DateTime(com.google.api.client.util.DateTime) ImmutableList(com.google.common.collect.ImmutableList) Post(com.google.api.services.blogger.model.Post) ResultType(org.datatransferproject.spi.transfer.provider.ImportResult.ResultType) BlogList(com.google.api.services.blogger.model.BlogList) File(com.google.api.services.drive.model.File) SocialActivityActor(org.datatransferproject.types.common.models.social.SocialActivityActor) Credential(com.google.api.client.auth.oauth2.Credential) Importer(org.datatransferproject.spi.transfer.provider.Importer) GoogleCredentialFactory(org.datatransferproject.datatransfer.google.common.GoogleCredentialFactory) ImageStreamProvider(org.datatransferproject.transfer.ImageStreamProvider) IdempotentImportExecutor(org.datatransferproject.spi.transfer.idempotentexecutor.IdempotentImportExecutor) Collection(java.util.Collection) SocialActivityType(org.datatransferproject.types.common.models.social.SocialActivityType) IOException(java.io.IOException) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) Stream(java.util.stream.Stream) TokensAndUrlAuthData(org.datatransferproject.types.transfer.auth.TokensAndUrlAuthData) InputStreamContent(com.google.api.client.http.InputStreamContent) LocalDate(java.time.LocalDate) SocialActivityAttachmentType(org.datatransferproject.types.common.models.social.SocialActivityAttachmentType) SocialActivityModel(org.datatransferproject.types.common.models.social.SocialActivityModel) InputStream(java.io.InputStream) Post(com.google.api.services.blogger.model.Post) Drive(com.google.api.services.drive.Drive) SocialActivityAttachment(org.datatransferproject.types.common.models.social.SocialActivityAttachment) IOException(java.io.IOException) DateTime(com.google.api.client.util.DateTime)

Example 2 with TokensAndUrlAuthData

use of org.datatransferproject.types.transfer.auth.TokensAndUrlAuthData in project data-transfer-project by google.

the class FacebookPhotosExporterTest method testExportAlbum.

@Test
public void testExportAlbum() throws CopyExceptionWithFailureReason {
    ExportResult<PhotosContainerResource> result = facebookPhotosExporter.export(uuid, new TokensAndUrlAuthData("accessToken", null, null), Optional.empty());
    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));
}
Also used : PhotosContainerResource(org.datatransferproject.types.common.models.photos.PhotosContainerResource) TokensAndUrlAuthData(org.datatransferproject.types.transfer.auth.TokensAndUrlAuthData) IdOnlyContainerResource(org.datatransferproject.types.common.models.IdOnlyContainerResource) PhotoAlbum(org.datatransferproject.types.common.models.photos.PhotoAlbum) Test(org.junit.Test)

Example 3 with TokensAndUrlAuthData

use of org.datatransferproject.types.transfer.auth.TokensAndUrlAuthData 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));
}
Also used : PhotosContainerResource(org.datatransferproject.types.common.models.photos.PhotosContainerResource) ExportInformation(org.datatransferproject.types.common.ExportInformation) TokensAndUrlAuthData(org.datatransferproject.types.transfer.auth.TokensAndUrlAuthData) ArrayList(java.util.ArrayList) IdOnlyContainerResource(org.datatransferproject.types.common.models.IdOnlyContainerResource) PhotoAlbum(org.datatransferproject.types.common.models.photos.PhotoAlbum) StringPaginationToken(org.datatransferproject.types.common.StringPaginationToken) Test(org.junit.Test)

Example 4 with TokensAndUrlAuthData

use of org.datatransferproject.types.transfer.auth.TokensAndUrlAuthData in project data-transfer-project by google.

the class KoofrVideosExporterTest method setUp.

@Before
public void setUp() throws Exception {
    client = mock(KoofrClient.class);
    clientFactory = mock(KoofrClientFactory.class);
    when(clientFactory.create(any())).thenReturn(client);
    monitor = mock(Monitor.class);
    exporter = new KoofrVideosExporter(clientFactory, monitor);
    authData = new TokensAndUrlAuthData("acc", "refresh", "");
}
Also used : KoofrClientFactory(org.datatransferproject.transfer.koofr.common.KoofrClientFactory) Monitor(org.datatransferproject.api.launcher.Monitor) KoofrClient(org.datatransferproject.transfer.koofr.common.KoofrClient) TokensAndUrlAuthData(org.datatransferproject.types.transfer.auth.TokensAndUrlAuthData) Before(org.junit.Before)

Example 5 with TokensAndUrlAuthData

use of org.datatransferproject.types.transfer.auth.TokensAndUrlAuthData in project data-transfer-project by google.

the class KoofrPhotosImporterTest method setUp.

@Before
public void setUp() throws Exception {
    server = new MockWebServer();
    server.start();
    client = mock(KoofrClient.class);
    clientFactory = mock(KoofrClientFactory.class);
    when(clientFactory.create(any())).thenReturn(client);
    monitor = mock(Monitor.class);
    jobStore = mock(JobStore.class);
    importer = new KoofrPhotosImporter(clientFactory, monitor, jobStore);
    executor = mock(IdempotentImportExecutor.class);
    when(executor.executeAndSwallowIOExceptions(any(), any(), any())).then((InvocationOnMock invocation) -> {
        Callable<String> callable = invocation.getArgument(2);
        return callable.call();
    });
    authData = new TokensAndUrlAuthData("acc", "refresh", "");
}
Also used : KoofrClientFactory(org.datatransferproject.transfer.koofr.common.KoofrClientFactory) Monitor(org.datatransferproject.api.launcher.Monitor) InvocationOnMock(org.mockito.invocation.InvocationOnMock) MockWebServer(okhttp3.mockwebserver.MockWebServer) KoofrClient(org.datatransferproject.transfer.koofr.common.KoofrClient) TokensAndUrlAuthData(org.datatransferproject.types.transfer.auth.TokensAndUrlAuthData) JobStore(org.datatransferproject.spi.cloud.storage.JobStore) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) IdempotentImportExecutor(org.datatransferproject.spi.transfer.idempotentexecutor.IdempotentImportExecutor) Before(org.junit.Before)

Aggregations

TokensAndUrlAuthData (org.datatransferproject.types.transfer.auth.TokensAndUrlAuthData)14 Monitor (org.datatransferproject.api.launcher.Monitor)7 Before (org.junit.Before)6 IdempotentImportExecutor (org.datatransferproject.spi.transfer.idempotentexecutor.IdempotentImportExecutor)5 IOException (java.io.IOException)4 UUID (java.util.UUID)4 ExportInformation (org.datatransferproject.types.common.ExportInformation)4 Test (org.junit.Test)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 Collection (java.util.Collection)3 Collectors (java.util.stream.Collectors)3 ImportResult (org.datatransferproject.spi.transfer.provider.ImportResult)3 Importer (org.datatransferproject.spi.transfer.provider.Importer)3 KoofrClient (org.datatransferproject.transfer.koofr.common.KoofrClient)3 KoofrClientFactory (org.datatransferproject.transfer.koofr.common.KoofrClientFactory)3 Credential (com.google.api.client.auth.oauth2.Credential)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 Preconditions (com.google.common.base.Preconditions)2 Strings (com.google.common.base.Strings)2 HashMap (java.util.HashMap)2