Search in sources :

Example 6 with DataTransferResponse

use of org.dataportabilityproject.types.client.transfer.DataTransferResponse in project data-transfer-project by google.

the class StartCopyHandler method handle.

@Override
public void handle(HttpExchange exchange) throws IOException {
    Preconditions.checkArgument(ReferenceApiUtils.validateRequest(exchange, HttpMethods.POST, PATH));
    UUID jobId = ReferenceApiUtils.validateJobId(exchange.getRequestHeaders(), tokenManager);
    // Validate auth data is present in cookies
    String exportAuthCookieValue = ReferenceApiUtils.getCookie(exchange.getRequestHeaders(), JsonKeys.EXPORT_AUTH_DATA_COOKIE_KEY);
    Preconditions.checkArgument(!Strings.isNullOrEmpty(exportAuthCookieValue), "Export auth cookie required");
    String importAuthCookieValue = ReferenceApiUtils.getCookie(exchange.getRequestHeaders(), JsonKeys.IMPORT_AUTH_DATA_COOKIE_KEY);
    Preconditions.checkArgument(!Strings.isNullOrEmpty(importAuthCookieValue), "Import auth cookie required");
    // We have the data, now update to 'pending worker assignment' so a worker may be assigned
    StartJobActionRequest request = new StartJobActionRequest(jobId, exportAuthCookieValue, importAuthCookieValue);
    StartJobActionResponse response = startJobAction.handle(request);
    // TODO: Determine if we need more fields populated or a new object
    DataTransferResponse dataTransferResponse = new DataTransferResponse(// job.exportService(),
    "", // job.importService(),
    "", // job.transferDataType(),
    "", Status.INPROCESS, // FrontendConstantUrls.URL_COPY_PAGE);
    "");
    // Mark the response as type Json and send
    exchange.getResponseHeaders().set(HttpHeaders.CONTENT_TYPE, "application/json; charset=" + StandardCharsets.UTF_8.name());
    exchange.sendResponseHeaders(200, 0);
    objectMapper.writeValue(exchange.getResponseBody(), dataTransferResponse);
}
Also used : StartJobActionRequest(org.dataportabilityproject.gateway.action.startjob.StartJobActionRequest) StartJobActionResponse(org.dataportabilityproject.gateway.action.startjob.StartJobActionResponse) DataTransferResponse(org.dataportabilityproject.types.client.transfer.DataTransferResponse) UUID(java.util.UUID)

Example 7 with DataTransferResponse

use of org.dataportabilityproject.types.client.transfer.DataTransferResponse in project data-transfer-project by google.

the class DataTransferHandler method handle.

/**
 * Services the {@link CreateJobAction} via the {@link HttpExchange}.
 */
@Override
public void handle(HttpExchange exchange) throws IOException {
    Preconditions.checkArgument(ReferenceApiUtils.validateRequest(exchange, HttpMethods.POST, PATH), PATH + " only supports POST.");
    logger.debug("received request: {}", exchange.getRequestURI());
    DataTransferRequest request = objectMapper.readValue(exchange.getRequestBody(), DataTransferRequest.class);
    CreateJobActionRequest actionRequest = new CreateJobActionRequest(request.getSource(), request.getDestination(), request.getTransferDataType());
    CreateJobActionResponse actionResponse = createJobAction.handle(actionRequest);
    DataTransferResponse dataTransferResponse;
    if (actionResponse.getErrorMsg() != null) {
        logger.warn("Error during action: {}", actionResponse.getErrorMsg());
        handleError(exchange, request);
        return;
    }
    // Set new cookie
    String encodedJobId = ReferenceApiUtils.encodeJobId(actionResponse.getId());
    HttpCookie cookie = new HttpCookie(JsonKeys.ID_COOKIE_KEY, encodedJobId);
    exchange.getResponseHeaders().add(HttpHeaders.SET_COOKIE, cookie.toString() + ReferenceApiUtils.COOKIE_ATTRIBUTES);
    // Initial auth flow url
    AuthDataGenerator generator = registry.getAuthDataGenerator(request.getSource(), request.getTransferDataType(), AuthMode.EXPORT);
    Preconditions.checkNotNull(generator, "Generator not found for type: %s, service: %s", request.getTransferDataType(), request.getSource());
    AuthFlowConfiguration authFlowConfiguration = generator.generateConfiguration(baseApiUrl, encodedJobId);
    Preconditions.checkNotNull(authFlowConfiguration, "AuthFlowConfiguration not found for type: %s, service: %s", request.getTransferDataType(), request.getSource());
    PortabilityJob job = store.findJob(actionResponse.getId());
    logger.debug("Found job: {} in DTH", job);
    // If present, store initial auth data for export services, e.g. used for oauth1
    if (authFlowConfiguration.getInitialAuthData() != null) {
        // Retrieve and parse the session key from the job
        String sessionKey = job.jobAuthorization().sessionSecretKey();
        SecretKey key = symmetricKeyGenerator.parse(BaseEncoding.base64Url().decode(sessionKey));
        // Ensure intial auth data for export has not already been set
        Preconditions.checkState(Strings.isNullOrEmpty(job.jobAuthorization().encryptedInitialExportAuthData()));
        // Serialize and encrypt the initial auth data
        String serialized = objectMapper.writeValueAsString(authFlowConfiguration.getInitialAuthData());
        String encryptedInitialAuthData = EncrypterFactory.create(key).encrypt(serialized);
        // Add the serialized and encrypted initial auth data to the job authorization
        JobAuthorization updatedJobAuthorization = job.jobAuthorization().toBuilder().setEncryptedInitialExportAuthData(encryptedInitialAuthData).build();
        // Persist the updated PortabilityJob with the updated JobAuthorization
        PortabilityJob updatedPortabilityJob = job.toBuilder().setAndValidateJobAuthorization(updatedJobAuthorization).build();
        store.updateJob(actionResponse.getId(), updatedPortabilityJob);
        logger.debug("Updated job is: {}", updatedPortabilityJob);
        PortabilityJob storejob = store.findJob(actionResponse.getId());
        logger.debug("Job looked up in jobstore is: {} -> {}", actionResponse.getId(), storejob);
    }
    dataTransferResponse = new DataTransferResponse(request.getSource(), request.getDestination(), request.getTransferDataType(), Status.INPROCESS, authFlowConfiguration.getUrl());
    logger.debug("redirecting to: {}", authFlowConfiguration.getUrl());
    // Mark the response as type Json and send
    exchange.getResponseHeaders().set(CONTENT_TYPE, "application/json; charset=" + StandardCharsets.UTF_8.name());
    exchange.sendResponseHeaders(200, 0);
    objectMapper.writeValue(exchange.getResponseBody(), dataTransferResponse);
}
Also used : AuthFlowConfiguration(org.dataportabilityproject.spi.gateway.types.AuthFlowConfiguration) AuthDataGenerator(org.dataportabilityproject.spi.gateway.auth.AuthDataGenerator) PortabilityJob(org.dataportabilityproject.spi.cloud.types.PortabilityJob) JobAuthorization(org.dataportabilityproject.spi.cloud.types.JobAuthorization) SecretKey(javax.crypto.SecretKey) DataTransferRequest(org.dataportabilityproject.types.client.transfer.DataTransferRequest) DataTransferResponse(org.dataportabilityproject.types.client.transfer.DataTransferResponse) HttpCookie(java.net.HttpCookie) CreateJobActionRequest(org.dataportabilityproject.gateway.action.createjob.CreateJobActionRequest) CreateJobActionResponse(org.dataportabilityproject.gateway.action.createjob.CreateJobActionResponse)

Aggregations

DataTransferResponse (org.dataportabilityproject.types.client.transfer.DataTransferResponse)7 PortabilityJob (org.dataportabilityproject.spi.cloud.types.PortabilityJob)4 UUID (java.util.UUID)3 SecretKey (javax.crypto.SecretKey)3 AuthDataGenerator (org.dataportabilityproject.spi.gateway.auth.AuthDataGenerator)3 IOException (java.io.IOException)2 HttpCookie (java.net.HttpCookie)2 JobAuthorization (org.dataportabilityproject.spi.cloud.types.JobAuthorization)2 AuthFlowConfiguration (org.dataportabilityproject.spi.gateway.types.AuthFlowConfiguration)2 Headers (com.sun.net.httpserver.Headers)1 HttpHeaders (org.apache.http.HttpHeaders)1 CreateJobActionRequest (org.dataportabilityproject.gateway.action.createjob.CreateJobActionRequest)1 CreateJobActionResponse (org.dataportabilityproject.gateway.action.createjob.CreateJobActionResponse)1 StartJobActionRequest (org.dataportabilityproject.gateway.action.startjob.StartJobActionRequest)1 StartJobActionResponse (org.dataportabilityproject.gateway.action.startjob.StartJobActionResponse)1 AuthMode (org.dataportabilityproject.spi.gateway.auth.AuthServiceProviderRegistry.AuthMode)1 DataTransferRequest (org.dataportabilityproject.types.client.transfer.DataTransferRequest)1 SimpleLoginRequest (org.dataportabilityproject.types.client.transfer.SimpleLoginRequest)1 AuthData (org.dataportabilityproject.types.transfer.auth.AuthData)1