Search in sources :

Example 26 with EFhirClientException

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

the class FhirRequestBuilder method unmarshalReference.

/**
 * Unmarshalls a resource from the response stream.
 */
@SuppressWarnings("unchecked")
protected <T extends Resource> T unmarshalReference(Response response, String format) {
    T resource = null;
    OperationOutcome error = null;
    if (response.body() != null) {
        try {
            byte[] body = response.body().bytes();
            resource = (T) getParser(format).parse(body);
            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.r5.model.OperationOutcome) IOException(java.io.IOException) EFhirClientException(org.hl7.fhir.r5.utils.client.EFhirClientException) EFhirClientException(org.hl7.fhir.r5.utils.client.EFhirClientException) IOException(java.io.IOException)

Example 27 with EFhirClientException

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

the class FhirRequestBuilder method unmarshalFeed.

/**
 * Unmarshalls Bundle from response stream.
 */
protected Bundle unmarshalFeed(Response response, String format) {
    Bundle feed = null;
    OperationOutcome error = null;
    try {
        byte[] body = response.body().bytes();
        String contentType = response.header("Content-Type");
        if (body != null) {
            if (contentType.contains(ResourceFormat.RESOURCE_XML.getHeader()) || contentType.contains("text/xml+fhir")) {
                Resource rf = getParser(format).parse(body);
                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.r5.model.Bundle) OperationOutcome(org.hl7.fhir.r5.model.OperationOutcome) Resource(org.hl7.fhir.r5.model.Resource) IOException(java.io.IOException) EFhirClientException(org.hl7.fhir.r5.utils.client.EFhirClientException) EFhirClientException(org.hl7.fhir.r5.utils.client.EFhirClientException) IOException(java.io.IOException)

Example 28 with EFhirClientException

use of org.hl7.fhir.r5.utils.client.EFhirClientException in project kindling by HL7.

the class BuildWorkerContext method lookupLoinc.

private String lookupLoinc(String code) throws Exception {
    if (true) {
        // (!triedServer || serverOk) {
        try {
            triedServer = true;
            // for this, we use the FHIR client
            if (txClient == null) {
                txClient = new TerminologyClientR5(tsServer);
                this.txLog = new HTMLClientLogger(null);
            }
            Map<String, String> params = new HashMap<String, String>();
            params.put("code", code);
            params.put("system", "http://loinc.org");
            Parameters result = txClient.lookupCode(params);
            for (ParametersParameterComponent p : result.getParameter()) {
                if (p.getName().equals("display"))
                    return ((StringType) p.getValue()).asStringValue();
            }
            throw new Exception("Did not find LOINC code in return values");
        } catch (EFhirClientException e) {
            serverOk = true;
            throw e;
        } catch (Exception e) {
            serverOk = false;
            throw e;
        }
    } else
        throw new Exception("Server is not available");
}
Also used : TerminologyClientR5(org.hl7.fhir.convertors.txClient.TerminologyClientR5) Parameters(org.hl7.fhir.r5.model.Parameters) HashMap(java.util.HashMap) HTMLClientLogger(org.hl7.fhir.r5.context.HTMLClientLogger) UcumException(org.fhir.ucum.UcumException) TerminologyServiceException(org.hl7.fhir.exceptions.TerminologyServiceException) FileNotFoundException(java.io.FileNotFoundException) SAXException(org.xml.sax.SAXException) DefinitionException(org.hl7.fhir.exceptions.DefinitionException) EFhirClientException(org.hl7.fhir.r5.utils.client.EFhirClientException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) FHIRException(org.hl7.fhir.exceptions.FHIRException) EFhirClientException(org.hl7.fhir.r5.utils.client.EFhirClientException) ParametersParameterComponent(org.hl7.fhir.r5.model.Parameters.ParametersParameterComponent)

Example 29 with EFhirClientException

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

the class FhirRequestBuilder method unmarshalFeed.

/**
 * Unmarshalls Bundle from response stream.
 */
protected Bundle unmarshalFeed(Response response, String format) {
    Bundle feed = null;
    OperationOutcome error = null;
    try {
        byte[] body = response.body().bytes();
        String contentType = response.header("Content-Type");
        if (body != null) {
            if (contentType.contains(ResourceFormat.RESOURCE_XML.getHeader()) || contentType.contains("text/xml+fhir")) {
                Resource rf = getParser(format).parse(body);
                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.r4b.model.Bundle) OperationOutcome(org.hl7.fhir.r4b.model.OperationOutcome) Resource(org.hl7.fhir.r4b.model.Resource) IOException(java.io.IOException) EFhirClientException(org.hl7.fhir.r4b.utils.client.EFhirClientException) EFhirClientException(org.hl7.fhir.r4b.utils.client.EFhirClientException) IOException(java.io.IOException)

Example 30 with EFhirClientException

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

the class FHIRToolingClient method operateType.

public <T extends Resource> Parameters operateType(Class<T> resourceClass, String name, Parameters params) {
    boolean complex = false;
    for (ParametersParameterComponent p : params.getParameter()) complex = complex || !(p.getValue() instanceof PrimitiveType);
    String ps = "";
    try {
        if (!complex)
            for (ParametersParameterComponent p : params.getParameter()) if (p.getValue() instanceof PrimitiveType)
                ps += p.getName() + "=" + Utilities.encodeUri(((PrimitiveType) p.getValue()).asStringValue()) + "&";
        ResourceRequest<T> result;
        URI url = resourceAddress.resolveOperationURLFromClass(resourceClass, name, ps);
        if (complex) {
            byte[] body = ByteUtils.resourceToByteArray(params, false, isJson(getPreferredResourceFormat()));
            if (client.getLogger() != null) {
                client.getLogger().logRequest("POST", url.toString(), null, body);
            }
            result = client.issuePostRequest(url, body, getPreferredResourceFormat(), generateHeaders(), "POST " + resourceClass.getName() + "/$" + name, TIMEOUT_OPERATION_LONG);
        } else {
            if (client.getLogger() != null) {
                client.getLogger().logRequest("GET", url.toString(), null, null);
            }
            result = client.issueGetResourceRequest(url, getPreferredResourceFormat(), generateHeaders(), "GET " + resourceClass.getName() + "/$" + name, TIMEOUT_OPERATION_LONG);
        }
        if (result.isUnsuccessfulRequest()) {
            throw new EFhirClientException("Server returned error code " + result.getHttpStatus(), (OperationOutcome) result.getPayload());
        }
        if (result.getPayload() instanceof Parameters) {
            return (Parameters) result.getPayload();
        } else {
            Parameters p_out = new Parameters();
            p_out.addParameter().setName("return").setResource(result.getPayload());
            return p_out;
        }
    } catch (Exception e) {
        handleException("Error performing tx3 operation '" + name + ": " + e.getMessage() + "' (parameters = \"" + ps + "\")", e);
    }
    return null;
}
Also used : URI(java.net.URI) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) FHIRException(org.hl7.fhir.exceptions.FHIRException) ParametersParameterComponent(org.hl7.fhir.dstu3.model.Parameters.ParametersParameterComponent)

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