Search in sources :

Example 86 with Server

use of org.hl7.gravity.refimpl.sdohexchange.model.Server 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.dstu2.model.OperationOutcome) Resource(org.hl7.fhir.dstu2.model.Resource) URISyntaxException(java.net.URISyntaxException)

Example 87 with Server

use of org.hl7.gravity.refimpl.sdohexchange.model.Server 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.dstu2.model.Parameters) Header(org.apache.http.Header) StringType(org.hl7.fhir.dstu2.model.StringType) Resource(org.hl7.fhir.dstu2.model.Resource) ValueSet(org.hl7.fhir.dstu2.model.ValueSet)

Example 88 with Server

use of org.hl7.gravity.refimpl.sdohexchange.model.Server 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 89 with Server

use of org.hl7.gravity.refimpl.sdohexchange.model.Server 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 90 with Server

use of org.hl7.gravity.refimpl.sdohexchange.model.Server 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)

Aggregations

Test (org.junit.Test)107 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)100 IOException (java.io.IOException)47 OperationOutcome (org.hl7.fhir.r4.model.OperationOutcome)42 FHIRException (org.hl7.fhir.exceptions.FHIRException)39 OperationOutcome (org.hl7.fhir.dstu3.model.OperationOutcome)38 ArrayList (java.util.ArrayList)25 BaseFhirIntegrationTest (org.openmrs.module.fhir2.BaseFhirIntegrationTest)24 FileNotFoundException (java.io.FileNotFoundException)21 TerminologyServiceException (org.hl7.fhir.exceptions.TerminologyServiceException)19 Date (java.util.Date)18 Bundle (org.hl7.fhir.r4.model.Bundle)17 URISyntaxException (java.net.URISyntaxException)16 HashMap (java.util.HashMap)16 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)16 FhirContext (ca.uhn.fhir.context.FhirContext)14 LocalDate (java.time.LocalDate)14 NoTerminologyServiceException (org.hl7.fhir.exceptions.NoTerminologyServiceException)13 File (java.io.File)12 Header (org.apache.http.Header)11