Search in sources :

Example 51 with IntegerValue

use of org.exist.xquery.value.IntegerValue 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);
}
Also used : Enumeration(java.util.Enumeration) Message(jakarta.mail.Message) Address(jakarta.mail.Address) XPathException(org.exist.xquery.XPathException) MessagingException(jakarta.mail.MessagingException) QName(org.exist.dom.QName) IntegerValue(org.exist.xquery.value.IntegerValue) Sequence(org.exist.xquery.value.Sequence) Flags(jakarta.mail.Flags) MemTreeBuilder(org.exist.dom.memtree.MemTreeBuilder) Header(jakarta.mail.Header)

Example 52 with IntegerValue

use of org.exist.xquery.value.IntegerValue in project exist by eXist-db.

the class MessageListFunctions method closeMessageList.

private Sequence closeMessageList(Sequence[] args, Sequence contextSequence) throws XPathException {
    // was a msgList handle specified?
    if (args[0].isEmpty()) {
        throw (new XPathException(this, "Message List handle not specified"));
    }
    // get the msgList
    long msgListHandle = ((IntegerValue) args[0].itemAt(0)).getLong();
    MailModule.removeMessageList(context, msgListHandle);
    return (Sequence.EMPTY_SEQUENCE);
}
Also used : XPathException(org.exist.xquery.XPathException) IntegerValue(org.exist.xquery.value.IntegerValue)

Example 53 with IntegerValue

use of org.exist.xquery.value.IntegerValue in project exist by eXist-db.

the class GetHeightFunction method eval.

/**
 * evaluate the call to the xquery get-height() function,
 * it is really the main entry point of this class
 *
 * @param args		arguments from the get-height() function call
 * @param contextSequence	the Context Sequence to operate on (not used here internally!)
 * @return		A sequence representing the result of the get-height() function call
 *
 * @see org.exist.xquery.BasicFunction#eval(org.exist.xquery.value.Sequence[], org.exist.xquery.value.Sequence)
 */
@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    // was an image speficifed
    if (args[0].isEmpty()) {
        return Sequence.EMPTY_SEQUENCE;
    }
    // get the image
    Image image = null;
    BinaryValue imageData = (BinaryValue) args[0].itemAt(0);
    try (InputStream inputStream = imageData.getInputStream()) {
        image = ImageIO.read(inputStream);
    } catch (IOException ioe) {
        logger.error("Unable to read image data!", ioe);
        return Sequence.EMPTY_SEQUENCE;
    }
    if (image == null) {
        logger.error("Unable to read image data!");
        return Sequence.EMPTY_SEQUENCE;
    }
    // Get the Height of the image
    int iHeight = image.getHeight(null);
    // did we get the Height of the image?
    if (iHeight == -1) {
        // no, log the error
        logger.error("Unable to read image data!");
        return Sequence.EMPTY_SEQUENCE;
    } else {
        // return the Height of the image
        return new IntegerValue(iHeight);
    }
}
Also used : InputStream(java.io.InputStream) IntegerValue(org.exist.xquery.value.IntegerValue) BinaryValue(org.exist.xquery.value.BinaryValue) IOException(java.io.IOException) Image(java.awt.Image)

Example 54 with IntegerValue

use of org.exist.xquery.value.IntegerValue in project exist by eXist-db.

the class GetWidthFunction method eval.

/**
 * evaluate the call to the xquery get-width() function,
 * it is really the main entry point of this class
 *
 * @param args		arguments from the get-width() function call
 * @param contextSequence	the Context Sequence to operate on (not used here internally!)
 * @return		A sequence representing the result of the get-width() function call
 *
 * @see org.exist.xquery.BasicFunction#eval(org.exist.xquery.value.Sequence[], org.exist.xquery.value.Sequence)
 */
@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    // was an image speficifed
    if (args[0].isEmpty()) {
        return Sequence.EMPTY_SEQUENCE;
    }
    // get the image
    Image image = null;
    BinaryValue imageData = (BinaryValue) args[0].itemAt(0);
    try (InputStream inputStream = imageData.getInputStream()) {
        image = ImageIO.read(inputStream);
    } catch (IOException ioe) {
        logger.error("Unable to read image data!", ioe);
        return Sequence.EMPTY_SEQUENCE;
    }
    if (image == null) {
        logger.error("Unable to read image data!");
        return Sequence.EMPTY_SEQUENCE;
    }
    // Get the width of the image
    int iWidth = image.getWidth(null);
    // did we get the width of the image?
    if (iWidth == -1) {
        // no, log the error
        logger.error("Unable to read image data!");
        return Sequence.EMPTY_SEQUENCE;
    } else {
        // return the width of the image
        return new IntegerValue(iWidth);
    }
}
Also used : InputStream(java.io.InputStream) IntegerValue(org.exist.xquery.value.IntegerValue) BinaryValue(org.exist.xquery.value.BinaryValue) IOException(java.io.IOException) Image(java.awt.Image)

Example 55 with IntegerValue

use of org.exist.xquery.value.IntegerValue in project exist by eXist-db.

the class GetDirContextFunction method eval.

@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    // Were properties specified
    if (args[0].isEmpty()) {
        return (Sequence.EMPTY_SEQUENCE);
    }
    try {
        DirContext dirCtx = null;
        Properties env = ParametersExtractor.parseProperties(((NodeValue) args[0].itemAt(0)).getNode());
        dirCtx = new InitialDirContext(env);
        return (new IntegerValue(JNDIModule.storeJNDIContext(context, dirCtx)));
    } catch (NamingException ne) {
        logger.error("jndi:get-dir-context() Cannot get JNDI directory context: ", ne);
        throw (new XPathException(this, "jndi:get-dir-context() Cannot get JNDI directory context: " + ne));
    }
}
Also used : XPathException(org.exist.xquery.XPathException) IntegerValue(org.exist.xquery.value.IntegerValue) NamingException(javax.naming.NamingException) DirContext(javax.naming.directory.DirContext) InitialDirContext(javax.naming.directory.InitialDirContext) InitialDirContext(javax.naming.directory.InitialDirContext) Properties(java.util.Properties)

Aggregations

IntegerValue (org.exist.xquery.value.IntegerValue)63 Sequence (org.exist.xquery.value.Sequence)33 XPathException (org.exist.xquery.XPathException)32 BrokerPool (org.exist.storage.BrokerPool)11 DBBroker (org.exist.storage.DBBroker)11 Test (org.junit.Test)11 Txn (org.exist.storage.txn.Txn)8 MessagingException (jakarta.mail.MessagingException)7 StringInputSource (org.exist.util.StringInputSource)7 NamingException (javax.naming.NamingException)6 DirContext (javax.naming.directory.DirContext)6 QName (org.exist.dom.QName)6 Source (org.exist.source.Source)6 StringSource (org.exist.source.StringSource)6 InputStream (java.io.InputStream)5 Map (java.util.Map)5 StringValue (org.exist.xquery.value.StringValue)5 ValueSequence (org.exist.xquery.value.ValueSequence)5 Folder (jakarta.mail.Folder)4 Image (java.awt.Image)4