diff --git a/src/main/java/org/fcrepo/auth/xacml/FedoraPolicyFinderModule.java b/src/main/java/org/fcrepo/auth/xacml/FedoraPolicyFinderModule.java index ea608f1..683283a 100644 --- a/src/main/java/org/fcrepo/auth/xacml/FedoraPolicyFinderModule.java +++ b/src/main/java/org/fcrepo/auth/xacml/FedoraPolicyFinderModule.java @@ -30,6 +30,7 @@ import org.fcrepo.http.commons.session.SessionFactory; import org.fcrepo.kernel.Datastream; +import org.fcrepo.kernel.FedoraBinary; import org.fcrepo.kernel.exception.RepositoryRuntimeException; import org.fcrepo.kernel.services.DatastreamService; import org.fcrepo.kernel.services.NodeService; @@ -101,16 +102,16 @@ public final boolean isIdReferenceSupported() { * @return */ private AbstractPolicy getPolicy(final Datastream policyDatastream) { - return loadPolicy(policyDatastream); + return loadPolicy(policyDatastream.getBinary()); } /** * Creates a new policy or policy set object from the given policy node * - * @param policyDatastream + * @param policyBinary * @return */ - private AbstractPolicy loadPolicy(final Datastream policyDatastream) { + private AbstractPolicy loadPolicy(final FedoraBinary policyBinary) { String policyName = "unparsed"; try { // create the factory @@ -122,7 +123,7 @@ private AbstractPolicy loadPolicy(final Datastream policyDatastream) { final DocumentBuilder db = factory.newDocumentBuilder(); // Parse the policy content - final Document doc = db.parse(policyDatastream.getContent()); + final Document doc = db.parse(policyBinary.getContent()); // handle the policy, if it's a known type final Element root = doc.getDocumentElement(); @@ -227,7 +228,7 @@ public final PolicyFinderResult findPolicy(final URI idReference, final String path = PolicyUtil.getPathForId(id); final Session internalSession = sessionFactory.getInternalSession(); - final Datastream policyDatastream = datastreamService.getDatastream(internalSession, path); + final Datastream policyDatastream = datastreamService.findOrCreateDatastream(internalSession, path); final AbstractPolicy policy = getPolicy(policyDatastream); return new PolicyFinderResult(policy); diff --git a/src/main/java/org/fcrepo/auth/xacml/XACMLWorkspaceInitializer.java b/src/main/java/org/fcrepo/auth/xacml/XACMLWorkspaceInitializer.java index c4af60e..22a2a45 100644 --- a/src/main/java/org/fcrepo/auth/xacml/XACMLWorkspaceInitializer.java +++ b/src/main/java/org/fcrepo/auth/xacml/XACMLWorkspaceInitializer.java @@ -30,7 +30,7 @@ import com.google.common.collect.ImmutableList; import org.apache.commons.io.FileUtils; import org.fcrepo.http.commons.session.SessionFactory; -import org.fcrepo.kernel.Datastream; +import org.fcrepo.kernel.FedoraBinary; import org.fcrepo.kernel.exception.InvalidChecksumException; import org.fcrepo.kernel.services.DatastreamService; import org.modeshape.jcr.api.nodetype.NodeTypeManager; @@ -154,12 +154,14 @@ private void loadInitialPolicies() { for (final File p : initialPoliciesDirectory.listFiles()) { final String id = PolicyUtil.getID(FileUtils.openInputStream(p)); final String repoPath = PolicyUtil.getPathForId(id); - final Datastream d = datastreamService.createDatastream(session, - repoPath, - "application/xml", - p.getName(), - new FileInputStream(p)); - LOGGER.info("Add initial policy {} at {}", p.getAbsolutePath(), d.getPath()); + final FedoraBinary binary = datastreamService.getBinary(session, repoPath); + binary.setContent(new FileInputStream(p), + "application/xml", + null, + p.getName(), + datastreamService.getStoragePolicyDecisionPoint()); + + LOGGER.info("Add initial policy {} at {}", p.getAbsolutePath(), binary.getPath()); } session.save(); } catch (final RepositoryException | InvalidChecksumException | IOException e) { diff --git a/src/test/java/org/fcrepo/auth/xacml/FedoraPolicyFinderModuleTest.java b/src/test/java/org/fcrepo/auth/xacml/FedoraPolicyFinderModuleTest.java index 7a280aa..0f6d844 100644 --- a/src/test/java/org/fcrepo/auth/xacml/FedoraPolicyFinderModuleTest.java +++ b/src/test/java/org/fcrepo/auth/xacml/FedoraPolicyFinderModuleTest.java @@ -40,6 +40,7 @@ import org.fcrepo.http.commons.session.SessionFactory; import org.fcrepo.kernel.Datastream; +import org.fcrepo.kernel.FedoraBinary; import org.fcrepo.kernel.services.DatastreamService; import org.jboss.security.xacml.sunxacml.EvaluationCtx; import org.jboss.security.xacml.sunxacml.PolicyReference; @@ -79,6 +80,9 @@ public class FedoraPolicyFinderModuleTest { @Mock private Datastream mockPolicyDs; + @Mock + private FedoraBinary mockBinary; + @Mock private DatastreamService mockDsService; @@ -134,7 +138,8 @@ public void testFindPolicyOnTargetNode() throws Exception { when(mockNode.hasProperty(eq(XACML_POLICY_PROPERTY))).thenReturn(true); when(mockNode.getProperty(eq(XACML_POLICY_PROPERTY))).thenReturn(mockPolicyProperty); - when(mockPolicyDs.getContent()).thenReturn(this.getClass().getResourceAsStream("/xacml/testPolicy.xml")); + when(mockPolicyDs.getBinary()).thenReturn(mockBinary); + when(mockBinary.getContent()).thenReturn(this.getClass().getResourceAsStream("/xacml/testPolicy.xml")); final FedoraEvaluationCtxBuilder ctxBuilder = new FedoraEvaluationCtxBuilder(); ctxBuilder.addResourceID("/{}myPath"); @@ -156,8 +161,9 @@ public void testFindPolicyByIdReference() throws Exception { final String idPath = POLICY_URI_PREFIX + policyPath; final URI idReference = new URI(idPath); - when(mockPolicyDs.getContent()).thenReturn(this.getClass().getResourceAsStream("/xacml/testPolicy.xml")); - when(mockDsService.getDatastream(any(Session.class), eq(policyPath))).thenReturn(mockPolicyDs); + when(mockPolicyDs.getBinary()).thenReturn(mockBinary); + when(mockBinary.getContent()).thenReturn(this.getClass().getResourceAsStream("/xacml/testPolicy.xml")); + when(mockDsService.findOrCreateDatastream(any(Session.class), eq(policyPath))).thenReturn(mockPolicyDs); final PolicyFinderResult result = finderModule.findPolicy(idReference, 0, null, null); @@ -172,14 +178,17 @@ public void testFindPolicySet() throws Exception { when(mockNode.hasProperty(eq(XACML_POLICY_PROPERTY))).thenReturn(true); when(mockNode.getProperty(eq(XACML_POLICY_PROPERTY))).thenReturn(mockPolicyProperty); - when(mockPolicyDs.getContent()) + when(mockPolicyDs.getBinary()).thenReturn(mockBinary); + when(mockBinary.getContent()) .thenReturn(this.getClass().getResourceAsStream("/xacml/adminRolePolicySet.xml")); final String referencedId = "fcrepo:policies/AdminPermissionPolicySet"; final Datastream referencedPolicyDs = mock(Datastream.class); - when(referencedPolicyDs.getContent()).thenReturn( + final FedoraBinary referencedPolicyBinary = mock(FedoraBinary.class); + when(referencedPolicyDs.getBinary()).thenReturn(referencedPolicyBinary); + when(referencedPolicyBinary.getContent()).thenReturn( this.getClass().getResourceAsStream("/xacml/adminPermissionPolicySet.xml")); - when(mockDsService.getDatastream(any(Session.class), eq(referencedId))).thenReturn(referencedPolicyDs); + when(mockDsService.findOrCreateDatastream(any(Session.class), eq(referencedId))).thenReturn(referencedPolicyDs); final FedoraEvaluationCtxBuilder ctxBuilder = new FedoraEvaluationCtxBuilder(); ctxBuilder.addResourceID("/{}myPath"); diff --git a/src/test/java/org/fcrepo/auth/xacml/XACMLWorkspaceInitializerTest.java b/src/test/java/org/fcrepo/auth/xacml/XACMLWorkspaceInitializerTest.java index 6d2e049..3dce2aa 100644 --- a/src/test/java/org/fcrepo/auth/xacml/XACMLWorkspaceInitializerTest.java +++ b/src/test/java/org/fcrepo/auth/xacml/XACMLWorkspaceInitializerTest.java @@ -26,7 +26,6 @@ import static org.mockito.MockitoAnnotations.initMocks; import java.io.File; -import java.io.FileInputStream; import javax.jcr.Node; import javax.jcr.RepositoryException; @@ -34,12 +33,12 @@ import org.fcrepo.http.commons.session.SessionFactory; import org.fcrepo.kernel.Datastream; +import org.fcrepo.kernel.FedoraBinary; import org.fcrepo.kernel.exception.RepositoryRuntimeException; import org.fcrepo.kernel.services.DatastreamService; import org.junit.Before; import org.junit.Test; -import org.mockito.Matchers; import org.mockito.Mock; /** @@ -68,15 +67,16 @@ public class XACMLWorkspaceInitializerTest { @Mock Datastream mockDatastream; + @Mock + FedoraBinary mockBinary; + @Before public void setUp() throws Exception { initMocks(this); when(mockSessionFactory.getInternalSession()).thenReturn(mockSession); when(mockSession.getRootNode()).thenReturn(mockNode); - when( - mockDsService.createDatastream(eq(mockSession), anyString(), eq("application/xml"), anyString(), - Matchers.any(FileInputStream.class))).thenReturn(mockDatastream); + when(mockDsService.findOrCreateDatastream(eq(mockSession), anyString())).thenReturn(mockDatastream); when(mockDatastream.getPath()).thenReturn("/dummy/test/path"); final File initialPoliciesDirectory = policiesDirectory(); @@ -118,14 +118,12 @@ public void testConstructorEmptyDir() { @Test public void testInit() throws Exception { + when(mockDsService.getBinary(eq(mockSession), anyString())).thenReturn(mockBinary); + xacmlWI.init(); final int expectedFiles = policiesDirectory().list().length; - verify(mockDsService, times(expectedFiles)).createDatastream(eq(mockSession), - anyString(), - eq("application/xml"), - anyString(), - Matchers.any(FileInputStream.class)); + verify(mockDsService, times(expectedFiles)).getBinary(eq(mockSession), anyString()); verify(mockNode).addMixin("authz:xacmlAssignable"); verify(mockNode).setProperty(eq("authz:policy"), any(Node.class)); @@ -140,6 +138,7 @@ public void testInitInitialPoliciesException() throws Exception { @Test(expected = Error.class) public void testInitLinkRootToPolicyException() throws Exception { + when(mockDsService.getBinary(eq(mockSession), anyString())).thenReturn(mockBinary); when(mockSession.getRootNode()).thenThrow(new RepositoryException("expected")); xacmlWI.init();