Search in sources :

Example 1 with Response

use of org.opendatakit.briefcase.reused.http.response.Response in project briefcase by opendatakit.

the class CommonsHttpTest method can_handle_3xx_errors.

@Test
public void can_handle_3xx_errors() throws Exception {
    server.request(and(by(uri("/")), by(method(GET)))).response(status(302));
    running(server, () -> {
        Response response = http.execute(RequestBuilder.get(BASE_URL).build());
        assertThat(response.isSuccess(), is(false));
    });
}
Also used : Response(org.opendatakit.briefcase.reused.http.response.Response) Test(org.junit.Test)

Example 2 with Response

use of org.opendatakit.briefcase.reused.http.response.Response in project briefcase by opendatakit.

the class PullFormFromAggregate method pullFormFromAggregate.

public static void pullFormFromAggregate(Path storageDir, Optional<String> formId, String username, String password, URL server, boolean resumeLastPull, Optional<LocalDate> startFromDate, boolean includeIncomplete, Optional<Integer> maybeMaxHttpConnections) {
    CliEventsCompanion.attach(log);
    Path briefcaseDir = Common.getOrCreateBriefcaseDir(storageDir);
    FormCache formCache = FormCache.from(briefcaseDir);
    formCache.update();
    BriefcasePreferences appPreferences = BriefcasePreferences.appScoped();
    FormMetadataPort formMetadataPort = FileSystemFormMetadataAdapter.at(briefcaseDir);
    int maxHttpConnections = Optionals.race(maybeMaxHttpConnections, appPreferences.getMaxHttpConnections()).orElse(DEFAULT_HTTP_CONNECTIONS);
    Http http = appPreferences.getHttpProxy().map(host -> CommonsHttp.of(maxHttpConnections, host)).orElseGet(() -> CommonsHttp.of(maxHttpConnections));
    AggregateServer aggregateServer = AggregateServer.authenticated(server, new Credentials(username, password));
    Response<List<RemoteFormDefinition>> response = http.execute(aggregateServer.getFormListRequest());
    if (!response.isSuccess()) {
        System.err.println(response.isRedirection() ? "Error connecting to Aggregate: Redirection detected" : response.isUnauthorized() ? "Error connecting to Aggregate: Wrong credentials" : response.isNotFound() ? "Error connecting to Aggregate: Aggregate not found" : "Error connecting to Aggregate");
        return;
    }
    List<FormStatus> filteredForms = response.orElseThrow(BriefcaseException::new).stream().filter(f -> formId.map(id -> f.getFormId().equals(id)).orElse(true)).map(FormStatus::new).collect(toList());
    if (formId.isPresent() && filteredForms.isEmpty())
        throw new BriefcaseException("Form " + formId.get() + " not found");
    TransferForms forms = TransferForms.empty();
    forms.load(filteredForms);
    forms.selectAll();
    PullFromAggregate pullOp = new PullFromAggregate(http, aggregateServer, briefcaseDir, includeIncomplete, PullFormFromAggregate::onEvent, formMetadataPort);
    JobsRunner.launchAsync(forms.map(form -> pullOp.pull(form, resolveCursor(resumeLastPull, startFromDate, form, formMetadataPort))), PullFormFromAggregate::onError).waitForCompletion();
    System.out.println();
    System.out.println("All operations completed");
    System.out.println();
}
Also used : Path(java.nio.file.Path) Arrays(java.util.Arrays) FileSystemFormMetadataAdapter(org.opendatakit.briefcase.model.form.FileSystemFormMetadataAdapter) Credentials(org.opendatakit.briefcase.reused.http.Credentials) URL(java.net.URL) BriefcasePreferences(org.opendatakit.briefcase.model.BriefcasePreferences) Optionals(org.opendatakit.briefcase.reused.Optionals) Response(org.opendatakit.briefcase.reused.http.response.Response) LoggerFactory(org.slf4j.LoggerFactory) DEFAULT_HTTP_CONNECTIONS(org.opendatakit.briefcase.reused.http.Http.DEFAULT_HTTP_CONNECTIONS) MAX_HTTP_CONNECTIONS(org.opendatakit.briefcase.operations.Common.MAX_HTTP_CONNECTIONS) FORM_ID(org.opendatakit.briefcase.operations.Common.FORM_ID) Operation(org.opendatakit.common.cli.Operation) FormMetadataPort(org.opendatakit.briefcase.model.form.FormMetadataPort) BriefcaseException(org.opendatakit.briefcase.reused.BriefcaseException) AggregateServer(org.opendatakit.briefcase.reused.transfer.AggregateServer) TransferForms(org.opendatakit.briefcase.transfer.TransferForms) SERVER_URL(org.opendatakit.briefcase.operations.Common.SERVER_URL) CREDENTIALS_PASSWORD(org.opendatakit.briefcase.operations.Common.CREDENTIALS_PASSWORD) FormCache(org.opendatakit.briefcase.util.FormCache) Path(java.nio.file.Path) FormKey(org.opendatakit.briefcase.model.form.FormKey) CommonsHttp(org.opendatakit.briefcase.reused.http.CommonsHttp) RemoteFormDefinition(org.opendatakit.briefcase.model.RemoteFormDefinition) FormStatus(org.opendatakit.briefcase.model.FormStatus) FormStatusEvent(org.opendatakit.briefcase.model.FormStatusEvent) Logger(org.slf4j.Logger) FormMetadataQueries.lastCursorOf(org.opendatakit.briefcase.model.form.FormMetadataQueries.lastCursorOf) Param(org.opendatakit.common.cli.Param) Cursor(org.opendatakit.briefcase.pull.aggregate.Cursor) STORAGE_DIR(org.opendatakit.briefcase.operations.Common.STORAGE_DIR) JobsRunner(org.opendatakit.briefcase.reused.job.JobsRunner) Http(org.opendatakit.briefcase.reused.http.Http) CREDENTIALS_USERNAME(org.opendatakit.briefcase.operations.Common.CREDENTIALS_USERNAME) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) PullFromAggregate(org.opendatakit.briefcase.pull.aggregate.PullFromAggregate) LocalDate(java.time.LocalDate) Optional(java.util.Optional) FormCache(org.opendatakit.briefcase.util.FormCache) BriefcasePreferences(org.opendatakit.briefcase.model.BriefcasePreferences) FormMetadataPort(org.opendatakit.briefcase.model.form.FormMetadataPort) CommonsHttp(org.opendatakit.briefcase.reused.http.CommonsHttp) Http(org.opendatakit.briefcase.reused.http.Http) BriefcaseException(org.opendatakit.briefcase.reused.BriefcaseException) PullFromAggregate(org.opendatakit.briefcase.pull.aggregate.PullFromAggregate) TransferForms(org.opendatakit.briefcase.transfer.TransferForms) FormStatus(org.opendatakit.briefcase.model.FormStatus) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) AggregateServer(org.opendatakit.briefcase.reused.transfer.AggregateServer) Credentials(org.opendatakit.briefcase.reused.http.Credentials)

Example 3 with Response

use of org.opendatakit.briefcase.reused.http.response.Response in project briefcase by opendatakit.

the class PullFormFromCentral method pullFromCentral.

private static void pullFromCentral(Args args) {
    CliEventsCompanion.attach(log);
    Path briefcaseDir = Common.getOrCreateBriefcaseDir(args.get(STORAGE_DIR));
    FormCache formCache = FormCache.from(briefcaseDir);
    formCache.update();
    BriefcasePreferences appPreferences = BriefcasePreferences.appScoped();
    FormMetadataPort formMetadataPort = FileSystemFormMetadataAdapter.at(briefcaseDir);
    int maxHttpConnections = Optionals.race(args.getOptional(MAX_HTTP_CONNECTIONS), appPreferences.getMaxHttpConnections()).orElse(DEFAULT_HTTP_CONNECTIONS);
    Http http = appPreferences.getHttpProxy().map(host -> CommonsHttp.of(maxHttpConnections, host)).orElseGet(() -> CommonsHttp.of(maxHttpConnections));
    CentralServer server = CentralServer.of(args.get(SERVER_URL), args.get(PROJECT_ID), new Credentials(args.get(CREDENTIALS_EMAIL), args.get(CREDENTIALS_PASSWORD)));
    String token = http.execute(server.getSessionTokenRequest()).orElseThrow(() -> new BriefcaseException("Can't authenticate with ODK Central"));
    Response<List<RemoteFormDefinition>> response = http.execute(server.getFormsListRequest(token));
    if (!response.isSuccess()) {
        System.err.println(response.isRedirection() ? "Error connecting to Central: Redirection detected" : response.isUnauthorized() ? "Error connecting to Central: Wrong credentials" : response.isNotFound() ? "Error connecting to Central: Central not found" : "Error connecting to Central");
        return;
    }
    Optional<String> formId = args.getOptional(FORM_ID);
    List<FormStatus> filteredForms = response.orElseThrow(BriefcaseException::new).stream().filter(f -> formId.map(id -> f.getFormId().equals(id)).orElse(true)).map(FormStatus::new).collect(toList());
    if (formId.isPresent() && filteredForms.isEmpty())
        throw new BriefcaseException("Form " + formId.get() + " not found");
    TransferForms forms = TransferForms.empty();
    forms.load(filteredForms);
    forms.selectAll();
    PullFromCentral pullOp = new PullFromCentral(http, server, briefcaseDir, token, PullFormFromCentral::onEvent, formMetadataPort);
    JobsRunner.launchAsync(forms.map(pullOp::pull), PullFormFromCentral::onError).waitForCompletion();
    System.out.println();
    System.out.println("All operations completed");
    System.out.println();
}
Also used : Path(java.nio.file.Path) Arrays(java.util.Arrays) FileSystemFormMetadataAdapter(org.opendatakit.briefcase.model.form.FileSystemFormMetadataAdapter) Credentials(org.opendatakit.briefcase.reused.http.Credentials) BriefcasePreferences(org.opendatakit.briefcase.model.BriefcasePreferences) Optionals(org.opendatakit.briefcase.reused.Optionals) Response(org.opendatakit.briefcase.reused.http.response.Response) LoggerFactory(org.slf4j.LoggerFactory) DEFAULT_HTTP_CONNECTIONS(org.opendatakit.briefcase.reused.http.Http.DEFAULT_HTTP_CONNECTIONS) MAX_HTTP_CONNECTIONS(org.opendatakit.briefcase.operations.Common.MAX_HTTP_CONNECTIONS) FORM_ID(org.opendatakit.briefcase.operations.Common.FORM_ID) Operation(org.opendatakit.common.cli.Operation) FormMetadataPort(org.opendatakit.briefcase.model.form.FormMetadataPort) BriefcaseException(org.opendatakit.briefcase.reused.BriefcaseException) TransferForms(org.opendatakit.briefcase.transfer.TransferForms) SERVER_URL(org.opendatakit.briefcase.operations.Common.SERVER_URL) CREDENTIALS_PASSWORD(org.opendatakit.briefcase.operations.Common.CREDENTIALS_PASSWORD) FormCache(org.opendatakit.briefcase.util.FormCache) Path(java.nio.file.Path) CommonsHttp(org.opendatakit.briefcase.reused.http.CommonsHttp) RemoteFormDefinition(org.opendatakit.briefcase.model.RemoteFormDefinition) FormStatus(org.opendatakit.briefcase.model.FormStatus) FormStatusEvent(org.opendatakit.briefcase.model.FormStatusEvent) CentralServer(org.opendatakit.briefcase.reused.transfer.CentralServer) Logger(org.slf4j.Logger) Param(org.opendatakit.common.cli.Param) STORAGE_DIR(org.opendatakit.briefcase.operations.Common.STORAGE_DIR) JobsRunner(org.opendatakit.briefcase.reused.job.JobsRunner) Http(org.opendatakit.briefcase.reused.http.Http) PullFromCentral(org.opendatakit.briefcase.pull.central.PullFromCentral) Collectors.toList(java.util.stream.Collectors.toList) PROJECT_ID(org.opendatakit.briefcase.operations.Common.PROJECT_ID) List(java.util.List) CREDENTIALS_EMAIL(org.opendatakit.briefcase.operations.Common.CREDENTIALS_EMAIL) Optional(java.util.Optional) Args(org.opendatakit.common.cli.Args) FormCache(org.opendatakit.briefcase.util.FormCache) BriefcasePreferences(org.opendatakit.briefcase.model.BriefcasePreferences) FormMetadataPort(org.opendatakit.briefcase.model.form.FormMetadataPort) PullFromCentral(org.opendatakit.briefcase.pull.central.PullFromCentral) CommonsHttp(org.opendatakit.briefcase.reused.http.CommonsHttp) Http(org.opendatakit.briefcase.reused.http.Http) BriefcaseException(org.opendatakit.briefcase.reused.BriefcaseException) TransferForms(org.opendatakit.briefcase.transfer.TransferForms) FormStatus(org.opendatakit.briefcase.model.FormStatus) CentralServer(org.opendatakit.briefcase.reused.transfer.CentralServer) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) Credentials(org.opendatakit.briefcase.reused.http.Credentials)

Example 4 with Response

use of org.opendatakit.briefcase.reused.http.response.Response in project briefcase by opendatakit.

the class PullFromAggregate method downloadSubmissionAttachment.

void downloadSubmissionAttachment(FormStatus form, DownloadedSubmission submission, AggregateAttachment attachment, RunnerStatus runnerStatus, PullFromAggregateTracker tracker, int submissionNumber, int totalSubmissions, int attachmentNumber, int totalAttachments) {
    if (runnerStatus.isCancelled()) {
        tracker.trackCancellation("Download attachment " + attachmentNumber + " of " + totalAttachments + " of submission " + submissionNumber + " of " + totalSubmissions);
        return;
    }
    Path target = form.getSubmissionMediaFile(briefcaseDir, submission.getInstanceId(), attachment.getFilename());
    createDirectories(target.getParent());
    tracker.trackStartDownloadingSubmissionAttachment(submissionNumber, totalSubmissions, attachmentNumber, totalAttachments);
    Response response = http.execute(get(attachment.getDownloadUrl()).downloadTo(target).build());
    if (response.isSuccess())
        tracker.trackEndDownloadingSubmissionAttachment(submissionNumber, totalSubmissions, attachmentNumber, totalAttachments);
    else
        tracker.trackErrorDownloadingSubmissionAttachment(submissionNumber, totalSubmissions, attachmentNumber, totalAttachments, response);
}
Also used : Path(java.nio.file.Path) Response(org.opendatakit.briefcase.reused.http.response.Response)

Example 5 with Response

use of org.opendatakit.briefcase.reused.http.response.Response in project briefcase by opendatakit.

the class PullFromAggregate method downloadFormAttachment.

void downloadFormAttachment(FormStatus form, AggregateAttachment attachment, RunnerStatus runnerStatus, PullFromAggregateTracker tracker, int attachmentNumber, int totalAttachments) {
    if (runnerStatus.isCancelled()) {
        tracker.trackCancellation("Download form attachment " + attachment.getFilename());
        return;
    }
    Path target = form.getFormMediaFile(briefcaseDir, attachment.getFilename());
    createDirectories(target.getParent());
    tracker.trackStartDownloadingFormAttachment(attachmentNumber, totalAttachments);
    Response response = http.execute(get(attachment.getDownloadUrl()).downloadTo(target).build());
    if (response.isSuccess())
        tracker.trackEndDownloadingFormAttachment(attachmentNumber, totalAttachments);
    else
        tracker.trackErrorDownloadingFormAttachment(attachmentNumber, totalAttachments, response);
}
Also used : Path(java.nio.file.Path) Response(org.opendatakit.briefcase.reused.http.response.Response)

Aggregations

Response (org.opendatakit.briefcase.reused.http.response.Response)21 Path (java.nio.file.Path)9 Test (org.junit.Test)5 List (java.util.List)4 Optional (java.util.Optional)4 FormStatus (org.opendatakit.briefcase.model.FormStatus)4 FormStatusEvent (org.opendatakit.briefcase.model.FormStatusEvent)4 Http (org.opendatakit.briefcase.reused.http.Http)4 URL (java.net.URL)3 Arrays (java.util.Arrays)3 Collectors.toList (java.util.stream.Collectors.toList)3 BriefcasePreferences (org.opendatakit.briefcase.model.BriefcasePreferences)3 RemoteFormDefinition (org.opendatakit.briefcase.model.RemoteFormDefinition)3 FormMetadataPort (org.opendatakit.briefcase.model.form.FormMetadataPort)3 CREDENTIALS_PASSWORD (org.opendatakit.briefcase.operations.Common.CREDENTIALS_PASSWORD)3 FORM_ID (org.opendatakit.briefcase.operations.Common.FORM_ID)3 MAX_HTTP_CONNECTIONS (org.opendatakit.briefcase.operations.Common.MAX_HTTP_CONNECTIONS)3 SERVER_URL (org.opendatakit.briefcase.operations.Common.SERVER_URL)3 STORAGE_DIR (org.opendatakit.briefcase.operations.Common.STORAGE_DIR)3 BriefcaseException (org.opendatakit.briefcase.reused.BriefcaseException)3