Search in sources :

Example 1 with ResourceBinding

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

the class PatientJwtValidator method getPatientResourceBinding.

private ResourceBinding getPatientResourceBinding(IRestfulServerDefaults defaultServer) {
    ResourceBinding resourceBinding = null;
    if (defaultServer instanceof RestfulServer) {
        RestfulServer restfulServer = (RestfulServer) defaultServer;
        // if we get more than one Patient binding then return null
        resourceBinding = restfulServer.getResourceBindings().stream().filter(currentResourceBinding -> "Patient".equalsIgnoreCase(currentResourceBinding.getResourceName())).collect(Collectors.reducing((a, b) -> null)).orElse(null);
    }
    return resourceBinding;
}
Also used : BaseMethodBinding(ca.uhn.fhir.rest.server.method.BaseMethodBinding) IParameter(ca.uhn.fhir.rest.server.method.IParameter) IdDt(ca.uhn.fhir.model.primitive.IdDt) RestfulServer(ca.uhn.fhir.rest.server.RestfulServer) InvalidRequestException(ca.uhn.fhir.rest.server.exceptions.InvalidRequestException) ResourceBinding(ca.uhn.fhir.rest.server.ResourceBinding) Autowired(org.springframework.beans.factory.annotation.Autowired) ParameterUtil(ca.uhn.fhir.rest.param.ParameterUtil) Collectors(java.util.stream.Collectors) IssueType(org.hl7.fhir.dstu3.model.OperationOutcome.IssueType) Value(org.springframework.beans.factory.annotation.Value) OperationOutcomeFactory(uk.gov.hscic.OperationOutcomeFactory) RuleBuilder(ca.uhn.fhir.rest.server.interceptor.auth.RuleBuilder) List(java.util.List) Component(org.springframework.stereotype.Component) RequestTypeEnum(ca.uhn.fhir.rest.api.RequestTypeEnum) RequestDetails(ca.uhn.fhir.rest.api.server.RequestDetails) PatientResourceProvider(uk.gov.hscic.patient.PatientResourceProvider) IRestfulServerDefaults(ca.uhn.fhir.rest.server.IRestfulServerDefaults) IAuthRule(ca.uhn.fhir.rest.server.interceptor.auth.IAuthRule) SystemCode(uk.gov.hscic.SystemCode) ResourceNotFoundException(ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException) AuthorizationInterceptor(ca.uhn.fhir.rest.server.interceptor.auth.AuthorizationInterceptor) RestfulServer(ca.uhn.fhir.rest.server.RestfulServer) ResourceBinding(ca.uhn.fhir.rest.server.ResourceBinding)

Example 2 with ResourceBinding

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

Aggregations

IdDt (ca.uhn.fhir.model.primitive.IdDt)2 ResourceBinding (ca.uhn.fhir.rest.server.ResourceBinding)2 InvalidRequestException (ca.uhn.fhir.rest.server.exceptions.InvalidRequestException)2 ResourceNotFoundException (ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException)2 IParameter (ca.uhn.fhir.rest.server.method.IParameter)2 RequestTypeEnum (ca.uhn.fhir.rest.api.RequestTypeEnum)1 RequestDetails (ca.uhn.fhir.rest.api.server.RequestDetails)1 ParameterUtil (ca.uhn.fhir.rest.param.ParameterUtil)1 IRestfulServerDefaults (ca.uhn.fhir.rest.server.IRestfulServerDefaults)1 RestfulServer (ca.uhn.fhir.rest.server.RestfulServer)1 AuthorizationInterceptor (ca.uhn.fhir.rest.server.interceptor.auth.AuthorizationInterceptor)1 IAuthRule (ca.uhn.fhir.rest.server.interceptor.auth.IAuthRule)1 RuleBuilder (ca.uhn.fhir.rest.server.interceptor.auth.RuleBuilder)1 BaseMethodBinding (ca.uhn.fhir.rest.server.method.BaseMethodBinding)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 IssueType (org.hl7.fhir.dstu3.model.OperationOutcome.IssueType)1 Autowired (org.springframework.beans.factory.annotation.Autowired)1 Value (org.springframework.beans.factory.annotation.Value)1 Component (org.springframework.stereotype.Component)1