Search in sources :

Example 21 with EFhirClientException

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

the class Client method issuePostRequest.

public <T extends Resource> ResourceRequest<T> issuePostRequest(URI resourceUri, byte[] payload, String resourceFormat, Headers headers, String message, long timeout) throws IOException {
    if (payload == null)
        throw new EFhirClientException("POST requests require a non-null payload");
    this.payload = payload;
    RequestBody body = RequestBody.create(MediaType.parse(resourceFormat + ";charset=" + DEFAULT_CHARSET), payload);
    Request.Builder request = new Request.Builder().url(resourceUri.toURL()).post(body);
    return executeFhirRequest(request, resourceFormat, headers, message, retryCount, timeout);
}
Also used : Request(okhttp3.Request) EFhirClientException(org.hl7.fhir.r5.utils.client.EFhirClientException) RequestBody(okhttp3.RequestBody)

Example 22 with EFhirClientException

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

the class Client method issuePutRequest.

public <T extends Resource> ResourceRequest<T> issuePutRequest(URI resourceUri, byte[] payload, String resourceFormat, Headers headers, String message, long timeout) throws IOException {
    if (payload == null)
        throw new EFhirClientException("PUT requests require a non-null payload");
    this.payload = payload;
    RequestBody body = RequestBody.create(payload);
    Request.Builder request = new Request.Builder().url(resourceUri.toURL()).put(body);
    return executeFhirRequest(request, resourceFormat, headers, message, retryCount, timeout);
}
Also used : Request(okhttp3.Request) EFhirClientException(org.hl7.fhir.r5.utils.client.EFhirClientException) RequestBody(okhttp3.RequestBody)

Example 23 with EFhirClientException

use of org.hl7.fhir.r4b.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);
}
Also used : Request(okhttp3.Request) EFhirClientException(org.hl7.fhir.r5.utils.client.EFhirClientException) RequestBody(okhttp3.RequestBody)

Example 24 with EFhirClientException

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

the class Client method issuePutRequest.

public <T extends Resource> ResourceRequest<T> issuePutRequest(URI resourceUri, byte[] payload, String resourceFormat, Headers headers, String message, long timeout) throws IOException {
    if (payload == null)
        throw new EFhirClientException("PUT requests require a non-null payload");
    RequestBody body = RequestBody.create(payload);
    Request.Builder request = new Request.Builder().url(resourceUri.toURL()).put(body);
    return executeFhirRequest(request, resourceFormat, headers, message, retryCount, timeout);
}
Also used : Request(okhttp3.Request) EFhirClientException(org.hl7.fhir.r4.utils.client.EFhirClientException) RequestBody(okhttp3.RequestBody)

Example 25 with EFhirClientException

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

the class ByteUtils method resourceToByteArray.

public static <T extends Resource> byte[] resourceToByteArray(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 ? IParser.OutputStyle.PRETTY : IParser.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.r5.formats.XmlParser) ByteArrayOutputStream(java.io.ByteArrayOutputStream) EFhirClientException(org.hl7.fhir.r5.utils.client.EFhirClientException) IOException(java.io.IOException) EFhirClientException(org.hl7.fhir.r5.utils.client.EFhirClientException) IParser(org.hl7.fhir.r5.formats.IParser) JsonParser(org.hl7.fhir.r5.formats.JsonParser)

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