Search in sources :

Example 6 with EFhirClientException

use of org.hl7.fhir.r4b.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.dstu2.model.OperationOutcome) URISyntaxException(java.net.URISyntaxException)

Example 7 with EFhirClientException

use of org.hl7.fhir.r4b.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();
}
Also used : Parameters(org.hl7.fhir.dstu2.model.Parameters) Header(org.apache.http.Header) StringType(org.hl7.fhir.dstu2.model.StringType) HashMap(java.util.HashMap) Resource(org.hl7.fhir.dstu2.model.Resource) ConceptMap(org.hl7.fhir.dstu2.model.ConceptMap)

Example 8 with EFhirClientException

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

the class ClientUtils method getFeedAsByteArray.

public byte[] getFeedAsByteArray(Bundle feed, 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, feed);
        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;
}
Also used : XmlParser(org.hl7.fhir.dstu2016may.formats.XmlParser) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ParseException(java.text.ParseException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) IParser(org.hl7.fhir.dstu2016may.formats.IParser) JsonParser(org.hl7.fhir.dstu2016may.formats.JsonParser)

Example 9 with EFhirClientException

use of org.hl7.fhir.r4b.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;
}
Also used : OperationOutcome(org.hl7.fhir.dstu2016may.model.OperationOutcome) IOException(java.io.IOException) ParseException(java.text.ParseException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException)

Example 10 with EFhirClientException

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

the class ClientUtils method unmarshalFeed.

/**
 * Unmarshals Bundle from response stream.
 *
 * @param response
 * @return
 */
protected Bundle unmarshalFeed(HttpResponse response, String format) {
    Bundle feed = null;
    byte[] cnt = log(response);
    String contentType = response.getHeaders("Content-Type")[0].getValue();
    OperationOutcome error = null;
    try {
        if (cnt != null) {
            if (contentType.contains(ResourceFormat.RESOURCE_XML.getHeader()) || contentType.contains("text/xml+fhir")) {
                Resource rf = getParser(format).parse(cnt);
                if (rf instanceof Bundle)
                    feed = (Bundle) rf;
                else if (rf instanceof OperationOutcome && hasError((OperationOutcome) rf)) {
                    error = (OperationOutcome) rf;
                } else {
                    throw new EFhirClientException("Error reading server response: a resource was returned instead");
                }
            }
        }
    } catch (IOException ioe) {
        throw new EFhirClientException("Error reading Http Response", ioe);
    } catch (Exception e) {
        throw new EFhirClientException("Error parsing response message", e);
    }
    if (error != null) {
        throw new EFhirClientException("Error from server: " + ResourceUtilities.getErrorDescription(error), error);
    }
    return feed;
}
Also used : Bundle(org.hl7.fhir.dstu2016may.model.Bundle) OperationOutcome(org.hl7.fhir.dstu2016may.model.OperationOutcome) Resource(org.hl7.fhir.dstu2016may.model.Resource) IOException(java.io.IOException) ParseException(java.text.ParseException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException)

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