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"));
}
}
Aggregations