PolicyEnforcementPointImpl implements PolicyEnforcementPoint { @Override public RetrieveDocumentSetResponse retrieveDocumentSet( RetrieveDocumentSetRequest input) { RetrieveDocumentSetResponse 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(input)); 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(input, xacmlRequest); validateRetrieveDocumentSetRequest(input, messageId); final XacmlResponse xacmlResponse = contextHandler .enforcePolicy(xacmlRequest); logger.debug(messageId, aspectUtils.getInXMLFormat(xacmlResponse)); if (PERMIT.equals(xacmlResponse.getPdpDecision())) { response = xdsbRepository.retrieveDocumentSet(input, xacmlRequest.getPatientId(), xacmlRequest.getIntermediarySubjectNPI()); logger.debug(messageId, aspectUtils.getInXMLFormat(response)); validateRetrieveDocumentSetResponse(response, messageId); segmentResponse(response, messageId, xacmlRequest, xacmlResponse); } 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 .errorRetrieveDocumentSetInternalServerError(); } catch (final SamlTokenParserException e) { pepResponseStatus = PolicyEnforcementPointResponseStatus.ERROR_SYSTEM; logger.error(messageId, "SamlTokenParser failed.", e); response = xdsbErrorFactory .errorRetrieveDocumentSetInternalServerError(); } catch (final AuditException e) { pepResponseStatus = PolicyEnforcementPointResponseStatus.ERROR_SYSTEM; logger.error(messageId, "AuditService failed.", e); response = xdsbErrorFactory .errorRetrieveDocumentSetInternalServerError(); } catch (final MultipleRepositoryIdException e) { pepResponseStatus = PolicyEnforcementPointResponseStatus.ERROR_SYSTEM; logger.info(messageId, e.getMessage()); logger.debug(messageId, e.getMessage(), e); response = xdsbErrorFactory .errorRetrieveDocumentSetResponseConstructByErrorMessage(e .getMessage()); } catch (final NoPolicyFoundException e) { pepResponseStatus = PolicyEnforcementPointResponseStatus.ERROR_CONSENT; logger.info(messageId, e.getMessage()); logger.debug(messageId, e.getMessage(), e); response = xdsbErrorFactory .errorRetrieveDocumentSetResponseConstructByErrorMessage(e .getMessage()); } catch (final PolicyProviderException e) { pepResponseStatus = PolicyEnforcementPointResponseStatus.ERROR_SYSTEM; logger.error(messageId, "PolicyProvider failed.", e); response = xdsbErrorFactory .errorRetrieveDocumentSetInternalServerError(); } catch (final XdsbRepositoryAdapterException e) { pepResponseStatus = PolicyEnforcementPointResponseStatus.ERROR_SYSTEM; logger.error(messageId, "XdsbRepositoryAdapter failed.", e); response = xdsbErrorFactory .errorRetrieveDocumentSetResponseRepositoryNotAvailable(); } catch (final NoDocumentFoundException e) { pepResponseStatus = PolicyEnforcementPointResponseStatus.ERROR_SYSTEM; logger.info(messageId, e.getMessage()); logger.debug(messageId, e.getMessage(), e); response = xdsbErrorFactory .errorRetrieveDocumentSetResponseNotExistsOrAccessible(input); } catch (final SimpleMarshallerException e) { pepResponseStatus = PolicyEnforcementPointResponseStatus.ERROR_SYSTEM; logger.error(messageId, "SimpleMarshaller failed.", e); response = xdsbErrorFactory .errorRetrieveDocumentSetInternalServerError(); } catch (final Throwable t) { pepResponseStatus = PolicyEnforcementPointResponseStatus.ERROR_SYSTEM; logger.error(messageId, "Unexpected exception occured in PolicyEnforcementPoint.", t); response = xdsbErrorFactory .errorRetrieveDocumentSetInternalServerError(); } 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 .errorRetrieveDocumentSetInternalServerError(); } } 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 testRetrieveDocumentSet() throws Throwable { when(xdsbRegistry.getPatientUniqueId(anyString(), anyString())) .thenReturn(patientUniqueId); when(contextHandler.enforcePolicy(isA(XacmlRequest.class))).thenReturn( xacmlResponse).thenReturn(xacmlResponse); when( xdsbRepository.retrieveDocumentSet( any(RetrieveDocumentSetRequest.class), anyString(), anyString())).thenReturn(retrieveDocumentSetResponse); when(marshaller.marshal(isA(Object.class))).thenReturn( enforcementPolicies).thenReturn(enforcementPolicies); when(documentSegmentation.segmentDocument(isA(DSSRequest.class))) .thenReturn(segmentDocumentResponse1).thenReturn( segmentDocumentResponse2); 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.4.357&ISO"); when( samlTokenParser.parse(samlTokenPrincipal.getToken(), "urn:oasis:names:tc:xacml:1.0:subject-category:intermediary-subject")) .thenReturn("1114252178"); 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("1760717789"); final Map<PredicateKey, String> mockMap = mock(Map.class); when(auditService.createPredicateMap()).thenReturn(mockMap); when(aspectUtils.getInXMLFormat(any())).thenReturn(anyString()); final RetrieveDocumentSetResponse response = pep .retrieveDocumentSet(retrieveDocumentSetRequest); assertEquals("1111048131312.1312011.43613.91155.1111312013196213", response.getDocumentResponse().get(0).getDocumentUniqueId()); assertEquals("44212111915.14792.41394.914113.0152268155381113", response.getDocumentResponse().get(1).getDocumentUniqueId()); verify(documentSegmentation, times(2)).segmentDocument( argThat(matching((DSSRequest req) -> enforcementPolicies .equals(req.getEnforcementPoliciesXml()) && req.isAudited().equals(true) && req.isAuditFailureByPass().equals(false)))); }