Search in sources :

Example 1 with DataFormatException

use of ca.uhn.fhir.parser.DataFormatException in project gpconnect-demonstrator by nhsconnect.

the class ValueSetValidator method findValueSet.

private ValueSet findValueSet(String systemUrl) {
    int valueSetNamePos = systemUrl.lastIndexOf("/") + 1;
    String valueSetFilename = String.format("%s.xml", systemUrl.substring(valueSetNamePos));
    ValueSet valSet = null;
    String xmlContent = null;
    if (fhirValueSetsCheckWebFirst == true) {
        xmlContent = readValueSetFromWeb(valueSetFilename);
    }
    if (xmlContent == null) {
        xmlContent = readValueSetFromDisk(valueSetFilename);
    }
    if (fhirValueSetsCheckWebFirst == false && xmlContent == null) {
        xmlContent = readValueSetFromWeb(valueSetFilename);
    }
    if (xmlContent != null) {
        try {
            FhirContext fhirCtx = FhirContext.forDstu3();
            IParser parser = fhirCtx.newXmlParser();
            valSet = parser.parseResource(ValueSet.class, xmlContent);
        } catch (DataFormatException ex) {
            LOG.error(String.format("Error parsing valueSetFilename: %s", valueSetFilename));
        }
    }
    if (valSet == null) {
        throw OperationOutcomeFactory.buildOperationOutcomeException(new UnprocessableEntityException(String.format("Could not find or parse Value Set [SystemUrl: %s] at: %s. See system log for details.", systemUrl, valueSetFilename)), SystemCode.REFERENCE_NOT_FOUND, IssueType.NOTFOUND);
    }
    return valSet;
}
Also used : UnprocessableEntityException(ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException) FhirContext(ca.uhn.fhir.context.FhirContext) DataFormatException(ca.uhn.fhir.parser.DataFormatException) ValueSet(org.hl7.fhir.dstu3.model.ValueSet) IParser(ca.uhn.fhir.parser.IParser)

Example 2 with DataFormatException

use of ca.uhn.fhir.parser.DataFormatException in project gpconnect-demonstrator by nhsconnect.

the class WebTokenFactory method jwtParseResourcesValidation.

/**
 * handles the second part of the JWT object - the payload of claims checks
 * for presence of mandatory items, absence of forbidden itsms and
 * validity of some json objects which will be converted to hapifhir
 * resource objects
 *
 * @param claimsJsonString
 */
private void jwtParseResourcesValidation(String claimsJsonString) {
    if (fhirJsonParser == null) {
        fhirJsonParser = FhirContext.forDstu3().newJsonParser().setParserErrorHandler(new StrictErrorHandler());
    }
    String thisClaim = null;
    try {
        JsonNode jsonNode = new ObjectMapper().readTree(claimsJsonString);
        // Check for json objects that are not allowed
        for (String claim : new String[] { // #170 requested_record is not allowed
        "requested_record" }) {
            if (jsonNode.get(claim) != null) {
                throwInvalidRequest400_BadRequestException(String.format("JWT claim %s should not be present", claim));
            }
        }
        // we now see if they can be converted to valid fhir resources
        for (String claim : new String[] { "requesting_practitioner", "requesting_device", "requesting_organization" }) {
            thisClaim = claim;
            if (jsonNode.get(claim) == null) {
                throwInvalidRequest400_BadRequestException(String.format("JWT required claim %s is not present", thisClaim));
            }
            // are these valid json objects also valid fhir resources?
            fhirJsonParser.parseResource(jsonNode.get(claim).toString());
        }
    } catch (DataFormatException e) {
        // NB This is a fhir exception not a jackson json parsing exception
        // TODO NB This is UnprocessableEntity is that correct?
        throwUnprocessableEntity422_BadRequestException(String.format("Invalid Resource claim %s in JWT (Not a valid Fhir Resource - %s)", thisClaim, e.getMessage()));
    } catch (IOException ex) {
        throwInvalidRequest400_BadRequestException(String.format("Unparsable JSON retrieving JWT claim %s", thisClaim));
    }
}
Also used : DataFormatException(ca.uhn.fhir.parser.DataFormatException) StrictErrorHandler(ca.uhn.fhir.parser.StrictErrorHandler) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 3 with DataFormatException

use of ca.uhn.fhir.parser.DataFormatException in project gpconnect-demonstrator by nhsconnect.

the class FhirRequestGenericIntercepter method preProcessOutgoingException.

/**
 * Listens for any exceptions thrown. In the case of invalid parameters, we
 * need to catch this and throw it as a UnprocessableEntityException.
 *
 * @param theRequestDetails
 * @param theException
 * @param theServletRequest
 * @return UnprocessableEntityException if a InvalidRequestException was
 * thrown.
 * @throws javax.servlet.ServletException
 */
@Override
public BaseServerResponseException preProcessOutgoingException(RequestDetails theRequestDetails, Throwable theException, HttpServletRequest theServletRequest) throws ServletException {
    LOG.info("Response Exception");
    LOG.info(theException.getMessage());
    LOG.info("stackTrace: ", theException);
    // how else to pick up on just the relevant exceptions!
    if (theException instanceof InvalidRequestException && theException.getMessage().contains("Invalid attribute value")) {
        return OperationOutcomeFactory.buildOperationOutcomeException(new UnprocessableEntityException(theException.getMessage()), SystemCode.INVALID_PARAMETER, IssueType.INVALID);
    }
    if (theException instanceof InvalidRequestException && theException.getMessage().contains("Unknown resource in URI")) {
        return OperationOutcomeFactory.buildOperationOutcomeException(new ResourceNotFoundException(theException.getMessage()), SystemCode.BAD_REQUEST, IssueType.INVALID);
    }
    if (theException instanceof InvalidRequestException && theException.getMessage().contains("Can not have multiple date range parameters for the same param ")) {
        return OperationOutcomeFactory.buildOperationOutcomeException(new UnprocessableEntityException(theException.getMessage()), SystemCode.INVALID_PARAMETER, IssueType.INVALID);
    }
    if (theException instanceof DataFormatException) {
        return OperationOutcomeFactory.buildOperationOutcomeException(new UnprocessableEntityException(theException.getMessage()), SystemCode.INVALID_PARAMETER, IssueType.INVALID);
    }
    if (theException instanceof MethodNotAllowedException && theException.getMessage().contains("request must use HTTP GET")) {
        return OperationOutcomeFactory.buildOperationOutcomeException(new UnprocessableEntityException(theException.getMessage()), SystemCode.BAD_REQUEST, IssueType.INVALID);
    }
    if (theException instanceof InvalidRequestException && theException.getMessage().startsWith("Failed to parse request body as JSON resource. Error was: ")) {
        // #250 422 INVALID_RESOURCE not 400 BAD_REQUEST
        return OperationOutcomeFactory.buildOperationOutcomeException(new UnprocessableEntityException(theException.getMessage()), SystemCode.INVALID_RESOURCE, IssueType.INVALID);
    }
    if (theException instanceof InvalidRequestException && theException.getMessage().startsWith("Invalid request: ")) {
        return OperationOutcomeFactory.buildOperationOutcomeException(new InvalidRequestException(theException.getMessage()), SystemCode.BAD_REQUEST, IssueType.INVALID);
    }
    if (theException instanceof InvalidRequestException && theException.getMessage().contains("non-repeatable parameter")) {
        return OperationOutcomeFactory.buildOperationOutcomeException(new InvalidRequestException(theException.getMessage()), SystemCode.BAD_REQUEST, IssueType.INVALID);
    }
    if (theException instanceof InvalidRequestException && theException.getMessage().contains("header blank")) {
        return OperationOutcomeFactory.buildOperationOutcomeException(new InvalidRequestException(theException.getMessage()), SystemCode.BAD_REQUEST, IssueType.INVALID);
    }
    if (theException instanceof InvalidRequestException && theException.getMessage().contains("InvalidResourceType")) {
        return OperationOutcomeFactory.buildOperationOutcomeException(new UnprocessableEntityException(theException.getMessage()), SystemCode.INVALID_RESOURCE, IssueType.INVALID);
    }
    if (theException instanceof InvalidRequestException && theException.getMessage().contains("Can not create resource with ID")) {
        return OperationOutcomeFactory.buildOperationOutcomeException(new UnprocessableEntityException(theException.getMessage()), SystemCode.BAD_REQUEST, IssueType.INVALID);
    }
    if (theException instanceof ResourceNotFoundException && theException.getMessage().contains("Unknown resource type")) {
        return OperationOutcomeFactory.buildOperationOutcomeException((ResourceNotFoundException) theException, SystemCode.BAD_REQUEST, IssueType.INVALID);
    }
    // }
    if (theException instanceof ResourceVersionConflictException && theException.getMessage().contains("Slot is already in use.")) {
        ResourceVersionConflictException exception = (ResourceVersionConflictException) theException;
        return OperationOutcomeFactory.buildOperationOutcomeException(exception, SystemCode.DUPLICATE_REJECTED, IssueType.CONFLICT);
    }
    if (theException instanceof ResourceVersionConflictException) {
        ResourceVersionConflictException exception = (ResourceVersionConflictException) theException;
        return OperationOutcomeFactory.buildOperationOutcomeException(exception, SystemCode.FHIR_CONSTRAINT_VIOLATION, IssueType.CONFLICT);
    }
    if (theException instanceof BaseServerResponseException) {
        BaseServerResponseException baseServerResponseException = (BaseServerResponseException) theException;
        // If the OperationalOutcome is already set, just return it.
        return null == baseServerResponseException.getOperationOutcome() ? OperationOutcomeFactory.buildOperationOutcomeException(baseServerResponseException, SystemCode.BAD_REQUEST, IssueType.INVALID) : baseServerResponseException;
    }
    // Default catch all.
    return OperationOutcomeFactory.buildOperationOutcomeException(new InvalidRequestException(theException.getMessage()), SystemCode.BAD_REQUEST, IssueType.INVALID);
}
Also used : UnprocessableEntityException(ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException) DataFormatException(ca.uhn.fhir.parser.DataFormatException) MethodNotAllowedException(ca.uhn.fhir.rest.server.exceptions.MethodNotAllowedException) InvalidRequestException(ca.uhn.fhir.rest.server.exceptions.InvalidRequestException) ResourceNotFoundException(ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException) ResourceVersionConflictException(ca.uhn.fhir.rest.server.exceptions.ResourceVersionConflictException) BaseServerResponseException(ca.uhn.fhir.rest.server.exceptions.BaseServerResponseException)

Aggregations

DataFormatException (ca.uhn.fhir.parser.DataFormatException)3 UnprocessableEntityException (ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException)2 FhirContext (ca.uhn.fhir.context.FhirContext)1 IParser (ca.uhn.fhir.parser.IParser)1 StrictErrorHandler (ca.uhn.fhir.parser.StrictErrorHandler)1 BaseServerResponseException (ca.uhn.fhir.rest.server.exceptions.BaseServerResponseException)1 InvalidRequestException (ca.uhn.fhir.rest.server.exceptions.InvalidRequestException)1 MethodNotAllowedException (ca.uhn.fhir.rest.server.exceptions.MethodNotAllowedException)1 ResourceNotFoundException (ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException)1 ResourceVersionConflictException (ca.uhn.fhir.rest.server.exceptions.ResourceVersionConflictException)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 IOException (java.io.IOException)1 ValueSet (org.hl7.fhir.dstu3.model.ValueSet)1