use of org.hl7.fhir.r5.formats.IParser in project org.hl7.fhir.core by hapifhir.
the class BatchLoader method main.
public static void main(String[] args) throws IOException, Exception {
if (args.length < 4) {
System.out.println("Batch uploader takes 4 parameters in order: server base url, file/folder to upload, xml/json, and batch size");
} else {
String server = args[0];
String file = args[1];
// args[2].equals("json") ? new JsonParser() : new XmlParser();
IParser p = new JsonParser();
int size = Integer.parseInt(args[3]);
size = 500;
if (file.endsWith(".xml")) {
throw new FHIRException("Unimplemented file type " + file);
} else if (file.endsWith(".json")) {
throw new FHIRException("Unimplemented file type " + file);
} else if (file.endsWith(".zip")) {
LoadZipFile(server, file, p, size, 0, -1);
} else if (new File(file).isDirectory()) {
LoadDirectory(server, file, p, size);
} else
throw new FHIRException("Unknown file type " + file);
}
}
use of org.hl7.fhir.r5.formats.IParser in project org.hl7.fhir.core by hapifhir.
the class ByteUtils method resourceToByteArray.
public static <T extends Resource> byte[] resourceToByteArray(T resource, boolean pretty, boolean isJson) {
ByteArrayOutputStream baos = null;
byte[] byteArray = null;
try {
baos = new ByteArrayOutputStream();
IParser parser = null;
if (isJson) {
parser = new JsonParser();
} else {
parser = new XmlParser();
}
parser.setOutputStyle(pretty ? IParser.OutputStyle.PRETTY : IParser.OutputStyle.NORMAL);
parser.compose(baos, resource);
baos.close();
byteArray = baos.toByteArray();
baos.close();
} catch (Exception e) {
try {
baos.close();
} catch (Exception ex) {
throw new EFhirClientException("Error closing output stream", ex);
}
throw new EFhirClientException("Error converting output stream to byte array", e);
}
return byteArray;
}
use of org.hl7.fhir.r5.formats.IParser in project fhir-bridge by ehrbase.
the class FhirValidationConfiguration method validationSupport.
public IValidationSupport validationSupport() {
ValidationSupportChain validationSupportChain = new ValidationSupportChain();
// Validates core structure definitions
DefaultProfileValidationSupport defaultProfileValidationSupport = new DefaultProfileValidationSupport(fhirContext);
defaultProfileValidationSupport.fetchAllStructureDefinitions();
defaultProfileValidationSupport.fetchCodeSystem("");
validationSupportChain.addValidationSupport(defaultProfileValidationSupport);
// Validates custom profiles (loaded from classpath)
PrePopulatedValidationSupport prePopulatedValidationSupport = new PrePopulatedValidationSupport(fhirContext);
IParser parser = fhirContext.newXmlParser();
try {
for (Resource resource : applicationContext.getResources("classpath:/profiles/*")) {
StructureDefinition profile = parser.parseResource(StructureDefinition.class, resource.getInputStream());
if (properties.isOptionalIdentifier()) {
modifyProfile(profile);
}
prePopulatedValidationSupport.addStructureDefinition(profile);
}
} catch (IOException e) {
throw new FhirBridgeException("An I/O exception occurred while loading custom profiles");
}
validationSupportChain.fetchAllStructureDefinitions();
defaultProfileValidationSupport.fetchCodeSystem("");
validationSupportChain.addValidationSupport(prePopulatedValidationSupport);
// Validates terminology: CodeSystems and ValueSets (using the internal and/or remote terminology service)
if (isTerminologyValidationEnabled()) {
TerminologyValidationMode mode = properties.getTerminology().getMode();
if (mode == TerminologyValidationMode.REMOTE || mode == TerminologyValidationMode.MIXED) {
RemoteTerminologyServiceValidationSupport remoteTerminologyServerValidationSupport = new RemoteTerminologyServiceValidationSupport(fhirContext);
remoteTerminologyServerValidationSupport.setBaseUrl(properties.getTerminology().getServerBaseUrl());
validationSupportChain.addValidationSupport(remoteTerminologyServerValidationSupport);
}
if (mode == TerminologyValidationMode.INTERNAL || mode == TerminologyValidationMode.MIXED) {
validationSupportChain.addValidationSupport(new InMemoryTerminologyServerValidationSupport(fhirContext));
validationSupportChain.addValidationSupport(new CommonCodeSystemsTerminologyService(fhirContext));
}
}
return new CachingValidationSupport(validationSupportChain);
}
use of org.hl7.fhir.r5.formats.IParser in project geoprism-registry by terraframe.
the class FhirBulkDataImporter method synchronize.
public void synchronize() {
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS);
HttpClientBuilder builder = HttpClientBuilder.create();
builder.setConnectionManager(connectionManager);
CloseableHttpClient myClient = builder.build();
FhirContext ctx = FhirContext.forR4();
String statusUrl = initiateBulkExport(myClient, ctx);
if (statusUrl != null) {
final List<String> outputs = getExportResults(myClient, statusUrl);
IGenericClient client = ctx.newRestfulGenericClient(this.system.getUrl());
for (String binaryUrl : outputs) {
Binary binary = client.fetchResourceFromUrl(Binary.class, binaryUrl);
String base64 = binary.getContentAsBase64();
byte[] result = Base64.getDecoder().decode(base64);
IParser parser = ctx.newJsonParser();
String message = new String(result);
try (BufferedReader reader = new BufferedReader(new StringReader(message))) {
String line = null;
while ((line = reader.readLine()) != null) {
IBaseResource resource = parser.parseResource(line);
IIdType id = resource.getIdElement();
String resourceType = id.getResourceType();
if (resourceType.equals(ResourceTypes.LOCATION.toCode())) {
Location location = (Location) resource;
this.processor.process(location);
} else if (resourceType.equals(ResourceTypes.ORGANIZATION.toCode())) {
Organization organization = (Organization) resource;
this.processor.process(organization);
}
}
} catch (IOException e) {
throw new ProgrammingErrorException(e);
}
}
}
}
use of org.hl7.fhir.r5.formats.IParser in project geoprism-registry by terraframe.
the class FhirExportSynchronizationManager method writeToFile.
public File writeToFile() throws IOException {
final FhirExternalSystem system = (FhirExternalSystem) this.config.getSystem();
try (FhirConnection connection = FhirConnectionFactory.get(system)) {
String name = SessionPredicate.generateId();
File root = new File(new File(VaultProperties.getPath("vault.default"), "files"), name);
root.mkdirs();
Bundle bundle = this.generateBundle(connection);
FhirContext ctx = FhirContext.forR4();
IParser parser = ctx.newJsonParser();
try {
parser.encodeResourceToWriter(bundle, new FileWriter(new File(root, "bundle.json")));
} catch (DataFormatException | IOException e) {
throw new ProgrammingErrorException(e);
}
return root;
} catch (Exception e) {
throw new HttpError(e);
}
}
Aggregations