Java源码示例:ca.uhn.fhir.rest.client.interceptor.BasicAuthInterceptor
示例1
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;
}
示例2
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;
}
示例3
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()
);
}
}
示例4
@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());
}
}
示例5
@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());
}
}