You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jetspeed-dev@portals.apache.org by dd...@apache.org on 2008/09/05 19:26:18 UTC

svn commit: r692497 [1/2] - in /portals/jetspeed-2/portal/branches/security-refactoring: ./ components/jetspeed-security/ components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/ components/jetspeed-security/src/main/java/org/ap...

Author: ddam
Date: Fri Sep  5 10:26:17 2008
New Revision: 692497

URL: http://svn.apache.org/viewvc?rev=692497&view=rev
Log:
JS2-872 : initial commit of LDAP Security Mapping component.

This component can be used for synchronizing security data from and to LDAP. See the issue JS2-872 for details.

Added:
    portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/
    portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/SecurityEntityManager.java
    portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/ldap/
    portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/ldap/dao/
    portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/ldap/dao/DefaultEntityContextMapper.java
    portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/ldap/dao/DefaultLDAPEntityManager.java
    portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/ldap/dao/EntityDAO.java
    portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/ldap/dao/EntityFilterFactory.java
    portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/ldap/dao/EntityRelationDAO.java
    portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/ldap/dao/LDAPEntityDAOConfiguration.java
    portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/ldap/dao/SearchUtil.java
    portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/ldap/dao/impl/
    portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/ldap/dao/impl/AbstractRelationDAO.java
    portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/ldap/dao/impl/AttributeBasedRelationDAO.java
    portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/ldap/dao/impl/SpringLDAPEntityDAO.java
    portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/ldap/filter/
    portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/ldap/filter/SimpleFilter.java
    portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/model/
    portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/model/Attribute.java
    portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/model/AttributeDef.java
    portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/model/Entity.java
    portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/model/EntityDAO.java
    portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/model/EntityRelationDAO.java
    portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/model/impl/
    portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/model/impl/AttributeDefImpl.java
    portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/model/impl/AttributeImpl.java
    portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/model/impl/DefaultEntityRelationSearch.java
    portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/model/impl/EntityImpl.java
    portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/test/java/org/apache/jetspeed/security/mapping/
    portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/test/java/org/apache/jetspeed/security/mapping/ldap/
    portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/test/java/org/apache/jetspeed/security/mapping/ldap/AbstractLDAPTest.java
    portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/test/java/org/apache/jetspeed/security/mapping/ldap/BasicTestCases.java
    portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/test/java/org/apache/jetspeed/security/mapping/ldap/setup1/
    portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/test/java/org/apache/jetspeed/security/mapping/ldap/setup1/AbstractSetup1LDAPTest.java
    portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/test/java/org/apache/jetspeed/security/mapping/ldap/setup1/UserTests.java
    portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/test/java/org/apache/jetspeed/security/mapping/ldap/setup1/resources/
    portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/test/java/org/apache/jetspeed/security/mapping/ldap/setup1/resources/setup1.ldif
    portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/test/java/org/apache/jetspeed/security/mapping/ldap/setup2/
    portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/test/java/org/apache/jetspeed/security/mapping/ldap/setup2/AbstractSetup2LDAPTest.java
    portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/test/java/org/apache/jetspeed/security/mapping/ldap/setup2/UserTests.java
    portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/test/java/org/apache/jetspeed/security/mapping/ldap/setup2/resources/
    portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/test/java/org/apache/jetspeed/security/mapping/ldap/setup2/resources/setup2.ldif
Modified:
    portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/pom.xml
    portals/jetspeed-2/portal/branches/security-refactoring/pom.xml

Modified: portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/pom.xml
URL: http://svn.apache.org/viewvc/portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/pom.xml?rev=692497&r1=692496&r2=692497&view=diff
==============================================================================
--- portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/pom.xml (original)
+++ portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/pom.xml Fri Sep  5 10:26:17 2008
@@ -105,6 +105,27 @@
             <scope>provided</scope>
         </dependency>
         
+        <dependency>      
+            <artifactId>spring-ldap</artifactId>
+            <groupId>org.springframework.ldap</groupId>
+        </dependency>
+
+        <dependency>      
+            <artifactId>apacheds-server-unit</artifactId>
+            <groupId>org.apache.directory.server</groupId>
+            <version>1.5.3</version>
+            <scope>test</scope>
+        </dependency>
+        
+        <!-- used by Apache DS -->
+        <dependency>      
+            <artifactId>slf4j-jdk14</artifactId>
+            <groupId>org.slf4j</groupId>
+            <version>1.4.3</version>
+            <scope>test</scope>
+        </dependency>
+        
+        
     </dependencies>
 
     <build>

Added: portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/SecurityEntityManager.java
URL: http://svn.apache.org/viewvc/portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/SecurityEntityManager.java?rev=692497&view=auto
==============================================================================
--- portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/SecurityEntityManager.java (added)
+++ portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/SecurityEntityManager.java Fri Sep  5 10:26:17 2008
@@ -0,0 +1,26 @@
+package org.apache.jetspeed.security.mapping;
+
+
+import java.util.Collection;
+
+import org.apache.jetspeed.security.mapping.model.Entity;
+
+/**
+ * @author <a href="mailto:ddam@apache.org">Dennis Dam</a>
+ */
+public interface SecurityEntityManager
+{
+
+    Entity getEntity(String entityType, String entityId);
+
+    Collection<Entity> getAllEntities(String entityType);
+
+    void update(Entity entity);
+
+    Collection<Entity> getRelatedEntities(Entity sourceEntity,
+            String targetEntityType, String relationType);
+
+    void addRelatedEntity(Entity entity, Entity relatedEntity,
+            String relationType);
+
+}

Added: portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/ldap/dao/DefaultEntityContextMapper.java
URL: http://svn.apache.org/viewvc/portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/ldap/dao/DefaultEntityContextMapper.java?rev=692497&view=auto
==============================================================================
--- portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/ldap/dao/DefaultEntityContextMapper.java (added)
+++ portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/ldap/dao/DefaultEntityContextMapper.java Fri Sep  5 10:26:17 2008
@@ -0,0 +1,98 @@
+/* 
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jetspeed.security.mapping.ldap.dao;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.Set;
+
+import org.springframework.ldap.core.DirContextOperations;
+import org.springframework.ldap.core.support.AbstractContextMapper;
+
+import org.apache.jetspeed.security.mapping.model.Attribute;
+import org.apache.jetspeed.security.mapping.model.AttributeDef;
+import org.apache.jetspeed.security.mapping.model.impl.AttributeImpl;
+import org.apache.jetspeed.security.mapping.model.impl.EntityImpl;
+
+/**
+ * @author <a href="mailto:ddam@apache.org">Dennis Dam</a>
+ */
+public class DefaultEntityContextMapper extends AbstractContextMapper
+{
+
+    LDAPEntityDAOConfiguration searchConfiguration;
+
+    public DefaultEntityContextMapper(
+            LDAPEntityDAOConfiguration searchConfiguration)
+    {
+        this.searchConfiguration = searchConfiguration;
+    }
+
+    public Object doMapFromContext(DirContextOperations ctx)
+    {
+        String entityId = null;
+        Set<Attribute> attributes = new HashSet<Attribute>();
+        for (AttributeDef attrDef : searchConfiguration
+                .getAttributeDefinitions())
+        {
+
+            String[] values = ctx.getStringAttributes(attrDef.getName());
+            if (values != null && values.length > 0)
+            {
+                Attribute a = new AttributeImpl(attrDef);
+                if (attrDef.isMultiValue())
+                {
+                    Collection attrValues = new ArrayList();
+                    attrValues.addAll(Arrays.asList(values));
+                    a.setValues(attrValues);
+                } else
+                {
+                    if (attrDef.getName().equals(
+                            searchConfiguration.getLdapIdAttribute()))
+                    {
+                        entityId = values[0];
+                    }
+                    a.setValue(values[0]);
+                }
+                attributes.add(a);
+            }
+        }
+        if (entityId != null)
+        {
+            EntityImpl entity = new EntityImpl(searchConfiguration
+                    .getEntityType(), entityId, searchConfiguration
+                    .getAttributeDefinitions());
+            entity.setAttributes(attributes);
+            entity.setInternalId(ctx.getNameInNamespace().toString()); // set
+                                                                       // full
+                                                                       // DN
+                                                                       // (incl.
+                                                                       // base
+                                                                       // DN) as
+                                                                       // internal
+                                                                       // ID
+            entity.setType(searchConfiguration.getEntityType());
+            return entity;
+        } else
+        {
+            return null;
+        }
+    }
+
+}

Added: portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/ldap/dao/DefaultLDAPEntityManager.java
URL: http://svn.apache.org/viewvc/portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/ldap/dao/DefaultLDAPEntityManager.java?rev=692497&view=auto
==============================================================================
--- portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/ldap/dao/DefaultLDAPEntityManager.java (added)
+++ portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/ldap/dao/DefaultLDAPEntityManager.java Fri Sep  5 10:26:17 2008
@@ -0,0 +1,200 @@
+/* 
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jetspeed.security.mapping.ldap.dao;
+
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.jetspeed.security.mapping.SecurityEntityManager;
+import org.apache.jetspeed.security.mapping.model.Entity;
+
+/**
+ * @author <a href="mailto:ddam@apache.org">Dennis Dam</a>
+ */
+public class DefaultLDAPEntityManager implements SecurityEntityManager
+{
+
+    // entity type DAOs
+    protected Map<String, EntityDAO> entityDAOs;
+
+    protected Map<RelationDefinitionKey, EntityRelationDAO> entityRelationDAOs = new HashMap<RelationDefinitionKey, EntityRelationDAO>();
+
+    private EntityDAO getDAOForEntity(Entity entity)
+    {
+        return entity.getType() != null ? entityDAOs.get(entity.getType())
+                : null;
+    }
+
+    private EntityRelationDAO getRelationDAO(String relationType,
+            String fromEntityType, String targetEntityType)
+    {
+        return entityRelationDAOs.get(new RelationDefinitionKey(relationType,
+                fromEntityType, targetEntityType));
+    }
+
+    public void addRelatedEntity(Entity sourceEntity, Entity targetEntity,
+            String relationType)
+    {
+        EntityDAO sourceDao = getDAOForEntity(sourceEntity);
+        EntityDAO targetDao = getDAOForEntity(targetEntity);
+        if (sourceDao != null && targetDao != null)
+        {
+            EntityRelationDAO relationDAO = getRelationDAO(relationType,
+                    sourceEntity.getType(), targetEntity.getType());
+            if (relationDAO != null)
+            {
+                relationDAO.relate(sourceDao, targetDao, sourceEntity,
+                        targetEntity);
+            }
+        }
+    }
+
+    public Collection<Entity> getAllEntities(String entityType)
+    {
+        EntityDAO dao = entityDAOs.get(entityType);
+        return dao != null ? dao.getAllEntities() : null;
+    }
+
+    public Entity getEntity(String entityType, String entityId)
+    {
+        EntityDAO dao = entityDAOs.get(entityType);
+        return dao != null ? dao.getEntity(entityId) : null;
+    }
+
+    public Collection<Entity> getRelatedEntities(Entity sourceEntity,
+            String targetEntityType, String relationType)
+    {
+        EntityDAO sourceDAO = getDAOForEntity(sourceEntity);
+        EntityDAO targetDAO = entityDAOs.get(targetEntityType);
+        if (sourceDAO != null && targetDAO != null)
+        {
+            EntityRelationDAO relationDAO = getRelationDAO(relationType,
+                    sourceEntity.getType(), targetEntityType);
+            if (relationDAO != null) { return relationDAO.getRelatedEntities(
+                    sourceDAO, targetDAO, sourceEntity); }
+        }
+        return null; // todo : throw exception, since combination of entity
+                     // types and relation type is not configured.
+    }
+
+    public void update(Entity entity)
+    {
+        EntityDAO dao = getDAOForEntity(entity);
+        if (dao != null)
+        {
+            dao.update(entity);
+        }
+    }
+
+    public void setEntityDAOs(Map<String, EntityDAO> entityDAOs)
+    {
+        this.entityDAOs = entityDAOs;
+    }
+
+    public void setEntityRelationDAOs(
+            Collection<EntityRelationDAO> entityRelationDAOs)
+    {
+        this.entityRelationDAOs.clear();
+        for (EntityRelationDAO dao : entityRelationDAOs)
+        {
+            this.entityRelationDAOs.put(new RelationDefinitionKey(dao
+                    .getRelationType(), dao.getFromEntityType(), dao
+                    .getToEntityType()), dao);
+        }
+    }
+
+    private class RelationDefinitionKey
+    {
+
+        private String sourceEntityType, targetEntityType, relationType;
+
+        public RelationDefinitionKey(String relationType,
+                String sourceEntityType, String targetEntityType)
+        {
+            super();
+            this.relationType = relationType;
+            this.sourceEntityType = sourceEntityType;
+            this.targetEntityType = targetEntityType;
+        }
+
+        @Override
+        public int hashCode()
+        {
+            final int prime = 31;
+            int result = 1;
+            result = prime * result + getOuterType().hashCode();
+            result = prime * result
+                    + ((relationType == null) ? 0 : relationType.hashCode());
+            result = prime
+                    * result
+                    + ((sourceEntityType == null) ? 0 : sourceEntityType
+                            .hashCode());
+            result = prime
+                    * result
+                    + ((targetEntityType == null) ? 0 : targetEntityType
+                            .hashCode());
+            return result;
+        }
+
+        @Override
+        public boolean equals(Object obj)
+        {
+            if (this == obj) return true;
+            if (obj == null) return false;
+            if (getClass() != obj.getClass()) return false;
+            RelationDefinitionKey other = (RelationDefinitionKey) obj;
+            if (!getOuterType().equals(other.getOuterType())) return false;
+            if (relationType == null)
+            {
+                if (other.relationType != null) return false;
+            } else if (!relationType.equals(other.relationType)) return false;
+            if (sourceEntityType == null)
+            {
+                if (other.sourceEntityType != null) return false;
+            } else if (!sourceEntityType.equals(other.sourceEntityType))
+                return false;
+            if (targetEntityType == null)
+            {
+                if (other.targetEntityType != null) return false;
+            } else if (!targetEntityType.equals(other.targetEntityType))
+                return false;
+            return true;
+        }
+
+        public String getSourceEntityType()
+        {
+            return sourceEntityType;
+        }
+
+        public String getTargetEntityType()
+        {
+            return targetEntityType;
+        }
+
+        public String getRelationType()
+        {
+            return relationType;
+        }
+
+        private DefaultLDAPEntityManager getOuterType()
+        {
+            return DefaultLDAPEntityManager.this;
+        }
+
+    }
+}

Added: portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/ldap/dao/EntityDAO.java
URL: http://svn.apache.org/viewvc/portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/ldap/dao/EntityDAO.java?rev=692497&view=auto
==============================================================================
--- portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/ldap/dao/EntityDAO.java (added)
+++ portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/ldap/dao/EntityDAO.java Fri Sep  5 10:26:17 2008
@@ -0,0 +1,81 @@
+/* 
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jetspeed.security.mapping.ldap.dao;
+
+import java.util.Collection;
+
+import org.springframework.ldap.filter.Filter;
+
+import org.apache.jetspeed.security.mapping.model.Entity;
+
+/**
+ * @author <a href="mailto:ddam@apache.org">Dennis Dam</a>
+ */
+public interface EntityDAO
+{
+
+    /**
+     * Fetch entities by providing a list of specific entity IDs.
+     * 
+     * @param entity
+     *            IDs
+     * @return found entities
+     */
+    Collection<Entity> getEntitiesById(Collection<String> entityIds);
+
+    /**
+     * Fetch entities by providing a list of specific *internal* entity IDs.
+     * 
+     * @param internal
+     *            entity IDs
+     * @return found entities
+     */
+    Collection<Entity> getEntitiesByInternalId(Collection<String> entityIds);
+
+    /**
+     * Method for applying a specific filter on the complete entity set returned
+     * by the DAO. The result would be the same as applying the specific filter
+     * to the result of getAllEntities().
+     * 
+     * @param filter
+     *            a specific filter to narrow the returned entity set
+     * @return found entities
+     */
+    Collection<Entity> getEntities(Filter filter);
+
+    /**
+     * Fetch a single entity by ID.
+     * 
+     * @param entityId
+     * @return the entity
+     */
+    Entity getEntity(String entityId);
+
+    /**
+     * Fetch all entities
+     * 
+     * @return found entities
+     */
+    Collection<Entity> getAllEntities();
+
+    void update(Entity entity);
+
+    void addEntity(Entity entity);
+
+    void removeEntity(Entity entity);
+
+}

Added: portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/ldap/dao/EntityFilterFactory.java
URL: http://svn.apache.org/viewvc/portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/ldap/dao/EntityFilterFactory.java?rev=692497&view=auto
==============================================================================
--- portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/ldap/dao/EntityFilterFactory.java (added)
+++ portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/ldap/dao/EntityFilterFactory.java Fri Sep  5 10:26:17 2008
@@ -0,0 +1,34 @@
+/* 
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jetspeed.security.mapping.ldap.dao;
+
+import java.util.Map;
+
+import org.springframework.ldap.filter.Filter;
+
+import org.apache.jetspeed.security.mapping.model.AttributeDef;
+import org.apache.jetspeed.security.mapping.model.Entity;
+
+/**
+ * @author <a href="mailto:ddam@apache.org">Dennis Dam</a>
+ */
+public interface EntityFilterFactory
+{
+
+    Filter createFilter(Entity entity);
+
+}

Added: portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/ldap/dao/EntityRelationDAO.java
URL: http://svn.apache.org/viewvc/portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/ldap/dao/EntityRelationDAO.java?rev=692497&view=auto
==============================================================================
--- portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/ldap/dao/EntityRelationDAO.java (added)
+++ portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/ldap/dao/EntityRelationDAO.java Fri Sep  5 10:26:17 2008
@@ -0,0 +1,41 @@
+/* 
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jetspeed.security.mapping.ldap.dao;
+
+import java.util.Collection;
+
+import org.apache.jetspeed.security.mapping.model.Entity;
+
+/**
+ * @author <a href="mailto:ddam@apache.org">Dennis Dam</a>
+ */
+public interface EntityRelationDAO
+{
+
+    String getFromEntityType();
+
+    String getToEntityType();
+
+    String getRelationType();
+
+    Collection<Entity> getRelatedEntities(EntityDAO sourceDao,
+            EntityDAO targetDao, Entity sourceEntity);
+
+    void relate(EntityDAO sourceDao, EntityDAO targetDao, Entity sourceEntity,
+            Entity targetEntity);
+
+}

Added: portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/ldap/dao/LDAPEntityDAOConfiguration.java
URL: http://svn.apache.org/viewvc/portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/ldap/dao/LDAPEntityDAOConfiguration.java?rev=692497&view=auto
==============================================================================
--- portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/ldap/dao/LDAPEntityDAOConfiguration.java (added)
+++ portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/ldap/dao/LDAPEntityDAOConfiguration.java Fri Sep  5 10:26:17 2008
@@ -0,0 +1,91 @@
+/* 
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jetspeed.security.mapping.ldap.dao;
+
+import java.util.Set;
+
+import org.springframework.ldap.filter.Filter;
+
+import org.apache.jetspeed.security.mapping.model.AttributeDef;
+
+/**
+ * @author <a href="mailto:ddam@apache.org">Dennis Dam</a>
+ */
+public class LDAPEntityDAOConfiguration
+{
+
+    private String baseDN;
+
+    private Filter baseFilter;
+
+    private String ldapIdAttribute;
+
+    private Set<AttributeDef> attributeDefinitions;
+
+    private String entityType;
+
+    public String getBaseDN()
+    {
+        return baseDN;
+    }
+
+    public void setBaseDN(String baseDN)
+    {
+        this.baseDN = baseDN;
+    }
+
+    public Filter getBaseFilter()
+    {
+        return baseFilter;
+    }
+
+    public void setBaseFilter(Filter baseFilter)
+    {
+        this.baseFilter = baseFilter;
+    }
+
+    public Set<AttributeDef> getAttributeDefinitions()
+    {
+        return attributeDefinitions;
+    }
+
+    public void setAttributeDefinitions(Set<AttributeDef> attributeDefinitions)
+    {
+        this.attributeDefinitions = attributeDefinitions;
+    }
+
+    public String getLdapIdAttribute()
+    {
+        return ldapIdAttribute;
+    }
+
+    public void setLdapIdAttribute(String ldapIdAttribute)
+    {
+        this.ldapIdAttribute = ldapIdAttribute;
+    }
+
+    public String getEntityType()
+    {
+        return entityType;
+    }
+
+    public void setEntityType(String entityType)
+    {
+        this.entityType = entityType;
+    }
+
+}

Added: portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/ldap/dao/SearchUtil.java
URL: http://svn.apache.org/viewvc/portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/ldap/dao/SearchUtil.java?rev=692497&view=auto
==============================================================================
--- portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/ldap/dao/SearchUtil.java (added)
+++ portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/ldap/dao/SearchUtil.java Fri Sep  5 10:26:17 2008
@@ -0,0 +1,70 @@
+/* 
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jetspeed.security.mapping.ldap.dao;
+
+import org.apache.commons.lang.StringUtils;
+import org.springframework.ldap.filter.AndFilter;
+import org.springframework.ldap.filter.EqualsFilter;
+import org.springframework.ldap.filter.Filter;
+import org.springframework.ldap.filter.OrFilter;
+
+import org.apache.jetspeed.security.mapping.ldap.filter.SimpleFilter;
+
+/**
+ * @author <a href="mailto:ddam@apache.org">Dennis Dam</a>
+ */
+public class SearchUtil
+{
+
+    public static Filter andFilters(Filter... filters)
+    {
+        AndFilter andFilter = new AndFilter();
+        for (Filter filter : filters)
+        {
+            andFilter.and(filter);
+        }
+        return andFilter;
+    }
+
+    public static Filter constructMatchingFieldsFilter(Filter baseFilter,
+            String[]... fieldNamesAndValues)
+    {
+        AndFilter filter = new AndFilter();
+        for (String[] nameAndValues : fieldNamesAndValues)
+        {
+            if (nameAndValues.length > 1)
+            {
+                String name = nameAndValues[0];
+                OrFilter fieldFilter = new OrFilter();
+                for (int i = 1; i < nameAndValues.length; i++)
+                {
+                    fieldFilter.or(new EqualsFilter(name, nameAndValues[i]));
+                }
+                filter.and(fieldFilter);
+            }
+        }
+        return baseFilter != null ? andFilters(baseFilter, filter) : filter;
+    }
+
+    public static Filter constructMatchingFieldsFilter(String baseFilter,
+            String[]... fieldNamesAndValues)
+    {
+        return constructMatchingFieldsFilter(new SimpleFilter(baseFilter),
+                fieldNamesAndValues);
+    }
+
+}

Added: portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/ldap/dao/impl/AbstractRelationDAO.java
URL: http://svn.apache.org/viewvc/portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/ldap/dao/impl/AbstractRelationDAO.java?rev=692497&view=auto
==============================================================================
--- portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/ldap/dao/impl/AbstractRelationDAO.java (added)
+++ portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/ldap/dao/impl/AbstractRelationDAO.java Fri Sep  5 10:26:17 2008
@@ -0,0 +1,68 @@
+/* 
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jetspeed.security.mapping.ldap.dao.impl;
+
+import org.springframework.ldap.core.LdapTemplate;
+
+import org.apache.jetspeed.security.mapping.ldap.dao.EntityRelationDAO;
+
+/**
+ * @author <a href="mailto:ddam@apache.org">Dennis Dam</a>
+ */
+public abstract class AbstractRelationDAO implements EntityRelationDAO
+{
+
+    private LdapTemplate ldapTemplate;
+
+    private String relationType, fromEntityType, toEntityType;
+
+    public String getFromEntityType()
+    {
+        return fromEntityType;
+    }
+
+    public String getRelationType()
+    {
+        return relationType;
+    }
+
+    public String getToEntityType()
+    {
+        return toEntityType;
+    }
+
+    public void setLdapTemplate(LdapTemplate ldapTemplate)
+    {
+        this.ldapTemplate = ldapTemplate;
+    }
+
+    public void setFromEntityType(String fromEntityType)
+    {
+        this.fromEntityType = fromEntityType;
+    }
+
+    public void setToEntityType(String toEntityType)
+    {
+        this.toEntityType = toEntityType;
+    }
+
+    public void setRelationType(String relationType)
+    {
+        this.relationType = relationType;
+    }
+
+}

Added: portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/ldap/dao/impl/AttributeBasedRelationDAO.java
URL: http://svn.apache.org/viewvc/portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/ldap/dao/impl/AttributeBasedRelationDAO.java?rev=692497&view=auto
==============================================================================
--- portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/ldap/dao/impl/AttributeBasedRelationDAO.java (added)
+++ portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/ldap/dao/impl/AttributeBasedRelationDAO.java Fri Sep  5 10:26:17 2008
@@ -0,0 +1,130 @@
+/* 
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jetspeed.security.mapping.ldap.dao.impl;
+
+import java.util.Collection;
+
+import org.apache.commons.lang.StringUtils;
+import org.springframework.ldap.filter.EqualsFilter;
+import org.springframework.ldap.filter.Filter;
+
+import org.apache.jetspeed.security.mapping.ldap.dao.EntityDAO;
+import org.apache.jetspeed.security.mapping.model.Attribute;
+import org.apache.jetspeed.security.mapping.model.Entity;
+
+/**
+ * DAO for fetching relationships between LDAP entities. A relationship between
+ * two entities is created by adding an attribute value to either the entity on
+ * the "from" site of the relationship, or the entity on the other side ("to").
+ * 
+ * An example: a relation "hasRole" from one entity (e.g. of type "user") to
+ * another entity (e.g. of type "role"), can be defined by using an attribute
+ * "role" on the user, which is a multi-value attribute. The value of that
+ * attribute uniquely identifies the role (the id of the role entity). The
+ * relationship can also be defined by specifying an attribute on the role which
+ * holds the unique id of the user, e.g. through a multi-value "member"
+ * attribute.
+ * 
+ * @author <a href="mailto:ddam@apache.org">Dennis Dam</a>
+ */
+public class AttributeBasedRelationDAO extends AbstractRelationDAO
+{
+
+    private String relationAttribute;
+
+    private boolean useFromEntityAttribute;
+
+    private boolean attributeContainsInternalId; // if internal ID ( = DN) is
+                                                 // not used, then the attribute
+                                                 // contains the ID9(s).
+
+    public Collection<Entity> getRelatedEntities(EntityDAO sourceDao,
+            EntityDAO targetDao, Entity fromEntity)
+    {
+        if (useFromEntityAttribute)
+        {
+            Attribute relationAttrValue = fromEntity
+                    .getAttribute(relationAttribute);
+            if (relationAttrValue != null)
+            {
+                Collection<String> values = relationAttrValue.getValues();
+                if (attributeContainsInternalId)
+                {
+                    return targetDao.getEntitiesByInternalId(values);
+                } else
+                {
+                    return targetDao.getEntitiesById(values);
+                }
+            }
+        } else
+        {
+            // can be either the id or the internalId of the from entity
+            String fromEntityUsedIdValue = attributeContainsInternalId ? getInternalId(
+                    fromEntity, sourceDao)
+                    : fromEntity.getId();
+            // TODO : throw exception when no ID / internal ID can be found for
+            // the entity
+            if (!StringUtils.isEmpty(fromEntityUsedIdValue))
+            {
+                // fetch entities using target Entity DAO with a specific filter
+                // on the member attribute
+                Filter roleMemberAttrFilter = new EqualsFilter(
+                        relationAttribute, fromEntityUsedIdValue);
+                return targetDao.getEntities(roleMemberAttrFilter);
+            }
+        }
+        return null;
+    }
+
+    private String getInternalId(Entity entity, EntityDAO entityDao)
+    {
+        if (StringUtils.isEmpty(entity.getInternalId()))
+        {
+            // apparently internalId is not stored in the DB => fetch it from
+            // LDAP store
+            entity = entityDao.getEntity(entity.getId());
+            return entity.getInternalId();
+        } else
+        {
+            return entity.getInternalId();
+        }
+    }
+
+    public void relate(EntityDAO sourceDao, EntityDAO targetDao,
+            Entity sourceEntity, Entity targetEntity)
+    {
+        // TODO Auto-generated method stub
+
+    }
+
+    public void setRelationAttribute(String relationAttribute)
+    {
+        this.relationAttribute = relationAttribute;
+    }
+
+    public void setUseFromEntityAttribute(boolean useFromEntityAttribute)
+    {
+        this.useFromEntityAttribute = useFromEntityAttribute;
+    }
+
+    public void setAttributeContainsInternalId(
+            boolean attributeContainsInternalId)
+    {
+        this.attributeContainsInternalId = attributeContainsInternalId;
+    }
+
+}

Added: portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/ldap/dao/impl/SpringLDAPEntityDAO.java
URL: http://svn.apache.org/viewvc/portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/ldap/dao/impl/SpringLDAPEntityDAO.java?rev=692497&view=auto
==============================================================================
--- portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/ldap/dao/impl/SpringLDAPEntityDAO.java (added)
+++ portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/ldap/dao/impl/SpringLDAPEntityDAO.java Fri Sep  5 10:26:17 2008
@@ -0,0 +1,187 @@
+/* 
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jetspeed.security.mapping.ldap.dao.impl;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Iterator;
+
+import javax.naming.directory.SearchControls;
+
+import org.apache.commons.lang.StringUtils;
+import org.springframework.ldap.core.ContextMapper;
+import org.springframework.ldap.core.LdapTemplate;
+import org.springframework.ldap.filter.EqualsFilter;
+import org.springframework.ldap.filter.Filter;
+import org.springframework.ldap.filter.OrFilter;
+
+import org.apache.jetspeed.security.mapping.ldap.dao.DefaultEntityContextMapper;
+import org.apache.jetspeed.security.mapping.ldap.dao.EntityDAO;
+import org.apache.jetspeed.security.mapping.ldap.dao.LDAPEntityDAOConfiguration;
+import org.apache.jetspeed.security.mapping.ldap.dao.SearchUtil;
+import org.apache.jetspeed.security.mapping.ldap.filter.SimpleFilter;
+import org.apache.jetspeed.security.mapping.model.Entity;
+
+/**
+ * @author <a href="mailto:ddam@apache.org">Dennis Dam</a>
+ */
+public class SpringLDAPEntityDAO implements EntityDAO
+{
+
+    protected LdapTemplate ldapTemplate;
+
+    protected LDAPEntityDAOConfiguration configuration;
+
+    private ContextMapper contextMapper;
+
+    public void initialize(LdapTemplate ldapTemplate)
+    {
+        this.ldapTemplate = ldapTemplate;
+    }
+
+    public Entity getEntity(String entityId)
+    {
+        Filter idFilter = createFilterForIdSearch(entityId);
+        Collection<Entity> entities = getEntities(idFilter);
+        if (entities != null && entities.size() == 1)
+        {
+            return entities.iterator().next();
+        } else
+        {
+            return null;
+        }
+    }
+
+    public Collection<Entity> getEntitiesById(Collection<String> entityIds)
+    {
+        OrFilter idFilter = new OrFilter();
+        String idAttr = configuration.getLdapIdAttribute();
+        for (String id : entityIds)
+        {
+            idFilter.or(new EqualsFilter(idAttr, id));
+        }
+        Filter combinedFilter = null;
+        if (configuration.getBaseFilter() != null)
+        {
+            combinedFilter = SearchUtil.andFilters(idFilter, configuration
+                    .getBaseFilter());
+        } else
+        {
+            combinedFilter = idFilter;
+        }
+        return getEntities(combinedFilter);
+    }
+
+    public Collection<Entity> getEntitiesByInternalId(
+            Collection<String> internalIds)
+    {
+        final Collection<Entity> resultSet = new ArrayList<Entity>();
+        for (Iterator<String> iterator = internalIds.iterator(); iterator
+                .hasNext();)
+        {
+            String internalId = (String) iterator.next();
+            Entity resultEntity = (Entity) ldapTemplate.lookup(internalId,
+                    contextMapper);
+            if (resultEntity != null)
+            {
+                resultSet.add(resultEntity);
+            }
+        }
+        return resultSet;
+    }
+
+    @SuppressWarnings("unchecked")
+    public Collection<Entity> getEntities(Filter filter)
+    {
+        if (configuration.getBaseFilter() != null)
+        {
+            if (filter == null)
+            {
+                filter = configuration.getBaseFilter();
+            } else
+            {
+                filter = SearchUtil.andFilters(configuration.getBaseFilter(),
+                        filter);
+            }
+        }
+        String filterStr = filter.toString();
+        if (StringUtils.isEmpty(filterStr))
+        {
+            filterStr = "(objectClass=*)"; // trivial search query
+        }
+        return (Collection<Entity>) ldapTemplate.search(configuration
+                .getBaseDN(), filterStr, SearchControls.SUBTREE_SCOPE,
+                getContextMapper());
+    }
+
+    public Collection<Entity> getAllEntities()
+    {
+        final String finalFilter = configuration.getBaseFilter() != null ? configuration
+                .getBaseFilter().encode()
+                : "(objectClass=*)";
+        return getEntities(new SimpleFilter(finalFilter));
+    }
+
+    public void update(Entity entity)
+    {
+    }
+
+    public void addEntity(Entity entity)
+    {
+    }
+
+    public void removeEntity(Entity entity)
+    {
+    }
+
+    public LDAPEntityDAOConfiguration getConfiguration()
+    {
+        return configuration;
+    }
+
+    public void setConfiguration(LDAPEntityDAOConfiguration configuration)
+    {
+        this.configuration = configuration;
+    }
+
+    protected Filter createFilterForIdSearch(String entityId)
+    {
+        return SearchUtil.constructMatchingFieldsFilter(configuration
+                .getBaseFilter(), new String[]
+        { configuration.getLdapIdAttribute(), entityId});
+    }
+
+    public ContextMapper getContextMapper()
+    {
+        if (contextMapper == null && configuration != null)
+        {
+            contextMapper = new DefaultEntityContextMapper(configuration);
+        }
+        return contextMapper;
+    }
+
+    public void setLdapTemplate(LdapTemplate ldapTemplate)
+    {
+        this.ldapTemplate = ldapTemplate;
+    }
+
+    public void setContextMapper(ContextMapper contextMapper)
+    {
+        this.contextMapper = contextMapper;
+    }
+
+}

Added: portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/ldap/filter/SimpleFilter.java
URL: http://svn.apache.org/viewvc/portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/ldap/filter/SimpleFilter.java?rev=692497&view=auto
==============================================================================
--- portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/ldap/filter/SimpleFilter.java (added)
+++ portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/ldap/filter/SimpleFilter.java Fri Sep  5 10:26:17 2008
@@ -0,0 +1,46 @@
+/* 
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jetspeed.security.mapping.ldap.filter;
+
+import org.springframework.ldap.filter.Filter;
+
+/**
+ * Filter which holds an LDAP query and simply returns it.
+ * 
+ * @author <a href="mailto:ddam@apache.org">Dennis Dam</a>
+ */
+public class SimpleFilter implements Filter
+{
+
+    private String ldapFilter;
+
+    public SimpleFilter(String ldapFilter)
+    {
+        this.ldapFilter = ldapFilter == null ? "" : ldapFilter;
+    }
+
+    public String encode()
+    {
+        return ldapFilter;
+    }
+
+    public StringBuffer encode(StringBuffer sb)
+    {
+        return sb.append(ldapFilter);
+    }
+
+}

Added: portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/model/Attribute.java
URL: http://svn.apache.org/viewvc/portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/model/Attribute.java?rev=692497&view=auto
==============================================================================
--- portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/model/Attribute.java (added)
+++ portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/model/Attribute.java Fri Sep  5 10:26:17 2008
@@ -0,0 +1,38 @@
+/* 
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jetspeed.security.mapping.model;
+
+import java.util.Collection;
+
+/**
+ * @author <a href="mailto:ddam@apache.org">Dennis Dam</a>
+ */
+public interface Attribute
+{
+
+    String getName();
+
+    String getValue();
+
+    void setValue(String o);
+
+    Collection<String> getValues();
+
+    void setValues(Collection<String> values);
+
+    AttributeDef getDefinition();
+}

Added: portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/model/AttributeDef.java
URL: http://svn.apache.org/viewvc/portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/model/AttributeDef.java?rev=692497&view=auto
==============================================================================
--- portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/model/AttributeDef.java (added)
+++ portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/model/AttributeDef.java Fri Sep  5 10:26:17 2008
@@ -0,0 +1,29 @@
+/* 
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jetspeed.security.mapping.model;
+
+/**
+ * @author <a href="mailto:ddam@apache.org">Dennis Dam</a>
+ */
+public interface AttributeDef
+{
+
+    String getName();
+
+    boolean isMultiValue();
+
+}

Added: portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/model/Entity.java
URL: http://svn.apache.org/viewvc/portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/model/Entity.java?rev=692497&view=auto
==============================================================================
--- portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/model/Entity.java (added)
+++ portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/model/Entity.java Fri Sep  5 10:26:17 2008
@@ -0,0 +1,44 @@
+/* 
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jetspeed.security.mapping.model;
+
+import java.util.Collection;
+import java.util.Set;
+
+/**
+ * @author <a href="mailto:ddam@apache.org">Dennis Dam</a>
+ */
+public interface Entity
+{
+
+    String getType();
+
+    String getId();
+
+    String getInternalId();
+
+    Attribute getAttribute(String name);
+
+    void setAttribute(String name, String value);
+
+    void setAttribute(String name, Collection<String> values);
+
+    void setAttributes(Set<Attribute> attributes);
+
+    Set<AttributeDef> getAllowedAttributes();
+
+}

Added: portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/model/EntityDAO.java
URL: http://svn.apache.org/viewvc/portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/model/EntityDAO.java?rev=692497&view=auto
==============================================================================
--- portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/model/EntityDAO.java (added)
+++ portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/model/EntityDAO.java Fri Sep  5 10:26:17 2008
@@ -0,0 +1,41 @@
+/* 
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jetspeed.security.mapping.model;
+
+import java.util.Collection;
+
+/**
+ * @author <a href="mailto:ddam@apache.org">Dennis Dam</a>
+ */
+public interface EntityDAO
+{
+
+    Entity getEntity(String entityId);
+
+    Collection<Entity> getEntities();
+
+    Collection<Entity> getEntities(Collection<String> entityIds);
+
+    void update(String entityId, Entity entity);
+
+    Collection<Entity> getRelatedEntities(Entity entity, String entityType,
+            String relationType);
+
+    void setRelatedEntities(String relationType, Entity entity,
+            Collection<Entity> entities);
+
+}

Added: portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/model/EntityRelationDAO.java
URL: http://svn.apache.org/viewvc/portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/model/EntityRelationDAO.java?rev=692497&view=auto
==============================================================================
--- portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/model/EntityRelationDAO.java (added)
+++ portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/model/EntityRelationDAO.java Fri Sep  5 10:26:17 2008
@@ -0,0 +1,31 @@
+/* 
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jetspeed.security.mapping.model;
+
+import java.util.Collection;
+
+/**
+ * @author <a href="mailto:ddam@apache.org">Dennis Dam</a>
+ */
+public interface EntityRelationDAO
+{
+
+    Collection<Entity> getRelatedEntities(Entity entity);
+
+    Entity getRelatedEntity(Entity entity);
+
+}

Added: portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/model/impl/AttributeDefImpl.java
URL: http://svn.apache.org/viewvc/portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/model/impl/AttributeDefImpl.java?rev=692497&view=auto
==============================================================================
--- portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/model/impl/AttributeDefImpl.java (added)
+++ portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/model/impl/AttributeDefImpl.java Fri Sep  5 10:26:17 2008
@@ -0,0 +1,83 @@
+/* 
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jetspeed.security.mapping.model.impl;
+
+import org.apache.jetspeed.security.mapping.model.AttributeDef;
+
+/**
+ * @author <a href="mailto:ddam@apache.org">Dennis Dam</a>
+ */
+public class AttributeDefImpl implements AttributeDef
+{
+
+    private String name;
+
+    private boolean multiValue; // default is single-valued
+
+    public AttributeDefImpl(String name)
+    {
+        super();
+        this.name = name;
+    }
+
+    public AttributeDefImpl(String name, boolean multiValue)
+    {
+        super();
+        this.name = name;
+        this.multiValue=multiValue;
+    }
+    public String getName()
+    {
+        return name;
+    }
+
+    public boolean isMultiValue()
+    {
+        return multiValue;
+    }
+
+    public void setMultiValue(boolean multiValue)
+    {
+        this.multiValue = multiValue;
+    }
+
+    @Override
+    public int hashCode()
+    {
+        final int prime = 31;
+        int result = 1;
+        result = prime * result + (multiValue ? 1231 : 1237);
+        result = prime * result + ((name == null) ? 0 : name.hashCode());
+        return result;
+    }
+
+    @Override
+    public boolean equals(Object obj)
+    {
+        if (this == obj) return true;
+        if (obj == null) return false;
+        if (getClass() != obj.getClass()) return false;
+        AttributeDefImpl other = (AttributeDefImpl) obj;
+        if (multiValue != other.multiValue) return false;
+        if (name == null)
+        {
+            if (other.name != null) return false;
+        } else if (!name.equals(other.name)) return false;
+        return true;
+    }
+
+}

Added: portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/model/impl/AttributeImpl.java
URL: http://svn.apache.org/viewvc/portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/model/impl/AttributeImpl.java?rev=692497&view=auto
==============================================================================
--- portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/model/impl/AttributeImpl.java (added)
+++ portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/model/impl/AttributeImpl.java Fri Sep  5 10:26:17 2008
@@ -0,0 +1,111 @@
+/* 
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jetspeed.security.mapping.model.impl;
+
+import java.util.ArrayList;
+import java.util.Collection;
+
+import org.apache.jetspeed.security.mapping.model.Attribute;
+import org.apache.jetspeed.security.mapping.model.AttributeDef;
+
+/**
+ * @author <a href="mailto:ddam@apache.org">Dennis Dam</a>
+ */
+public class AttributeImpl implements Attribute
+{
+
+    private Collection<String> values = new ArrayList<String>();
+
+    private String value;
+
+    private AttributeDef definition;
+
+    public AttributeImpl(AttributeDef definition)
+    {
+        super();
+        this.definition = definition;
+    }
+
+    public String getValue()
+    {
+        return getDefinition().isMultiValue() ? null : value;
+    }
+
+    public void setValue(String value)
+    {
+        this.values = null;
+        this.value = value;
+    }
+
+    public String getName()
+    {
+        return definition.getName();
+    }
+
+    public Collection<String> getValues()
+    {
+        return getDefinition().isMultiValue() ? values : null;
+    }
+
+    public void setValues(Collection<String> values)
+    {
+        if (getDefinition().isMultiValue())
+        {
+            this.values = values;
+        }
+    }
+
+    @Override
+    public int hashCode()
+    {
+        final int prime = 31;
+        int result = 1;
+        result = prime * result
+                + ((definition == null) ? 0 : definition.hashCode());
+        result = prime * result + ((value == null) ? 0 : value.hashCode());
+        result = prime * result + ((values == null) ? 0 : values.hashCode());
+        return result;
+    }
+
+    @Override
+    public boolean equals(Object obj)
+    {
+        if (this == obj) return true;
+        if (obj == null) return false;
+        if (getClass() != obj.getClass()) return false;
+        AttributeImpl other = (AttributeImpl) obj;
+        if (definition == null)
+        {
+            if (other.definition != null) return false;
+        } else if (!definition.equals(other.definition)) return false;
+        if (value == null)
+        {
+            if (other.value != null) return false;
+        } else if (!value.equals(other.value)) return false;
+        if (values == null)
+        {
+            if (other.values != null) return false;
+        } else if (!values.equals(other.values)) return false;
+        return true;
+    }
+
+    public AttributeDef getDefinition()
+    {
+        return definition;
+    }
+
+}

Added: portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/model/impl/DefaultEntityRelationSearch.java
URL: http://svn.apache.org/viewvc/portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/model/impl/DefaultEntityRelationSearch.java?rev=692497&view=auto
==============================================================================
--- portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/model/impl/DefaultEntityRelationSearch.java (added)
+++ portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/model/impl/DefaultEntityRelationSearch.java Fri Sep  5 10:26:17 2008
@@ -0,0 +1,103 @@
+/* 
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jetspeed.security.mapping.model.impl;
+
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.Set;
+
+import org.apache.commons.collections.CollectionUtils;
+
+import org.apache.jetspeed.security.mapping.model.Attribute;
+import org.apache.jetspeed.security.mapping.model.Entity;
+import org.apache.jetspeed.security.mapping.model.EntityRelationDAO;
+import org.apache.jetspeed.security.mapping.model.EntityDAO;
+
+/**
+ * @author <a href="mailto:ddam@apache.org">Dennis Dam</a>
+ */
+public class DefaultEntityRelationSearch implements EntityRelationDAO
+{
+
+    private EntityDAO toEntitySearch;
+
+    private String relationAttribute;
+
+    @SuppressWarnings("unchecked")
+    public Collection<Entity> getRelatedEntities(Entity entity)
+    {
+        Set<String> relatedEntityIds = getRelatedEntityIds(entity);
+        if (relatedEntityIds.size() > 0)
+        {
+            return toEntitySearch.getEntities(relatedEntityIds);
+        } else
+        {
+            return CollectionUtils.EMPTY_COLLECTION;
+        }
+    }
+
+    public Entity getRelatedEntity(Entity entity)
+    {
+        Collection<Entity> entities = getRelatedEntities(entity);
+        if (entities != null && entities.size() == 1)
+        {
+            return entities.iterator().next();
+        } else
+        {
+            return null;
+        }
+    }
+
+    protected Set<String> getRelatedEntityIds(Entity entity)
+    {
+        Attribute relatedAttr = entity.getAttribute(relationAttribute);
+        Set<String> foundIds = new HashSet();
+
+        if (relatedAttr != null)
+        {
+            if (relatedAttr.getDefinition().isMultiValue())
+            {
+                foundIds.addAll(relatedAttr.getValues());
+            } else
+            {
+                // TODO: if single value, parse value as CSV string
+            }
+        }
+        return foundIds;
+    }
+
+    public EntityDAO getToEntitySearch()
+    {
+        return toEntitySearch;
+    }
+
+    public void setToEntitySearch(EntityDAO toEntitySearch)
+    {
+        this.toEntitySearch = toEntitySearch;
+    }
+
+    public String getRelationAttribute()
+    {
+        return relationAttribute;
+    }
+
+    public void setRelationAttribute(String relationAttribute)
+    {
+        this.relationAttribute = relationAttribute;
+    }
+
+}

Added: portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/model/impl/EntityImpl.java
URL: http://svn.apache.org/viewvc/portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/model/impl/EntityImpl.java?rev=692497&view=auto
==============================================================================
--- portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/model/impl/EntityImpl.java (added)
+++ portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/model/impl/EntityImpl.java Fri Sep  5 10:26:17 2008
@@ -0,0 +1,185 @@
+/* 
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jetspeed.security.mapping.model.impl;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.jetspeed.security.mapping.model.Attribute;
+import org.apache.jetspeed.security.mapping.model.AttributeDef;
+import org.apache.jetspeed.security.mapping.model.Entity;
+
+/**
+ * @author <a href="mailto:ddam@apache.org">Dennis Dam</a>
+ */
+public class EntityImpl implements Entity
+{
+
+    private Map<String, Attribute> nameToAttributeMap = new HashMap<String, Attribute>();
+
+    private final Set<AttributeDef> allowedAttributes;
+
+    private String id;
+
+    private String internalId;
+
+    private String type;
+
+    public EntityImpl(String type, String id,
+            Set<AttributeDef> allowedAttributes)
+    {
+        this.type = type;
+        this.id = id;
+        this.allowedAttributes = Collections.unmodifiableSet(allowedAttributes);
+    }
+
+    public String getType()
+    {
+        return type;
+    }
+
+    public void setType(String type)
+    {
+        this.type = type;
+    }
+
+    public Attribute getAttribute(String name)
+    {
+        return nameToAttributeMap.get(name);
+    }
+
+    public Set<AttributeDef> getAllowedAttributes()
+    {
+        return allowedAttributes;
+    }
+
+    public String getId()
+    {
+        return id;
+    }
+
+    public void setId(String id)
+    {
+        this.id = id;
+    }
+
+    protected AttributeDef getAttributeDefinition(String name)
+    {
+        for (AttributeDef def : allowedAttributes)
+        {
+            if (def.getName().equals(name)) { return def; }
+        }
+        return null;
+    }
+
+    public void setAttribute(String name, String value)
+    {
+        Attribute attr = nameToAttributeMap.get(name);
+        if (attr == null)
+        {
+            AttributeDef def = getAttributeDefinition(name);
+            if (def == null) { return; // TODO: throw proper exception
+            }
+            if (def.isMultiValue()) { return; // TODO: throw proper exception
+            }
+            attr = new AttributeImpl(def);
+            nameToAttributeMap.put(name, attr);
+        }
+        attr.setValue(value);
+    }
+
+    public void setAttribute(String name, Collection<String> values)
+    {
+        Attribute attr = nameToAttributeMap.get(name);
+        if (attr == null)
+        {
+            AttributeDef def = getAttributeDefinition(name);
+            if (def == null) { return; // TODO: throw proper exception
+            }
+            if (!def.isMultiValue()) { return; // TODO: throw proper exception
+            }
+            attr = new AttributeImpl(def);
+            nameToAttributeMap.put(name, attr);
+        }
+        attr.setValues(values);
+    }
+
+    public void setAttributes(Set<Attribute> attributes)
+    {
+        for (Attribute attribute : attributes)
+        {
+            nameToAttributeMap.put(attribute.getName(), attribute);
+        }
+    }
+
+    public String getInternalId()
+    {
+        return internalId;
+    }
+
+    public void setInternalId(String internalId)
+    {
+        this.internalId = internalId;
+    }
+
+    @Override
+    public int hashCode()
+    {
+        final int prime = 31;
+        int result = 1;
+        result = prime * result + ((id == null) ? 0 : id.hashCode());
+        result = prime * result
+                + ((internalId == null) ? 0 : internalId.hashCode());
+        result = prime
+                * result
+                + ((nameToAttributeMap == null) ? 0 : nameToAttributeMap
+                        .hashCode());
+        result = prime * result + ((type == null) ? 0 : type.hashCode());
+        return result;
+    }
+
+    @Override
+    public boolean equals(Object obj)
+    {
+        if (this == obj) return true;
+        if (obj == null) return false;
+        if (getClass() != obj.getClass()) return false;
+        EntityImpl other = (EntityImpl) obj;
+        if (id == null)
+        {
+            if (other.id != null) return false;
+        } else if (!id.equals(other.id)) return false;
+        if (internalId == null)
+        {
+            if (other.internalId != null) return false;
+        } else if (!internalId.equals(other.internalId)) return false;
+        if (nameToAttributeMap == null)
+        {
+            if (other.nameToAttributeMap != null) return false;
+        } else if (!nameToAttributeMap.equals(other.nameToAttributeMap))
+            return false;
+        if (type == null)
+        {
+            if (other.type != null) return false;
+        } else if (!type.equals(other.type)) return false;
+        return true;
+    }
+
+}

Added: portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/test/java/org/apache/jetspeed/security/mapping/ldap/AbstractLDAPTest.java
URL: http://svn.apache.org/viewvc/portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/test/java/org/apache/jetspeed/security/mapping/ldap/AbstractLDAPTest.java?rev=692497&view=auto
==============================================================================
--- portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/test/java/org/apache/jetspeed/security/mapping/ldap/AbstractLDAPTest.java (added)
+++ portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/test/java/org/apache/jetspeed/security/mapping/ldap/AbstractLDAPTest.java Fri Sep  5 10:26:17 2008
@@ -0,0 +1,141 @@
+/* 
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jetspeed.security.mapping.ldap;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Set;
+
+import javax.naming.directory.DirContext;
+
+import junit.framework.TestCase;
+
+import org.apache.commons.io.IOUtils;
+import org.apache.directory.server.protocol.shared.store.LdifFileLoader;
+import org.springframework.core.io.Resource;
+import org.springframework.ldap.core.ContextSource;
+import org.springframework.ldap.core.LdapTemplate;
+import org.springframework.ldap.core.support.LdapContextSource;
+
+import org.apache.jetspeed.security.mapping.ldap.dao.DefaultLDAPEntityManager;
+import org.apache.jetspeed.security.mapping.ldap.dao.LDAPEntityDAOConfiguration;
+import org.apache.jetspeed.security.mapping.model.Attribute;
+import org.apache.jetspeed.security.mapping.model.AttributeDef;
+import org.apache.jetspeed.security.mapping.model.Entity;
+
+/**
+ * @author <a href="mailto:ddam@apache.org">Dennis Dam</a>
+ */
+public abstract class AbstractLDAPTest extends TestCase
+{
+
+    protected LdapTemplate ldapTemplate;
+
+    protected ContextSource contextSource;
+
+    protected String baseDN;
+
+    protected DefaultLDAPEntityManager entityManager;
+
+    protected LDAPEntityDAOConfiguration userSearchConfig;
+
+    protected boolean debugMode = false;
+
+    protected BasicTestCases basicTestCases;
+
+    public void setUp() throws Exception
+    {
+        baseDN = "o=sevenSeas";
+        // TODO : move config to build environment
+        LdapContextSource contextSource = new LdapContextSource();
+        contextSource.setUrl("ldap://localhost:389");
+        contextSource.setBase(baseDN);
+        contextSource.setUserDn("cn=admin,o=sevenSeas");
+        contextSource.setPassword("secret");
+        contextSource.afterPropertiesSet();
+        ldapTemplate = new LdapTemplate();
+        ldapTemplate.setContextSource(contextSource);
+
+        try
+        {
+            emptyLDAP();
+        } catch (Exception e)
+        {
+            if (debugMode)
+            {
+                e.printStackTrace();
+            }
+        }
+
+        DirContext dirContext = ldapTemplate.getContextSource()
+                .getReadWriteContext();
+        loadLdifs(ldapTemplate.getContextSource().getReadWriteContext(),
+                initializationData());
+        internalSetUp();
+
+        basicTestCases = new BasicTestCases(entityManager, debugMode);
+    }
+
+    public static void loadLdifs(DirContext context, Resource[] ldifFiles)
+            throws IOException
+    {
+
+        for (int i = 0; i < ldifFiles.length; i++)
+        {
+            File tempFile = File.createTempFile("spring_ldap_test", ".ldif");
+            try
+            {
+                InputStream inputStream = ldifFiles[i].getInputStream();
+                IOUtils.copy(inputStream, new FileOutputStream(tempFile));
+                LdifFileLoader fileLoader = new LdifFileLoader(context,
+                        tempFile.getAbsolutePath());
+                fileLoader.execute();
+            } finally
+            {
+                try
+                {
+                    tempFile.delete();
+                } catch (Exception e)
+                {
+                    // Ignore this
+                }
+            }
+        }
+    }
+
+    private void emptyLDAP() throws Exception
+    {
+        ldapTemplate.unbind("", true); // recursively delete root node of ldap
+    }
+
+    @Override
+    protected void tearDown() throws Exception
+    {
+        super.tearDown();
+        internaltearDown();
+        emptyLDAP();
+    }
+
+    public abstract void internalSetUp() throws Exception;
+
+    protected abstract void internaltearDown() throws Exception;
+
+    protected abstract Resource[] initializationData() throws Exception;
+
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: jetspeed-dev-unsubscribe@portals.apache.org
For additional commands, e-mail: jetspeed-dev-help@portals.apache.org