Search in sources :

Example 21 with Response

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

the class PushFormToAggregate method pushFormToAggregate.

private static void pushFormToAggregate(Path storageDir, Optional<String> formid, String username, String password, URL server, boolean forceSendBlank, Optional<Integer> maybeMaxConnections) {
    CliEventsCompanion.attach(log);
    Path briefcaseDir = Common.getOrCreateBriefcaseDir(storageDir);
    FormCache formCache = FormCache.from(briefcaseDir);
    formCache.update();
    BriefcasePreferences appPreferences = BriefcasePreferences.appScoped();
    int maxHttpConnections = Optionals.race(maybeMaxConnections, 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 response = http.execute(aggregateServer.getPushFormPreflightRequest());
    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> statuses;
    if (formid.isPresent()) {
        String requestedFormId = formid.get();
        FormStatus status = formCache.getForms().stream().filter(form -> form.getFormId().equals(requestedFormId)).map(FormStatus::new).findFirst().orElseThrow(() -> new BriefcaseException("Form " + requestedFormId + " not found"));
        statuses = Arrays.asList(status);
    } else {
        statuses = formCache.getForms().stream().map(FormStatus::new).collect(Collectors.toList());
    }
    TransferForms forms = TransferForms.of(statuses);
    forms.selectAll();
    PushToAggregate pushOp = new PushToAggregate(http, aggregateServer, briefcaseDir, forceSendBlank, PushFormToAggregate::onEvent);
    JobsRunner.launchAsync(forms.map(pushOp::push), PushFormToAggregate::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) 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) 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) CommonsHttp(org.opendatakit.briefcase.reused.http.CommonsHttp) PushToAggregate(org.opendatakit.briefcase.push.aggregate.PushToAggregate) FormStatus(org.opendatakit.briefcase.model.FormStatus) FormStatusEvent(org.opendatakit.briefcase.model.FormStatusEvent) 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) Collectors(java.util.stream.Collectors) Http(org.opendatakit.briefcase.reused.http.Http) CREDENTIALS_USERNAME(org.opendatakit.briefcase.operations.Common.CREDENTIALS_USERNAME) List(java.util.List) Optional(java.util.Optional) FormCache(org.opendatakit.briefcase.util.FormCache) BriefcasePreferences(org.opendatakit.briefcase.model.BriefcasePreferences) CommonsHttp(org.opendatakit.briefcase.reused.http.CommonsHttp) Http(org.opendatakit.briefcase.reused.http.Http) Response(org.opendatakit.briefcase.reused.http.response.Response) PushToAggregate(org.opendatakit.briefcase.push.aggregate.PushToAggregate) BriefcaseException(org.opendatakit.briefcase.reused.BriefcaseException) TransferForms(org.opendatakit.briefcase.transfer.TransferForms) FormStatus(org.opendatakit.briefcase.model.FormStatus) AggregateServer(org.opendatakit.briefcase.reused.transfer.AggregateServer) Credentials(org.opendatakit.briefcase.reused.http.Credentials)

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