ca.uhn.fhir.rest.client.interceptor.BasicAuthInterceptor Java Examples

The following examples show how to use ca.uhn.fhir.rest.client.interceptor.BasicAuthInterceptor. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Dstu3FhirTerminologyProvider.java    From cql_engine with Apache License 2.0 6 votes vote down vote up
public Dstu3FhirTerminologyProvider setEndpoint(String endpoint, boolean validation) {
    this.endpoint = endpoint;
    if (!validation) {
        this.fhirContext.getRestfulClientFactory().setServerValidationMode(ServerValidationModeEnum.NEVER);
    }
    fhirContext.getRestfulClientFactory().setSocketTimeout(1200 * 10000);
    fhirClient = fhirContext.newRestfulGenericClient(endpoint);

    if (this.headerInjectionInterceptor != null) {
        fhirClient.registerInterceptor(headerInjectionInterceptor);
    }

    if (userName != null && password != null) {
        BasicAuthInterceptor basicAuth = new BasicAuthInterceptor(userName, password);
        fhirClient.registerInterceptor(basicAuth);
    }
    return this;
}
 
Example #2
Source File: R4FhirTerminologyProvider.java    From cql_engine with Apache License 2.0 6 votes vote down vote up
public R4FhirTerminologyProvider setEndpoint(String endpoint, boolean validation) {
    this.endpoint = endpoint;
    if (!validation) {
        this.fhirContext.getRestfulClientFactory().setServerValidationMode(ServerValidationModeEnum.NEVER);
    }
    fhirContext.getRestfulClientFactory().setSocketTimeout(1200 * 10000);
    fhirClient = fhirContext.newRestfulGenericClient(endpoint);

    if (this.headerInjectionInterceptor != null) {
        fhirClient.registerInterceptor(headerInjectionInterceptor);
    }

    if (userName != null && password != null) {
        BasicAuthInterceptor basicAuth = new BasicAuthInterceptor(userName, password);
        fhirClient.registerInterceptor(basicAuth);
    }
    return this;
}
 
Example #3
Source File: FhirVerifierExtension.java    From syndesis with Apache License 2.0 4 votes vote down vote up
private static void verifyConnection(ResultBuilder builder, Map<String, Object> parameters) {
    if (!builder.build().getErrors().isEmpty()) {
        return;
    }
    final String serverUrl = ConnectorOptions.extractOption(parameters, SERVER_URL);
    final FhirVersionEnum fhirVersion = ConnectorOptions.extractOptionAsType(
        parameters, FHIR_VERSION, FhirVersionEnum.class);
    final String username = ConnectorOptions.extractOption(parameters, "username");
    final String password = ConnectorOptions.extractOption(parameters, "password");
    final String accessToken = ConnectorOptions.extractOption(parameters, "accessToken");

    LOG.debug("Validating FHIR connection to {} with FHIR version {}", serverUrl, fhirVersion);

    if (ObjectHelper.isNotEmpty(serverUrl)) {
        try {
            FhirContext fhirContext = new FhirContext(fhirVersion);
            IGenericClient iGenericClient = fhirContext.newRestfulGenericClient(serverUrl);

            if (ObjectHelper.isNotEmpty(username) || ObjectHelper.isNotEmpty(password)) {
                if (ObjectHelper.isEmpty(username) || ObjectHelper.isEmpty(password)) {
                    builder.error(
                        ResultErrorBuilder.withCodeAndDescription(VerificationError.StandardCode.ILLEGAL_PARAMETER_GROUP_COMBINATION,
                            "Both username and password must be provided to enable basic authentication")
                            .parameterKey("username")
                            .parameterKey("password")
                            .build());
                } else if (ObjectHelper.isNotEmpty(accessToken)) {
                    builder.error(
                        ResultErrorBuilder.withCodeAndDescription(VerificationError.StandardCode.ILLEGAL_PARAMETER_GROUP_COMBINATION,
                            "You must provide either username and password or bearer token to enable authentication")
                            .parameterKey("accessToken")
                            .build());
                } else {
                    iGenericClient.registerInterceptor(new BasicAuthInterceptor(username, password));
                }
            } else if (ObjectHelper.isNotEmpty(accessToken)) {
                iGenericClient.registerInterceptor(new BearerTokenAuthInterceptor(accessToken));
            }
            iGenericClient.forceConformanceCheck();
        } catch (Exception e) {
            builder.error(
                ResultErrorBuilder.withCodeAndDescription(VerificationError.StandardCode.ILLEGAL_PARAMETER_GROUP_COMBINATION, "Unable to connect to FHIR server")
                    .detail(VerificationError.ExceptionAttribute.EXCEPTION_INSTANCE, e)
                    .parameterKey(SERVER_URL)
                    .parameterKey(FHIR_VERSION)
                    .build()
            );
        }
    } else {
        builder.error(
            ResultErrorBuilder.withCodeAndDescription(VerificationError.StandardCode.ILLEGAL_PARAMETER_VALUE, "Invalid blank FHIR server URL")
                .parameterKey(SERVER_URL)
                .build()
        );
    }
}
 
Example #4
Source File: CacheValueSetsProvider.java    From cqf-ruler with Apache License 2.0 4 votes vote down vote up
@Operation(name="cache-valuesets", idempotent = true, type = Endpoint.class)
public Resource cacheValuesets(
        RequestDetails details,
        @IdParam IdType theId,
        @RequiredParam(name="valuesets") StringAndListParam valuesets,
        @OptionalParam(name="user") String userName,
        @OptionalParam(name="pass") String password
) {

    Endpoint endpoint = this.endpointDao.read(theId);

    if (endpoint == null) {
        return Helper.createErrorOutcome("Could not find Endpoint/" + theId);
    }

    IGenericClient client = this.systemDao.getContext().newRestfulGenericClient(endpoint.getAddress());

    if (userName != null || password != null) {
        if (userName == null) {
            Helper.createErrorOutcome("Password was provided, but not a user name.");
        }
        else if (password == null) {
            Helper.createErrorOutcome("User name was provided, but not a password.");
        }

        BasicAuthInterceptor basicAuth = new BasicAuthInterceptor(userName, password);
        client.registerInterceptor(basicAuth);

        // TODO - more advanced security like bearer tokens, etc...
    }

    try {
        Bundle bundleToPost = new Bundle();
        for (StringOrListParam params : valuesets.getValuesAsQueryTokens()) {
            for (StringParam valuesetId : params.getValuesAsQueryTokens()) {
                bundleToPost.addEntry()
                        .setRequest(new Bundle.BundleEntryRequestComponent().setMethod(Bundle.HTTPVerb.PUT).setUrl("ValueSet/" + valuesetId.getValue()))
                        .setResource(resolveValueSet(client, valuesetId.getValue()));
            }
        }

        return (Resource) systemDao.transaction(details, bundleToPost);
    } catch (Exception e) {
        return Helper.createErrorOutcome(e.getMessage());
    }
}
 
Example #5
Source File: CacheValueSetsProvider.java    From cqf-ruler with Apache License 2.0 4 votes vote down vote up
@Operation(name="cache-valuesets", idempotent = true, type = Endpoint.class)
public Resource cacheValuesets(
        RequestDetails details,
        @IdParam IdType theId,
        @RequiredParam(name="valuesets") StringAndListParam valuesets,
        @OptionalParam(name="user") String userName,
        @OptionalParam(name="pass") String password
) {

    Endpoint endpoint = this.endpointDao.read(theId);

    if (endpoint == null) {
        return Helper.createErrorOutcome("Could not find Endpoint/" + theId);
    }

    IGenericClient client = this.systemDao.getContext().newRestfulGenericClient(endpoint.getAddress());

    if (userName != null || password != null) {
        if (userName == null) {
            Helper.createErrorOutcome("Password was provided, but not a user name.");
        }
        else if (password == null) {
            Helper.createErrorOutcome("User name was provided, but not a password.");
        }

        BasicAuthInterceptor basicAuth = new BasicAuthInterceptor(userName, password);
        client.registerInterceptor(basicAuth);

        // TODO - more advanced security like bearer tokens, etc...
    }

    try {
        Bundle bundleToPost = new Bundle();
        for (StringOrListParam params : valuesets.getValuesAsQueryTokens()) {
            for (StringParam valuesetId : params.getValuesAsQueryTokens()) {
                bundleToPost.addEntry()
                        .setRequest(new Bundle.BundleEntryRequestComponent().setMethod(Bundle.HTTPVerb.PUT).setUrl("ValueSet/" + valuesetId.getValue()))
                        .setResource(resolveValueSet(client, valuesetId.getValue()));
            }
        }

        return (Resource) systemDao.transaction(details, bundleToPost);
    } catch (Exception e) {
        return Helper.createErrorOutcome(e.getMessage());
    }
}