Search in sources :

Example 1 with CreateTransferJob

use of org.datatransferproject.types.client.transfer.CreateTransferJob in project data-transfer-project by google.

the class CreateTransferJobAction method handle.

@Override
public TransferJob handle(CreateTransferJob request) {
    String dataType = request.getDataType();
    String exportService = request.getExportService();
    String importService = request.getImportService();
    Optional<ExportInformation> exportInformation = Optional.ofNullable(request.getExportInformation());
    String exportCallbackUrl = request.getExportCallbackUrl();
    String importCallbackUrl = request.getImportCallbackUrl();
    // Create a new job and persist
    UUID jobId = UUID.randomUUID();
    SecretKey sessionKey = symmetricKeyGenerator.generate();
    String encodedSessionKey = BaseEncoding.base64Url().encode(sessionKey.getEncoded());
    String encryptionScheme = request.getEncryptionScheme();
    PortabilityJob job;
    try {
        job = createJob(encodedSessionKey, dataType, exportService, importService, exportInformation, encryptionScheme);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    AuthDataGenerator exportGenerator = registry.getAuthDataGenerator(job.exportService(), job.transferDataType(), EXPORT);
    Preconditions.checkNotNull(exportGenerator, "Generator not found for type: %s, service: %s", job.transferDataType(), job.exportService());
    AuthDataGenerator importGenerator = registry.getAuthDataGenerator(job.importService(), job.transferDataType(), IMPORT);
    Preconditions.checkNotNull(importGenerator, "Generator not found for type: %s, service: %s", job.transferDataType(), job.importService());
    try {
        String encodedJobId = encodeJobId(jobId);
        AuthFlowConfiguration exportConfiguration = exportGenerator.generateConfiguration(exportCallbackUrl, encodedJobId);
        AuthFlowConfiguration importConfiguration = importGenerator.generateConfiguration(importCallbackUrl, encodedJobId);
        job = setInitialAuthDataOnJob(sessionKey, job, exportConfiguration, importConfiguration);
        jobStore.createJob(jobId, job);
        monitor.debug(() -> format("Created new transfer of type '%s' from '%s' to '%s' with jobId: %s", dataType, exportService, importService, jobId), jobId, EventCode.API_JOB_CREATED);
        return new TransferJob(encodedJobId, job.exportService(), job.importService(), job.transferDataType(), exportConfiguration.getAuthUrl(), importConfiguration.getAuthUrl(), exportConfiguration.getTokenUrl(), importConfiguration.getTokenUrl(), exportConfiguration.getAuthProtocol(), importConfiguration.getAuthProtocol());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : AuthFlowConfiguration(org.datatransferproject.spi.api.types.AuthFlowConfiguration) ExportInformation(org.datatransferproject.types.common.ExportInformation) PortabilityJob(org.datatransferproject.spi.cloud.types.PortabilityJob) AuthDataGenerator(org.datatransferproject.spi.api.auth.AuthDataGenerator) SecretKey(javax.crypto.SecretKey) IOException(java.io.IOException) UUID(java.util.UUID) TransferJob(org.datatransferproject.types.client.transfer.TransferJob) CreateTransferJob(org.datatransferproject.types.client.transfer.CreateTransferJob)

Aggregations

IOException (java.io.IOException)1 UUID (java.util.UUID)1 SecretKey (javax.crypto.SecretKey)1 AuthDataGenerator (org.datatransferproject.spi.api.auth.AuthDataGenerator)1 AuthFlowConfiguration (org.datatransferproject.spi.api.types.AuthFlowConfiguration)1 PortabilityJob (org.datatransferproject.spi.cloud.types.PortabilityJob)1 CreateTransferJob (org.datatransferproject.types.client.transfer.CreateTransferJob)1 TransferJob (org.datatransferproject.types.client.transfer.TransferJob)1 ExportInformation (org.datatransferproject.types.common.ExportInformation)1