Add parseAndClose to close streams automatically + minor fixes to support R4B

This commit is contained in:
Grahame Grieve 2021-01-20 16:54:31 +11:00
parent 00410604d7
commit ccf82b8ee3
5 changed files with 36 additions and 9 deletions

View File

@ -4,6 +4,7 @@ package org.hl7.fhir.r5.formats;
import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
/* /*
@ -43,6 +44,7 @@ import java.io.UnsupportedEncodingException;
import org.hl7.fhir.exceptions.FHIRFormatError; import org.hl7.fhir.exceptions.FHIRFormatError;
import org.hl7.fhir.r5.model.DataType; import org.hl7.fhir.r5.model.DataType;
import org.hl7.fhir.r5.model.Resource; import org.hl7.fhir.r5.model.Resource;
import org.hl7.fhir.r5.model.SearchParameter;
import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlPullParserException;
@ -113,6 +115,8 @@ public interface IParser {
*/ */
public Resource parse(InputStream input) throws IOException, FHIRFormatError; public Resource parse(InputStream input) throws IOException, FHIRFormatError;
public Resource parseAndClose(InputStream input) throws IOException, FHIRFormatError;
/** /**
* parse content that is known to be a resource * parse content that is known to be a resource
* @throws UnsupportedEncodingException * @throws UnsupportedEncodingException

View File

@ -36,7 +36,9 @@ package org.hl7.fhir.r5.formats;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.text.ParseException; import java.text.ParseException;
import java.util.HashMap; import java.util.HashMap;
@ -46,6 +48,7 @@ import org.apache.commons.codec.binary.Base64;
import org.hl7.fhir.exceptions.FHIRFormatError; import org.hl7.fhir.exceptions.FHIRFormatError;
import org.hl7.fhir.r5.model.DataType; import org.hl7.fhir.r5.model.DataType;
import org.hl7.fhir.r5.model.Resource; import org.hl7.fhir.r5.model.Resource;
import org.hl7.fhir.r5.model.SearchParameter;
import org.hl7.fhir.utilities.Utilities; import org.hl7.fhir.utilities.Utilities;
public abstract class ParserBase extends FormatUtilities implements IParser { public abstract class ParserBase extends FormatUtilities implements IParser {
@ -101,6 +104,7 @@ public abstract class ParserBase extends FormatUtilities implements IParser {
return bytes.toByteArray(); return bytes.toByteArray();
} }
// -- Parser Configuration -------------------------------- // -- Parser Configuration --------------------------------
protected String xhtmlMessage; protected String xhtmlMessage;
@ -233,5 +237,14 @@ public abstract class ParserBase extends FormatUtilities implements IParser {
return value; return value;
} }
@Override
public Resource parseAndClose(InputStream input) throws IOException, FHIRFormatError {
try {
return parse(input);
} finally {
input.close();
}
}
} }

View File

@ -33,6 +33,7 @@ package org.hl7.fhir.r5.model;
import org.hl7.fhir.instance.model.api.*; import org.hl7.fhir.instance.model.api.*;
import org.hl7.fhir.r5.model.Enumerations.FHIRVersion;
import org.hl7.fhir.exceptions.FHIRException; import org.hl7.fhir.exceptions.FHIRException;
public class Enumerations { public class Enumerations {
@ -6478,6 +6479,9 @@ The primary difference between a medicationusage and a medicationadministration
* added to help the parsers * added to help the parsers
*/ */
NULL; NULL;
public static final FHIRVersion R4B = FHIRVersion._4_0_1;
public static FHIRVersion fromCode(String codeString) throws FHIRException { public static FHIRVersion fromCode(String codeString) throws FHIRException {
if (codeString == null || "".equals(codeString)) if (codeString == null || "".equals(codeString))
return null; return null;

View File

@ -80,9 +80,9 @@ public class TypesUtilities {
} }
public static List<String> wildcardTypes() { public static List<String> wildcardTypes(String version) {
List<String> res = new ArrayList<String>(); List<String> res = new ArrayList<String>();
for (WildcardInformation wi : wildcards()) for (WildcardInformation wi : wildcards(version))
res.add(wi.getTypeName()); res.add(wi.getTypeName());
return res; return res;
} }
@ -94,7 +94,10 @@ public class TypesUtilities {
// Any of the IDMP data types // Any of the IDMP data types
// You have to walk into them to profile them. // You have to walk into them to profile them.
// //
public static List<WildcardInformation> wildcards() { public static List<WildcardInformation> wildcards(String version) {
if (version.startsWith("_")) {
throw new Error("underscore");
}
List<WildcardInformation> res = new ArrayList<WildcardInformation>(); List<WildcardInformation> res = new ArrayList<WildcardInformation>();
// primitive types // primitive types
@ -108,7 +111,9 @@ public class TypesUtilities {
res.add(new WildcardInformation("id", TypeClassification.PRIMITIVE)); res.add(new WildcardInformation("id", TypeClassification.PRIMITIVE));
res.add(new WildcardInformation("instant", TypeClassification.PRIMITIVE)); res.add(new WildcardInformation("instant", TypeClassification.PRIMITIVE));
res.add(new WildcardInformation("integer", TypeClassification.PRIMITIVE)); res.add(new WildcardInformation("integer", TypeClassification.PRIMITIVE));
if (!version.startsWith("4.0")) {
res.add(new WildcardInformation("integer64", TypeClassification.PRIMITIVE)); res.add(new WildcardInformation("integer64", TypeClassification.PRIMITIVE));
}
res.add(new WildcardInformation("markdown", TypeClassification.PRIMITIVE)); res.add(new WildcardInformation("markdown", TypeClassification.PRIMITIVE));
res.add(new WildcardInformation("oid", TypeClassification.PRIMITIVE)); res.add(new WildcardInformation("oid", TypeClassification.PRIMITIVE));
res.add(new WildcardInformation("positiveInt", TypeClassification.PRIMITIVE)); res.add(new WildcardInformation("positiveInt", TypeClassification.PRIMITIVE));

View File

@ -80,6 +80,7 @@ public class FHIRToolingClient {
private static final int TIMEOUT_NORMAL = 1500; private static final int TIMEOUT_NORMAL = 1500;
private static final int TIMEOUT_OPERATION = 30000; private static final int TIMEOUT_OPERATION = 30000;
private static final int TIMEOUT_ENTRY = 500;
private static final int TIMEOUT_OPERATION_LONG = 60000; private static final int TIMEOUT_OPERATION_LONG = 60000;
private static final int TIMEOUT_OPERATION_EXPAND = 120000; private static final int TIMEOUT_OPERATION_EXPAND = 120000;
@ -132,7 +133,7 @@ public class FHIRToolingClient {
capabilities = (TerminologyCapabilities) client.issueGetResourceRequest(resourceAddress.resolveMetadataTxCaps(), capabilities = (TerminologyCapabilities) client.issueGetResourceRequest(resourceAddress.resolveMetadataTxCaps(),
getPreferredResourceFormat(), "TerminologyCapabilities", TIMEOUT_NORMAL).getReference(); getPreferredResourceFormat(), "TerminologyCapabilities", TIMEOUT_NORMAL).getReference();
} catch (Exception e) { } catch (Exception e) {
handleException("An error has occurred while trying to fetch the server's terminology capabilities", e); throw new FHIRException("Error fetching the server's terminology capabilities", e);
} }
return capabilities; return capabilities;
} }
@ -143,7 +144,7 @@ public class FHIRToolingClient {
conformance = (CapabilityStatement) client.issueGetResourceRequest(resourceAddress.resolveMetadataUri(false), conformance = (CapabilityStatement) client.issueGetResourceRequest(resourceAddress.resolveMetadataUri(false),
getPreferredResourceFormat(), "CapabilitiesStatement", TIMEOUT_NORMAL).getReference(); getPreferredResourceFormat(), "CapabilitiesStatement", TIMEOUT_NORMAL).getReference();
} catch (Exception e) { } catch (Exception e) {
handleException("An error has occurred while trying to fetch the server's conformance statement", e); throw new FHIRException("Error fetching the server's conformance statement", e);
} }
return conformance; return conformance;
} }
@ -154,7 +155,7 @@ public class FHIRToolingClient {
capabilities = (CapabilityStatement) client.issueGetResourceRequest(resourceAddress.resolveMetadataUri(true), capabilities = (CapabilityStatement) client.issueGetResourceRequest(resourceAddress.resolveMetadataUri(true),
getPreferredResourceFormat(), "CapabilitiesStatement-Quick", TIMEOUT_NORMAL).getReference(); getPreferredResourceFormat(), "CapabilitiesStatement-Quick", TIMEOUT_NORMAL).getReference();
} catch (Exception e) { } catch (Exception e) {
handleException("An error fetching the server's capability statement: "+e.getMessage(), e); throw new FHIRException("Error fetching the server's capability statement: "+e.getMessage(), e);
} }
return capabilities; return capabilities;
} }
@ -182,7 +183,7 @@ public class FHIRToolingClient {
throw new EFhirClientException("Server returned error code " + result.getHttpStatus(), (OperationOutcome) result.getPayload()); throw new EFhirClientException("Server returned error code " + result.getHttpStatus(), (OperationOutcome) result.getPayload());
} }
} catch (Exception e) { } catch (Exception e) {
handleException("An error has occurred while trying to read this version of the resource", e); throw new FHIRException("Error trying to read this version of the resource", e);
} }
return result.getPayload(); return result.getPayload();
} }
@ -291,7 +292,7 @@ public class FHIRToolingClient {
public Bundle transaction(Bundle batch) { public Bundle transaction(Bundle batch) {
Bundle transactionResult = null; Bundle transactionResult = null;
try { try {
transactionResult = client.postBatchRequest(resourceAddress.getBaseServiceUri(), ByteUtils.resourceToByteArray(batch, false, isJson(getPreferredResourceFormat())), getPreferredResourceFormat(), "transaction", TIMEOUT_NORMAL + batch.getEntry().size()); transactionResult = client.postBatchRequest(resourceAddress.getBaseServiceUri(), ByteUtils.resourceToByteArray(batch, false, isJson(getPreferredResourceFormat())), getPreferredResourceFormat(), "transaction", TIMEOUT_OPERATION + (TIMEOUT_ENTRY * batch.getEntry().size()));
} catch (Exception e) { } catch (Exception e) {
handleException("An error occurred trying to process this transaction request", e); handleException("An error occurred trying to process this transaction request", e);
} }