Search in sources :

Example 1 with ContactsModelWrapper

use of org.dataportabilityproject.types.transfer.models.contacts.ContactsModelWrapper in project data-transfer-project by google.

the class GoogleContactsExporter method exportContacts.

private ExportResult<ContactsModelWrapper> exportContacts(TokensAndUrlAuthData authData, Optional<PaginationData> pageData) {
    try {
        // Set up connection
        Connections.List connectionsListRequest = getOrCreatePeopleService(authData).people().connections().list(SELF_RESOURCE);
        // Get next page, if we have a page token
        if (pageData.isPresent()) {
            StringPaginationToken paginationToken = (StringPaginationToken) pageData.get();
            connectionsListRequest.setPageToken(paginationToken.getToken());
        }
        // Get list of connections (nb: not a list containing full info of each Person)
        ListConnectionsResponse response = connectionsListRequest.setPersonFields(PERSON_FIELDS).execute();
        List<Person> peopleList = response.getConnections();
        // Get list of resource names, then get list of Persons
        List<String> resourceNames = peopleList.stream().map(Person::getResourceName).collect(Collectors.toList());
        GetPeopleResponse batchResponse = getOrCreatePeopleService(authData).people().getBatchGet().setResourceNames(resourceNames).setPersonFields(PERSON_FIELDS).execute();
        List<PersonResponse> personResponseList = batchResponse.getResponses();
        // Convert Persons to VCards
        List<VCard> vCards = personResponseList.stream().map(a -> convert(a.getPerson())).collect(Collectors.toList());
        // Determine if there's a next page
        StringPaginationToken nextPageData = null;
        if (response.getNextPageToken() != null) {
            nextPageData = new StringPaginationToken(response.getNextPageToken());
        }
        ContinuationData continuationData = new ContinuationData(nextPageData);
        ContactsModelWrapper wrapper = new ContactsModelWrapper(makeVCardString(vCards));
        // Get result type
        ResultType resultType = ResultType.CONTINUE;
        if (nextPageData == null) {
            resultType = ResultType.END;
        }
        return new ExportResult<ContactsModelWrapper>(resultType, wrapper, continuationData);
    } catch (IOException e) {
        return new ExportResult<ContactsModelWrapper>(ResultType.ERROR, e.getMessage());
    }
}
Also used : Connections(com.google.api.services.people.v1.PeopleService.People.Connections) EmailAddress(com.google.api.services.people.v1.model.EmailAddress) Connections(com.google.api.services.people.v1.PeopleService.People.Connections) ExportResult(org.dataportabilityproject.spi.transfer.provider.ExportResult) LoggerFactory(org.slf4j.LoggerFactory) PhoneNumber(com.google.api.services.people.v1.model.PhoneNumber) StructuredName(ezvcard.property.StructuredName) TokensAndUrlAuthData(org.dataportabilityproject.types.transfer.auth.TokensAndUrlAuthData) Person(com.google.api.services.people.v1.model.Person) ContinuationData(org.dataportabilityproject.spi.transfer.types.ContinuationData) Credential(com.google.api.client.auth.oauth2.Credential) VCARD_PRIMARY_PREF(org.dataportabilityproject.datatransfer.google.common.GoogleStaticObjects.VCARD_PRIMARY_PREF) LinkedList(java.util.LinkedList) GetPeopleResponse(com.google.api.services.people.v1.model.GetPeopleResponse) PaginationData(org.dataportabilityproject.spi.transfer.types.PaginationData) SELF_RESOURCE(org.dataportabilityproject.datatransfer.google.common.GoogleStaticObjects.SELF_RESOURCE) ListConnectionsResponse(com.google.api.services.people.v1.model.ListConnectionsResponse) VCard(ezvcard.VCard) PeopleService(com.google.api.services.people.v1.PeopleService) ExportInformation(org.dataportabilityproject.spi.transfer.types.ExportInformation) Exporter(org.dataportabilityproject.spi.transfer.provider.Exporter) StringPaginationToken(org.dataportabilityproject.spi.transfer.types.StringPaginationToken) Logger(org.slf4j.Logger) BearerToken(com.google.api.client.auth.oauth2.BearerToken) StringWriter(java.io.StringWriter) Telephone(ezvcard.property.Telephone) ResultType(org.dataportabilityproject.spi.transfer.provider.ExportResult.ResultType) IOException(java.io.IOException) UUID(java.util.UUID) JCardWriter(ezvcard.io.json.JCardWriter) Collectors(java.util.stream.Collectors) SOURCE_PARAM_NAME_TYPE(org.dataportabilityproject.datatransfer.google.common.GoogleStaticObjects.SOURCE_PARAM_NAME_TYPE) List(java.util.List) PERSON_FIELDS(org.dataportabilityproject.datatransfer.google.common.GoogleStaticObjects.PERSON_FIELDS) FieldMetadata(com.google.api.services.people.v1.model.FieldMetadata) PersonResponse(com.google.api.services.people.v1.model.PersonResponse) Email(ezvcard.property.Email) ContactsModelWrapper(org.dataportabilityproject.types.transfer.models.contacts.ContactsModelWrapper) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Name(com.google.api.services.people.v1.model.Name) GoogleStaticObjects(org.dataportabilityproject.datatransfer.google.common.GoogleStaticObjects) ListConnectionsResponse(com.google.api.services.people.v1.model.ListConnectionsResponse) ContinuationData(org.dataportabilityproject.spi.transfer.types.ContinuationData) ContactsModelWrapper(org.dataportabilityproject.types.transfer.models.contacts.ContactsModelWrapper) ResultType(org.dataportabilityproject.spi.transfer.provider.ExportResult.ResultType) IOException(java.io.IOException) GetPeopleResponse(com.google.api.services.people.v1.model.GetPeopleResponse) Person(com.google.api.services.people.v1.model.Person) VCard(ezvcard.VCard) StringPaginationToken(org.dataportabilityproject.spi.transfer.types.StringPaginationToken) PersonResponse(com.google.api.services.people.v1.model.PersonResponse) ExportResult(org.dataportabilityproject.spi.transfer.provider.ExportResult)

Example 2 with ContactsModelWrapper

use of org.dataportabilityproject.types.transfer.models.contacts.ContactsModelWrapper in project data-transfer-project by google.

the class GoogleContactsExporterTest method exportFirstPage.

@Test
public void exportFirstPage() throws IOException {
    setUpSinglePersonResponse();
    // Looking at first page, with at least one page after it
    listConnectionsResponse.setNextPageToken(NEXT_PAGE_TOKEN);
    ExportResult<ContactsModelWrapper> result = contactsService.export(UUID.randomUUID(), null);
    // Check that correct methods were called
    verify(connections).list(SELF_RESOURCE);
    InOrder inOrder = Mockito.inOrder(getBatchGet);
    inOrder.verify(getBatchGet).setResourceNames(Collections.singletonList(RESOURCE_NAME));
    inOrder.verify(getBatchGet).setPersonFields(PERSON_FIELDS);
    inOrder.verify(getBatchGet).execute();
    // Check continuation data
    ContinuationData continuationData = (ContinuationData) result.getContinuationData();
    assertThat(continuationData.getContainerResources()).isEmpty();
    StringPaginationToken paginationToken = (StringPaginationToken) ((ContinuationData) result.getContinuationData()).getPaginationData();
    assertThat(paginationToken.getToken()).isEqualTo(NEXT_PAGE_TOKEN);
    // Check that the right number of VCards was returned
    JCardReader reader = new JCardReader(result.getExportedData().getVCards());
    List<VCard> vCardList = reader.readAll();
    assertThat(vCardList.size()).isEqualTo(connectionsList.size());
}
Also used : InOrder(org.mockito.InOrder) ContinuationData(org.dataportabilityproject.spi.transfer.types.ContinuationData) ContactsModelWrapper(org.dataportabilityproject.types.transfer.models.contacts.ContactsModelWrapper) JCardReader(ezvcard.io.json.JCardReader) VCard(ezvcard.VCard) StringPaginationToken(org.dataportabilityproject.spi.transfer.types.StringPaginationToken) Test(org.junit.Test)

Example 3 with ContactsModelWrapper

use of org.dataportabilityproject.types.transfer.models.contacts.ContactsModelWrapper in project data-transfer-project by google.

the class GoogleContactsImporterTest method importFirstResources.

@Test
public void importFirstResources() throws IOException {
    // Set up: small number of VCards to be imported
    int numberOfVCards = 5;
    List<VCard> vCardList = new LinkedList<>();
    for (int i = 0; i < numberOfVCards; i++) {
        StructuredName structuredName = new StructuredName();
        structuredName.setFamily("Family" + i);
        structuredName.setParameter(SOURCE_PARAM_NAME_TYPE, CONTACT_SOURCE_TYPE);
        VCard vCard = new VCard();
        vCard.setStructuredName(structuredName);
        vCardList.add(vCard);
    }
    String vCardString = GoogleContactsExporter.makeVCardString(vCardList);
    ContactsModelWrapper wrapper = new ContactsModelWrapper(vCardString);
    // Run test
    contactsService.importItem(UUID.randomUUID(), null, wrapper);
    // Check that the right methods were called
    verify(people, times(numberOfVCards)).createContact(any(Person.class));
    verify(createContact, times(numberOfVCards)).execute();
}
Also used : ContactsModelWrapper(org.dataportabilityproject.types.transfer.models.contacts.ContactsModelWrapper) VCard(ezvcard.VCard) StructuredName(ezvcard.property.StructuredName) Person(com.google.api.services.people.v1.model.Person) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 4 with ContactsModelWrapper

use of org.dataportabilityproject.types.transfer.models.contacts.ContactsModelWrapper in project data-transfer-project by google.

the class MicrosoftContactsExporter method doExport.

@SuppressWarnings("unchecked")
private ExportResult<ContactsModelWrapper> doExport(TokenAuthData authData, String url) {
    Request.Builder graphReqBuilder = new Request.Builder().url(url);
    graphReqBuilder.header("Authorization", "Bearer " + authData.getToken());
    try (Response graphResponse = client.newCall(graphReqBuilder.build()).execute()) {
        ResponseBody body = graphResponse.body();
        if (body == null) {
            return new ExportResult<>(ExportResult.ResultType.ERROR, "Error retrieving contacts: response body was null");
        }
        String graphBody = new String(body.bytes());
        Map graphMap = objectMapper.reader().forType(Map.class).readValue(graphBody);
        String nextLink = (String) graphMap.get(ODATA_NEXT);
        ContinuationData continuationData = nextLink == null ? null : new ContinuationData(new GraphPagination(nextLink));
        List<Map<String, Object>> rawContacts = (List<Map<String, Object>>) graphMap.get("value");
        if (rawContacts == null) {
            return new ExportResult<>(ExportResult.ResultType.END);
        }
        ContactsModelWrapper wrapper = transform(rawContacts);
        return new ExportResult<>(ExportResult.ResultType.CONTINUE, wrapper, continuationData);
    } catch (IOException e) {
        // FIXME log error
        e.printStackTrace();
        return new ExportResult<>(ExportResult.ResultType.ERROR, "Error retrieving contacts: " + e.getMessage());
    }
}
Also used : GraphPagination(org.dataportabilityproject.transfer.microsoft.types.GraphPagination) Request(okhttp3.Request) ContinuationData(org.dataportabilityproject.spi.transfer.types.ContinuationData) ContactsModelWrapper(org.dataportabilityproject.types.transfer.models.contacts.ContactsModelWrapper) IOException(java.io.IOException) ResponseBody(okhttp3.ResponseBody) Response(okhttp3.Response) List(java.util.List) Map(java.util.Map) ExportResult(org.dataportabilityproject.spi.transfer.provider.ExportResult)

Example 5 with ContactsModelWrapper

use of org.dataportabilityproject.types.transfer.models.contacts.ContactsModelWrapper in project data-transfer-project by google.

the class LocalExportTestRunner method main.

@SuppressWarnings("unchecked")
public static void main(String... args) throws Exception {
    AuthTestDriver authTestDriver = new AuthTestDriver();
    MicrosoftTransferExtension serviceProvider = new MicrosoftTransferExtension();
    TokenAuthData token = authTestDriver.getOAuthTokenCode();
    Exporter<TokenAuthData, ContactsModelWrapper> contacts = (Exporter<TokenAuthData, ContactsModelWrapper>) serviceProvider.getExporter("contacts");
    ExportResult<ContactsModelWrapper> wrapper = contacts.export(UUID.randomUUID(), token);
}
Also used : TokenAuthData(org.dataportabilityproject.types.transfer.auth.TokenAuthData) MicrosoftTransferExtension(org.dataportabilityproject.transfer.microsoft.MicrosoftTransferExtension) ContactsModelWrapper(org.dataportabilityproject.types.transfer.models.contacts.ContactsModelWrapper) AuthTestDriver(org.dataportabilityproject.auth.microsoft.harness.AuthTestDriver) Exporter(org.dataportabilityproject.spi.transfer.provider.Exporter)

Aggregations

ContactsModelWrapper (org.dataportabilityproject.types.transfer.models.contacts.ContactsModelWrapper)9 VCard (ezvcard.VCard)5 IOException (java.io.IOException)4 ContinuationData (org.dataportabilityproject.spi.transfer.types.ContinuationData)4 List (java.util.List)3 StringPaginationToken (org.dataportabilityproject.spi.transfer.types.StringPaginationToken)3 Test (org.junit.Test)3 Person (com.google.api.services.people.v1.model.Person)2 JCardReader (ezvcard.io.json.JCardReader)2 JCardWriter (ezvcard.io.json.JCardWriter)2 StructuredName (ezvcard.property.StructuredName)2 StringWriter (java.io.StringWriter)2 LinkedList (java.util.LinkedList)2 Map (java.util.Map)2 UUID (java.util.UUID)2 AuthTestDriver (org.dataportabilityproject.auth.microsoft.harness.AuthTestDriver)2 ExportResult (org.dataportabilityproject.spi.transfer.provider.ExportResult)2 Exporter (org.dataportabilityproject.spi.transfer.provider.Exporter)2 ImportResult (org.dataportabilityproject.spi.transfer.provider.ImportResult)2 Importer (org.dataportabilityproject.spi.transfer.provider.Importer)2