Search in sources :

Example 46 with EFhirClientException

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

Example 48 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 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();
}
Also used : Header(org.apache.http.Header) OperationOutcome(org.hl7.fhir.dstu2016may.model.OperationOutcome) Resource(org.hl7.fhir.dstu2016may.model.Resource) URISyntaxException(java.net.URISyntaxException)

Example 49 with EFhirClientException

use of org.hl7.fhir.r4b.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();
}
Also used : Parameters(org.hl7.fhir.dstu2016may.model.Parameters) Header(org.apache.http.Header) Resource(org.hl7.fhir.dstu2016may.model.Resource) ValueSet(org.hl7.fhir.dstu2016may.model.ValueSet)

Example 50 with EFhirClientException

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

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