use of org.hl7.fhir.r4.utils.client.EFhirClientException in project org.hl7.fhir.core by hapifhir.
the class Client method postBatchRequest.
public Bundle postBatchRequest(URI resourceUri, byte[] payload, String resourceFormat, Headers headers, String message, int timeout) throws IOException {
if (payload == null)
throw new EFhirClientException("POST requests require a non-null payload");
RequestBody body = RequestBody.create(MediaType.parse(resourceFormat + ";charset=" + DEFAULT_CHARSET), payload);
Request.Builder request = new Request.Builder().url(resourceUri.toURL()).post(body);
return executeBundleRequest(request, resourceFormat, headers, message, retryCount, timeout);
}
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 ClientUtils method unmarshalReference.
/**
* Unmarshals a resource from the response stream.
*
* @param response
* @return
*/
@SuppressWarnings("unchecked")
protected <T extends Resource> T unmarshalReference(HttpResponse response, String format) {
T resource = null;
OperationOutcome error = null;
byte[] cnt = log(response);
if (cnt != null) {
try {
resource = (T) getParser(format).parse(cnt);
if (resource instanceof OperationOutcome && hasError((OperationOutcome) resource)) {
error = (OperationOutcome) resource;
}
} catch (IOException ioe) {
throw new EFhirClientException("Error reading Http Response: " + ioe.getMessage(), ioe);
} catch (Exception e) {
throw new EFhirClientException("Error parsing response message: " + e.getMessage(), e);
}
}
if (error != null) {
throw new EFhirClientException("Error from server: " + ResourceUtilities.getErrorDescription(error), error);
}
return resource;
}
use of org.hl7.fhir.r4.utils.client.EFhirClientException in project org.hl7.fhir.core by hapifhir.
the class FHIRToolingClient method initializeClosure.
public ConceptMap initializeClosure(String name) {
Parameters params = new Parameters();
params.addParameter().setName("name").setValue(new StringType(name));
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_NORMAL);
// 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 getCanonical.
// GET fhir/ValueSet?url=http://hl7.org/fhir/ValueSet/clinical-findings&version=0.8
public <T extends Resource> T getCanonical(Class<T> resourceClass, String canonicalURL) {
ResourceRequest<T> result = null;
try {
result = utils.issueGetResourceRequest(resourceAddress.resolveGetUriFromResourceClassAndCanonical(resourceClass, canonicalURL), getPreferredResourceFormat(), TIMEOUT_NORMAL);
// gone
result.addErrorStatus(410);
// unknown
result.addErrorStatus(404);
// unknown
result.addErrorStatus(405);
// Only one for now
result.addSuccessStatus(200);
if (result.isUnsuccessfulRequest()) {
throw new EFhirClientException("Server returned error code " + result.getHttpStatus(), (OperationOutcome) result.getPayload());
}
} catch (Exception e) {
handleException("An error has occurred while trying to read this version of the resource", e);
}
Bundle bnd = (Bundle) result.getPayload();
if (bnd.getEntry().size() == 0)
throw new EFhirClientException("No matching resource found for canonical URL '" + canonicalURL + "'");
if (bnd.getEntry().size() > 1)
throw new EFhirClientException("Multiple matching resources found for canonical URL '" + canonicalURL + "'");
return (T) bnd.getEntry().get(0).getResource();
}
Aggregations