Search in sources :

Example 6 with InvalidRequestException

use of ca.uhn.fhir.rest.server.exceptions.InvalidRequestException in project gpconnect-demonstrator by nhsconnect.

the class PatientResourceProvider method registerPatient.

@Operation(name = REGISTER_PATIENT_OPERATION_NAME)
public Bundle registerPatient(@ResourceParam Parameters params) {
    Patient registeredPatient = null;
    validateParameterNames(params, registerPatientParams);
    Patient unregisteredPatient = params.getParameter().stream().filter(param -> "registerPatient".equalsIgnoreCase(param.getName())).map(ParametersParameterComponent::getResource).map(Patient.class::cast).findFirst().orElse(null);
    String nnn = nhsNumber.fromPatientResource(unregisteredPatient);
    // if its patient 14 spoof not on PDS and return the required error
    if (nnn.equals(patientNotOnSpine)) {
        throw OperationOutcomeFactory.buildOperationOutcomeException(new InvalidRequestException(String.format("Patient (NHS number - %s) not present on PDS", nnn)), SystemCode.INVALID_PATIENT_DEMOGRAPHICS, IssueType.INVALID);
    } else if (nnn.equals(patientSuperseded)) {
        throw OperationOutcomeFactory.buildOperationOutcomeException(new InvalidRequestException(String.format("Patient (NHS number - %s) is superseded", nnn)), SystemCode.INVALID_NHS_NUMBER, IssueType.INVALID);
    }
    if (unregisteredPatient != null) {
        validatePatient(unregisteredPatient);
        // check if the patient already exists
        PatientDetails patientDetails = patientSearch.findPatient(nhsNumber.fromPatientResource(unregisteredPatient));
        if (patientDetails == null || IsInactiveTemporaryPatient(patientDetails)) {
            if (patientDetails == null) {
                patientDetails = registerPatientResourceConverterToPatientDetail(unregisteredPatient);
                patientStore.create(patientDetails);
            } else {
                // reactivate inactive non temporary patient
                patientDetails.setRegistrationStatus(ACTIVE_REGISTRATION_STATUS);
                updateAddressAndTelecom(unregisteredPatient, patientDetails);
                patientStore.update(patientDetails);
            }
            try {
                registeredPatient = patientDetailsToRegisterPatientResourceConverter(patientSearch.findPatient(unregisteredPatient.getIdentifierFirstRep().getValue()));
                addPreferredBranchSurgeryExtension(registeredPatient);
            } catch (FHIRException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } else if (patientDetails.isDeceased() || patientDetails.isSensitive()) {
            throw OperationOutcomeFactory.buildOperationOutcomeException(new InvalidRequestException(String.format("Patient (NHS number - %s) has invalid demographics", nnn)), SystemCode.INVALID_PATIENT_DEMOGRAPHICS, IssueType.INVALID);
        } else {
            throw OperationOutcomeFactory.buildOperationOutcomeException(new UnclassifiedServerFailureException(409, String.format("Patient (NHS number - %s) already exists", nnn)), SystemCode.DUPLICATE_REJECTED, IssueType.INVALID);
        }
    } else {
        throw OperationOutcomeFactory.buildOperationOutcomeException(new UnprocessableEntityException("Patient record not found"), SystemCode.INVALID_PARAMETER, IssueType.INVALID);
    }
    Bundle bundle = new Bundle().setType(BundleType.SEARCHSET);
    bundle.getMeta().addProfile(SystemURL.SD_GPC_SRCHSET_BUNDLE);
    bundle.addEntry().setResource(registeredPatient);
    return bundle;
}
Also used : UnprocessableEntityException(ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException) UnclassifiedServerFailureException(ca.uhn.fhir.rest.server.exceptions.UnclassifiedServerFailureException) PatientDetails(uk.gov.hscic.model.patient.PatientDetails) PopulateMedicationBundle(uk.gov.hscic.medications.PopulateMedicationBundle) InvalidRequestException(ca.uhn.fhir.rest.server.exceptions.InvalidRequestException) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Example 7 with InvalidRequestException

use of ca.uhn.fhir.rest.server.exceptions.InvalidRequestException in project gpconnect-demonstrator by nhsconnect.

the class PatientJwtValidator method getNhsNumber.

private String getNhsNumber(RequestDetails requestDetails) {
    ResourceBinding patientResourceBinding = getPatientResourceBinding(requestDetails.getServer());
    if (patientResourceBinding != null) {
        BaseMethodBinding<?> methodBinding = patientResourceBinding.getMethod(requestDetails);
        // the request may not be for the patient resource in which case
        // we would not expect a method binding
        Object parameterValue = null;
        if (methodBinding != null) {
            for (IParameter parameter : methodBinding.getParameters()) {
                parameterValue = parameter.translateQueryParametersIntoServerArgument(requestDetails, methodBinding);
                // the identifier may have been passed in the URL
                if (parameterValue == null) {
                    parameterValue = ParameterUtil.convertIdToType(requestDetails.getId(), IdDt.class);
                }
                if (parameterValue != null) {
                    String nhsNumber = patientResourceProvider.getNhsNumber(parameterValue);
                    if (null != nhsNumber) {
                        return nhsNumber;
                    }
                }
            }
        }
        if (parameterValue != null) {
            if (RequestTypeEnum.GET == requestDetails.getRequestType()) {
                throw OperationOutcomeFactory.buildOperationOutcomeException(new ResourceNotFoundException("No patient details found for patient ID: " + parameterValue), SystemCode.PATIENT_NOT_FOUND, IssueType.INVALID);
            }
            // Otherwise, there should have been an identifier header
            throw OperationOutcomeFactory.buildOperationOutcomeException(new InvalidRequestException("No NHS number submitted: " + parameterValue), SystemCode.INVALID_NHS_NUMBER, IssueType.INVALID);
        }
    }
    return null;
}
Also used : IParameter(ca.uhn.fhir.rest.server.method.IParameter) IdDt(ca.uhn.fhir.model.primitive.IdDt) InvalidRequestException(ca.uhn.fhir.rest.server.exceptions.InvalidRequestException) ResourceNotFoundException(ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException) ResourceBinding(ca.uhn.fhir.rest.server.ResourceBinding)

Example 8 with InvalidRequestException

use of ca.uhn.fhir.rest.server.exceptions.InvalidRequestException in project gpconnect-demonstrator by nhsconnect.

the class PatientResourceProvider method validateParameterNames.

private void validateParameterNames(Parameters parameters, Map<String, Boolean> parameterDefinitions) {
    List<String> parameterNames = parameters.getParameter().stream().map(ParametersParameterComponent::getName).collect(Collectors.toList());
    Set<String> parameterDefinitionNames = parameterDefinitions.keySet();
    if (parameterNames.isEmpty() == false) {
        for (String parameterDefinition : parameterDefinitionNames) {
            boolean mandatory = parameterDefinitions.get(parameterDefinition);
            if (mandatory) {
                if (parameterNames.contains(parameterDefinition) == false) {
                    throw OperationOutcomeFactory.buildOperationOutcomeException(new InvalidRequestException("Not all mandatory parameters have been provided"), SystemCode.INVALID_PARAMETER, IssueType.INVALID);
                }
            }
        }
        if (parameterDefinitionNames.containsAll(parameterNames) == false) {
            parameterNames.removeAll(parameterDefinitionNames);
            throwInvalidRequest400_BadRequestException("Unrecognised parameters have been provided - " + parameterNames.toString());
        }
    } else {
        throw OperationOutcomeFactory.buildOperationOutcomeException(new InvalidRequestException("Not all mandatory parameters have been provided"), SystemCode.INVALID_PARAMETER, IssueType.INVALID);
    }
}
Also used : InvalidRequestException(ca.uhn.fhir.rest.server.exceptions.InvalidRequestException)

Example 9 with InvalidRequestException

use of ca.uhn.fhir.rest.server.exceptions.InvalidRequestException 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)

Example 10 with InvalidRequestException

use of ca.uhn.fhir.rest.server.exceptions.InvalidRequestException in project gpconnect-demonstrator by nhsconnect.

the class WebTokenValidator method validateWebToken.

public static void validateWebToken(WebToken webToken, int futureRequestLeeway) {
    verifyNoNullValues(webToken);
    verifyTimeValues(webToken, futureRequestLeeway);
    verifyRequestedResourceValues(webToken);
    // Checking the practionerId and the sub are equal in value
    if (!(webToken.getRequestingPractitioner().getId().equals(webToken.getSub()))) {
        throw OperationOutcomeFactory.buildOperationOutcomeException(new InvalidRequestException("Practitioner ids do not match!"), SystemCode.BAD_REQUEST, IssueType.INVALID);
    }
    if (!PERMITTED_REQUESTED_SCOPES.contains(webToken.getRequestedScope())) {
        throw OperationOutcomeFactory.buildOperationOutcomeException(new InvalidRequestException("Bad Request Exception"), SystemCode.BAD_REQUEST, IssueType.INVALID);
    }
    if (!SystemURL.AUTHORIZATION_TOKEN.equals(webToken.getAud())) {
        throw OperationOutcomeFactory.buildOperationOutcomeException(new InvalidRequestException("Bad Request Exception"), SystemCode.BAD_REQUEST, IssueType.INVALID);
    }
}
Also used : InvalidRequestException(ca.uhn.fhir.rest.server.exceptions.InvalidRequestException)

Aggregations

InvalidRequestException (ca.uhn.fhir.rest.server.exceptions.InvalidRequestException)10 UnprocessableEntityException (ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException)4 ResourceNotFoundException (ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException)3 IdDt (ca.uhn.fhir.model.primitive.IdDt)2 Search (ca.uhn.fhir.rest.annotation.Search)2 AdministrativeGender (org.hl7.fhir.dstu3.model.Enumerations.AdministrativeGender)2 FHIRException (org.hl7.fhir.exceptions.FHIRException)2 PatientDetails (uk.gov.hscic.model.patient.PatientDetails)2 DateTimeDt (ca.uhn.fhir.model.primitive.DateTimeDt)1 DataFormatException (ca.uhn.fhir.parser.DataFormatException)1 Count (ca.uhn.fhir.rest.annotation.Count)1 IdParam (ca.uhn.fhir.rest.annotation.IdParam)1 Operation (ca.uhn.fhir.rest.annotation.Operation)1 OptionalParam (ca.uhn.fhir.rest.annotation.OptionalParam)1 Read (ca.uhn.fhir.rest.annotation.Read)1 RequiredParam (ca.uhn.fhir.rest.annotation.RequiredParam)1 ResourceParam (ca.uhn.fhir.rest.annotation.ResourceParam)1 Sort (ca.uhn.fhir.rest.annotation.Sort)1 SortSpec (ca.uhn.fhir.rest.api.SortSpec)1 DateAndListParam (ca.uhn.fhir.rest.param.DateAndListParam)1