Search in sources :

Example 51 with EFhirClientException

use of org.hl7.fhir.r5.utils.client.EFhirClientException in project org.hl7.fhir.core by hapifhir.

the class FHIRToolingClient method update.

public <T extends Resource> T update(Class<T> resourceClass, T resource, String id) {
    ResourceRequest<T> result = null;
    try {
        List<Header> headers = null;
        result = utils.issuePutRequest(resourceAddress.resolveGetUriFromResourceClassAndId(resourceClass, id), 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(resourceClass, resVersionedIdentifier.getId(), resVersionedIdentifier.getVersionId());
    } catch (ClassCastException e) {
    // if we fall throught we have the correct type already in the create
    }
    return result.getPayload();
}
Also used : Header(org.apache.http.Header) OperationOutcome(org.hl7.fhir.dstu2016may.model.OperationOutcome) URISyntaxException(java.net.URISyntaxException)

Example 52 with EFhirClientException

use of org.hl7.fhir.r5.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();
}
Also used : Bundle(org.hl7.fhir.dstu2016may.model.Bundle) URISyntaxException(java.net.URISyntaxException)

Aggregations

IOException (java.io.IOException)25 URISyntaxException (java.net.URISyntaxException)12 Request (okhttp3.Request)12 RequestBody (okhttp3.RequestBody)12 Header (org.apache.http.Header)11 MalformedURLException (java.net.MalformedURLException)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)8 UnsupportedEncodingException (java.io.UnsupportedEncodingException)8 ParseException (java.text.ParseException)8 Resource (org.hl7.fhir.dstu2016may.model.Resource)6 EFhirClientException (org.hl7.fhir.dstu3.utils.client.EFhirClientException)6 EFhirClientException (org.hl7.fhir.r4.utils.client.EFhirClientException)6 EFhirClientException (org.hl7.fhir.r4b.utils.client.EFhirClientException)6 EFhirClientException (org.hl7.fhir.r5.utils.client.EFhirClientException)6 HashMap (java.util.HashMap)5 OperationOutcome (org.hl7.fhir.dstu2.model.OperationOutcome)5 Resource (org.hl7.fhir.dstu2.model.Resource)5 OperationOutcome (org.hl7.fhir.dstu2016may.model.OperationOutcome)5 Parameters (org.hl7.fhir.dstu2016may.model.Parameters)5 FHIRException (org.hl7.fhir.exceptions.FHIRException)5