Search in sources :

Example 6 with Validated

use of org.hl7.gravity.refimpl.sdohexchange.dto.Validated in project org.hl7.fhir.core by hapifhir.

the class InstanceValidator method startInner.

public void startInner(ValidatorHostContext hostContext, List<ValidationMessage> errors, Element resource, Element element, StructureDefinition defn, NodeStack stack, boolean checkSpecials) {
    // the first piece of business is to see if we've validated this resource against this profile before.
    // if we have (*or if we still are*), then we'll just return our existing errors
    ResourceValidationTracker resTracker = getResourceTracker(element);
    List<ValidationMessage> cachedErrors = resTracker.getOutcomes(defn);
    if (cachedErrors != null) {
        for (ValidationMessage vm : cachedErrors) {
            if (!errors.contains(vm)) {
                errors.add(vm);
            }
        }
        return;
    }
    if (rule(errors, IssueType.STRUCTURE, element.line(), element.col(), stack.getLiteralPath(), defn.hasSnapshot(), I18nConstants.VALIDATION_VAL_PROFILE_NOSNAPSHOT, defn.getUrl())) {
        List<ValidationMessage> localErrors = new ArrayList<ValidationMessage>();
        resTracker.startValidating(defn);
        trackUsage(defn, hostContext, element);
        validateElement(hostContext, localErrors, defn, defn.getSnapshot().getElement().get(0), null, null, resource, element, element.getName(), stack, false, true, null);
        resTracker.storeOutcomes(defn, localErrors);
        for (ValidationMessage vm : localErrors) {
            if (!errors.contains(vm)) {
                errors.add(vm);
            }
        }
    }
    if (checkSpecials) {
        checkSpecials(hostContext, errors, element, stack, checkSpecials);
        validateResourceRules(errors, element, stack);
    }
}
Also used : ValidationMessage(org.hl7.fhir.utilities.validation.ValidationMessage) ResourceValidationTracker(org.hl7.fhir.validation.instance.utils.ResourceValidationTracker) ArrayList(java.util.ArrayList)

Example 7 with Validated

use of org.hl7.gravity.refimpl.sdohexchange.dto.Validated in project org.hl7.fhir.core by hapifhir.

the class SHCParser method parse.

public List<NamedElement> parse(InputStream stream) throws IOException, FHIRFormatError, DefinitionException, FHIRException {
    List<NamedElement> res = new ArrayList<>();
    String src = TextFile.streamToString(stream).trim();
    List<String> list = new ArrayList<>();
    String pfx = null;
    if (src.startsWith("{")) {
        JsonObject json = JsonTrackingParser.parseJson(src);
        if (checkProperty(json, "$", "verifiableCredential", true, "Array")) {
            pfx = "verifiableCredential";
            JsonArray arr = json.getAsJsonArray("verifiableCredential");
            int i = 0;
            for (JsonElement e : arr) {
                if (!(e instanceof JsonPrimitive)) {
                    logError(line(e), col(e), "$.verifiableCredential[" + i + "]", IssueType.STRUCTURE, "Wrong Property verifiableCredential in JSON Payload. Expected : String but found " + JSONUtil.type(e), IssueSeverity.ERROR);
                } else {
                    list.add(e.getAsString());
                }
                i++;
            }
        } else {
            return res;
        }
    } else {
        list.add(src);
    }
    int c = 0;
    for (String ssrc : list) {
        String prefix = pfx == null ? "" : pfx + "[" + Integer.toString(c) + "].";
        c++;
        JWT jwt = null;
        try {
            jwt = decodeJWT(ssrc);
        } catch (Exception e) {
            logError(1, 1, prefix + "JWT", IssueType.INVALID, "Unable to decode JWT token", IssueSeverity.ERROR);
            return res;
        }
        map = jwt.map;
        checkNamedProperties(jwt.getPayload(), prefix + "payload", "iss", "nbf", "vc");
        checkProperty(jwt.getPayload(), prefix + "payload", "iss", true, "String");
        logError(1, 1, prefix + "JWT", IssueType.INFORMATIONAL, "The FHIR Validator does not check the JWT signature " + "(see https://demo-portals.smarthealth.cards/VerifierPortal.html or https://github.com/smart-on-fhir/health-cards-dev-tools) (Issuer = '" + jwt.getPayload().get("iss").getAsString() + "')", IssueSeverity.INFORMATION);
        checkProperty(jwt.getPayload(), prefix + "payload", "nbf", true, "Number");
        JsonObject vc = jwt.getPayload().getAsJsonObject("vc");
        if (vc == null) {
            logError(1, 1, "JWT", IssueType.STRUCTURE, "Unable to find property 'vc' in the payload", IssueSeverity.ERROR);
            return res;
        }
        String path = prefix + "payload.vc";
        checkNamedProperties(vc, path, "type", "credentialSubject");
        if (!checkProperty(vc, path, "type", true, "Array")) {
            return res;
        }
        JsonArray type = vc.getAsJsonArray("type");
        int i = 0;
        for (JsonElement e : type) {
            if (!(e instanceof JsonPrimitive)) {
                logError(line(e), col(e), path + ".type[" + i + "]", IssueType.STRUCTURE, "Wrong Property Type in JSON Payload. Expected : String but found " + JSONUtil.type(e), IssueSeverity.ERROR);
            } else {
                types.add(e.getAsString());
            }
            i++;
        }
        if (!types.contains("https://smarthealth.cards#health-card")) {
            logError(line(vc), col(vc), path, IssueType.STRUCTURE, "Card does not claim to be of type https://smarthealth.cards#health-card, cannot validate", IssueSeverity.ERROR);
            return res;
        }
        if (!checkProperty(vc, path, "credentialSubject", true, "Object")) {
            return res;
        }
        JsonObject cs = vc.getAsJsonObject("credentialSubject");
        path = path + ".credentialSubject";
        if (!checkProperty(cs, path, "fhirVersion", true, "String")) {
            return res;
        }
        JsonElement fv = cs.get("fhirVersion");
        if (!VersionUtilities.versionsCompatible(context.getVersion(), fv.getAsString())) {
            logError(line(fv), col(fv), path + ".fhirVersion", IssueType.STRUCTURE, "Card claims to be of version " + fv.getAsString() + ", cannot be validated against version " + context.getVersion(), IssueSeverity.ERROR);
            return res;
        }
        if (!checkProperty(cs, path, "fhirBundle", true, "Object")) {
            return res;
        }
        // ok. all checks passed, we can now validate the bundle
        Element e = jsonParser.parse(cs.getAsJsonObject("fhirBundle"), map);
        if (e != null) {
            res.add(new NamedElement(path, e));
        }
    }
    return res;
}
Also used : JsonPrimitive(com.google.gson.JsonPrimitive) JsonElement(com.google.gson.JsonElement) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) DefinitionException(org.hl7.fhir.exceptions.DefinitionException) DataFormatException(java.util.zip.DataFormatException) IOException(java.io.IOException) FHIRException(org.hl7.fhir.exceptions.FHIRException) JsonArray(com.google.gson.JsonArray) JsonElement(com.google.gson.JsonElement)

Example 8 with Validated

use of org.hl7.gravity.refimpl.sdohexchange.dto.Validated in project fhir-bridge by ehrbase.

the class FhirProfileValidator method process.

@Override
public void process(Exchange exchange) {
    Resource resource = exchange.getIn().getBody(Resource.class);
    LOG.trace("Validating {} resource...", resource.getResourceType());
    List<String> profiles = Resources.getProfileUris(resource);
    if (profiles.isEmpty()) {
        validateDefault(resource, exchange);
    } else {
        validateProfiles(resource, exchange);
    }
    LOG.info("{} resource validated", resource.getResourceType());
}
Also used : Resource(org.hl7.fhir.r4.model.Resource)

Example 9 with Validated

use of org.hl7.gravity.refimpl.sdohexchange.dto.Validated in project Gravity-SDOH-Exchange-RI by FHIR.

the class ResourceService method getTaskResources.

public TaskJsonResourcesDto getTaskResources(String id) {
    // Getting task by id with Patient, requester Organization and ServiceRequest
    Bundle taskBundle = cpClient.search().forResource(Task.class).where(Task.RES_ID.exactly().code(id)).include(Task.INCLUDE_FOCUS).include(Task.INCLUDE_PATIENT).include(Task.INCLUDE_REQUESTER).returnBundle(Bundle.class).execute();
    Task task = FhirUtil.getFromBundle(taskBundle, Task.class).stream().findFirst().orElseThrow(() -> new ResourceNotFoundException(new IdType(Task.class.getSimpleName(), id)));
    // We expect that task was validated and contains all resources
    ServiceRequest serviceRequest = FhirUtil.getFirstFromBundle(taskBundle, ServiceRequest.class);
    Patient patient = FhirUtil.getFirstFromBundle(taskBundle, Patient.class);
    Organization requester = FhirUtil.getFirstFromBundle(taskBundle, Organization.class);
    // Load all Task Procedures and ServiceRequest required resources as one transaction
    Map<Class<? extends Resource>, List<Resource>> loadedResources = resourceLoader.getResources(cpClient, collectAllReferences(task, serviceRequest));
    TaskJsonResourcesDto resourcesDto = new TaskJsonResourcesDto();
    resourcesDto.setTask(resourceParser.parse(task));
    resourcesDto.setServiceRequest(resourceParser.parse(serviceRequest));
    resourcesDto.setPatient(resourceParser.parse(patient));
    resourcesDto.setRequester(resourceParser.parse(requester));
    resourcesDto.setConsent(resourceParser.parse(loadedResources.get(Consent.class)).stream().findFirst().orElse(null));
    resourcesDto.setConditions(resourceParser.parse(loadedResources.get(Condition.class)));
    resourcesDto.setGoals(resourceParser.parse(loadedResources.get(Goal.class)));
    resourcesDto.setProcedures(resourceParser.parse(loadedResources.get(Procedure.class)));
    return resourcesDto;
}
Also used : Task(org.hl7.fhir.r4.model.Task) Organization(org.hl7.fhir.r4.model.Organization) Bundle(org.hl7.fhir.r4.model.Bundle) Resource(org.hl7.fhir.r4.model.Resource) Patient(org.hl7.fhir.r4.model.Patient) ServiceRequest(org.hl7.fhir.r4.model.ServiceRequest) IdType(org.hl7.fhir.r4.model.IdType) TaskJsonResourcesDto(org.hl7.gravity.refimpl.sdohexchange.dto.response.TaskJsonResourcesDto) Consent(org.hl7.fhir.r4.model.Consent) ArrayList(java.util.ArrayList) List(java.util.List) ResourceNotFoundException(ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException)

Example 10 with Validated

use of org.hl7.gravity.refimpl.sdohexchange.dto.Validated in project Gravity-SDOH-Exchange-RI by FHIR.

the class ResourceService method getTaskResources.

public TaskJsonResourcesDto getTaskResources(Integer serverId, String taskId) {
    Server server = serverRepository.findById(serverId).orElseThrow(() -> new ServerNotFoundException(String.format("No server was found by id '%s'", serverId)));
    IGenericClient fhirClient = fhirContext.newRestfulGenericClient(server.getFhirServerUrl());
    // Doesn't support now
    // fhirClient.registerInterceptor(new BearerTokenAuthInterceptor(
    // authorizationClient.getTokenResponse(URI.create(server.getAuthServerUrl()), server.getClientId(),
    // server.getClientSecret(), SCOPE)
    // .getAccessToken()));
    // Getting task by id with Patient, requester Organization and ServiceRequest
    Bundle taskBundle = fhirClient.search().forResource(Task.class).where(Task.RES_ID.exactly().code(taskId)).include(Task.INCLUDE_FOCUS).include(Task.INCLUDE_PATIENT).include(Task.INCLUDE_REQUESTER).returnBundle(Bundle.class).execute();
    Task task = FhirUtil.getFromBundle(taskBundle, Task.class).stream().findFirst().orElseThrow(() -> new ResourceNotFoundException(new IdType(Task.class.getSimpleName(), taskId)));
    // We expect that task was validated and contains all resources
    ServiceRequest serviceRequest = FhirUtil.getFirstFromBundle(taskBundle, ServiceRequest.class);
    Patient patient = FhirUtil.getFirstFromBundle(taskBundle, Patient.class);
    Organization requester = FhirUtil.getFirstFromBundle(taskBundle, Organization.class);
    // Load all Task Procedures and ServiceRequest required resources as one transaction
    Map<Class<? extends Resource>, List<Resource>> loadedResources = resourceLoader.getResources(fhirClient, collectAllReferences(task, serviceRequest));
    TaskJsonResourcesDto resourcesDto = new TaskJsonResourcesDto();
    resourcesDto.setTask(resourceParser.parse(task));
    resourcesDto.setServiceRequest(resourceParser.parse(serviceRequest));
    resourcesDto.setPatient(resourceParser.parse(patient));
    resourcesDto.setRequester(resourceParser.parse(requester));
    resourcesDto.setConsent(resourceParser.parse(loadedResources.get(Consent.class)).stream().findFirst().orElse(null));
    resourcesDto.setConditions(resourceParser.parse(loadedResources.get(Condition.class)));
    resourcesDto.setGoals(resourceParser.parse(loadedResources.get(Goal.class)));
    resourcesDto.setProcedures(resourceParser.parse(loadedResources.get(Procedure.class)));
    return resourcesDto;
}
Also used : Task(org.hl7.fhir.r4.model.Task) Organization(org.hl7.fhir.r4.model.Organization) Server(org.hl7.gravity.refimpl.sdohexchange.model.Server) IGenericClient(ca.uhn.fhir.rest.client.api.IGenericClient) Bundle(org.hl7.fhir.r4.model.Bundle) Resource(org.hl7.fhir.r4.model.Resource) Patient(org.hl7.fhir.r4.model.Patient) ServiceRequest(org.hl7.fhir.r4.model.ServiceRequest) IdType(org.hl7.fhir.r4.model.IdType) TaskJsonResourcesDto(org.hl7.gravity.refimpl.sdohexchange.dto.response.TaskJsonResourcesDto) Consent(org.hl7.fhir.r4.model.Consent) ArrayList(java.util.ArrayList) List(java.util.List) ServerNotFoundException(org.hl7.gravity.refimpl.sdohexchange.exception.ServerNotFoundException) ResourceNotFoundException(ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException)

Aggregations

ArrayList (java.util.ArrayList)7 FHIRException (org.hl7.fhir.exceptions.FHIRException)4 Resource (org.hl7.fhir.r4.model.Resource)4 IOException (java.io.IOException)3 List (java.util.List)3 ResourceNotFoundException (ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException)2 JsonArray (com.google.gson.JsonArray)2 JsonElement (com.google.gson.JsonElement)2 JsonObject (com.google.gson.JsonObject)2 JsonPrimitive (com.google.gson.JsonPrimitive)2 DataFormatException (java.util.zip.DataFormatException)2 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)2 Bundle (org.hl7.fhir.r4.model.Bundle)2 Consent (org.hl7.fhir.r4.model.Consent)2 IdType (org.hl7.fhir.r4.model.IdType)2 Organization (org.hl7.fhir.r4.model.Organization)2 Patient (org.hl7.fhir.r4.model.Patient)2 ServiceRequest (org.hl7.fhir.r4.model.ServiceRequest)2 Task (org.hl7.fhir.r4.model.Task)2 UriType (org.hl7.fhir.r5.model.UriType)2