use of org.exist.util.sax.event.SAXEvent in project exist by eXist-db.
the class DeferrableFilteringTrigger method applyDeferredEvents.
/**
* Applies any deferred events
* by dispatching to the appropriate _deferred method
* @throws SAXException in case of an error
*/
protected void applyDeferredEvents() throws SAXException {
SAXEvent event = null;
while ((event = deferred.poll()) != null) {
if (event instanceof SetDocumentLocator) {
final SetDocumentLocator setDocumentLocator = (SetDocumentLocator) event;
setDocumentLocator_deferred(setDocumentLocator.locator);
} else if (event instanceof StartDocument) {
startDocument_deferred();
} else if (event instanceof EndDocument) {
endDocument_deferred();
} else if (event instanceof StartPrefixMapping) {
final StartPrefixMapping startPrefixMapping = (StartPrefixMapping) event;
startPrefixMapping_deferred(startPrefixMapping.prefix, startPrefixMapping.uri);
} else if (event instanceof EndPrefixMapping) {
final EndPrefixMapping endPrefixMapping = (EndPrefixMapping) event;
endPrefixMapping_deferred(endPrefixMapping.prefix);
} else if (event instanceof StartElement) {
final StartElement startElement = (StartElement) event;
startElement_deferred(startElement.namespaceURI, startElement.localName, startElement.qname, startElement.attributes);
} else if (event instanceof EndElement) {
final EndElement endElement = (EndElement) event;
endElement_deferred(endElement.namespaceURI, endElement.localName, endElement.qname);
} else if (event instanceof Characters) {
final Characters characters = (Characters) event;
characters_deferred(characters.ch, 0, characters.ch.length);
} else if (event instanceof IgnorableWhitespace) {
final IgnorableWhitespace ignorableWhitespace = (IgnorableWhitespace) event;
ignorableWhitespace_deferred(ignorableWhitespace.ch, 0, ignorableWhitespace.ch.length);
} else if (event instanceof ProcessingInstruction) {
final ProcessingInstruction processingInstruction = (ProcessingInstruction) event;
processingInstruction_deferred(processingInstruction.target, processingInstruction.data);
} else if (event instanceof SkippedEntity) {
final SkippedEntity skippedEntity = (SkippedEntity) event;
skippedEntity_deferred(skippedEntity.name);
} else if (event instanceof StartDTD) {
final StartDTD startDTD = (StartDTD) event;
startDTD_deferred(startDTD.name, startDTD.publicId, startDTD.systemId);
} else if (event instanceof EndDTD) {
endDTD_deferred();
} else if (event instanceof StartEntity) {
final StartEntity startEntity = (StartEntity) event;
startEntity_deferred(startEntity.name);
} else if (event instanceof EndEntity) {
final EndEntity endEntity = (EndEntity) event;
endEntity_deferred(endEntity.name);
} else if (event instanceof StartCDATA) {
startCDATA_deferred();
} else if (event instanceof EndCDATA) {
endCDATA_deferred();
} else if (event instanceof Comment) {
final Comment comment = (Comment) event;
comment_deferred(comment.ch, 0, comment.ch.length);
}
}
}
use of org.exist.util.sax.event.SAXEvent in project exist by eXist-db.
the class ConfigurationDocumentTrigger method setAccountPrimaryGroupToNoGroup.
private void setAccountPrimaryGroupToNoGroup() throws SAXException {
final SAXEvent firstEvent = deferred.peek();
if (!(firstEvent instanceof StartElement)) {
throw new SAXException("Unbalanced SAX Events");
}
StartElement start = ((StartElement) firstEvent);
if (start.namespaceURI == null || !start.namespaceURI.equals(Configuration.NS) || !start.localName.equals(PrincipalType.ACCOUNT.getElementName())) {
throw new SAXException("First element does not match ending '" + PrincipalType.ACCOUNT.getElementName() + "' element");
}
start = (StartElement) deferred.pop();
final AttributesImpl attrs = new AttributesImpl();
attrs.addAttribute("", "name", "name", "CDATA", SecurityManager.UNKNOWN_GROUP);
final StartElement startPrimaryGroup = new StartElement(start.namespaceURI, PrincipalType.GROUP.getElementName(), PrincipalType.GROUP.getElementName(), attrs);
final EndElement endPrimaryGroup = new EndElement(startPrimaryGroup.namespaceURI, startPrimaryGroup.localName, startPrimaryGroup.qname);
deferred.push(endPrimaryGroup);
deferred.push(startPrimaryGroup);
deferred.push(start);
}
use of org.exist.util.sax.event.SAXEvent in project exist by eXist-db.
the class ConfigurationDocumentTrigger method findName.
/**
* Attempts to find and extract the text value
* of the name element from the deferred elements
*
* @return The text value of the name element, or null otherwise
*/
private String findName() {
boolean inName = false;
final StringBuilder name = new StringBuilder();
for (final SAXEvent event : deferred) {
if (event instanceof Element) {
final Element element = (Element) event;
if (element.namespaceURI != null && element.namespaceURI.equals(Configuration.NS) && element.localName.equals("name")) {
inName = !inName;
}
}
if (inName && event instanceof Characters) {
name.append(((Characters) event).ch);
}
}
if (name.length() > 0) {
return name.toString().trim();
} else {
return null;
}
}
use of org.exist.util.sax.event.SAXEvent in project exist by eXist-db.
the class ConfigurationDocumentTrigger method processPrincipal.
/**
* When configuring a Principal (Account or Group) we need to
* make sure of two things:
*
* 1) If the principal uses an old style id, i.e. before ACL Permissions
* were introduced then we have to modernise this id
*
* 2) If the principal uses a name or id which already exists in
* the database then we must avoid conflicts
*/
private void processPrincipal(final PrincipalType principalType) throws SAXException {
final SAXEvent firstEvent = deferred.peek();
if (!(firstEvent instanceof StartElement)) {
throw new SAXException("Unbalanced SAX Events");
}
final StartElement start = ((StartElement) firstEvent);
if (start.namespaceURI == null || !start.namespaceURI.equals(Configuration.NS) || !start.localName.equals(principalType.getElementName())) {
throw new SAXException("First element does not match ending '" + principalType.getElementName() + "' element");
}
final SecurityManager sm = broker.getBrokerPool().getSecurityManager();
// if needed, update old style id to new style id
final AttributesImpl attrs = new AttributesImpl(migrateIdAttribute(sm, start.attributes, principalType));
// check if there is a name collision, i.e. another principal with the same name
final String principalName = findName();
// first check if the account or group exists before trying to retrieve it
// otherwise the LDAP realm will create a new user, leading to an endless loop
final boolean principalExists = principalName != null && principalType.hasPrincipal(sm, principalName);
Principal existingPrincipleByName = null;
if (principalExists) {
existingPrincipleByName = principalType.getPrincipal(sm, principalName);
}
final int newId;
if (existingPrincipleByName != null) {
// use id of existing principal which has the same name
newId = existingPrincipleByName.getId();
} else {
// check if there is an id collision, i.e. another principal with the same id
final Integer id = Integer.valueOf(attrs.getValue(ID_ATTR));
final boolean principalIdExists = principalType.hasPrincipal(sm, id);
Principal existingPrincipalById = null;
if (principalIdExists) {
existingPrincipalById = principalType.getPrincipal(sm, id);
}
if (existingPrincipalById != null) {
// pre-allocate a new id, so as not to collide with the existing principal
if (isValidating()) {
try {
principalType.preAllocateId(sm, preAllocatedId);
} catch (final PermissionDeniedException | EXistException e) {
throw new SAXException("Unable to pre-allocate principle id for " + principalType.getElementName() + ": " + principalName, e);
}
}
newId = preAllocatedId.getId();
if (!isValidating()) {
preAllocatedId.clear();
}
} else {
// use the provided id as it is currently unallocated
newId = id;
}
}
// update attributes of the principal in deferred
attrs.setValue(attrs.getIndex(ID_ATTR), String.valueOf(newId));
final StartElement prevPrincipalStart = (StartElement) deferred.poll();
deferred.addFirst(new StartElement(prevPrincipalStart.namespaceURI, prevPrincipalStart.localName, prevPrincipalStart.qname, attrs));
}
Aggregations