Search in sources :

Example 76 with UserException

use of com.dexels.navajo.script.api.UserException in project navajo by Dexels.

the class JDBCMap method getStreamingResultSet.

@Override
public Iterator<ResultSetMap> getStreamingResultSet() throws UserException {
    ResultSet rs = null;
    try {
        if (resultSet == null) {
            rs = getDBResultSet(false);
        }
        if (debug) {
            Access.writeToConsole(myAccess, "SQLMAP, QUERY HAS BEEN EXECUTED, RETRIEVING RESULTSET\n");
        }
        if (rs != null) {
            int columns = 0;
            ResultSetMetaData meta = null;
            try {
                meta = rs.getMetaData();
                columns = meta.getColumnCount();
            } catch (Exception e) {
                throw new UserException(-1, "Error getting metadata / columns", e);
            }
            // Check if previous version exists, if so, close it.
            if (myResultSetIterator != null) {
                myResultSetIterator.close();
            }
            myResultSetIterator = new ResultSetIterator(rs, meta, columns);
            return myResultSetIterator;
        } else {
            return null;
        }
    } catch (SQLException sqle) {
        AuditLog.log("SQLMap", sqle.getMessage(), Level.SEVERE, (myAccess != null ? (myAccess != null ? myAccess.accessID : "unknown access") : "unknown access"));
        throw new UserException(-1, sqle.getMessage(), sqle);
    }
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) ResultSetIterator(com.dexels.navajo.adapter.sqlmap.ResultSetIterator) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) UserException(com.dexels.navajo.script.api.UserException) UserException(com.dexels.navajo.script.api.UserException) SQLException(java.sql.SQLException) MappableException(com.dexels.navajo.script.api.MappableException) IOException(java.io.IOException)

Example 77 with UserException

use of com.dexels.navajo.script.api.UserException in project navajo by Dexels.

the class FileMap method setContent.

public void setContent(Binary b) throws UserException {
    if (fileName == null) {
        throw new UserException(-1, "Set filename before setting content");
    }
    if (b == null) {
        throw new UserException(-1, "No or empty content specified");
    }
    this.content = b;
    try {
        FileOutputStream fo = new FileOutputStream(this.fileName);
        b.write(fo);
        fo.flush();
        fo.close();
        this.fileName = null;
    } catch (Exception e) {
        throw new UserException("Error writing file: " + this.fileName, e);
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) UserException(com.dexels.navajo.script.api.UserException) IOException(java.io.IOException) UserException(com.dexels.navajo.script.api.UserException) MappableException(com.dexels.navajo.script.api.MappableException)

Example 78 with UserException

use of com.dexels.navajo.script.api.UserException in project navajo by Dexels.

the class FileMap method getContent.

// TODO Should be streaming, easy rewrite
public Binary getContent() throws UserException {
    try {
        Binary b = new Binary(getBytes());
        b.setMimeType("application/text");
        return b;
    } catch (Exception e) {
        throw new UserException(-1, e.getMessage());
    }
}
Also used : Binary(com.dexels.navajo.document.types.Binary) UserException(com.dexels.navajo.script.api.UserException) IOException(java.io.IOException) UserException(com.dexels.navajo.script.api.UserException) MappableException(com.dexels.navajo.script.api.MappableException)

Example 79 with UserException

use of com.dexels.navajo.script.api.UserException in project navajo by Dexels.

the class MailMap method sendMail.

private final void sendMail() throws UserException {
    final ClassLoader current = Thread.currentThread().getContextClassLoader();
    retries++;
    try {
        Thread.currentThread().setContextClassLoader(javax.mail.Session.class.getClassLoader());
        String result = "";
        result = text;
        Session session = createSession();
        javax.mail.Message msg = new MimeMessage(session);
        if (sender == null || "".equals(sender)) {
            throw new UserException(-1, "Error: Required sender address not set!");
        }
        msg.setFrom(new InternetAddress(sender));
        InternetAddress[] recipients = convertToInternetAddresses(this.recipientArray);
        msg.setRecipients(javax.mail.Message.RecipientType.TO, recipients);
        InternetAddress[] ccrecipients = null;
        if (ccArray != null) {
            ccrecipients = convertToInternetAddresses(this.ccArray);
            msg.setRecipients(javax.mail.Message.RecipientType.CC, ccrecipients);
        }
        InternetAddress[] bccrecipients = null;
        if (bccArray != null) {
            bccrecipients = convertToInternetAddresses(this.bccArray);
            msg.setRecipients(javax.mail.Message.RecipientType.BCC, bccrecipients);
        }
        msg.setSubject(subject);
        msg.setSentDate(new java.util.Date());
        // Use stylesheet if specified.
        if (!xslFile.equals("")) {
            java.io.File xsl = new java.io.File(xslFile);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            doc.write(bos);
            bos.close();
            ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
            Document dc = XMLDocumentUtils.createDocument(bis, false);
            bis.close();
            result = XMLDocumentUtils.transform(dc, xsl);
        }
        if (attachments == null && contentType.equals("text/plain")) {
            msg.setText(result);
        } else if (attachments == null || attachments.isEmpty()) {
            msg.setContent(result, contentType);
        } else {
            Multipart multipart = (relatedMultipart ? new MimeMultipart("related") : new MimeMultipart());
            BodyPart textBody = new MimeBodyPart();
            textBody.setContent(result, contentType);
            multipart.addBodyPart(textBody);
            if (attachments != null) {
                for (int i = 0; i < attachments.size(); i++) {
                    AttachmentMapInterface am = attachments.get(i);
                    String file = am.getAttachFile();
                    String userFileName = am.getAttachFileName();
                    Binary content = am.getAttachFileContent();
                    String encoding = am.getEncoding();
                    MimeBodyPart bp = new MimeBodyPart();
                    if (file != null) {
                        if (userFileName == null) {
                            userFileName = file;
                        }
                        FileDataSource fileDatasource = new FileDataSource(file);
                        bp.setDataHandler(new DataHandler(fileDatasource));
                    } else if (content != null) {
                        BinaryDataSource bds = new BinaryDataSource(content, "");
                        DataHandler dh = new DataHandler(bds);
                        bp.setDataHandler(dh);
                        bp.setFileName(userFileName);
                        if (encoding != null) {
                            bp.setHeader("Content-Transfer-Encoding", encoding);
                            encoding = null;
                        }
                    }
                    if (relatedMultipart) {
                        bp.setHeader("Content-ID", "<attach-nr-" + i + ">");
                    }
                    bp.getFileName();
                    // iPhone headers
                    bp.setDisposition(am.getAttachContentDisposition());
                    multipart.addBodyPart(bp);
                }
            }
            msg.setContent(multipart);
        }
        logger.info("Sending mail to: {}, cc: {}, bcc: {} with subject: {}", Arrays.toString(recipients), ccrecipients != null ? Arrays.toString(ccrecipients) : "[]", bccrecipients != null ? Arrays.toString(bccrecipients) : "[]", subject);
        Transport.send(msg);
    } catch (Exception e) {
        logger.error("Exception on sending mail!", e);
        if (ignoreFailures) {
            AuditLog.log("MailMap", e.getMessage(), e, Level.WARNING, myAccess.accessID);
            logger.warn("ingoreFailures flag is set. Ignoring the invalid e-mail address.");
            failureBuffer.append(e.getMessage());
            failureBuffer.append(";");
        } else {
            AuditLog.log("MailMap", e.getMessage(), e, Level.SEVERE, myAccess.accessID);
            throw new UserException(-1, e.getMessage(), e);
        }
    } finally {
        Thread.currentThread().setContextClassLoader(current);
    }
}
Also used : MimeBodyPart(javax.mail.internet.MimeBodyPart) BodyPart(javax.mail.BodyPart) InternetAddress(javax.mail.internet.InternetAddress) Multipart(javax.mail.Multipart) MimeMultipart(javax.mail.internet.MimeMultipart) DataHandler(javax.activation.DataHandler) Document(org.w3c.dom.Document) BinaryDataSource(com.dexels.navajo.datasource.BinaryDataSource) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) FileDataSource(javax.activation.FileDataSource) UserException(com.dexels.navajo.script.api.UserException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AttachmentMapInterface(com.dexels.navajo.adapter.mailmap.AttachmentMapInterface) UserException(com.dexels.navajo.script.api.UserException) MappableException(com.dexels.navajo.script.api.MappableException) ByteArrayInputStream(java.io.ByteArrayInputStream) Binary(com.dexels.navajo.document.types.Binary) MimeBodyPart(javax.mail.internet.MimeBodyPart) Session(javax.mail.Session)

Example 80 with UserException

use of com.dexels.navajo.script.api.UserException in project navajo by Dexels.

the class MailMap method convertToInternetAddresses.

/**
 * Converts each e-mail string of an array of strings to their corresponding
 * InternetAddreses and returns an array with InternetAddreses
 *
 * If ignoreFailures is set, then no exceptions will be thrown
 */
private InternetAddress[] convertToInternetAddresses(String[] strAddreses) throws UserException {
    ArrayList<InternetAddress> addresses = new ArrayList<>();
    for (int i = 0; i < strAddreses.length; i++) {
        try {
            addresses.add(new InternetAddress(strAddreses[i]));
        } catch (Exception e) {
            logger.warn("Invalid e-mail address was provided : {}", strAddreses[i]);
            if (ignoreFailures) {
                AuditLog.log("MailMap", e.getMessage(), e, Level.WARNING, myAccess.accessID);
                logger.warn("ingoreFailures flag is set. Ignoring the invalid e-mail address.");
                failureBuffer.append(e.getMessage());
                failureBuffer.append(";");
            } else {
                AuditLog.log("MailMap", e.getMessage(), e, Level.SEVERE, myAccess.accessID);
                throw new UserException(-1, e.getMessage(), e);
            }
        }
    }
    InternetAddress[] addr = new InternetAddress[addresses.size()];
    for (int i = 0; i < addresses.size(); i++) {
        addr[i] = addresses.get(i);
    }
    return addr;
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) ArrayList(java.util.ArrayList) UserException(com.dexels.navajo.script.api.UserException) UserException(com.dexels.navajo.script.api.UserException) MappableException(com.dexels.navajo.script.api.MappableException)

Aggregations

UserException (com.dexels.navajo.script.api.UserException)113 MappableException (com.dexels.navajo.script.api.MappableException)54 IOException (java.io.IOException)33 NavajoException (com.dexels.navajo.document.NavajoException)25 Message (com.dexels.navajo.document.Message)22 SQLException (java.sql.SQLException)19 SystemException (com.dexels.navajo.script.api.SystemException)18 Binary (com.dexels.navajo.document.types.Binary)14 ConditionErrorException (com.dexels.navajo.server.ConditionErrorException)13 Property (com.dexels.navajo.document.Property)12 ArrayList (java.util.ArrayList)12 Navajo (com.dexels.navajo.document.Navajo)11 AuthorizationException (com.dexels.navajo.script.api.AuthorizationException)11 ResultSet (java.sql.ResultSet)11 MappingException (com.dexels.navajo.script.api.MappingException)10 ResultSetMetaData (java.sql.ResultSetMetaData)9 Element (org.w3c.dom.Element)8 CompilationException (com.dexels.navajo.script.api.CompilationException)7 File (java.io.File)7 NodeList (org.w3c.dom.NodeList)7