Search in sources :

Example 6 with Resource

use of com.sun.identity.xacml.context.Resource in project OpenAM by OpenRock.

the class RequestImpl method processElement.

private void processElement(Element element) throws XACMLException {
    if (element == null) {
        XACMLSDKUtils.debug.error("RequestImpl.processElement(): invalid root element");
        throw new XACMLException(XACMLSDKUtils.xacmlResourceBundle.getString("invalid_element"));
    }
    String elemName = element.getLocalName();
    if (elemName == null) {
        XACMLSDKUtils.debug.error("RequestImpl.processElement(): local name missing");
        throw new XACMLException(XACMLSDKUtils.xacmlResourceBundle.getString("missing_local_name"));
    }
    if (!elemName.equals(XACMLConstants.REQUEST)) {
        XACMLSDKUtils.debug.error("RequestImpl.processElement(): invalid local name " + elemName);
        throw new XACMLException(XACMLSDKUtils.xacmlResourceBundle.getString("invalid_local_name"));
    }
    // starts processing subelements
    NodeList nodes = element.getChildNodes();
    int numOfNodes = nodes.getLength();
    if (numOfNodes < 1) {
        XACMLSDKUtils.debug.error("RequestImpl.processElement(): request has no subelements");
        throw new XACMLException(XACMLSDKUtils.xacmlResourceBundle.getString("missing_subelements"));
    }
    ContextFactory factory = ContextFactory.getInstance();
    List children = new ArrayList();
    int i = 0;
    Node child;
    while (i < numOfNodes) {
        child = (Node) nodes.item(i);
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            children.add(child);
        }
        i++;
    }
    if (children.isEmpty()) {
        XACMLSDKUtils.debug.error("RequestImpl.processElement():" + " request has no subelements");
        throw new XACMLException(XACMLSDKUtils.xacmlResourceBundle.getString("missing_subelements"));
    }
    child = (Node) children.get(0);
    // The first subelement should be <Subject>
    String childName = child.getLocalName();
    if ((childName == null) || (!childName.equals(XACMLConstants.SUBJECT))) {
        XACMLSDKUtils.debug.error("RequestImpl.processElement():" + " the first element is not <Subject>");
        throw new XACMLException(XACMLSDKUtils.xacmlResourceBundle.getString("missing_subelement_subject"));
    }
    Subject subject = factory.getInstance().createSubject((Element) child);
    if (!supportedSubjectCategory.contains(subject.getSubjectCategory().toString())) {
        XACMLSDKUtils.debug.error("RequestImpl.processElement():subject " + "category in subject not supported");
        throw new XACMLException(XACMLSDKUtils.xacmlResourceBundle.getString("unsupported_subject_category"));
    }
    subjects.add(subject);
    boolean resourceFound = false;
    boolean actionFound = false;
    boolean envFound = false;
    for (int j = 1; j < children.size(); j++) {
        child = (Node) children.get(j);
        // so far <Resource> is not encountered
        // Go through next sub elements for <Subject> and <Resource>
        // The next subelement may be <Resource> or <Subject>
        childName = child.getLocalName();
        if ((childName != null) && (childName.equals(XACMLConstants.RESOURCE) || childName.equals(XACMLConstants.SUBJECT))) {
            if (resourceFound) {
                if (childName.equals(XACMLConstants.SUBJECT)) {
                    // all <Subject> should be before <Resource>
                    XACMLSDKUtils.debug.error("RequestImpl." + "processElement(): <Subject> should be " + "before <Resource>");
                    throw new XACMLException(XACMLSDKUtils.xacmlResourceBundle.getString("element_out_of_place"));
                } else {
                    // found another resource
                    Resource resource = factory.getInstance().createResource((Element) child);
                    resources.add(resource);
                }
            } else if (childName.equals(XACMLConstants.SUBJECT)) {
                subject = factory.getInstance().createSubject((Element) child);
                subjects.add(subject);
            } else {
                // childname is resource
                resourceFound = true;
                Resource resource = factory.getInstance().createResource((Element) child);
                resources.add(resource);
            }
        } else if ((childName != null) && (childName.equals(XACMLConstants.ACTION))) {
            if (!resourceFound) {
                XACMLSDKUtils.debug.error("RequestImpl." + "processElement(): <Resource> should be " + "before <Action>");
                throw new XACMLException(XACMLSDKUtils.xacmlResourceBundle.getString("element_out_of_place"));
            } else {
                actionFound = true;
                action = factory.createAction((Element) child);
            }
        } else if ((childName != null) && (childName.equals(XACMLConstants.ENVIRONMENT))) {
            if (!resourceFound || !actionFound) {
                XACMLSDKUtils.debug.error("RequestImpl." + "processElement(): <Resource> and " + "Action should be before <Environment>");
                throw new XACMLException(XACMLSDKUtils.xacmlResourceBundle.getString("element_out_of_place"));
            } else {
                envFound = true;
                env = factory.createEnvironment((Element) child);
            }
        }
    }
    if (XACMLSDKUtils.debug.messageEnabled()) {
        XACMLSDKUtils.debug.message("resourceFound:" + resourceFound);
        XACMLSDKUtils.debug.message("actionFound:" + actionFound);
        XACMLSDKUtils.debug.message("envFound:" + envFound);
    }
    if (!resourceFound || !actionFound || !envFound) {
        XACMLSDKUtils.debug.error("RequestImpl.processElement(): Some" + "of required elements are missing");
        throw new XACMLException(XACMLSDKUtils.xacmlResourceBundle.getString("missing_subelements"));
    }
}
Also used : ContextFactory(com.sun.identity.xacml.context.ContextFactory) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) Resource(com.sun.identity.xacml.context.Resource) NodeList(org.w3c.dom.NodeList) ArrayList(java.util.ArrayList) List(java.util.List) Subject(com.sun.identity.xacml.context.Subject) XACMLException(com.sun.identity.xacml.common.XACMLException)

Aggregations

Resource (com.sun.identity.xacml.context.Resource)6 Subject (com.sun.identity.xacml.context.Subject)5 ArrayList (java.util.ArrayList)5 Attribute (com.sun.identity.xacml.context.Attribute)4 Request (com.sun.identity.xacml.context.Request)4 URI (java.net.URI)4 List (java.util.List)4 XACMLException (com.sun.identity.xacml.common.XACMLException)3 Action (com.sun.identity.xacml.context.Action)3 Environment (com.sun.identity.xacml.context.Environment)3 Decision (com.sun.identity.xacml.context.Decision)2 Response (com.sun.identity.xacml.context.Response)2 Result (com.sun.identity.xacml.context.Result)2 Element (org.w3c.dom.Element)2 SSOException (com.iplanet.sso.SSOException)1 SSOToken (com.iplanet.sso.SSOToken)1 PolicyEvaluator (com.sun.identity.policy.PolicyEvaluator)1 PolicyException (com.sun.identity.policy.PolicyException)1 ResourceResult (com.sun.identity.policy.ResourceResult)1 SAML2Exception (com.sun.identity.saml2.common.SAML2Exception)1