3
votes

I implement a simple method, that iterate JSF components tree and sets the components to disabled. (So the user cannot change the values). But this method doesn't function for composite components. How can I detect a composite component at least? Then I can try to set the special attribute to disabled.

1
You can't detect whether something is a composite, since the only limitation on a composite component backing bean type is implementing NamingContainer, but there are plenty of other things implementing that interface. - rdcrng
Please ask one question per question. Do not chameleonize existing questions. It makes answers incomplete or even invalid. - BalusC
@BalusC Sorry, but this was my first question and the idea about security comes later when I thought about this. Could you recreate your answer? Your answer was very useful. - Tony

1 Answers

4
votes

The UIComponent class has a isCompositeComponent() helper method for exactly this purpose.

So, this should just do:

for (UIComponent child : component.getChildren()) {
    if (UIComponent.isCompositeComponent(child)) {
        // It's a composite child!
    }
}

For the interested in "under the covers" workings, here's the implementation source code from Mojarra 2.1.25:

public static boolean isCompositeComponent(UIComponent component) {

    if (component == null) {
        throw new NullPointerException();
    }
    boolean result = false;
    if (null != component.isCompositeComponent) {
        result = component.isCompositeComponent.booleanValue();
    } else {
        result = component.isCompositeComponent =
                (component.getAttributes().containsKey(
                           Resource.COMPONENT_RESOURCE_KEY));
    }
    return result;

}

It's thus identified by the presence of a component attribute with the name as definied by Resource.COMPONENT_RESOURCE_KEY which has the value of "javax.faces.application.Resource.ComponentResource".