Show
Ignore:
Timestamp:
09/04/10 22:05:44 (21 months ago)
Author:
ffray
Message:

Added navigateTo-elements to auth-constraints allowing to create
conditional navigation-cases.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • branches/release-0.13.x/pfixcore/src/org/pustefixframework/config/contextxmlservice/parser/ConditionParsingHandler.java

    r3822 r5341  
    3838import de.schlund.pfixcore.auth.conditions.ConditionGroup; 
    3939import de.schlund.pfixcore.auth.conditions.HasRole; 
     40import de.schlund.pfixcore.auth.conditions.NavigationCase; 
    4041import de.schlund.pfixcore.auth.conditions.Not; 
    4142import de.schlund.pfixcore.auth.conditions.Or; 
     
    4748 */ 
    4849public class ConditionParsingHandler implements ParsingHandler { 
    49  
    50     public void handleNode(HandlerContext context) throws ParserException { 
    51  
    52         Element element = (Element)context.getNode(); 
    53         ParsingUtils.checkAttributes(element, null, new String[] {"name", "class", "ref", "id"}); 
    54          
    55         ContextXMLServletConfigImpl config = ParsingUtils.getSingleTopObject(ContextXMLServletConfigImpl.class, context); 
    56          
     50     
     51    private Condition findParentCondition(HandlerContext context) { 
    5752        Iterator<Condition> parentConditions = context.getObjectTreeElement().getObjectsOfTypeFromTopTree(Condition.class).iterator(); 
    5853        Condition parentCondition = null; 
    5954        if(parentConditions.hasNext()) parentCondition = parentConditions.next(); 
    60         boolean inCondition = ( parentCondition != null); 
    61         boolean inPageRequest = isInPageRequest(element); 
    62          
    63         String name = element.getNodeName(); 
    64         Condition condition = null; 
     55        return parentCondition; 
     56    } 
     57 
     58    public void handleNode(HandlerContext context) throws ParserException { 
     59        Condition condition = parseCondition(context); 
     60 
     61        //assemble the parent element by setting or adding the parsed condition 
     62        Condition parentCondition = findParentCondition(context); 
     63        if(parentCondition != null) { 
     64            if (parentCondition instanceof NavigationCase) { 
     65                ((NavigationCase) parentCondition).setCondition(condition); 
     66            } else if (parentCondition instanceof AuthConstraint) { 
     67                AuthConstraintImpl authConstraint = (AuthConstraintImpl) parentCondition; 
     68                if (condition instanceof NavigationCase) { 
     69                    authConstraint.addNavigationCase(((NavigationCase) condition)); 
     70                } else { 
     71                    authConstraint.setCondition(condition); 
     72                } 
     73            } else if (parentCondition instanceof ConditionGroup) { 
     74                ((ConditionGroup) parentCondition).add(condition); 
     75            } else if (parentCondition instanceof Not) { 
     76                ((Not) parentCondition).set(condition); 
     77            } else throw new ParserException("Illegal object: " + parentCondition.getClass().getName()); 
     78        } 
     79 
     80        context.getObjectTreeElement().addObject(condition); 
     81    } 
     82 
     83    private ContextXMLServletConfigImpl getConfig(HandlerContext context) throws ParserException { 
     84        return ParsingUtils.getSingleTopObject(ContextXMLServletConfigImpl.class, context); 
     85    } 
     86 
     87    private Condition parseCondition(HandlerContext context) throws ParserException { 
     88 
     89        Element element = (Element)context.getNode(); 
     90        ParsingUtils.checkAttributes(element, null, new String[] {"page", "name", "class", "ref", "id"}); 
     91 
     92        String name = element.getTagName(); 
     93 
     94        Condition condition; 
    6595        if (name.equals("or")) { 
    6696            condition = new Or(); 
     
    70100            condition = new Not(); 
    71101        } else if (name.equals("hasrole")) { 
     102            ContextXMLServletConfigImpl config = getConfig(context); 
    72103            String roleName = element.getAttribute("name").trim(); 
    73104            if(roleName.equals("")) throw new ParserException("Element 'hasrole' requires 'name' attribute value."); 
     
    76107            if (role == null) throw new ParserException("Condition hasrole references unknown role: " + roleName); 
    77108        } else if (name.equals("condition")) { 
    78             if (!inCondition) { 
     109            if (findParentCondition(context) == null) { 
    79110                String id = element.getAttribute("id").trim(); 
    80111                condition = createCondition(element); 
     112                ContextXMLServletConfigImpl config = getConfig(context); 
    81113                config.getContextConfig().addCondition(id, condition); 
    82114            } else { 
    83115                String ref = element.getAttribute("ref").trim(); 
    84116                if (ref != null) { 
     117                    ContextXMLServletConfigImpl config = ParsingUtils.getSingleTopObject(ContextXMLServletConfigImpl.class, context); 
    85118                    condition = config.getContextConfig().getCondition(ref); 
    86119                    if (condition == null) throw new ParserException("Condition reference not found: " + ref); 
     
    92125            String ref = element.getAttribute("ref").trim(); 
    93126            if(ref.equals("")) throw new ParserException("Nested authconstraint requires 'ref' attribute."); 
    94             if(inPageRequest) { 
     127            if(isInPageRequest(element)) { 
     128                ContextXMLServletConfigImpl config = getConfig(context); 
    95129                AuthConstraint constraint = config.getContextConfig().getAuthConstraint(ref); 
    96130                if(constraint == null) throw new ParserException("Referenced authconstraint not found: "+ref); 
     
    99133                condition = new AuthConstraintRef(ref); 
    100134            } 
    101         } else throw new ParserException("Unsupported condition: " + name); 
    102        
    103         if(inCondition) { 
    104             if (parentCondition instanceof AuthConstraint) { 
    105                 ((AuthConstraintImpl) parentCondition).setCondition(condition); 
    106             } else if (parentCondition instanceof ConditionGroup) { 
    107                 ((ConditionGroup) parentCondition).add(condition); 
    108             } else if (parentCondition instanceof Not) { 
    109                 ((Not) parentCondition).set(condition); 
    110             } else throw new ParserException("Illegal object: " + parentCondition.getClass().getName()); 
     135        } else if (name.equals("navigateTo")) { 
     136            String page = element.getAttribute("page").trim(); 
     137            if (page.equals("")) { 
     138                throw new ParserException("Element navigation requires 'page' attribute."); 
     139            } 
     140            NavigationCase navigationCase = new NavigationCase(page); 
     141            condition = navigationCase; 
     142        } else { 
     143            throw new ParserException("Unsupported condition: " + name); 
    111144        } 
    112          
     145 
    113146        PropertyParsingUtils.setProperties(condition, element); 
    114          
    115         context.getObjectTreeElement().addObject(condition); 
     147 
     148        return condition; 
    116149    } 
    117      
     150 
    118151    private Condition createCondition(Element element) throws ParserException { 
    119152        String className = element.getAttribute("class").trim(); 
     
    128161        } 
    129162    } 
    130      
     163 
    131164    private boolean isInPageRequest(Element element) { 
    132165        Node parent = element.getParentNode();