Search in sources :

Example 41 with XPathException

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

the class MessageListFunctions method parseChildSearchTerms.

private SearchTerm[] parseChildSearchTerms(Node terms) throws XPathException {
    // Parent allows multiple child search terms
    ArrayList<SearchTerm> st = new ArrayList<>();
    NodeList children = terms.getChildNodes();
    if (children.getLength() > 0) {
        for (int i = 0; i < children.getLength(); i++) {
            Node child = children.item(i);
            st.add(parseSearchTerms(child));
        }
    } else {
        throw (new XPathException(this, "At least one child term is required for term with type: " + ((Element) terms).getAttribute("type")));
    }
    return st.toArray(new SearchTerm[0]);
}
Also used : XPathException(org.exist.xquery.XPathException) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) SearchTerm(jakarta.mail.search.SearchTerm)

Example 42 with XPathException

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

the class ScaleFunction method eval.

/**
 * evaluate the call to the xquery scale() function,
 * it is really the main entry point of this class
 *
 * @param args		arguments from the scale() function call
 * @param contextSequence	the Context Sequence to operate on (not used here internally!)
 * @return		A sequence representing the result of the scale() function call
 *
 * @see org.exist.xquery.BasicFunction#eval(org.exist.xquery.value.Sequence[], org.exist.xquery.value.Sequence)
 */
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    // was an image and a mime-type speficifed
    if (args[0].isEmpty() || args[2].isEmpty()) {
        return Sequence.EMPTY_SEQUENCE;
    }
    // get the maximum dimensions to scale to
    int maxHeight = MAXHEIGHT;
    int maxWidth = MAXWIDTH;
    if (!args[1].isEmpty()) {
        maxHeight = ((IntegerValue) args[1].itemAt(0)).getInt();
        if (args[1].hasMany())
            maxWidth = ((IntegerValue) args[1].itemAt(1)).getInt();
    }
    // get the mime-type
    String mimeType = args[2].itemAt(0).getStringValue();
    String formatName = mimeType.substring(mimeType.indexOf("/") + 1);
    // TODO currently ONLY tested for JPEG!!!
    Image image = null;
    BufferedImage bImage = null;
    try (// get the image data
    InputStream inputStream = ((BinaryValue) args[0].itemAt(0)).getInputStream()) {
        image = ImageIO.read(inputStream);
        if (image == null) {
            logger.error("Unable to read image data!");
            return Sequence.EMPTY_SEQUENCE;
        }
        // scale the image
        bImage = ImageModule.createThumb(image, maxHeight, maxWidth);
        // get the new scaled image
        try (final UnsynchronizedByteArrayOutputStream os = new UnsynchronizedByteArrayOutputStream()) {
            ImageIO.write(bImage, formatName, os);
            // return the new scaled image data
            return BinaryValueFromInputStream.getInstance(context, new Base64BinaryValueType(), os.toInputStream());
        }
    } catch (Exception e) {
        throw new XPathException(this, e.getMessage());
    }
}
Also used : XPathException(org.exist.xquery.XPathException) BinaryValueFromInputStream(org.exist.xquery.value.BinaryValueFromInputStream) InputStream(java.io.InputStream) IntegerValue(org.exist.xquery.value.IntegerValue) BinaryValue(org.exist.xquery.value.BinaryValue) Base64BinaryValueType(org.exist.xquery.value.Base64BinaryValueType) Image(java.awt.Image) BufferedImage(java.awt.image.BufferedImage) UnsynchronizedByteArrayOutputStream(org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream) BufferedImage(java.awt.image.BufferedImage) XPathException(org.exist.xquery.XPathException)

Example 43 with XPathException

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

the class CreateFunction method eval.

@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    // Was context handle or DN specified?
    if (!(args[0].isEmpty()) && !(args[1].isEmpty())) {
        String dn = args[1].getStringValue();
        try {
            long ctxID = ((IntegerValue) args[0].itemAt(0)).getLong();
            DirContext ctx = (DirContext) JNDIModule.retrieveJNDIContext(context, ctxID);
            if (ctx == null) {
                logger.error("jndi:create() - Invalid JNDI context handle provided: {}", ctxID);
            } else {
                BasicAttributes attributes = JNDIModule.parseAttributes(args[2]);
                if (attributes.size() > 0) {
                    ctx.createSubcontext(dn, attributes);
                } else {
                    ctx.createSubcontext(dn);
                }
            }
        } catch (NamingException ne) {
            logger.error("jndi:create() Create failed for dn [{}]: ", dn, ne);
            throw (new XPathException(this, "jndi:create() Create failed for dn [" + dn + "]: " + ne));
        }
    }
    return (Sequence.EMPTY_SEQUENCE);
}
Also used : BasicAttributes(javax.naming.directory.BasicAttributes) XPathException(org.exist.xquery.XPathException) IntegerValue(org.exist.xquery.value.IntegerValue) NamingException(javax.naming.NamingException) DirContext(javax.naming.directory.DirContext)

Example 44 with XPathException

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

the class MailFolderFunctions method closeMailFolder.

private Sequence closeMailFolder(Sequence[] args, Sequence contextSequence) throws XPathException {
    // was a folder handle specified?
    if (args[0].isEmpty()) {
        throw (new XPathException(this, "Folder handle not specified"));
    }
    boolean expunge = ((BooleanValue) args[1].itemAt(0)).effectiveBooleanValue();
    // 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"));
    }
    try {
        folder.close(expunge);
    } catch (MessagingException me) {
        throw (new XPathException(this, "Failed to close mail folder", me));
    } finally {
        MailModule.removeFolder(context, folderHandle);
    }
    return (Sequence.EMPTY_SEQUENCE);
}
Also used : XPathException(org.exist.xquery.XPathException) MessagingException(jakarta.mail.MessagingException) BooleanValue(org.exist.xquery.value.BooleanValue) IntegerValue(org.exist.xquery.value.IntegerValue) Folder(jakarta.mail.Folder)

Example 45 with XPathException

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

the class GetScheduledJobs method eval.

/**
 * evaluate the call to the xquery function, it is really the main entry point of this class.
 *
 * @param   args             arguments from the function call
 * @param   contextSequence  the Context Sequence to operate on (not used here internally!)
 *
 * @return  A sequence representing the result of the function call
 *
 * @throws  XPathException  DOCUMENT ME!
 *
 * @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 {
    Subject user = context.getSubject();
    boolean userhasDBARole = user.hasDbaRole();
    StringBuilder xmlBuf = new StringBuilder();
    int iJobs = 0;
    List<String> groups = scheduler.getJobGroupNames();
    List<ScheduledJobInfo> scheduledJobs = scheduler.getScheduledJobs();
    for (String group : groups) {
        if (userhasDBARole || group.equals(UserJob.JOB_GROUP)) {
            xmlBuf.append("<" + SchedulerModule.PREFIX + ":group name=\"").append(group).append("\">");
            for (ScheduledJobInfo scheduledJob : scheduledJobs) {
                if (scheduledJob.getGroup().equals(group)) {
                    xmlBuf.append("<" + SchedulerModule.PREFIX + ":job name=\"").append(scheduledJob.getName()).append("\">");
                    xmlBuf.append("<" + SchedulerModule.PREFIX + ":trigger name=\"").append(scheduledJob.getTriggerName()).append("\">");
                    xmlBuf.append("<expression>");
                    xmlBuf.append(scheduledJob.getTriggerExpression());
                    xmlBuf.append("</expression>");
                    xmlBuf.append("<state>");
                    xmlBuf.append(scheduledJob.getTriggerState());
                    xmlBuf.append("</state>");
                    xmlBuf.append("<start>");
                    xmlBuf.append(new DateTimeValue(scheduledJob.getStartTime()));
                    xmlBuf.append("</start>");
                    xmlBuf.append("<end>");
                    Date endTime = scheduledJob.getEndTime();
                    if (endTime != null) {
                        xmlBuf.append(new DateTimeValue(endTime));
                    }
                    xmlBuf.append("</end>");
                    xmlBuf.append("<previous>");
                    Date previousTime = scheduledJob.getPreviousFireTime();
                    if (previousTime != null) {
                        xmlBuf.append(new DateTimeValue(scheduledJob.getPreviousFireTime()));
                    }
                    xmlBuf.append("</previous>");
                    xmlBuf.append("<next>");
                    Date nextTime = scheduledJob.getNextFireTime();
                    if (nextTime != null) {
                        xmlBuf.append(new DateTimeValue());
                    }
                    xmlBuf.append("</next>");
                    xmlBuf.append("<final>");
                    Date finalTime = scheduledJob.getFinalFireTime();
                    if ((endTime != null) && (finalTime != null)) {
                        xmlBuf.append(new DateTimeValue());
                    }
                    xmlBuf.append("</final>");
                    xmlBuf.append("</" + SchedulerModule.PREFIX + ":trigger>");
                    xmlBuf.append("</" + SchedulerModule.PREFIX + ":job>");
                    iJobs++;
                }
            }
            xmlBuf.append("</" + SchedulerModule.PREFIX + ":group>");
        }
    }
    xmlBuf.insert(0, "<" + SchedulerModule.PREFIX + ":jobs xmlns:scheduler=\"" + SchedulerModule.NAMESPACE_URI + "\" count=\"" + iJobs + "\">");
    xmlBuf.append("</" + SchedulerModule.PREFIX + ":jobs>");
    try {
        return ModuleUtils.stringToXML(context, xmlBuf.toString());
    } catch (SAXException | IOException se) {
        throw new XPathException(this, se.getMessage(), se);
    }
}
Also used : DateTimeValue(org.exist.xquery.value.DateTimeValue) XPathException(org.exist.xquery.XPathException) IOException(java.io.IOException) Subject(org.exist.security.Subject) Date(java.util.Date) SAXException(org.xml.sax.SAXException) ScheduledJobInfo(org.exist.scheduler.ScheduledJobInfo)

Aggregations

XPathException (org.exist.xquery.XPathException)306 Sequence (org.exist.xquery.value.Sequence)86 IOException (java.io.IOException)61 SAXException (org.xml.sax.SAXException)43 StringValue (org.exist.xquery.value.StringValue)40 PermissionDeniedException (org.exist.security.PermissionDeniedException)34 NodeValue (org.exist.xquery.value.NodeValue)34 DBBroker (org.exist.storage.DBBroker)32 IntegerValue (org.exist.xquery.value.IntegerValue)32 ValueSequence (org.exist.xquery.value.ValueSequence)27 Item (org.exist.xquery.value.Item)26 MemTreeBuilder (org.exist.dom.memtree.MemTreeBuilder)24 EXistException (org.exist.EXistException)23 Path (java.nio.file.Path)22 XmldbURI (org.exist.xmldb.XmldbURI)22 BrokerPool (org.exist.storage.BrokerPool)21 Txn (org.exist.storage.txn.Txn)21 XQueryContext (org.exist.xquery.XQueryContext)21 Element (org.w3c.dom.Element)21 XQuery (org.exist.xquery.XQuery)20