use of org.exist.dom.memtree.MemTreeBuilder in project exist by eXist-db.
the class MessageFunctions method getMessages.
private Sequence getMessages(Sequence[] args, Sequence contextSequence) throws XPathException {
Sequence ret = Sequence.EMPTY_SEQUENCE;
// was a folder handle specified?
if (args[0].isEmpty()) {
throw (new XPathException(this, "Mail folder handle not specified"));
}
// get the Folder
long folderHandle = ((IntegerValue) args[0].itemAt(0)).getLong();
Folder folder = MailModule.retrieveFolder(context, folderHandle);
if (folder == null) {
throw (new XPathException(this, "Invalid Folder handle specified"));
}
if (args[1].isEmpty()) {
// no messages requested
return ret;
}
context.pushDocumentContext();
try {
MemTreeBuilder builder = context.getDocumentBuilder();
builder.startDocument();
builder.startElement(new QName("messages", MailModule.NAMESPACE_URI, MailModule.PREFIX), null);
try {
int counter = args[1].getItemCount();
for (int i = 0; i < counter; i++) {
Message message = null;
int msgNum = ((IntegerValue) args[1].itemAt(i)).getInt();
try {
// get the requested message number
message = folder.getMessage(msgNum);
} catch (IndexOutOfBoundsException iex) {
logger.info("There is no message number {}", msgNum);
continue;
}
builder.startElement(new QName("message", MailModule.NAMESPACE_URI, MailModule.PREFIX), null);
builder.addAttribute(new QName("number", null, null), String.valueOf(message.getMessageNumber()));
String contentType = message.getContentType();
mimeParamsToAttributes(builder, contentType);
// Subject
builder.startElement(new QName("subject", MailModule.NAMESPACE_URI, MailModule.PREFIX), null);
builder.characters(message.getSubject());
builder.endElement();
// Sent Date
if (message.getSentDate() != null) {
builder.startElement(new QName("sent", MailModule.NAMESPACE_URI, MailModule.PREFIX), null);
builder.characters(formatDate(message.getSentDate()));
builder.endElement();
}
// Received Date
if (message.getReceivedDate() != null) {
builder.startElement(new QName("received", MailModule.NAMESPACE_URI, MailModule.PREFIX), null);
builder.characters(formatDate(message.getReceivedDate()));
builder.endElement();
}
// From
if (message.getFrom() != null) {
addAddress(builder, "from", null, message.getFrom()[0]);
}
// Recipients
builder.startElement(new QName("recipients", MailModule.NAMESPACE_URI, MailModule.PREFIX), null);
// To Recipients
Address[] toAddresses = message.getRecipients(Message.RecipientType.TO);
if (toAddresses != null) {
for (Address to : toAddresses) {
addAddress(builder, "recipient", "to", to);
}
}
// cc Recipients
Address[] ccAddresses = message.getRecipients(Message.RecipientType.CC);
if (ccAddresses != null) {
for (Address ccAddress : ccAddresses) {
addAddress(builder, "recipient", "cc", ccAddress);
}
}
// bcc Recipients
Address[] bccAddresses = message.getRecipients(Message.RecipientType.BCC);
if (bccAddresses != null) {
for (Address bccAddress : bccAddresses) {
addAddress(builder, "recipient", "bcc", bccAddress);
}
}
// recipients
builder.endElement();
// Handle the content
Object content = message.getContent();
if (content instanceof Multipart) {
handleMultipart((Multipart) content, builder);
} else {
handlePart(message, builder);
}
// Flags
Flags flags = message.getFlags();
Flags.Flag[] systemFlags = flags.getSystemFlags();
String[] userFlags = flags.getUserFlags();
if (systemFlags.length > 0 || userFlags.length > 0) {
builder.startElement(new QName("flags", MailModule.NAMESPACE_URI, MailModule.PREFIX), null);
for (Flags.Flag systemFlag : systemFlags) {
if (systemFlag == Flags.Flag.ANSWERED) {
addFlag(builder, "answered");
} else if (systemFlag == Flags.Flag.DELETED) {
addFlag(builder, "deleted");
} else if (systemFlag == Flags.Flag.DRAFT) {
addFlag(builder, "draft");
} else if (systemFlag == Flags.Flag.FLAGGED) {
addFlag(builder, "flagged");
} else if (systemFlag == Flags.Flag.RECENT) {
addFlag(builder, "recent");
} else if (systemFlag == Flags.Flag.SEEN) {
addFlag(builder, "seen");
}
}
for (String userFlag : userFlags) {
builder.startElement(new QName("flag", MailModule.NAMESPACE_URI, MailModule.PREFIX), null);
builder.addAttribute(new QName("type", null, null), "user");
builder.addAttribute(new QName("value", null, null), userFlag);
builder.endElement();
}
builder.endElement();
}
builder.endElement();
}
} catch (MessagingException me) {
throw (new XPathException(this, "Failed to retrieve messages from list", me));
} catch (IOException | SAXException e) {
e.printStackTrace();
}
builder.endElement();
builder.endDocument();
ret = (NodeValue) builder.getDocument().getDocumentElement();
return (ret);
} finally {
context.popDocumentContext();
}
}
use of org.exist.dom.memtree.MemTreeBuilder in project exist by eXist-db.
the class SearchFunction method renderSearchResultsAsDSML.
private Sequence renderSearchResultsAsDSML(NamingEnumeration results, String dn) throws NamingException {
Sequence xmlResult = Sequence.EMPTY_SEQUENCE;
context.pushDocumentContext();
try {
MemTreeBuilder builder = context.getDocumentBuilder();
builder.startDocument();
builder.startElement(new QName("dsml", DSML_NAMESPACE, DSML_PREFIX), null);
builder.addAttribute(new QName("dn", null, null), dn);
builder.startElement(new QName("directory-entries", DSML_NAMESPACE, DSML_PREFIX), null);
while (results.hasMore()) {
SearchResult result = (SearchResult) results.next();
builder.startElement(new QName("entry", DSML_NAMESPACE, DSML_PREFIX), null);
builder.addAttribute(new QName("dn", null, null), result.getName());
// Handle objectClass attributes
Attribute ocattr = result.getAttributes().get("objectClass");
if (ocattr != null) {
builder.startElement(new QName("objectclass", DSML_NAMESPACE, DSML_PREFIX), null);
for (int i = 0; i < ocattr.size(); i++) {
Object value = ocattr.get(i);
builder.startElement(new QName("oc-value", DSML_NAMESPACE, DSML_PREFIX), null);
builder.characters(value.toString());
builder.endElement();
}
builder.endElement();
}
NamingEnumeration attrs = result.getAttributes().getAll();
// Handle all other attributes
while (attrs.hasMore()) {
Attribute attr = (Attribute) attrs.next();
String name = attr.getID();
if (!name.equals("objectClass")) {
builder.startElement(new QName("attr", DSML_NAMESPACE, DSML_PREFIX), null);
builder.addAttribute(new QName("name", null, null), name);
for (int i = 0; i < attr.size(); i++) {
Object value = attr.get(i);
builder.startElement(new QName("value", DSML_NAMESPACE, DSML_PREFIX), null);
if (name.equals("userPassword")) {
builder.characters(new String((byte[]) value));
} else {
builder.characters(value.toString());
}
builder.endElement();
}
builder.endElement();
}
}
builder.endElement();
}
builder.endElement();
builder.endElement();
xmlResult = (NodeValue) builder.getDocument().getDocumentElement();
return (xmlResult);
} finally {
context.popDocumentContext();
}
}
use of org.exist.dom.memtree.MemTreeBuilder in project exist by eXist-db.
the class MessageListFunctions method getMessageListAsXML.
private Sequence getMessageListAsXML(Sequence[] args, Sequence contextSequence) throws XPathException {
Message[] msgList;
Sequence ret = Sequence.EMPTY_SEQUENCE;
// was a msgList handle specified?
if (args[0].isEmpty()) {
throw (new XPathException(this, "Message List handle not specified"));
}
// get the MessageList
long msgListHandle = ((IntegerValue) args[0].itemAt(0)).getLong();
msgList = MailModule.retrieveMessageList(context, msgListHandle);
if (msgList == null) {
throw (new XPathException(this, "Invalid Message List handle specified"));
}
if (msgList.length > 0) {
boolean includeHeaders = args[1].effectiveBooleanValue();
context.pushDocumentContext();
try {
MemTreeBuilder builder = context.getDocumentBuilder();
builder.startDocument();
builder.startElement(new QName("messages", MailModule.NAMESPACE_URI, MailModule.PREFIX), null);
builder.addAttribute(new QName("count", null, null), String.valueOf(msgList.length));
try {
for (Message message : msgList) {
builder.startElement(new QName("message", MailModule.NAMESPACE_URI, MailModule.PREFIX), null);
builder.addAttribute(new QName("number", null, null), String.valueOf(message.getMessageNumber()));
// Sent Date
if (message.getSentDate() != null) {
builder.startElement(new QName("sent", MailModule.NAMESPACE_URI, MailModule.PREFIX), null);
builder.characters(formatDate(message.getSentDate()));
builder.endElement();
}
// Received Date
if (message.getReceivedDate() != null) {
builder.startElement(new QName("received", MailModule.NAMESPACE_URI, MailModule.PREFIX), null);
builder.characters(formatDate(message.getReceivedDate()));
builder.endElement();
}
// From
if (message.getFrom() != null) {
builder.startElement(new QName("from", MailModule.NAMESPACE_URI, MailModule.PREFIX), null);
builder.characters(message.getFrom()[0].toString());
builder.endElement();
}
// Recipients
builder.startElement(new QName("recipients", MailModule.NAMESPACE_URI, MailModule.PREFIX), null);
// To Recipients
Address[] toAddresses = message.getRecipients(Message.RecipientType.TO);
if (toAddresses != null) {
for (Address to : toAddresses) {
builder.startElement(new QName("recipient", MailModule.NAMESPACE_URI, MailModule.PREFIX), null);
builder.addAttribute(new QName("type", null, null), "to");
builder.characters(to.toString());
builder.endElement();
}
}
// cc Recipients
Address[] ccAddresses = message.getRecipients(Message.RecipientType.CC);
if (ccAddresses != null) {
for (Address ccAddress : ccAddresses) {
builder.startElement(new QName("recipient", MailModule.NAMESPACE_URI, MailModule.PREFIX), null);
builder.addAttribute(new QName("type", null, null), "cc");
builder.characters(ccAddress.toString());
builder.endElement();
}
}
// bcc Recipients
Address[] bccAddresses = message.getRecipients(Message.RecipientType.BCC);
if (bccAddresses != null) {
for (Address bccAddress : bccAddresses) {
builder.startElement(new QName("recipient", MailModule.NAMESPACE_URI, MailModule.PREFIX), null);
builder.addAttribute(new QName("type", null, null), "bcc");
builder.characters(bccAddress.toString());
builder.endElement();
}
}
builder.endElement();
// Flags
Flags flags = message.getFlags();
Flags.Flag[] systemFlags = flags.getSystemFlags();
String[] userFlags = flags.getUserFlags();
if (systemFlags.length > 0 || userFlags.length > 0) {
builder.startElement(new QName("flags", MailModule.NAMESPACE_URI, MailModule.PREFIX), null);
for (Flags.Flag systemFlag : systemFlags) {
if (systemFlag == Flags.Flag.ANSWERED) {
builder.startElement(new QName("flag", MailModule.NAMESPACE_URI, MailModule.PREFIX), null);
builder.addAttribute(new QName("type", null, null), "answered");
builder.endElement();
} else if (systemFlag == Flags.Flag.DELETED) {
builder.startElement(new QName("flag", MailModule.NAMESPACE_URI, MailModule.PREFIX), null);
builder.addAttribute(new QName("type", null, null), "deleted");
builder.endElement();
} else if (systemFlag == Flags.Flag.DRAFT) {
builder.startElement(new QName("flag", MailModule.NAMESPACE_URI, MailModule.PREFIX), null);
builder.addAttribute(new QName("type", null, null), "draft");
builder.endElement();
} else if (systemFlag == Flags.Flag.FLAGGED) {
builder.startElement(new QName("flag", MailModule.NAMESPACE_URI, MailModule.PREFIX), null);
builder.addAttribute(new QName("type", null, null), "flagged");
builder.endElement();
} else if (systemFlag == Flags.Flag.RECENT) {
builder.startElement(new QName("flag", MailModule.NAMESPACE_URI, MailModule.PREFIX), null);
builder.addAttribute(new QName("type", null, null), "recent");
builder.endElement();
} else if (systemFlag == Flags.Flag.SEEN) {
builder.startElement(new QName("flag", MailModule.NAMESPACE_URI, MailModule.PREFIX), null);
builder.addAttribute(new QName("type", null, null), "seen");
builder.endElement();
}
}
for (String userFlag : userFlags) {
builder.startElement(new QName("flag", MailModule.NAMESPACE_URI, MailModule.PREFIX), null);
builder.addAttribute(new QName("type", null, null), "user");
builder.addAttribute(new QName("value", null, null), userFlag);
builder.endElement();
}
builder.endElement();
}
if (includeHeaders) {
Enumeration headers = message.getAllHeaders();
if (headers.hasMoreElements()) {
builder.startElement(new QName("headers", MailModule.NAMESPACE_URI, MailModule.PREFIX), null);
while (headers.hasMoreElements()) {
Header header = (Header) headers.nextElement();
builder.startElement(new QName("header", MailModule.NAMESPACE_URI, MailModule.PREFIX), null);
builder.addAttribute(new QName("name", null, null), header.getName());
builder.addAttribute(new QName("value", null, null), header.getValue());
builder.endElement();
}
builder.endElement();
}
}
// Subject
builder.startElement(new QName("subject", MailModule.NAMESPACE_URI, MailModule.PREFIX), null);
builder.characters(message.getSubject());
builder.endElement();
builder.endElement();
}
} catch (MessagingException me) {
throw (new XPathException(this, "Failed to retrieve messages from list", me));
}
builder.endElement();
ret = (NodeValue) builder.getDocument().getDocumentElement();
} finally {
context.popDocumentContext();
}
}
return (ret);
}
use of org.exist.dom.memtree.MemTreeBuilder in project exist by eXist-db.
the class DirectoryList method eval.
@Override
public Sequence eval(final Sequence[] args, final Sequence contextSequence) throws XPathException {
if (!context.getSubject().hasDbaRole()) {
XPathException xPathException = new XPathException(this, "Permission denied, calling user '" + context.getSubject().getName() + "' must be a DBA to call this function.");
logger.error("Invalid user", xPathException);
throw xPathException;
}
final String inputPath = args[0].getStringValue();
final Path baseDir = FileModuleHelper.getFile(inputPath);
final Sequence patterns = args[1];
if (logger.isDebugEnabled()) {
logger.debug("Listing matching files in directory: {}", baseDir);
}
context.pushDocumentContext();
final MemTreeBuilder builder = context.getDocumentBuilder();
builder.startDocument();
builder.startElement(new QName("list", NAMESPACE_URI, PREFIX), null);
builder.addAttribute(new QName("directory", null, null), baseDir.toString());
try {
final int patternsLen = patterns.getItemCount();
final String[] includes = new String[patternsLen];
for (int i = 0; i < patternsLen; i++) {
includes[i] = patterns.itemAt(0).getStringValue();
}
final DirectoryScanner directoryScanner = new DirectoryScanner();
directoryScanner.setIncludes(includes);
directoryScanner.setBasedir(baseDir.toFile());
directoryScanner.setCaseSensitive(true);
directoryScanner.scan();
for (final String includedFile : directoryScanner.getIncludedFiles()) {
final Path file = baseDir.resolve(includedFile);
if (logger.isDebugEnabled()) {
logger.debug("Found: {}", file.toAbsolutePath());
}
String relPath = file.toString().substring(baseDir.toString().length() + 1);
int lastSeparatorPosition = relPath.lastIndexOf(java.io.File.separatorChar);
String relDir = null;
if (lastSeparatorPosition >= 0) {
relDir = relPath.substring(0, lastSeparatorPosition);
relDir = relDir.replace(java.io.File.separatorChar, '/');
}
builder.startElement(new QName("file", NAMESPACE_URI, PREFIX), null);
builder.addAttribute(new QName("name", null, null), FileUtils.fileName(file));
Long sizeLong = FileUtils.sizeQuietly(file);
String sizeString = Long.toString(sizeLong);
String humanSize = getHumanSize(sizeLong, sizeString);
builder.addAttribute(new QName("size", null, null), sizeString);
builder.addAttribute(new QName("human-size", null, null), humanSize);
builder.addAttribute(new QName("modified", null, null), new DateTimeValue(new Date(Files.getLastModifiedTime(file).toMillis())).getStringValue());
if (relDir != null && !relDir.isEmpty()) {
builder.addAttribute(new QName("subdir", null, null), relDir);
}
builder.endElement();
}
builder.endElement();
return (NodeValue) builder.getDocument().getDocumentElement();
} catch (final IOException e) {
throw new XPathException(this, e.getMessage());
} finally {
context.popDocumentContext();
}
}
use of org.exist.dom.memtree.MemTreeBuilder in project exist by eXist-db.
the class Compile method response.
private Sequence response(XQueryContext pContext, String error, ErrorCode code, int line, int column) {
context.pushDocumentContext();
try {
final MemTreeBuilder builder = context.getDocumentBuilder();
builder.startElement(QNAME_INFO, null);
builder.addAttribute(QNAME_RESULT_ATTR, error == null ? "pass" : "fail");
if (error != null) {
builder.startElement(ERROR_INFO, null);
if (code != null) {
builder.addAttribute(QNAME_ERRCODE_ATTR, code.toString());
}
if (line > -1) {
builder.addAttribute(QNAME_LINE_ATTR, Integer.toString(line));
builder.addAttribute(QNAME_COLUMN_ATTR, Integer.toString(column));
}
builder.characters(error);
builder.endElement();
}
builder.endElement();
return builder.getDocument().getNode(1);
} finally {
context.popDocumentContext();
}
}
Aggregations