use of org.hl7.fhir.r5.model.Parameters.ParametersParameterComponent in project summary-care-record-api by NHSDigital.
the class AcsService method getPermission.
private static AcsPermission getPermission(ParametersParameterComponent parameter) {
Coding coding = (Coding) parameter.getPart().stream().filter(p -> PERMISSION_CODE_PART_NAME.equals(p.getName())).filter(p -> PERMISSION_CODE_SYSTEM.equals(((Coding) p.getValue()).getSystem())).findFirst().get().getValue();
String permissionValue = coding.getCode();
return AcsPermission.fromValue(permissionValue);
}
use of org.hl7.fhir.r5.model.Parameters.ParametersParameterComponent in project pathling by aehrc.
the class ImportExecutor method readStringsFromUrl.
@Nonnull
private Dataset<String> readStringsFromUrl(@Nonnull final ParametersParameterComponent urlParam) {
final String url = ((UrlType) urlParam.getValue()).getValueAsString();
final String decodedUrl = URLDecoder.decode(url, StandardCharsets.UTF_8);
final String convertedUrl = PersistenceScheme.convertS3ToS3aUrl(decodedUrl);
final Dataset<String> jsonStrings;
try {
// Check that the user is authorized to execute the operation.
accessRules.ifPresent(ar -> ar.checkCanImportFrom(convertedUrl));
final FilterFunction<String> nonBlanks = s -> !s.isBlank();
jsonStrings = spark.read().textFile(convertedUrl).filter(nonBlanks);
} catch (final SecurityError e) {
throw new InvalidUserInputError("Not allowed to import from URL: " + convertedUrl, e);
} catch (final Exception e) {
throw new InvalidUserInputError("Error reading from URL: " + convertedUrl, e);
}
return jsonStrings;
}
use of org.hl7.fhir.r5.model.Parameters.ParametersParameterComponent in project pathling by aehrc.
the class ImportTest method buildImportParameters.
@SuppressWarnings("SameParameterValue")
@Nonnull
Parameters buildImportParameters(@Nonnull final URL jsonURL, @Nonnull final ResourceType resourceType, @Nonnull final ImportMode mode) {
final Parameters parameters = buildImportParameters(jsonURL, resourceType);
final ParametersParameterComponent sourceParam = parameters.getParameter().stream().filter(p -> p.getName().equals("source")).findFirst().orElseThrow();
sourceParam.addPart().setName("mode").setValue(new CodeType(mode.getCode()));
return parameters;
}
use of org.hl7.fhir.r5.model.Parameters.ParametersParameterComponent in project pathling by aehrc.
the class ImportTest method buildImportParameters.
@SuppressWarnings("SameParameterValue")
@Nonnull
Parameters buildImportParameters(@Nonnull final URL jsonURL, @Nonnull final ResourceType resourceType) {
final Parameters parameters = new Parameters();
final ParametersParameterComponent sourceParam = parameters.addParameter().setName("source");
sourceParam.addPart().setName("resourceType").setValue(new CodeType(resourceType.toCode()));
sourceParam.addPart().setName("url").setValue(new UrlType(jsonURL.toExternalForm()));
return parameters;
}
use of org.hl7.fhir.r5.model.Parameters.ParametersParameterComponent in project pathling by aehrc.
the class DockerImageTest method importDataAndQuery.
@Test
void importDataAndQuery() throws JSONException {
try {
// Get the token endpoint from the CapabilityStatement.
final CapabilityStatement capabilities = getCapabilityStatement();
final String tokenUrl = capabilities.getRest().stream().findFirst().map(rest -> ((UriType) rest.getSecurity().getExtensionByUrl("http://fhir-registry.smarthealthit.org/StructureDefinition/oauth-uris").getExtensionByUrl("token").getValue()).asStringValue()).orElseThrow();
// Get an access token from the token endpoint.
final HttpPost clientCredentialsGrant = new HttpPost(tokenUrl);
final List<? extends NameValuePair> nameValuePairs = Arrays.asList(new BasicNameValuePair("grant_type", "client_credentials"), new BasicNameValuePair("client_id", CLIENT_ID), new BasicNameValuePair("client_secret", CLIENT_SECRET), new BasicNameValuePair("scope", REQUESTED_SCOPE));
clientCredentialsGrant.setEntity(new UrlEncodedFormEntity(nameValuePairs));
log.info("Requesting client credentials grant");
final String accessToken;
try (final CloseableHttpResponse response = (CloseableHttpResponse) httpClient.execute(clientCredentialsGrant)) {
assertThat(response.getStatusLine().getStatusCode()).withFailMessage("Client credentials grant did not succeed").isEqualTo(200);
final InputStream clientCredentialsStream = response.getEntity().getContent();
final Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();
final ClientCredentialsResponse ccResponse = gson.fromJson(new InputStreamReader(clientCredentialsStream), ClientCredentialsResponse.class);
accessToken = ccResponse.getAccessToken();
}
// Create a request to the $import operation, referencing the NDJSON files we have loaded into
// the staging area.
final InputStream requestStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("import/SystemTest/request.Parameters.json");
assertThat(requestStream).isNotNull();
final HttpPost importRequest = new HttpPost("http://localhost:8091/fhir/$import");
importRequest.setEntity(new InputStreamEntity(requestStream));
importRequest.addHeader("Content-Type", "application/json");
importRequest.addHeader("Accept", "application/fhir+json");
importRequest.addHeader("Authorization", "Bearer " + accessToken);
log.info("Sending import request");
final OperationOutcome importOutcome;
try (final CloseableHttpResponse response = (CloseableHttpResponse) httpClient.execute(importRequest)) {
final InputStream importResponseStream = response.getEntity().getContent();
importOutcome = (OperationOutcome) jsonParser.parseResource(importResponseStream);
assertThat(response.getStatusLine().getStatusCode()).withFailMessage(importOutcome.getIssueFirstRep().getDiagnostics()).isEqualTo(200);
}
assertThat(importOutcome.getIssueFirstRep().getDiagnostics()).isEqualTo("Data import completed successfully");
log.info("Import completed successfully");
final Parameters inParams = new Parameters();
// Set subject resource parameter.
final ParametersParameterComponent subjectResourceParam = new ParametersParameterComponent();
subjectResourceParam.setName("subjectResource");
subjectResourceParam.setValue(new CodeType("Patient"));
// Add aggregation, number of patients.
final ParametersParameterComponent aggregationParam = new ParametersParameterComponent();
aggregationParam.setName("aggregation");
aggregationParam.setValue(new StringType("count()"));
// Add grouping, has the patient been diagnosed with a chronic disease?
final ParametersParameterComponent groupingParam = new ParametersParameterComponent();
groupingParam.setName("grouping");
groupingParam.setValue(new StringType("reverseResolve(Condition.subject)" + ".code" + ".memberOf('http://snomed.info/sct?fhir_vs=ecl/" + "^ 32570581000036105 : " + "<< 263502005 = << 90734009')"));
// Add filter, females only.
final ParametersParameterComponent filterParam = new ParametersParameterComponent();
filterParam.setName("filter");
filterParam.setValue(new StringType("gender = 'female'"));
inParams.getParameter().add(subjectResourceParam);
inParams.getParameter().add(aggregationParam);
inParams.getParameter().add(groupingParam);
inParams.getParameter().add(filterParam);
// Send a request to the `$aggregate` operation on the FHIR server.
final String requestString = jsonParser.encodeResourceToString(inParams);
final HttpPost queryRequest = new HttpPost("http://localhost:8091/fhir/Patient/$aggregate");
queryRequest.setEntity(new StringEntity(requestString));
queryRequest.addHeader("Content-Type", "application/fhir+json");
queryRequest.addHeader("Accept", "application/fhir+json");
queryRequest.addHeader("Authorization", "Bearer " + accessToken);
log.info("Sending query request");
try (final CloseableHttpResponse response = (CloseableHttpResponse) httpClient.execute(queryRequest)) {
final int statusCode = response.getStatusLine().getStatusCode();
final InputStream queryResponseStream = response.getEntity().getContent();
if (statusCode == 200) {
final StringWriter writer = new StringWriter();
IOUtils.copy(queryResponseStream, writer, StandardCharsets.UTF_8);
assertJson("responses/DockerImageTest/importDataAndQuery.Parameters.json", writer.toString());
} else {
captureLogs();
final OperationOutcome opOutcome = (OperationOutcome) jsonParser.parseResource(queryResponseStream);
assertEquals(200, statusCode, opOutcome.getIssueFirstRep().getDiagnostics());
}
}
} catch (final Exception e) {
captureLogs();
} finally {
stopContainer(dockerClient, fhirServerContainerId);
fhirServerContainerId = null;
getRuntime().removeShutdownHook(shutdownHook);
}
}
Aggregations