PolicyEnforcementPointImpl implements PolicyEnforcementPoint { @Override public AdhocQueryResponse registryStoredQuery(AdhocQueryRequest req) { AdhocQueryResponse response = null; XacmlRequest xacmlRequest = null; String resourceId = null; String intermediarySubject = null; String purposeOfUse = null; String recipientSubject = null; final String messageId = createMessageId(); PolicyEnforcementPointResponseStatus pepResponseStatus = PolicyEnforcementPointResponseStatus.SUCCESS; try { logger.debug(messageId, aspectUtils.getInXMLFormat(req)); final SAMLTokenPrincipal samlTokenPrincipal = getUserPrincipal(messageId); resourceId = parseSamlToken(samlTokenPrincipal, URN_RESOURCE_ID, messageId); intermediarySubject = parseSamlToken(samlTokenPrincipal, URN_INTERMEDIARY_SUBJECT, messageId); purposeOfUse = parseSamlToken(samlTokenPrincipal, URN_PURPOSE_OF_USE, messageId); recipientSubject = parseSamlToken(samlTokenPrincipal, URN_RECIPIENT_SUBJECT, messageId); xacmlRequest = setXacmlRequest(resourceId, purposeOfUse, intermediarySubject, recipientSubject, messageId); logger.debug(messageId, aspectUtils.getInXMLFormat(xacmlRequest)); auditRequest(req, xacmlRequest); validateAdhocQueryRequest(req, messageId); final String formatCode = xdsbRegistry.extractFormatCode(req); validateFormatCode(formatCode, messageId); validateResponseOptionType(req, messageId); final String patientUniqueId = xdsbRegistry.extractPatientId(req); validatePatientUniqueIdConsistency(patientUniqueId, xacmlRequest.getPatientUniqueId(), messageId); final XacmlResponse xacmlResponse = contextHandler .enforcePolicy(xacmlRequest); logger.info(messageId, "PDP Decision : " + xacmlResponse.getPdpDecision()); logger.debug(messageId, aspectUtils.getInXMLFormat(xacmlResponse)); if (PERMIT.equals(xacmlResponse.getPdpDecision())) { response = xdsbRegistry.registryStoredQuery(req, intermediarySubject, messageId); logger.debug(messageId, aspectUtils.getInXMLFormat(response)); validateResponse(response, patientUniqueId, intermediarySubject, messageId); } else { handlePDPDeny(messageId, xacmlRequest, xacmlResponse); } } catch (final SamlTokenPrincipalException e) { pepResponseStatus = PolicyEnforcementPointResponseStatus.ERROR_SYSTEM; logger.error( messageId, "SAMLTokenPrincipal cannot be retrieved from WebServiceContext.", e); response = xdsbErrorFactory .errorAdhocQueryResponseInternalServerError(); } catch (final SamlTokenParserException e) { pepResponseStatus = PolicyEnforcementPointResponseStatus.ERROR_SYSTEM; logger.error(messageId, "SamlTokenParser failed.", e); response = xdsbErrorFactory .errorAdhocQueryResponseInternalServerError(); } catch (final AuditException e) { pepResponseStatus = PolicyEnforcementPointResponseStatus.ERROR_SYSTEM; logger.error(messageId, "AuditService failed.", e); response = xdsbErrorFactory .errorAdhocQueryResponseInternalServerError(); } catch (final XdsbRegistryAdapterException e) { pepResponseStatus = PolicyEnforcementPointResponseStatus.ERROR_SYSTEM; logger.error(messageId, "XdsbRegistryAdapter failed.", e); response = xdsbErrorFactory .errorAdhocQueryResponseRegistryNotAvailable(); } catch (final MissingParameterException e) { pepResponseStatus = PolicyEnforcementPointResponseStatus.ERROR_SYSTEM; logger.info(messageId, e.getMessage()); logger.debug(messageId, e.getMessage(), e); response = xdsbErrorFactory .errorAdhocQueryResponseMissingParameters(); } catch (final UnsupportedFormatCodeException e) { pepResponseStatus = PolicyEnforcementPointResponseStatus.ERROR_SYSTEM; logger.info(messageId, e.getMessage()); logger.debug(messageId, e.getMessage(), e); response = xdsbErrorFactory .errorAdhocQueryResponseConstructByErrorMessage(e .getMessage()); } catch (final UnsupportedResponseOptionTypeException e) { pepResponseStatus = PolicyEnforcementPointResponseStatus.ERROR_SYSTEM; logger.info(messageId, e.getMessage()); logger.debug(messageId, e.getMessage(), e); response = xdsbErrorFactory .errorAdhocQueryResponseConstructByErrorMessage(e .getMessage()); } catch (final InconsistentPatientUniqueIdException e) { pepResponseStatus = PolicyEnforcementPointResponseStatus.ERROR_SYSTEM; logger.info(messageId, e.getMessage()); logger.debug(messageId, e.getMessage(), e); response = xdsbErrorFactory .errorAdhocQueryResponseConstructByErrorMessage(e .getMessage()); } catch (final NoPolicyFoundException e) { pepResponseStatus = PolicyEnforcementPointResponseStatus.ERROR_CONSENT; logger.info(messageId, e.getMessage()); logger.debug(messageId, e.getMessage(), e); response = xdsbErrorFactory .errorAdhocQueryResponseConstructByErrorMessage(e .getMessage()); } catch (final PolicyProviderException e) { pepResponseStatus = PolicyEnforcementPointResponseStatus.ERROR_SYSTEM; logger.error(messageId, "PolicyProvider failed.", e); response = xdsbErrorFactory .errorAdhocQueryResponseInternalServerError(); } catch (final NoDocumentFoundException e) { pepResponseStatus = PolicyEnforcementPointResponseStatus.ERROR_SYSTEM; logger.info(messageId, e.getMessage()); logger.debug(messageId, e.getMessage(), e); response = xdsbErrorFactory .errorAdhocQueryResponseConstructByErrorMessage(e .getMessage()); } catch (final Throwable t) { pepResponseStatus = PolicyEnforcementPointResponseStatus.ERROR_SYSTEM; logger.error(messageId, "Unexpected exception occured in PolicyEnforcementPoint.", t); response = xdsbErrorFactory .errorAdhocQueryResponseInternalServerError(); } finally { try { if (xacmlRequest == null) { xacmlRequest = setXacmlRequestIfNull(resourceId, messageId); } auditResponse(response, xacmlRequest, pepResponseStatus); } catch (final Throwable e) { logger.error(messageId, "Cannot audit the response.", e); response = xdsbErrorFactory .errorAdhocQueryResponseInternalServerError(); } } Assert.notNull(response, "The response cannot be null."); return response; } PolicyEnforcementPointImpl(boolean isAuditFailureByPass,
			XdsbRegistryAdapter xdsbRegistry,
			XdsbRepositoryAdapter xdsbRepository,
			XdsbErrorFactory xdsbErrorFactory, ContextHandler contextHandler,
			DSSWebServiceClient documentSegmentation,
			SimpleMarshaller marshaller, WebServiceContext context,
			SamlTokenParser parser,
			DataHandlerToBytesConverter dataHandlerToBytesConverter,
			AuditService auditService, AspectUtils aspectUtils); @Override DirectEmailSendResponse directEmailSend(DirectEmailSendRequest input); @Override AdhocQueryResponse registryStoredQuery(AdhocQueryRequest req); @Override RetrieveDocumentSetResponse retrieveDocumentSet(
			RetrieveDocumentSetRequest input); XacmlRequest setXacmlRequest(String resourceId, String purposeOfUse,
			String intermediarySubject, String recipientSubject,
			String messageId); XacmlRequest setXacmlRequestIfNull(String resourceId,
			final String messageId); static final String PERMIT; static final String SUPPORTED_ADHOCQUERY_RESPONSE_TYPE; static final String URN_RESOURCE_ID; static final String URN_INTERMEDIARY_SUBJECT; static final String URN_RECIPIENT_SUBJECT; static final String URN_PURPOSE_OF_USE; }
@Test public void testRegistryStoredQuery() throws Throwable { when(xdsbRegistry.extractFormatCode(adhocQuery)).thenReturn(formatCode) .thenReturn(formatCode); when(xdsbRegistry.extractDocumentEntryStatus(adhocQuery)).thenReturn( docEntryStatus); when(xdsbRegistry.extractPatientId(adhocQuery)).thenReturn( patientUniqueId).thenReturn(patientUniqueId); when(xdsbRegistry.getPatientUniqueId(anyString(), anyString())) .thenReturn(patientUniqueId); when(xdsbRegistry.extractResponseOptionReturnType(adhocQuery)) .thenReturn(responseOptionType); when( xdsbRegistry.registryStoredQuery(eq(adhocQuery), eq(intermediarySubjectNPI), anyString())).thenReturn( adhocQueryResponse); when(contextHandler.enforcePolicy(isA(XacmlRequest.class))).thenReturn( xacmlResponse); when(context.getUserPrincipal()).thenReturn(samlTokenPrincipal); when( samlTokenParser.parse(samlTokenPrincipal.getToken(), "urn:oasis:names:tc:xacml:1.0:resource:resource-id")) .thenReturn( "d3bb3930-7241-11e3-b4f7-00155d3a2124^^^&2.16.840.1.113883.10.20.1^^HITSP"); when( samlTokenParser.parse(samlTokenPrincipal.getToken(), "urn:oasis:names:tc:xacml:1.0:subject-category:intermediary-subject")) .thenReturn(intermediarySubjectNPI); when( samlTokenParser.parse(samlTokenPrincipal.getToken(), "urn:oasis:names:tc:xspa:1.0:subject:purposeofuse")) .thenReturn("TREATMENT"); when( samlTokenParser.parse(samlTokenPrincipal.getToken(), "urn:oasis:names:tc:xacml:1.0:subject-category:recipient-subject")) .thenReturn(recepientSubjectNPI); final AdhocQueryResponse response = pep.registryStoredQuery(adhocQuery); assertEquals(adhocQueryResponse, response); }