Author: ate
Date: Fri Mar 19 17:30:50 2010
New Revision: 925347

URL: http://svn.apache.org/viewvc?rev=925347&view=rev
Log:
JS2-1031: Ldap AttributeBasedRelationDAO class gets NullPointerException when 
trying to create a relation between two ldap entities
See: http://issues.apache.org/jira/browse/JS2-1031
Fixed by adding a new method Entity.getAttribute(String name, boolean create) 
which can automatically add an attribute of the right type (multi or single 
value). 

Modified:
    
portals/jetspeed-2/portal/trunk/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/ldap/dao/impl/AttributeBasedRelationDAO.java
    
portals/jetspeed-2/portal/trunk/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/model/Entity.java
    
portals/jetspeed-2/portal/trunk/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/model/impl/EntityImpl.java

Modified: 
portals/jetspeed-2/portal/trunk/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/trunk/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/ldap/dao/impl/AttributeBasedRelationDAO.java?rev=925347&r1=925346&r2=925347&view=diff
==============================================================================
--- 
portals/jetspeed-2/portal/trunk/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/ldap/dao/impl/AttributeBasedRelationDAO.java
 (original)
+++ 
portals/jetspeed-2/portal/trunk/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/ldap/dao/impl/AttributeBasedRelationDAO.java
 Fri Mar 19 17:30:50 2010
@@ -16,7 +16,6 @@
  */
 package org.apache.jetspeed.security.mapping.ldap.dao.impl;
 
-import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Iterator;
 
@@ -161,18 +160,11 @@ public class AttributeBasedRelationDAO e
         {
             attrValue = toEntity.getId();
         }
-        Attribute relationAttribute = 
fromEntity.getAttribute(this.relationAttribute);
-        if (relationAttribute == null)
+        Attribute relationAttribute = 
fromEntity.getAttribute(this.relationAttribute, true);
+        if (relationAttribute.getValues().contains(attrValue))
         {
-            fromEntity.setAttribute(this.relationAttribute, new 
ArrayList<String>());
-        }
-        else
-        {
-            if (relationAttribute.getValues().contains(attrValue))
-            {
-                throw new 
SecurityException(SecurityException.PRINCIPAL_ASSOCIATION_ALREADY_EXISTS.createScoped(fromEntity.getType(),
 fromEntity.getId(),
-                                                                               
                                 relationAttribute, toEntity.getId()));
-            }
+            throw new 
SecurityException(SecurityException.PRINCIPAL_ASSOCIATION_ALREADY_EXISTS.createScoped(fromEntity.getType(),
 fromEntity.getId(),
+                                                                               
                             relationAttribute, toEntity.getId()));
         }
         if (relationAttribute.getDefinition().isMultiValue())
         {
@@ -188,51 +180,54 @@ public class AttributeBasedRelationDAO e
     private void internalRemoveRelation(EntityDAO fromEntityDAO, EntityDAO 
toEntityDAO, Entity fromEntity, Entity toEntity) throws SecurityException
     {
         fromEntity = fromEntityDAO.getEntity(fromEntity.getId());
-        toEntity = toEntityDAO.getEntity(toEntity.getId());
-        String attrValue = null;
-        if (attributeContainsInternalId)
-        {
-            if (toEntity.getInternalId() == null)
-            {
-                // internal ID (ldap DN) is not present, refetch the entity 
from LDAP to get the DN
-                toEntity = toEntityDAO.getEntity(toEntity.getId());
-            }
-            attrValue = toEntity.getInternalId();
-        }
-        else
-        {
-            attrValue = toEntity.getId();
-        }
         Attribute relationAttribute = 
fromEntity.getAttribute(this.relationAttribute);
-        if (relationAttribute.getDefinition().isMultiValue())
+        if (relationAttribute != null)
         {
-            DistinguishedName attrib = new DistinguishedName(attrValue);
+            toEntity = toEntityDAO.getEntity(toEntity.getId());
+            String attrValue = null;
             if (attributeContainsInternalId)
             {
-                boolean found = false;
-                String attribValue = null;
-                Iterator<String> iterator = 
relationAttribute.getValues().iterator();
-                while (iterator.hasNext() && !found)
-                {
-                    attribValue = iterator.next();
-                    DistinguishedName ldapAttr = new 
DistinguishedName(attribValue);
-                    if (ldapAttr.equals(attrib))
+                if (toEntity.getInternalId() == null)
+                {
+                    // internal ID (ldap DN) is not present, refetch the 
entity from LDAP to get the DN
+                    toEntity = toEntityDAO.getEntity(toEntity.getId());
+                }
+                attrValue = toEntity.getInternalId();
+            }
+            else
+            {
+                attrValue = toEntity.getId();
+            }
+            if (relationAttribute.getDefinition().isMultiValue())
+            {
+                DistinguishedName attrib = new DistinguishedName(attrValue);
+                if (attributeContainsInternalId)
+                {
+                    boolean found = false;
+                    String attribValue = null;
+                    Iterator<String> iterator = 
relationAttribute.getValues().iterator();
+                    while (iterator.hasNext() && !found)
                     {
-                        relationAttribute.getValues().remove(attribValue);
-                        found = true;
+                        attribValue = iterator.next();
+                        DistinguishedName ldapAttr = new 
DistinguishedName(attribValue);
+                        if (ldapAttr.equals(attrib))
+                        {
+                            relationAttribute.getValues().remove(attribValue);
+                            found = true;
+                        }
                     }
                 }
+                else
+                {
+                    relationAttribute.getValues().remove(attrValue);
+                }
             }
             else
             {
-                relationAttribute.getValues().remove(attrValue);
+                relationAttribute.setValue(null);
             }
+            fromEntityDAO.updateInternalAttributes(fromEntity);
         }
-        else
-        {
-            relationAttribute.setValue(null);
-        }
-        fromEntityDAO.updateInternalAttributes(fromEntity);
     }
 
     public void addRelation(EntityDAO sourceDao, EntityDAO targetDao, Entity 
sourceEntity, Entity targetEntity) throws SecurityException

Modified: 
portals/jetspeed-2/portal/trunk/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/model/Entity.java
URL: 
http://svn.apache.org/viewvc/portals/jetspeed-2/portal/trunk/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/model/Entity.java?rev=925347&r1=925346&r2=925347&view=diff
==============================================================================
--- 
portals/jetspeed-2/portal/trunk/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/model/Entity.java
 (original)
+++ 
portals/jetspeed-2/portal/trunk/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/model/Entity.java
 Fri Mar 19 17:30:50 2010
@@ -34,6 +34,8 @@ public interface Entity
 
     Attribute getAttribute(String name);
 
+    Attribute getAttribute(String name, boolean create);
+
     /**
      * Returns a read-only map of attributes (name to attribute). To add 
attributes, call one of the setAttribute() methods
      * 

Modified: 
portals/jetspeed-2/portal/trunk/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/trunk/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/model/impl/EntityImpl.java?rev=925347&r1=925346&r2=925347&view=diff
==============================================================================
--- 
portals/jetspeed-2/portal/trunk/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/model/impl/EntityImpl.java
 (original)
+++ 
portals/jetspeed-2/portal/trunk/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/mapping/model/impl/EntityImpl.java
 Fri Mar 19 17:30:50 2010
@@ -16,6 +16,7 @@
  */
 package org.apache.jetspeed.security.mapping.model.impl;
 
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
@@ -58,7 +59,31 @@ public class EntityImpl implements Entit
 
     public Attribute getAttribute(String name)
     {
-        return nameToAttributeMap.get(name);
+        return getAttribute(name,false);
+    }
+    
+    public Attribute getAttribute(String name, boolean create)
+    {
+        Attribute attr = nameToAttributeMap.get(name);
+        
+        if (attr == null && create)
+        {
+            AttributeDef def = getAttributeDefinition(name);
+            if (def == null)
+            {
+                // TODO: throw proper exception
+            }
+            else
+            {
+                attr = new AttributeImpl(def);
+                nameToAttributeMap.put(name, attr);
+                if (def.isMultiValue())
+                {
+                    attr.setValues(new ArrayList<String>());
+                }
+            }
+        }
+        return attr;
     }
 
     public Map<String, Attribute> getAttributes()



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to