int count = 0;
boolean added = false;
String xml = getActionParameter(requestContext, "xml");
if (xml == null)
throw new AJAXException("Missing 'xml' parameter");
try
{
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = domFactory.newDocumentBuilder();
String charset = requestContext.getCharacterEncoding();
byte [] bytes = (charset != null ? xml.getBytes(charset) : xml.getBytes());
Document document = builder.parse(new ByteArrayInputStream(bytes));
Element root = document.getDocumentElement();
String name = root.getAttribute("name");
PageSecurity pageSecurity = pageManager.getPageSecurity();
SecurityConstraintsDef def = pageSecurity.getSecurityConstraintsDef(name);
int defsSize = 0;
if (def == null)
{
def = pageManager.newSecurityConstraintsDef();
def.setName(name);
added = true;
}
NodeList xmlConstraints = root.getElementsByTagName("security-constraint");
int xmlSize = xmlConstraints.getLength();
if (added == false)
{
defsSize = def.getSecurityConstraints().size();
}
int min = (xmlSize < defsSize) ? xmlSize : defsSize;
List constraints = def.getSecurityConstraints();
NodeList owners = root.getElementsByTagName("owner");
if (owners.getLength() == 1)
{
}
for (int ix = 0; ix < min; ix++)
{
Element xmlConstraint = (Element)xmlConstraints.item(ix);
SecurityConstraint constraint = (SecurityConstraint)constraints.get(ix);
updateConstraintValues(xmlConstraint, constraint);
count++;
}
if (xmlSize < defsSize)
{
// remove constraints
List deletes = new ArrayList(defsSize - xmlSize);
for (int ix = min; ix < defsSize; ix++)
{
deletes.add(constraints.get(ix));
}
for (int ix = 0; ix < deletes.size(); ix++)
{
constraints.remove(deletes.get(ix));
count++;
}
}
else if (xmlSize > defsSize)
{
// add new constraints
for (int ix = min; ix < xmlSize; ix++)
{
Element xmlConstraint = (Element)xmlConstraints.item(ix);
SecurityConstraint constraint = pageManager.newPageSecuritySecurityConstraint();
updateConstraintValues(xmlConstraint, constraint);
constraints.add(constraint);
count++;
}
}
if (added)
{
pageSecurity.getSecurityConstraintsDefs().add(def);
pageSecurity.setSecurityConstraintsDefs(pageSecurity.getSecurityConstraintsDefs());
}
pageManager.updatePageSecurity(pageSecurity);
}
catch (Exception e)
{
System.out.println( "SecurityConstraintsAction updateConstraintDefinition failure caused by " + e.getClass().getName() + " " + e.getMessage() );
e.printStackTrace();
log.error( "SecurityConstraintsAction updateConstraintDefinition failure caused by " + e.getClass().getName() + " " + e.getMessage(), e );
throw new AJAXException(e);
}
return count;
}