ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException Java Examples

The following examples show how to use ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: EnableWhenEvaluator.java    From org.hl7.fhir.core with Apache License 2.0 6 votes vote down vote up
protected boolean evaluateAnswer(Element answer, DataType expectedAnswer, QuestionnaireItemOperator questionnaireItemOperator) {
  DataType actualAnswer;
  if (isExtension(answer)) {
    return false;
  }
  try {
    actualAnswer = convertToType(answer);
    if (actualAnswer == null) {
      return false;
    }
  } catch (FHIRException e) {
    throw new UnprocessableEntityException("Unexpected answer type", e);
  }
  if (!actualAnswer.getClass().equals(expectedAnswer.getClass())) {
    throw new UnprocessableEntityException("Expected answer and actual answer have incompatible types");
  }
  if (expectedAnswer instanceof Coding) {
    return compareCodingAnswer((Coding) expectedAnswer, (Coding) actualAnswer, questionnaireItemOperator);
  } else if ((expectedAnswer instanceof PrimitiveType)) {
    return comparePrimitiveAnswer((PrimitiveType<?>) actualAnswer, (PrimitiveType<?>) expectedAnswer, questionnaireItemOperator);
  } else if (expectedAnswer instanceof Quantity) {
    return compareQuantityAnswer((Quantity) actualAnswer, (Quantity) expectedAnswer, questionnaireItemOperator);
  }
  // TODO: Attachment, reference?
  throw new UnprocessableEntityException("Unimplemented answer type: " + expectedAnswer.getClass());
}
 
Example #2
Source File: EnableWhenEvaluator.java    From org.hl7.fhir.core with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({"rawtypes", "unchecked"})
private boolean compareComparable(Comparable actual, Comparable expected,
                                  QuestionnaireItemOperator questionnaireItemOperator) {
  int result = actual.compareTo(expected);

  if (questionnaireItemOperator == QuestionnaireItemOperator.EQUAL) {
    return result == 0;
  } else if (questionnaireItemOperator == QuestionnaireItemOperator.NOT_EQUAL) {
    return result != 0;
  } else if (questionnaireItemOperator == QuestionnaireItemOperator.GREATER_OR_EQUAL) {
    return result >= 0;
  } else if (questionnaireItemOperator == QuestionnaireItemOperator.LESS_OR_EQUAL) {
    return result <= 0;
  } else if (questionnaireItemOperator == QuestionnaireItemOperator.LESS_THAN) {
    return result < 0;
  } else if (questionnaireItemOperator == QuestionnaireItemOperator.GREATER_THAN) {
    return result > 0;
  }

  throw new UnprocessableEntityException("Bad operator for PrimitiveType comparison: " + questionnaireItemOperator.toCode());

}
 
Example #3
Source File: EnableWhenEvaluator.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
protected EnableWhenResult evaluateCondition(QuestionnaireItemEnableWhenComponent enableCondition, QuestionnaireItemComponent qitem, QStack qstack) {
  List<Element> answerItems = findQuestionAnswers(qstack, qitem, enableCondition);
  QuestionnaireItemOperator operator = enableCondition.getOperator();
  if (operator == QuestionnaireItemOperator.EXISTS) {
    DataType answer = enableCondition.getAnswer();
    if (!(answer instanceof BooleanType)) {
      throw new UnprocessableEntityException("Exists-operator requires answerBoolean");
    }
    return new EnableWhenResult(((BooleanType) answer).booleanValue() != answerItems.isEmpty(), enableCondition);
  }
  boolean result = answerItems
    .stream()
    .anyMatch(answer -> evaluateAnswer(answer, enableCondition.getAnswer(), enableCondition.getOperator()));
  return new EnableWhenResult(result, enableCondition);
}
 
Example #4
Source File: EnableWhenEvaluator.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
private boolean comparePrimitiveAnswer(PrimitiveType<?> actualAnswer, PrimitiveType<?> expectedAnswer, QuestionnaireItemOperator questionnaireItemOperator) {
  if (actualAnswer.getValue() instanceof Comparable) {
    return compareComparable((Comparable<?>) actualAnswer.getValue(), (Comparable<?>) expectedAnswer.getValue(), questionnaireItemOperator);
  } else if (questionnaireItemOperator == QuestionnaireItemOperator.EQUAL) {
    return actualAnswer.equalsShallow(expectedAnswer);
  } else if (questionnaireItemOperator == QuestionnaireItemOperator.NOT_EQUAL) {
    return !actualAnswer.equalsShallow(expectedAnswer);
  }
  throw new UnprocessableEntityException("Bad operator for PrimitiveType comparison");
}
 
Example #5
Source File: EnableWhenEvaluator.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
private boolean compareCodingAnswer(Coding expectedAnswer, Coding actualAnswer, QuestionnaireItemOperator questionnaireItemOperator) {
  boolean result = compareSystems(expectedAnswer, actualAnswer) && compareCodes(expectedAnswer, actualAnswer);
  if (questionnaireItemOperator == QuestionnaireItemOperator.EQUAL) {
    return result == true;
  } else if (questionnaireItemOperator == QuestionnaireItemOperator.NOT_EQUAL) {
    return result == false;
  }
  throw new UnprocessableEntityException("Bad operator for Coding comparison");
}
 
Example #6
Source File: PatientResourceProvider.java    From fhirstarters with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * This method just provides simple business validation for resources we are storing.
 *
 * @param thePatient The patient to validate
 */
private void validateResource(Patient thePatient) {
   /*
    * Our server will have a rule that patients must have a family name or we will reject them
    */
   if (thePatient.getNameFirstRep().getFamily().isEmpty()) {
      OperationOutcome outcome = new OperationOutcome();
      outcome.addIssue().setSeverity(IssueSeverity.FATAL).setDiagnostics("No family name provided, Patient resources must have at least one family name.");
      throw new UnprocessableEntityException(FhirContext.forDstu3(), outcome);
   }
}
 
Example #7
Source File: CCRequestValidatingInterceptor.java    From careconnect-reference-implementation with Apache License 2.0 4 votes vote down vote up
public boolean incomingRequestPostProcessed(RequestDetails theRequestDetails, HttpServletRequest theRequest, HttpServletResponse theResponse) throws AuthenticationException {
    EncodingEnum encoding = RestfulServerUtils.determineRequestEncodingNoDefault(theRequestDetails);

    if (encoding == null || theRequestDetails.getOperation() != null) {
        log.trace("Incoming request does not appear to be FHIR, not going to validate");
        return true;
    } else {
        Charset charset = ResourceParameter.determineRequestCharset(theRequestDetails);
        String requestText = new String(theRequestDetails.loadRequestContents(), charset);
        if (StringUtils.isBlank(requestText)) {
            log.trace("Incoming request does not have a body");
            return true;
        } else {
            //log.info(theRequest.getMethod());
            if ((theRequest.getMethod().equals("POST") && !theRequest.getRequestURI().contains("$validate") ) || theRequest.getMethod().equals("PUT")) {


                IBaseResource resource = null;
                switch (encoding) {
                    case JSON:
                        resource = ctx.newJsonParser().parseResource(requestText);
                        break;
                    case XML:
                        resource = ctx.newXmlParser().parseResource(requestText);
                        break;
                }
                if (resource instanceof Bundle) {
                    Bundle bundle = (Bundle) resource;
                    for (Bundle.BundleEntryComponent entry : bundle.getEntry()) {
                       entry.setResource((Resource) setProfile(entry.getResource()));
                    }
                } else {
                    resource = setProfile(resource);
                }

                // Should not need to convert in the interceptor.


                try {

                    results = this.fhirValidator.validateWithResult(resource);
                } catch (Exception val) {
                    log.error(val.getMessage());
                    return true;
                }

                //OperationOutcome outcomeR4 = ;

                OperationOutcome outcome = null;
                if (results.toOperationOutcome() instanceof org.hl7.fhir.r4.model.OperationOutcome) {OperationOutcomeFactory.removeUnsupportedIssues((org.hl7.fhir.r4.model.OperationOutcome) results.toOperationOutcome(), null);
                    outcome = OperationOutcomeFactory.removeUnsupportedIssues((org.hl7.fhir.r4.model.OperationOutcome) results.toOperationOutcome(), null);
                } else {
                    outcome = OperationOutcomeFactory.removeUnsupportedIssues((OperationOutcome) results.toOperationOutcome());
                }


                if (!pass(outcome)) {
                    log.info("VALIDATION FAILED");
                    System.out.println(ctx.newXmlParser().setPrettyPrint(true).encodeResourceToString(outcome));
                    throw new UnprocessableEntityException(theRequestDetails.getServer().getFhirContext(), outcome);
                }
            }
            return true;
        }
    }
}