Search in sources :

Example 1 with IPentahoStreamSource

use of org.pentaho.commons.connection.IPentahoStreamSource in project pentaho-platform by pentaho.

the class EmailComponent method executeAction.

@Override
public boolean executeAction() {
    EmailAction emailAction = (EmailAction) getActionDefinition();
    String messagePlain = emailAction.getMessagePlain().getStringValue();
    String messageHtml = emailAction.getMessageHtml().getStringValue();
    String subject = emailAction.getSubject().getStringValue();
    String to = emailAction.getTo().getStringValue();
    String cc = emailAction.getCc().getStringValue();
    String bcc = emailAction.getBcc().getStringValue();
    String from = emailAction.getFrom().getStringValue(defaultFrom);
    if (from.trim().length() == 0) {
        from = defaultFrom;
    }
    if (ComponentBase.debug) {
        // $NON-NLS-1$
        debug(Messages.getInstance().getString("Email.DEBUG_TO_FROM", to, from));
        // $NON-NLS-1$
        debug(Messages.getInstance().getString("Email.DEBUG_CC_BCC", cc, bcc));
        // $NON-NLS-1$
        debug(Messages.getInstance().getString("Email.DEBUG_SUBJECT", subject));
        // $NON-NLS-1$
        debug(Messages.getInstance().getString("Email.DEBUG_PLAIN_MESSAGE", messagePlain));
        // $NON-NLS-1$
        debug(Messages.getInstance().getString("Email.DEBUG_HTML_MESSAGE", messageHtml));
    }
    if ((to == null) || (to.trim().length() == 0)) {
        // Get the output stream that the feedback is going into
        OutputStream feedbackStream = getFeedbackOutputStream();
        if (feedbackStream != null) {
            createFeedbackParameter("to", Messages.getInstance().getString("Email.USER_ENTER_EMAIL_ADDRESS"), "", "", // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
            true);
            // $NON-NLS-1$
            setFeedbackMimeType("text/html");
            return true;
        } else {
            return false;
        }
    }
    if (subject == null) {
        // $NON-NLS-1$
        error(Messages.getInstance().getErrorString("Email.ERROR_0005_NULL_SUBJECT", getActionName()));
        return false;
    }
    if ((messagePlain == null) && (messageHtml == null)) {
        // $NON-NLS-1$
        error(Messages.getInstance().getErrorString("Email.ERROR_0006_NULL_BODY", getActionName()));
        return false;
    }
    if (getRuntimeContext().isPromptPending()) {
        return true;
    }
    try {
        Properties props = new Properties();
        final IEmailService service = PentahoSystem.get(IEmailService.class, "IEmailService", PentahoSessionHolder.getSession());
        props.put("mail.smtp.host", service.getEmailConfig().getSmtpHost());
        props.put("mail.smtp.port", ObjectUtils.toString(service.getEmailConfig().getSmtpPort()));
        props.put("mail.transport.protocol", service.getEmailConfig().getSmtpProtocol());
        props.put("mail.smtp.starttls.enable", ObjectUtils.toString(service.getEmailConfig().isUseStartTls()));
        props.put("mail.smtp.auth", ObjectUtils.toString(service.getEmailConfig().isAuthenticate()));
        props.put("mail.smtp.ssl", ObjectUtils.toString(service.getEmailConfig().isUseSsl()));
        props.put("mail.smtp.quitwait", ObjectUtils.toString(service.getEmailConfig().isSmtpQuitWait()));
        props.put("mail.from.default", service.getEmailConfig().getDefaultFrom());
        String fromName = service.getEmailConfig().getFromName();
        if (StringUtils.isEmpty(fromName)) {
            fromName = Messages.getInstance().getString("schedulerEmailFromName");
        }
        props.put("mail.from.name", fromName);
        props.put("mail.debug", ObjectUtils.toString(service.getEmailConfig().isDebug()));
        Session session;
        if (service.getEmailConfig().isAuthenticate()) {
            props.put("mail.userid", service.getEmailConfig().getUserId());
            props.put("mail.password", decrypt(service.getEmailConfig().getPassword()));
            Authenticator authenticator = new EmailAuthenticator();
            session = Session.getInstance(props, authenticator);
        } else {
            session = Session.getInstance(props);
        }
        // debugging is on if either component (xaction) or email config debug is on
        if (service.getEmailConfig().isDebug() || ComponentBase.debug) {
            session.setDebug(true);
        }
        // construct the message
        MimeMessage msg = new MimeMessage(session);
        if (from != null) {
            msg.setFrom(new InternetAddress(from));
        } else {
            // There should be no way to get here
            // $NON-NLS-1$
            error(Messages.getInstance().getString("Email.ERROR_0012_FROM_NOT_DEFINED"));
        }
        if ((to != null) && (to.trim().length() > 0)) {
            msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));
        }
        if ((cc != null) && (cc.trim().length() > 0)) {
            msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc, false));
        }
        if ((bcc != null) && (bcc.trim().length() > 0)) {
            msg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(bcc, false));
        }
        if (subject != null) {
            msg.setSubject(subject, LocaleHelper.getSystemEncoding());
        }
        EmailAttachment[] emailAttachments = emailAction.getAttachments();
        if ((messagePlain != null) && (messageHtml == null) && (emailAttachments.length == 0)) {
            msg.setText(messagePlain, LocaleHelper.getSystemEncoding());
        } else if (emailAttachments.length == 0) {
            if (messagePlain != null) {
                // $NON-NLS-1$
                msg.setContent(messagePlain, "text/plain; charset=" + LocaleHelper.getSystemEncoding());
            }
            if (messageHtml != null) {
                // $NON-NLS-1$
                msg.setContent(messageHtml, "text/html; charset=" + LocaleHelper.getSystemEncoding());
            }
        } else {
            // need to create a multi-part message...
            // create the Multipart and add its parts to it
            Multipart multipart = new MimeMultipart();
            // create and fill the first message part
            if (messageHtml != null) {
                // create and fill the first message part
                MimeBodyPart htmlBodyPart = new MimeBodyPart();
                // $NON-NLS-1$
                htmlBodyPart.setContent(messageHtml, "text/html; charset=" + LocaleHelper.getSystemEncoding());
                multipart.addBodyPart(htmlBodyPart);
            }
            if (messagePlain != null) {
                MimeBodyPart textBodyPart = new MimeBodyPart();
                // $NON-NLS-1$
                textBodyPart.setContent(messagePlain, "text/plain; charset=" + LocaleHelper.getSystemEncoding());
                multipart.addBodyPart(textBodyPart);
            }
            for (EmailAttachment element : emailAttachments) {
                IPentahoStreamSource source = element.getContent();
                if (source == null) {
                    // $NON-NLS-1$
                    error(Messages.getInstance().getErrorString("Email.ERROR_0015_ATTACHMENT_FAILED"));
                    return false;
                }
                DataSource dataSource = new ActivationHelper.PentahoStreamSourceWrapper(source);
                String attachmentName = element.getName();
                if (ComponentBase.debug) {
                    // $NON-NLS-1$
                    debug(Messages.getInstance().getString("Email.DEBUG_ADDING_ATTACHMENT", attachmentName));
                }
                // create the second message part
                MimeBodyPart attachmentBodyPart = new MimeBodyPart();
                // attach the file to the message
                attachmentBodyPart.setDataHandler(new DataHandler(dataSource));
                attachmentBodyPart.setFileName(attachmentName);
                if (ComponentBase.debug) {
                    // $NON-NLS-1$
                    debug(Messages.getInstance().getString("Email.DEBUG_ATTACHMENT_SOURCE", dataSource.getName()));
                }
                multipart.addBodyPart(attachmentBodyPart);
            }
            // add the Multipart to the message
            msg.setContent(multipart);
        }
        // $NON-NLS-1$
        msg.setHeader("X-Mailer", EmailComponent.MAILER);
        msg.setSentDate(new Date());
        Transport.send(msg);
        if (ComponentBase.debug) {
            // $NON-NLS-1$
            debug(Messages.getInstance().getString("Email.DEBUG_EMAIL_SUCCESS"));
        }
        return true;
    // TODO: persist the content set for a while...
    } catch (SendFailedException e) {
        // $NON-NLS-1$
        error(Messages.getInstance().getErrorString("Email.ERROR_0011_SEND_FAILED", to), e);
    /*
       * Exception ne; MessagingException sfe = e; while ((ne = sfe.getNextException()) != null && ne instanceof
       * MessagingException) { sfe = (MessagingException) ne;
       * error(Messages.getInstance().getErrorString("Email.ERROR_0011_SEND_FAILED", sfe.toString()), sfe);
       * //$NON-NLS-1$ }
       */
    } catch (AuthenticationFailedException e) {
        // $NON-NLS-1$
        error(Messages.getInstance().getString("Email.ERROR_0014_AUTHENTICATION_FAILED", to), e);
    } catch (Throwable e) {
        // $NON-NLS-1$
        error(Messages.getInstance().getErrorString("Email.ERROR_0011_SEND_FAILED", to), e);
    }
    return false;
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) Multipart(javax.mail.Multipart) MimeMultipart(javax.mail.internet.MimeMultipart) SendFailedException(javax.mail.SendFailedException) AuthenticationFailedException(javax.mail.AuthenticationFailedException) EmailAttachment(org.pentaho.actionsequence.dom.actions.EmailAttachment) OutputStream(java.io.OutputStream) DataHandler(javax.activation.DataHandler) Properties(java.util.Properties) IPentahoStreamSource(org.pentaho.commons.connection.IPentahoStreamSource) Date(java.util.Date) DataSource(javax.activation.DataSource) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) ActivationHelper(org.pentaho.commons.connection.ActivationHelper) EmailAction(org.pentaho.actionsequence.dom.actions.EmailAction) MimeBodyPart(javax.mail.internet.MimeBodyPart) IEmailService(org.pentaho.platform.api.email.IEmailService) Authenticator(javax.mail.Authenticator) Session(javax.mail.Session)

Example 2 with IPentahoStreamSource

use of org.pentaho.commons.connection.IPentahoStreamSource in project pentaho-platform by pentaho.

the class RuntimeContext method getDataSource.

public IPentahoStreamSource getDataSource(final String parameterName) {
    IPentahoStreamSource dataSource = null;
    // TODO Temp workaround for content repos bug
    IActionParameter actionParameter = paramManager.getCurrentInput(parameterName);
    if (actionParameter == null) {
        throw new InvalidParameterException(Messages.getInstance().getErrorString("RuntimeContext.ERROR_0019_INVALID_INPUT_REQUEST", parameterName, // $NON-NLS-1$
        actionSequence.getSequenceName()));
    }
    Object locObj = actionParameter.getValue();
    if (locObj != null) {
        if (locObj instanceof IContentItem) {
            // At this point we have an IContentItem so why do anything else?
            dataSource = ((IContentItem) locObj).getDataSource();
        }
    }
    // This will return null if the locObj is null
    return dataSource;
}
Also used : InvalidParameterException(org.pentaho.platform.api.engine.InvalidParameterException) IContentItem(org.pentaho.platform.api.repository.IContentItem) IPentahoStreamSource(org.pentaho.commons.connection.IPentahoStreamSource) IActionParameter(org.pentaho.platform.api.engine.IActionParameter)

Example 3 with IPentahoStreamSource

use of org.pentaho.commons.connection.IPentahoStreamSource in project pentaho-platform by pentaho.

the class PojoComponent method callMethods.

protected void callMethods(List<Method> methods, Object value) throws Throwable {
    if (value instanceof String) {
        callMethodWithString(methods, value.toString());
        return;
    }
    boolean done = false;
    for (Method method : methods) {
        Class<?>[] paramClasses = method.getParameterTypes();
        if (paramClasses.length != 1) {
            // we don't know how to handle this
            throw new GenericSignatureFormatError();
        }
        Class<?> paramclass = paramClasses[0];
        // do some type safety. this would be the point to do automatic type conversions
        if (value instanceof IPentahoResultSet && paramclass.equals(IPentahoResultSet.class)) {
            done = true;
            method.invoke(pojo, new Object[] { (IPentahoResultSet) value });
            break;
        } else if (value instanceof java.lang.Boolean && (paramclass.equals(Boolean.class) || paramclass.equals(boolean.class))) {
            done = true;
            method.invoke(pojo, new Object[] { value });
            break;
        } else if (value instanceof java.lang.Integer && (paramclass.equals(Integer.class) || paramclass.equals(int.class))) {
            done = true;
            method.invoke(pojo, new Object[] { value });
            break;
        } else if (value instanceof java.lang.Long && (paramclass.equals(Long.class) || paramclass.equals(long.class))) {
            done = true;
            method.invoke(pojo, new Object[] { value });
            break;
        } else if (value instanceof java.lang.Double && (paramclass.equals(Double.class) || paramclass.equals(double.class))) {
            done = true;
            method.invoke(pojo, new Object[] { value });
            break;
        } else if (value instanceof java.lang.Float && (paramclass.equals(Float.class) || paramclass.equals(float.class))) {
            done = true;
            method.invoke(pojo, new Object[] { value });
            break;
        } else if (value instanceof IPentahoStreamSource && paramclass.equals(IPentahoStreamSource.class)) {
            done = true;
            method.invoke(pojo, new Object[] { value });
            break;
        } else if (value instanceof Date && paramclass.equals(Date.class)) {
            done = true;
            method.invoke(pojo, new Object[] { value });
            break;
        } else if (value instanceof BigDecimal && paramclass.equals(BigDecimal.class)) {
            done = true;
            method.invoke(pojo, new Object[] { value });
            break;
        } else if (value instanceof IContentItem && paramclass.equals(IContentItem.class)) {
            done = true;
            method.invoke(pojo, new Object[] { value });
            break;
        } else if (value instanceof IContentItem && paramclass.equals(String.class)) {
            done = true;
            method.invoke(pojo, new Object[] { value.toString() });
            break;
        } else if (paramclass.equals(IPentahoSession.class)) {
            done = true;
            method.invoke(pojo, new Object[] { (IPentahoSession) value });
            break;
        } else if (paramclass.equals(Log.class)) {
            done = true;
            method.invoke(pojo, new Object[] { (Log) value });
            break;
        }
    }
    if (!done) {
        // Try invoking the first instance with what we have
        try {
            methods.get(0).invoke(pojo, new Object[] { value });
        } catch (Exception ex) {
            throw new IllegalArgumentException(// $NON-NLS-1$ //$NON-NLS-2$
            "No implementation of method \"" + Method.class.getName() + "\" takes a " + value.getClass());
        }
    }
}
Also used : IPentahoStreamSource(org.pentaho.commons.connection.IPentahoStreamSource) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) Method(java.lang.reflect.Method) Date(java.util.Date) BigDecimal(java.math.BigDecimal) PluginBeanException(org.pentaho.platform.api.engine.PluginBeanException) GenericSignatureFormatError(java.lang.reflect.GenericSignatureFormatError) IPentahoResultSet(org.pentaho.commons.connection.IPentahoResultSet) IContentItem(org.pentaho.platform.api.repository.IContentItem)

Example 4 with IPentahoStreamSource

use of org.pentaho.commons.connection.IPentahoStreamSource in project pentaho-platform by pentaho.

the class ActionSequenceParameterMgr method getDataSource.

public IPentahoStreamSource getDataSource(final IActionResource actionResource) throws FileNotFoundException {
    IPentahoStreamSource dataSrc = null;
    IActionSequenceResource resource = runtimeContext.getResourceDefintion(actionResource.getName());
    if (resource != null) {
        dataSrc = runtimeContext.getResourceDataSource(resource);
    }
    return dataSrc;
}
Also used : IPentahoStreamSource(org.pentaho.commons.connection.IPentahoStreamSource) IActionSequenceResource(org.pentaho.platform.api.engine.IActionSequenceResource)

Aggregations

IPentahoStreamSource (org.pentaho.commons.connection.IPentahoStreamSource)4 Date (java.util.Date)2 IContentItem (org.pentaho.platform.api.repository.IContentItem)2 OutputStream (java.io.OutputStream)1 GenericSignatureFormatError (java.lang.reflect.GenericSignatureFormatError)1 Method (java.lang.reflect.Method)1 BigDecimal (java.math.BigDecimal)1 Properties (java.util.Properties)1 DataHandler (javax.activation.DataHandler)1 DataSource (javax.activation.DataSource)1 AuthenticationFailedException (javax.mail.AuthenticationFailedException)1 Authenticator (javax.mail.Authenticator)1 Multipart (javax.mail.Multipart)1 SendFailedException (javax.mail.SendFailedException)1 Session (javax.mail.Session)1 InternetAddress (javax.mail.internet.InternetAddress)1 MimeBodyPart (javax.mail.internet.MimeBodyPart)1 MimeMessage (javax.mail.internet.MimeMessage)1 MimeMultipart (javax.mail.internet.MimeMultipart)1 EmailAction (org.pentaho.actionsequence.dom.actions.EmailAction)1