Search in sources :

Example 51 with UserException

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

the class MailMapAlternative method sendMail.

private final void sendMail() throws UserException {
    retries++;
    try {
        String result = "";
        result = text;
        Properties props = System.getProperties();
        props.put("mail.smtp.host", mailServer);
        Session session = Session.getInstance(props);
        javax.mail.Message msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress(sender));
        InternetAddress[] addresses = new InternetAddress[this.recipientArray.length];
        for (int i = 0; i < this.recipientArray.length; i++) {
            addresses[i] = new InternetAddress(this.recipientArray[i]);
        }
        msg.setRecipients(javax.mail.Message.RecipientType.TO, addresses);
        if (ccArray != null) {
            InternetAddress[] extra = new InternetAddress[this.ccArray.length];
            for (int i = 0; i < this.ccArray.length; i++) {
                extra[i] = new InternetAddress(this.ccArray[i]);
            }
            msg.setRecipients(javax.mail.Message.RecipientType.CC, extra);
        }
        if (bccArray != null) {
            InternetAddress[] extra = new InternetAddress[this.bccArray.length];
            for (int i = 0; i < this.bccArray.length; i++) {
                extra[i] = new InternetAddress(this.bccArray[i]);
            }
            msg.setRecipients(javax.mail.Message.RecipientType.BCC, extra);
        }
        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 navajoDoc = XMLDocumentUtils.createDocument(bis, false);
            bis.close();
            result = XMLDocumentUtils.transform(navajoDoc, xsl);
        }
        if (attachments == null && contentType.equals("text/plain")) {
            msg.setText(result);
        } else {
            Multipart multipart = new MimeMultipart("mixed");
            BodyPart textBody = new MimeBodyPart();
            textBody.setContent(result, contentType);
            Multipart related = new MimeMultipart("related");
            related.addBodyPart(textBody);
            if (bodyparts != null && !bodyparts.isEmpty()) {
                // Put related bodyparts in related.
                for (int i = 0; i < bodyparts.size(); i++) {
                    AttachmentMapInterface am = bodyparts.get(i);
                    String file = am.getAttachFile();
                    String userFileName = am.getAttachFileName();
                    Binary content = am.getAttachFileContent();
                    String encoding = am.getEncoding();
                    String attachContentType = am.getAttachContentType();
                    MimeBodyPart bp = new MimeBodyPart();
                    logger.debug("Embedding: {}", userFileName);
                    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);
                        if (encoding != null) {
                            bp.setHeader("Content-Transfer-Encoding", encoding);
                            encoding = null;
                        }
                    }
                    bp.setFileName(userFileName);
                    if (attachContentType != null) {
                        bp.setHeader("Content-Type", attachContentType);
                    }
                    bp.setHeader("Content-ID", "<attach-nr-" + i + ">");
                    related.addBodyPart(bp);
                }
            }
            MimeBodyPart bop = new MimeBodyPart();
            bop.setContent(related);
            multipart.addBodyPart(bop);
            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();
                    String attachContentType = am.getAttachContentType();
                    String attachContentDisposition = am.getAttachContentDisposition();
                    MimeBodyPart bp = new MimeBodyPart();
                    logger.debug("Attaching: {}", userFileName);
                    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);
                        if (encoding != null) {
                            bp.setHeader("Content-Transfer-Encoding", encoding);
                        }
                    }
                    bp.setFileName(userFileName);
                    if (attachContentType != null) {
                        bp.setHeader("Content-Type", attachContentType);
                    }
                    bp.setDisposition(attachContentDisposition);
                    multipart.addBodyPart(bp);
                }
            }
            msg.setContent(multipart);
        }
        logger.info("Sending mail to {} cc: {} bcc: {} with subject: {}", recipients, cc, bcc, subject);
        Transport.send(msg);
    } catch (Exception e) {
        if (ignoreFailures) {
            AuditLog.log("MailMap", e.getMessage(), e, Level.WARNING, myAccess.accessID);
            failure = e.getMessage();
        } else {
            AuditLog.log("MailMap", e.getMessage(), e, Level.SEVERE, myAccess.accessID);
            throw new UserException(-1, e.getMessage());
        }
    }
}
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) Properties(java.util.Properties) 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 52 with UserException

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

the class NavajoMap method getSelections.

public OptionMap[] getSelections() throws UserException {
    if (selectionPointer == null) {
        throw new UserException(-1, "Set selectionPointer first.");
    }
    Property p = getPropertyObject(selectionPointer);
    if (!p.getType().equals(Property.SELECTION_PROPERTY)) {
        throw new UserException(-1, "selections only supported for selection properties");
    }
    List<Selection> all = p.getAllSelections();
    OptionMap[] om = new OptionMap[all.size()];
    for (int i = 0; i < all.size(); i++) {
        Selection s = all.get(i);
        om[i] = new OptionMap();
        om[i].setOptionName(s.getName());
        om[i].setOptionValue(s.getValue());
        om[i].setOptionSelected(s.isSelected());
    }
    return om;
}
Also used : Selection(com.dexels.navajo.document.Selection) UserException(com.dexels.navajo.script.api.UserException) Property(com.dexels.navajo.document.Property)

Example 53 with UserException

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

the class NavajoMap method getMessage.

public Message getMessage(String fullName) throws UserException {
    waitForResult();
    Message msg = null;
    if (msgPointer != null)
        msg = msgPointer.getMessage(fullName);
    else
        msg = inDoc.getMessage(fullName);
    if (msg == null)
        throw new UserException(-1, "Message " + fullName + " does not exists in response document");
    return msg;
}
Also used : Message(com.dexels.navajo.document.Message) UserException(com.dexels.navajo.script.api.UserException)

Example 54 with UserException

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

the class NavajoMap method setAppendParms.

/**
 * @param b
 * @throws UserException
 *
 *             if messageOffset is '/', the messages of the received Doc will be appended to the root param block or to the current param message.
 */
public final void setAppendParms(String messageOffset) throws UserException {
    waitForResult();
    try {
        Message parm = (access.getCompiledScript().getCurrentParamMsg() == null ? access.getInDoc().getMessage("__parms__") : access.getCompiledScript().getCurrentParamMsg());
        List<Message> list = null;
        // If append message equals '/'.
        if (messageOffset.equals(Navajo.MESSAGE_SEPARATOR)) {
            list = inDoc.getAllMessages();
        } else if (inDoc.getMessage(messageOffset) == null) {
            return;
        } else if (inDoc.getMessage(messageOffset).getType().equals(Message.MSG_TYPE_ARRAY)) {
            list = new ArrayList<>();
            list.add(inDoc.getMessage(messageOffset));
        } else {
            list = inDoc.getMessages(messageOffset);
        }
        for (int i = 0; i < list.size(); i++) {
            Message inMsg = list.get(i);
            // Clone message and append it to currentMsg if it exists, else
            // directly under currentDoc.
            // currentDoc.importMessage(inMsg)
            Message clone = inDoc.copyMessage(inMsg, parm.getRootDoc());
            parm.addMessage(clone, true);
        }
    } catch (NavajoException ne) {
        throw new UserException(-1, ne.getMessage());
    }
}
Also used : Message(com.dexels.navajo.document.Message) NavajoException(com.dexels.navajo.document.NavajoException) UserException(com.dexels.navajo.script.api.UserException)

Example 55 with UserException

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

the class NavajoMap method setSelections.

public void setSelections(OptionMap[] selections) throws UserException {
    if (currentProperty == null) {
        throw new UserException(-1, "Set property name first.");
    }
    currentProperty.setType(Property.SELECTION_PROPERTY);
    currentProperty.clearValue();
    currentProperty.clearSelections();
    int selected = 0;
    for (int i = 0; i < selections.length; i++) {
        Selection s = NavajoFactory.getInstance().createSelection(currentProperty.getRootDoc(), selections[i].getOptionName(), selections[i].getOptionValue(), selections[i].getOptionSelected());
        currentProperty.addSelection(s);
        if (selections[i].getOptionSelected()) {
            selected++;
        }
    }
    if (selected > 1) {
        currentProperty.setCardinality("+");
    } else {
        currentProperty.setCardinality("1");
    }
    addProperty(currentProperty);
}
Also used : Selection(com.dexels.navajo.document.Selection) UserException(com.dexels.navajo.script.api.UserException)

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