use of org.hl7.fhir.r4.utils.client.EFhirClientException in project org.hl7.fhir.core by hapifhir.
the class ClientUtils method getResourceAsByteArray.
/**
****************************************************************
* Other general helper methods
* ***************************************************************
*/
public <T extends Resource> byte[] getResourceAsByteArray(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 ? OutputStyle.PRETTY : 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.r4.utils.client.EFhirClientException in project org.hl7.fhir.core by hapifhir.
the class FHIRToolingClient method updateClosure.
public ConceptMap updateClosure(String name, Coding coding) {
Parameters params = new Parameters();
params.addParameter().setName("name").setValue(new StringType(name));
params.addParameter().setName("concept").setValue(coding);
List<Header> headers = null;
ResourceRequest<Resource> result = utils.issuePostRequest(resourceAddress.resolveOperationUri(null, "closure", new HashMap<String, String>()), utils.getResourceAsByteArray(params, false, isJson(getPreferredResourceFormat())), getPreferredResourceFormat(), headers, TIMEOUT_OPERATION);
// gone
result.addErrorStatus(410);
// unknown
result.addErrorStatus(404);
result.addErrorStatus(405);
// Unprocessable Entity
result.addErrorStatus(422);
result.addSuccessStatus(200);
result.addSuccessStatus(201);
if (result.isUnsuccessfulRequest()) {
throw new EFhirClientException("Server returned error code " + result.getHttpStatus(), (OperationOutcome) result.getPayload());
}
return (ConceptMap) result.getPayload();
}
use of org.hl7.fhir.r4.utils.client.EFhirClientException in project org.hl7.fhir.core by hapifhir.
the class FHIRToolingClient method update.
public Resource update(Resource resource) {
ResourceRequest<Resource> result = null;
try {
List<Header> headers = null;
result = utils.issuePutRequest(resourceAddress.resolveGetUriFromResourceClassAndId(resource.getClass(), resource.getId()), utils.getResourceAsByteArray(resource, false, isJson(getPreferredResourceFormat())), getPreferredResourceFormat(), headers, TIMEOUT_OPERATION);
// gone
result.addErrorStatus(410);
// unknown
result.addErrorStatus(404);
result.addErrorStatus(405);
// Unprocessable Entity
result.addErrorStatus(422);
result.addSuccessStatus(200);
result.addSuccessStatus(201);
if (result.isUnsuccessfulRequest()) {
throw new EFhirClientException("Server returned error code " + result.getHttpStatus(), (OperationOutcome) result.getPayload());
}
} catch (Exception e) {
throw new EFhirClientException("An error has occurred while trying to update this resource", e);
}
// TODO oe 26.1.2015 could be made nicer if only OperationOutcome locationheader is returned with an operationOutcome would be returned (and not the resource also) we make another read
try {
OperationOutcome operationOutcome = (OperationOutcome) result.getPayload();
ResourceAddress.ResourceVersionedIdentifier resVersionedIdentifier = ResourceAddress.parseCreateLocation(result.getLocation());
return this.vread(resource.getClass(), resVersionedIdentifier.getId(), resVersionedIdentifier.getVersionId());
} catch (ClassCastException e) {
// if we fall throught we have the correct type already in the create
}
return result.getPayload();
}
use of org.hl7.fhir.r4.utils.client.EFhirClientException in project org.hl7.fhir.core by hapifhir.
the class FHIRToolingClient method expandValueset.
public ValueSet expandValueset(ValueSet source, Parameters expParams) {
List<Header> headers = null;
Parameters p = expParams == null ? new Parameters() : expParams.copy();
p.addParameter().setName("valueSet").setResource(source);
ResourceRequest<Resource> result = utils.issuePostRequest(resourceAddress.resolveOperationUri(ValueSet.class, "expand"), utils.getResourceAsByteArray(p, false, isJson(getPreferredResourceFormat())), getPreferredResourceFormat(), headers, 4);
// gone
result.addErrorStatus(410);
// unknown
result.addErrorStatus(404);
result.addErrorStatus(405);
// Unprocessable Entity
result.addErrorStatus(422);
result.addSuccessStatus(200);
result.addSuccessStatus(201);
if (result.isUnsuccessfulRequest()) {
throw new EFhirClientException("Server returned error code " + result.getHttpStatus(), (OperationOutcome) result.getPayload());
}
return (ValueSet) result.getPayload();
}
use of org.hl7.fhir.r4.utils.client.EFhirClientException in project org.hl7.fhir.core by hapifhir.
the class FHIRToolingClient method expandValueset.
public ValueSet expandValueset(ValueSet source, Parameters expParams, Map<String, String> params) {
List<Header> headers = null;
Parameters p = expParams == null ? new Parameters() : expParams.copy();
p.addParameter().setName("valueSet").setResource(source);
for (String n : params.keySet()) p.addParameter().setName(n).setValue(new StringType(params.get(n)));
ResourceRequest<Resource> result = utils.issuePostRequest(resourceAddress.resolveOperationUri(ValueSet.class, "expand", params), utils.getResourceAsByteArray(p, false, isJson(getPreferredResourceFormat())), getPreferredResourceFormat(), headers, 4);
// gone
result.addErrorStatus(410);
// unknown
result.addErrorStatus(404);
result.addErrorStatus(405);
// Unprocessable Entity
result.addErrorStatus(422);
result.addSuccessStatus(200);
result.addSuccessStatus(201);
if (result.isUnsuccessfulRequest()) {
throw new EFhirClientException("Server returned error code " + result.getHttpStatus(), (OperationOutcome) result.getPayload());
}
return (ValueSet) result.getPayload();
}
Aggregations