Search in sources :

Example 46 with UserException

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

the class NavajoMap method getPropertyObject.

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

Example 47 with UserException

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

the class CommonsMailMap method sendMail.

/**
 * This is where the actual mail is constructed and send
 * Inline images can be used through attachments
 * Annotation is cid:{?}. This will be replaced with cid:?
 * The first ? refers to the index number in the attachments starting with 0
 * The second ? will contain the generated id
 * EXAMPLE:
 * <hmtl>Some text <img src=\"cid:{0}\"></html>
 * The {0} will then be replaced with the first generated inline tag
 * @throws UserException
 */
public void sendMail() throws UserException {
    final ClassLoader current = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(javax.mail.Session.class.getClassLoader());
        // Create the email message and fill the basics
        HtmlEmail email = getNewHtmlEmail();
        if (debug) {
            email.setDebug(debug);
        }
        fillHtmlEmailBasics(email);
        // add attachments
        List<String> inlineImages = new ArrayList<>();
        if (this.attachments != null) {
            logger.debug("# of attachments found: {}", attachments.size());
            for (int i = 0; i < this.attachments.size(); i++) {
                AttachmentMapInterface am = this.attachments.get(i);
                String file = am.getAttachFile();
                String userFileName = am.getAttachFileName();
                Binary content = am.getAttachFileContent();
                String contentDisposition = am.getAttachContentDisposition();
                // Figure out how to get the path and then the url
                String fileName = "";
                if (content != null) {
                    fileName = content.getTempFileName(false);
                } else {
                    fileName = file;
                }
                File fl = new File(fileName);
                URL url = fl.toURI().toURL();
                logger.debug("Using url: {}", url);
                if (contentDisposition != null && contentDisposition.equalsIgnoreCase("Inline")) {
                    // embed the image and get the content id
                    inlineImages.add(email.embed(url, userFileName));
                } else {
                    email.attach(this.getEmailAttachment(fileName, url, contentDisposition, userFileName, userFileName));
                }
            }
        } else {
            logger.debug("No attachments");
        }
        logger.debug("Setting body, before replace: " + bodyText);
        // Replace any inline image tags with the created ones
        bodyText = replaceInlineImageTags(bodyText, inlineImages);
        // Finally set the complete html
        logger.debug("Setting body: {}", bodyText);
        email.setHtmlMsg(bodyText);
        // set the alternative message
        email.setTextMsg(this.getNonHtmlText());
        logger.info("Sending mail to {} cc: {} bcc: {} with subject: {}", to, cc, bcc, subject);
        // send the email
        email.send();
    } catch (Exception e) {
        if (ignoreFailures) {
            AuditLog.log("CommonsMailMap", e.getMessage(), e, Level.WARNING, myAccess.accessID);
            failure = e.getMessage();
        } else {
            AuditLog.log("CommonsMailMap", e.getMessage(), e, Level.SEVERE, myAccess.accessID);
            throw new UserException(-1, e.getMessage(), e);
        }
    } finally {
        Thread.currentThread().setContextClassLoader(current);
    }
}
Also used : HtmlEmail(org.apache.commons.mail.HtmlEmail) ArrayList(java.util.ArrayList) AttachmentMapInterface(com.dexels.navajo.adapter.mailmap.AttachmentMapInterface) URL(java.net.URL) UserException(com.dexels.navajo.script.api.UserException) MappableException(com.dexels.navajo.script.api.MappableException) EmailException(org.apache.commons.mail.EmailException) Binary(com.dexels.navajo.document.types.Binary) UserException(com.dexels.navajo.script.api.UserException) File(java.io.File) Session(javax.mail.Session)

Example 48 with UserException

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

the class CommonsMailMap method getNewHtmlEmail.

/**
 * Create the new HtmlEmail message and set session props
 * @return HtmlEmail
 * @throws UserException
 * @throws EmailException
 */
private HtmlEmail getNewHtmlEmail() throws UserException {
    // Create the email message
    Session session = createSession();
    HtmlEmail email = new HtmlEmail();
    email.setMailSession(session);
    email.setCharset(org.apache.commons.mail.EmailConstants.UTF_8);
    if (from == null || "".equals(from)) {
        throw new UserException(-1, "Error: Required sender address not set!");
    }
    return email;
}
Also used : HtmlEmail(org.apache.commons.mail.HtmlEmail) UserException(com.dexels.navajo.script.api.UserException) Session(javax.mail.Session)

Example 49 with UserException

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

the class IncludeMap method store.

@Override
public void store() throws MappableException, UserException {
    try {
        File f = new File(navajoObject);
        if (type == null || type.equals("tml") || type.equals("tsl")) {
            Navajo n = (type != null && type.equals("tsl") ? NavajoFactory.getInstance().createNavaScript(new FileInputStream(f)) : NavajoFactory.getInstance().createNavajo(new FileInputStream(f)));
            Message current = access.getCurrentOutMessage();
            List<Message> msgList = n.getAllMessages();
            for (int i = 0; i < msgList.size(); i++) {
                Message tbc = msgList.get(i);
                Message copy = tbc.copy(access.getOutputDoc());
                if (current != null) {
                    current.addMessage(copy);
                } else {
                    access.getOutputDoc().addMessage(copy);
                }
            }
        }
    } catch (Exception e) {
        throw new UserException(-1, e.getMessage(), e);
    }
}
Also used : Message(com.dexels.navajo.document.Message) Navajo(com.dexels.navajo.document.Navajo) UserException(com.dexels.navajo.script.api.UserException) File(java.io.File) FileInputStream(java.io.FileInputStream) UserException(com.dexels.navajo.script.api.UserException) MappableException(com.dexels.navajo.script.api.MappableException)

Example 50 with UserException

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

the class HTTPMap method sendOverHTTP.

private final void sendOverHTTP() throws UserException {
    increaseInstanceCount();
    if (!isBelowInstanceThreshold()) {
        logger.warn("WARNING: More than 100 waiting HTTP requests");
    }
    logger.info("About to send to: {}", url);
    URL u = null;
    try {
        if (!url.startsWith("http://") && (!url.startsWith("https://"))) {
            logger.warn("No protocol. Always prepend protocol. Assuming http.");
            u = new URL("http://" + url);
        } else {
            u = new URL(url);
        }
    } catch (MalformedURLException e1) {
        throw new UserException("Malformed URL: " + url, e1);
    }
    try {
        HttpURLConnection con = createConnectionWithProxy(u);
        con.setConnectTimeout(connectTimeOut);
        if (readTimeOut != -1) {
            con.setReadTimeout(readTimeOut);
        }
        con.setRequestMethod(method);
        if (method.equals("POST") || method.equals("PUT") || method.equals("DELETE")) {
            con.setDoOutput(true);
            if (!method.equals("DELETE")) {
                con.setDoInput(true);
            }
        }
        con.setUseCaches(false);
        if (contentType != null) {
            con.setRequestProperty("Content-type", contentType);
        }
        if (contentLength > 0) {
            con.setRequestProperty("Content-length", contentLength + "");
        }
        // Add headers
        if (headers.size() > 0) {
            Iterator<String> keySet = headers.keySet().iterator();
            while (keySet.hasNext()) {
                String key = keySet.next();
                con.setRequestProperty(key, headers.get(key));
            }
        }
        if (textContent != null) {
            OutputStreamWriter osw = null;
            osw = new OutputStreamWriter(con.getOutputStream());
            try {
                osw.write(textContent);
            } finally {
                osw.close();
            }
        } else if (content != null && !method.equals("GET") && !method.equals("HEAD")) {
            OutputStream os = null;
            os = con.getOutputStream();
            try {
                content.write(os);
            } finally {
                if (os != null) {
                    os.close();
                }
            }
        } else {
            if (method.equals("POST")) {
                logger.warn("Empty content.");
                throw new UserException(-1, "");
            }
        }
        responseCode = con.getResponseCode();
        responseMessage = con.getResponseMessage();
        responseContentType = con.getHeaderField("Content-Type");
        InputStream is = (responseCode < 400 ? con.getInputStream() : con.getErrorStream());
        if (responseCode > 299) {
            logger.warn("Got a {} response code back on call to {}: {}", responseCode, url, responseMessage);
        }
        try {
            result = new Binary(is);
        } finally {
            if (is != null) {
                is.close();
            }
        }
    } catch (java.net.SocketTimeoutException sto) {
        // 
        if (!catchConnectionTimeOut) {
            logger.error("Got connectiontimeout for url: {}", url);
            throw new UserException(-1, sto.getMessage(), sto);
        } else {
            logger.warn("Got connectiontimeout for url: {}", url);
            hasConnectionTimeOut = true;
        }
    } catch (Exception e) {
        throw new UserException(-1, e.getMessage(), e);
    } finally {
        decreaseInstanceCount();
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) URL(java.net.URL) UserException(com.dexels.navajo.script.api.UserException) MappableException(com.dexels.navajo.script.api.MappableException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) HttpURLConnection(java.net.HttpURLConnection) OutputStreamWriter(java.io.OutputStreamWriter) UserException(com.dexels.navajo.script.api.UserException) Binary(com.dexels.navajo.document.types.Binary)

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