Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Cleanup into Java 8
  • Loading branch information
ajs6f authored and Andrew Woods committed Jul 1, 2015
1 parent 29a2c16 commit 377491a
Show file tree
Hide file tree
Showing 15 changed files with 82 additions and 196 deletions.
4 changes: 0 additions & 4 deletions pom.xml
Expand Up @@ -55,26 +55,22 @@
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.9.5</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.glassfish.grizzly</groupId>
<artifactId>grizzly-http-server</artifactId>
<scope>test</scope>
<version>${grizzly.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.grizzly</groupId>
<artifactId>grizzly-http-servlet</artifactId>
<scope>test</scope>
<version>${grizzly.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
<artifactId>jersey-test-framework-provider-grizzly2</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
Expand Down
Expand Up @@ -33,7 +33,6 @@
import org.jboss.security.xacml.sunxacml.BasicEvaluationCtx;
import org.jboss.security.xacml.sunxacml.EvaluationCtx;
import org.jboss.security.xacml.sunxacml.ParsingException;
import org.jboss.security.xacml.sunxacml.attr.AttributeValue;
import org.jboss.security.xacml.sunxacml.attr.StringAttribute;
import org.jboss.security.xacml.sunxacml.ctx.Attribute;
import org.jboss.security.xacml.sunxacml.ctx.RequestCtx;
Expand All @@ -58,32 +57,27 @@ public class FedoraEvaluationCtxBuilder {
/**
* The list of other subjects.
*/
private final List<Subject> subjectList = new ArrayList<Subject>();
private final List<Subject> subjectList = new ArrayList<>();

/**
* This list of resource attributes.
*/
private final List<Attribute> resourceList = new ArrayList<Attribute>();

/**
* The ID of the resource.
*/
private AttributeValue resourceId;
private final List<Attribute> resourceList = new ArrayList<>();

/**
* This list of action attributes.
*/
private final List<Attribute> actionList = new ArrayList<Attribute>();
private final List<Attribute> actionList = new ArrayList<>();

/**
* This list of environment attributes.
*/
private final List<Attribute> environmentList = new ArrayList<Attribute>();
private final List<Attribute> environmentList = new ArrayList<>();

/**
* The list of attribute finder modules.
*/
private final List<AttributeFinderModule> attributeFinderModules = new ArrayList<AttributeFinderModule>();
private final List<AttributeFinderModule> attributeFinderModules = new ArrayList<>();

/**
* Build the evaluation context.
Expand Down
Expand Up @@ -27,6 +27,7 @@
import javax.jcr.NodeIterator;
import javax.jcr.RepositoryException;
import javax.jcr.Session;

import org.fcrepo.http.commons.session.SessionFactory;

import org.jboss.security.xacml.sunxacml.EvaluationCtx;
Expand Down Expand Up @@ -109,11 +110,11 @@ private ResourceFinderResult findChildren( final AttributeValue parent, final bo
try {
final Session session = sessionFactory.getInternalSession();
final Node node = session.getNode( parent.getValue().toString() );
final Set<String> children = new HashSet<String>();
final Set<String> children = new HashSet<>();
findChildren( node, children, recurse );
return new ResourceFinderResult( children );
} catch ( RepositoryException ex ) {
final HashMap errors = new HashMap();
} catch ( final RepositoryException ex ) {
final HashMap<AttributeValue, String> errors = new HashMap<>();
errors.put( parent, STATUS_PROCESSING_ERROR );
return new ResourceFinderResult( errors );
}
Expand All @@ -129,7 +130,7 @@ private void findChildren( final Node node, final Set<String> children, final bo
throws RepositoryException {
for ( final NodeIterator nodes = node.getNodes(); nodes.hasNext(); ) {
final Node child = nodes.nextNode();
if ( !isInternalNode.apply(child) && !child.getName().equals(JCR_CONTENT) ) {
if ( !isInternalNode.test(child) && !child.getName().equals(JCR_CONTENT) ) {

children.add( child.getPath() );

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/fcrepo/auth/xacml/PolicyUtil.java
Expand Up @@ -144,7 +144,7 @@ public static Node getFirstRealNode(final String modepath, final Session session
* @return a set of actions
*/
public static Set<String> getActions(final EvaluationCtx context) {
final Set<String> result = new HashSet<String>();
final Set<String> result = new HashSet<>();
final EvaluationResult eval =
context.getActionAttribute(URI.create("http://www.w3.org/2001/XMLSchema#string"),
URIConstants.ATTRIBUTEID_ACTION_ID, null);
Expand Down
Expand Up @@ -76,7 +76,6 @@ public final Set<Integer> getSupportedDesignatorTypes() {
* @see org.jboss.security.xacml.sunxacml.finder.AttributeFinderModule#
* getSupportedIds()
*/
@SuppressWarnings("unchecked")
@Override
public final Set<URI> getSupportedIds() {
// TODO Auto-generated method stub
Expand Down
Expand Up @@ -21,22 +21,18 @@
import java.util.Set;

import javax.annotation.PostConstruct;
import javax.jcr.RepositoryException;
import javax.inject.Inject;
import javax.jcr.Session;
import javax.servlet.http.HttpServletRequest;

import org.fcrepo.auth.roles.common.AbstractRolesAuthorizationDelegate;
import org.fcrepo.auth.roles.common.AccessRolesProvider;
import org.fcrepo.http.commons.session.SessionFactory;
import org.fcrepo.kernel.services.NodeService;
import org.jboss.security.xacml.sunxacml.EvaluationCtx;
import org.jboss.security.xacml.sunxacml.PDP;
import org.jboss.security.xacml.sunxacml.ctx.ResponseCtx;
import org.jboss.security.xacml.sunxacml.ctx.Result;
import org.jboss.security.xacml.sunxacml.finder.impl.CurrentEnvModule;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
Expand All @@ -53,17 +49,7 @@ public class XACMLAuthorizationDelegate extends AbstractRolesAuthorizationDelega
*/
private static final Logger LOGGER = LoggerFactory.getLogger(XACMLAuthorizationDelegate.class);

/**
* The name of Fedora's subject finder module bean. (prototype)
*/
private static final String SUBJECT_ATTRIBUTE_FINDER_BEAN = "subjectAttributeFinderModule";

/**
* The name of Fedora's environment finder module bean. (prototype)
*/
private static final String ENVIRONMENT_ATTRIBUTE_FINDER_BEAN = "environmentAttributeFinderModule";

@Autowired
@Inject
private PDPFactory pdpFactory;

/**
Expand All @@ -74,43 +60,27 @@ public class XACMLAuthorizationDelegate extends AbstractRolesAuthorizationDelega
/**
* The standard environment attribute finder, supplies date/time.
*/
private CurrentEnvModule currentEnvironmentAttributeModule = new CurrentEnvModule();
private final CurrentEnvModule currentEnvironmentAttributeModule = new CurrentEnvModule();

/**
* The triple-based resource attribute finder module.
*/
@Autowired
@Inject
private TripleAttributeFinderModule tripleResourceAttributeFinderModule;

/**
* The SPARQL-based resource attribute finder module.
*/
@Autowired
@Inject
private SparqlResourceAttributeFinderModule sparqlResourceAttributeFinderModule;

/**
* The provider for access roles.
*/
@Autowired
private AccessRolesProvider accessRolesProvider;

/**
* Fedora's ModeShape session factory.
*/
@Autowired
private SessionFactory sessionFactory;

@Autowired
private NodeService nodeService;

/**
* Configures the delegate.
*
* @throws IOException if IO exception occurred
* @throws RepositoryException if repository exception occurred
*/
@PostConstruct
public final void init() throws RepositoryException, IOException {
public final void init() {
pdp = pdpFactory.makePDP();
if (pdp == null) {
throw new Error("There is no PDP wired by the factory in the Spring context.");
Expand Down Expand Up @@ -209,6 +179,7 @@ private EvaluationCtx buildEvaluationContext(final Session session,
builder.addOriginalRequestIP(request.getRemoteAddr());

// add user's groups
@SuppressWarnings("unchecked")
final Set<Principal> allGroups = (Set<Principal>) session.getAttribute(FEDORA_ALL_PRINCIPALS);
LOGGER.debug("effective groups: {}", allGroups);
builder.addGroups(user, allGroups);
Expand Down
Expand Up @@ -61,9 +61,9 @@ public class XACMLWorkspaceInitializer {
@Autowired
private BinaryService binaryService;

private File initialPoliciesDirectory;
private final File initialPoliciesDirectory;

private File initialRootPolicyFile;
private final File initialRootPolicyFile;

/**
* Constructor
Expand Down Expand Up @@ -157,12 +157,9 @@ private void loadInitialPolicies() {
final String id = PolicyUtil.getID(FileUtils.openInputStream(p));
final String repoPath = PolicyUtil.getPathForId(id);
final FedoraBinary binary = binaryService.findOrCreate(session, repoPath);
binary.setContent(new FileInputStream(p),
"application/xml",
null,
p.getName(),
null);

try (FileInputStream stream = new FileInputStream(p)) {
binary.setContent(stream, "application/xml", null, p.getName(), null);
}
LOGGER.info("Add initial policy {} at {}", p.getAbsolutePath(), binary.getPath());
}
session.save();
Expand Down
Expand Up @@ -23,44 +23,31 @@
import org.jboss.security.xacml.sunxacml.EvaluationCtx;
import org.jboss.security.xacml.sunxacml.cond.EvaluationResult;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.runners.MockitoJUnitRunner;
import org.modeshape.jcr.api.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Test the behavior of the XACML eval context builder.
*
* @author Gregory Jansen
*/
@RunWith(MockitoJUnitRunner.class)
public class FedoraEvaluationCtxBuilderTest {

private static final Logger LOGGER = LoggerFactory.getLogger(FedoraEvaluationCtxBuilderTest.class);

@Mock
private Session session;

/**
* Setup the test case.
*/
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
}

/**
* Test a builder of evaluation context.
*
* @throws Exception
*/
@Test
public void test() throws Exception {
public void test() {
// use builder to create context.
final FedoraEvaluationCtxBuilder builder = new FedoraEvaluationCtxBuilder();
final Set<String> roles = new HashSet<String>();
final Set<String> roles = new HashSet<>();
roles.add("reader");
builder.addSubject("testuser", roles);
builder.addResourceID("/testobject/testdatastream/myproperty1");
Expand Down
Expand Up @@ -36,6 +36,7 @@

import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;

import org.fcrepo.http.commons.session.SessionFactory;
Expand All @@ -44,6 +45,7 @@
import org.fcrepo.kernel.models.FedoraBinary;
import org.fcrepo.kernel.services.BinaryService;
import org.fcrepo.kernel.services.NodeService;

import org.jboss.security.xacml.sunxacml.EvaluationCtx;
import org.jboss.security.xacml.sunxacml.PolicyReference;
import org.jboss.security.xacml.sunxacml.attr.AttributeValue;
Expand Down Expand Up @@ -138,17 +140,17 @@ public void setUp() throws Exception {
}

@Test
public void testIsRequestSupported() throws Exception {
public void testIsRequestSupported() {
assertTrue(finderModule.isRequestSupported());
}

@Test
public void testIsIdReferenceSupported() throws Exception {
public void testIsIdReferenceSupported() {
assertTrue(finderModule.isIdReferenceSupported());
}

@Test
public void testFindPolicyOnTargetNode() throws Exception {
public void testFindPolicyOnTargetNode() throws RepositoryException {

when(mockNode.hasProperty(eq(XACML_POLICY_PROPERTY))).thenReturn(true);
when(mockNode.getProperty(eq(XACML_POLICY_PROPERTY))).thenReturn(mockPolicyProperty);
Expand All @@ -171,10 +173,10 @@ public void testFindPolicyOnTargetNode() throws Exception {
}

@Test
public void testFindPolicyByIdReference() throws Exception {
public void testFindPolicyByIdReference() {
final String policyPath = "/path/to/policy";
final String idPath = POLICY_URI_PREFIX + policyPath;
final URI idReference = new URI(idPath);
final URI idReference = URI.create(idPath);

when(mockPolicyDs.getDescribedResource()).thenReturn(mockBinary);
when(mockBinary.getContent()).thenReturn(this.getClass().getResourceAsStream("/xacml/testPolicy.xml"));
Expand All @@ -187,7 +189,7 @@ public void testFindPolicyByIdReference() throws Exception {
}

@Test
public void testFindPolicySet() throws Exception {
public void testFindPolicySet() throws RepositoryException {

when(mockNode.hasProperty(eq(XACML_POLICY_PROPERTY))).thenReturn(true);
when(mockNode.getProperty(eq(XACML_POLICY_PROPERTY))).thenReturn(mockPolicyProperty);
Expand All @@ -196,7 +198,6 @@ public void testFindPolicySet() throws Exception {
when(mockBinary.getContent())
.thenReturn(this.getClass().getResourceAsStream("/xacml/adminRolePolicySet.xml"));

final String referencedId = "fcrepo:policies/AdminPermissionPolicySet";
final NonRdfSourceDescription referencedPolicyDs = mock(NonRdfSourceDescription.class);
final FedoraBinary referencedPolicyBinary = mock(FedoraBinary.class);
when(referencedPolicyDs.getDescribedResource()).thenReturn(referencedPolicyBinary);
Expand Down

0 comments on commit 377491a

Please sign in to comment.