You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by ha...@apache.org on 2003/11/08 18:42:37 UTC

cvs commit: cocoon-2.1/src/blocks/mail/java/org/apache/cocoon/acting Sendmail.java

haul        2003/11/08 09:42:36

  Modified:    src/blocks/mail/java/org/apache/cocoon/mail
                        MailMessageSender.java
               src/blocks/mail/java/org/apache/cocoon/acting Sendmail.java
  Log:
  Fixes 24388: [PATCH] Enhanced Sendmail action: Generate message body from a URI
  by andrzej@chaeron.com (Andrzej Taramina)
  
  Revision  Changes    Path
  1.6       +102 -17   cocoon-2.1/src/blocks/mail/java/org/apache/cocoon/mail/MailMessageSender.java
  
  Index: MailMessageSender.java
  ===================================================================
  RCS file: /home/cvs/cocoon-2.1/src/blocks/mail/java/org/apache/cocoon/mail/MailMessageSender.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- MailMessageSender.java	24 Sep 2003 22:34:53 -0000	1.5
  +++ MailMessageSender.java	8 Nov 2003 17:42:36 -0000	1.6
  @@ -97,6 +97,8 @@
       private String bcc;
       private String subject;
       private String charset;
  +    private String src;
  +    private String srcMimeType;
       private String body;
       private List attachmentList;
       private Exception exception = null;
  @@ -215,7 +217,8 @@
        * @throws AddressException when problems with email addresses are found
        * @throws MessagingException when message could not be send.
        */
  -    public void send(SourceResolver resolver) throws AddressException, MessagingException {
  +    public void send(SourceResolver resolver)
  +        throws AddressException, MessagingException {
           List sourcesList = new ArrayList();
   
           if (this.from == null) {
  @@ -224,7 +227,8 @@
               try {
                   this.message.setFrom(new InternetAddress(this.from));
               } catch (AddressException e) {
  -                throw new AddressException("invalid from address: " + this.from + ": " + e.getMessage());
  +                throw new AddressException(
  +                    "invalid from address: " + this.from + ": " + e.getMessage());
               }
           }
   
  @@ -232,25 +236,34 @@
               throw new AddressException("no to address");
           } else {
               try {
  -                this.message.setRecipients(RecipientType.TO, InternetAddress.parse(this.to));
  +                this.message.setRecipients(
  +                    RecipientType.TO,
  +                    InternetAddress.parse(this.to));
               } catch (AddressException e) {
  -                throw new AddressException("invalid to address: " + this.to + ": " + e.getMessage());
  +                throw new AddressException(
  +                    "invalid to address: " + this.to + ": " + e.getMessage());
               }
           }
   
           if (this.cc != null) {
               try {
  -                this.message.setRecipients(RecipientType.CC, InternetAddress.parse(this.cc));
  +                this.message.setRecipients(
  +                    RecipientType.CC,
  +                    InternetAddress.parse(this.cc));
               } catch (AddressException e) {
  -                throw new AddressException("invalid cc address: " + this.cc + ": " + e.getMessage());
  +                throw new AddressException(
  +                    "invalid cc address: " + this.cc + ": " + e.getMessage());
               }
           }
   
           if (this.bcc != null) {
               try {
  -                this.message.setRecipients(RecipientType.BCC, InternetAddress.parse(this.bcc));
  +                this.message.setRecipients(
  +                    RecipientType.BCC,
  +                    InternetAddress.parse(this.bcc));
               } catch (AddressException e) {
  -                throw new AddressException("invalid bcc address: " + this.bcc + ": " + e.getMessage());
  +                throw new AddressException(
  +                    "invalid bcc address: " + this.bcc + ": " + e.getMessage());
               }
           }
   
  @@ -264,7 +277,24 @@
           try {
   
               if (this.attachmentList.isEmpty()) {
  -                if (this.body != null) {
  +                if (this.src != null) {
  +                    DataSource ds = null;
  +
  +                    Source source = resolver.resolveURI(this.src);
  +                    sourcesList.add(source);
  +                    if (source.exists()) {
  +                        ds =
  +                            new SourceDataSource(
  +                                source,
  +                                (this.srcMimeType == null
  +                                    ? source.getMimeType()
  +                                    : this.srcMimeType),
  +                                this.src.substring(this.src.lastIndexOf('/') + 1));
  +                    }
  +
  +                    this.message.setDataHandler(new DataHandler(ds));
  +
  +                } else if (this.body != null) {
                       if (this.charset != null) {
                           this.message.setText(this.body, this.charset);
                       } else {
  @@ -274,7 +304,28 @@
               } else {
                   Multipart multipart = new MimeMultipart();
                   BodyPart bodypart = null;
  -                if (this.body != null) {
  +
  +                if (this.src != null) {
  +                    DataSource ds = null;
  +
  +                    Source source = resolver.resolveURI(this.src);
  +                    sourcesList.add(source);
  +                    if (source.exists()) {
  +                        ds =
  +                            new SourceDataSource(
  +                                source,
  +                                (this.srcMimeType == null
  +                                    ? source.getMimeType()
  +                                    : this.srcMimeType),
  +                                this.src.substring(this.src.lastIndexOf('/') + 1));
  +                    }
  +
  +                    bodypart.setDataHandler(new DataHandler(ds));
  +                    bodypart.setFileName(ds.getName());
  +
  +                    multipart.addBodyPart(bodypart);
  +
  +                } else if (this.body != null) {
                       bodypart = new MimeBodyPart();
                       bodypart.setText(this.body);
                       multipart.addBodyPart(bodypart);
  @@ -289,21 +340,24 @@
                           Source src = resolver.resolveURI(name);
                           sourcesList.add(src);
                           if (src.exists()) {
  -                            ds = new SourceDataSource(
  +                            ds =
  +                                new SourceDataSource(
                                       src,
  -                                    a.getType(src.getMimeType()), 
  +                                    a.getType(src.getMimeType()),
                                       a.getName(name.substring(name.lastIndexOf('/') + 1)));
                           }
                       } else {
                           if (a.getObject() instanceof Part) {
                               Part part = (Part) a.getObject();
  -                            ds = new FilePartDataSource(
  +                            ds =
  +                                new FilePartDataSource(
                                       part,
                                       a.getType(part.getMimeType()),
                                       a.getName(part.getUploadName()));
                           } else {
                               // TODO: other classes?
  -                            throw new AddressException("Not yet supported: " + a.getObject());
  +                            throw new AddressException(
  +                                "Not yet supported: " + a.getObject());
                           }
                       }
   
  @@ -318,10 +372,17 @@
           } catch (MessagingException me) {
               throw new MessagingException(me.getMessage());
           } catch (MalformedURLException e) {
  -            throw new AddressException("Malformed attachment URL: " + a.getObject() + " error " + e.getMessage());
  +            throw new AddressException(
  +                "Malformed attachment URL: "
  +                    + a.getObject()
  +                    + " error "
  +                    + e.getMessage());
           } catch (IOException e) {
               throw new AddressException(
  -                "IOException accessing attachment URL: " + a.getObject() + " error " + e.getMessage());
  +                "IOException accessing attachment URL: "
  +                    + a.getObject()
  +                    + " error "
  +                    + e.getMessage());
           } finally {
               if (sourcesList != null) {
                   for (Iterator j = sourcesList.iterator(); j.hasNext();) {
  @@ -424,11 +485,35 @@
       }
   
       /** Sets the body text of the email message.
  +     * If both a text body and a body read from a source are set,
  +     * only the latter will be used.
  +     *
        * @param body The body text of the email message
        */
       public void setBody(String body) {
           if (!("".equals(body) || "null".equals(body))) {
               this.body = body;
  +        }
  +    }
  +
  +    /** Sets the body source URL of the email message.
  +     * If both a text body and a body read from a source are set,
  +     * only the latter will be used.
  +     *
  +     * @param src The body source URL of the email message
  +     */
  +    public void setBodyFromSrc(String src) {
  +        if (!("".equals(src) || "null".equals(src))) {
  +            this.src = src;
  +        }
  +    }
  +
  +    /** Sets the optional body source Mime Type of the email message.
  +     * @param srcMimeTyoe The optional body source Mime Type of the email message
  +     */
  +    public void setBodyFromSrcMimeType(String srcMimeType) {
  +        if (!("".equals(srcMimeType) || "null".equals(srcMimeType))) {
  +            this.srcMimeType = srcMimeType;
           }
       }
   
  
  
  
  1.5       +48 -21    cocoon-2.1/src/blocks/mail/java/org/apache/cocoon/acting/Sendmail.java
  
  Index: Sendmail.java
  ===================================================================
  RCS file: /home/cvs/cocoon-2.1/src/blocks/mail/java/org/apache/cocoon/acting/Sendmail.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- Sendmail.java	22 Oct 2003 04:44:26 -0000	1.4
  +++ Sendmail.java	8 Nov 2003 17:42:36 -0000	1.5
  @@ -56,7 +56,6 @@
   import org.apache.avalon.framework.parameters.Parameters;
   import org.apache.avalon.framework.thread.ThreadSafe;
   
  -//import org.apache.cocoon.servlet.multipart.MultipartHttpServletRequest;
   import org.apache.cocoon.environment.ObjectModelHelper;
   import org.apache.cocoon.environment.Redirector;
   import org.apache.cocoon.environment.Request;
  @@ -83,8 +82,12 @@
    *     be multiple addresses separated with commas.</dd>
    *   <dt>subject</dt>
    *   <dd>the subject line of the email</dd>
  + *   <dt>src</dt>
  + *   <dd>A url specifying the source of the text body of the email</dd>
  + *   <dt>srcMimeType</dt>
  + *   <dd>The optional Mime Type of the  source of the text body of the email if you specified src</dd>
    *   <dt>body</dt>
  - *   <dd>the text body of the email</dd>
  + *   <dd>the text body of the email, if src is specified, body will be ignored</dd>
    * </dl>
    *
    * The following optionals parameters can be used:
  @@ -135,6 +138,7 @@
    * @author <a href="mailto:frank.ridderbusch@gmx.de">Frank Ridderbusch</a>
    * @author <a href="mailto:haul@apache.org">Christian Haul</a>
    * @author <a href="mailto:balld@apache.org">Donald Ball</a>
  + * @author <a href="mailto:andrzej@chaeron.com">Andrzej Taramina</a>
    * @since 2.1
    * @version CVS $Id$
    */
  @@ -154,11 +158,17 @@
           smtpHost = conf.getAttribute("smtphost", "127.0.0.1");
   
           if (this.getLogger().isDebugEnabled()) {
  -            getLogger().debug("SendmailAction: using " + smtpHost + " as the smtp server");
  +            getLogger().debug(
  +                "SendmailAction: using " + smtpHost + " as the smtp server");
           }
       }
   
  -    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters)
  +    public Map act(
  +        Redirector redirector,
  +        SourceResolver resolver,
  +        Map objectModel,
  +        String source,
  +        Parameters parameters)
           throws Exception {
           boolean success = false;
           Map status = null;
  @@ -174,7 +184,9 @@
                   smtpHost = parameters.getParameter("smtphost", null);
   
                   if (this.getLogger().isDebugEnabled()) {
  -                    getLogger().debug("SendmailAction: overriding default smtp server, using " + smtpHost);
  +                    getLogger().debug(
  +                        "SendmailAction: overriding default smtp server, using "
  +                            + smtpHost);
                   }
               }
   
  @@ -204,7 +216,13 @@
                   mms.setCharset(parameters.getParameter("charset", null));
               }
   
  -            if (parameters.isParameter("body")) {
  +            if (parameters.isParameter("src")) {
  +                mms.setBodyFromSrc(parameters.getParameter("src", null));
  +                if (parameters.isParameter("srcMimeType")) {
  +                    mms.setBodyFromSrcMimeType(
  +                        parameters.getParameter("srcMimeType", null));
  +                }
  +            } else if (parameters.isParameter("body")) {
                   mms.setBody(parameters.getParameter("body", null));
               }
   
  @@ -215,15 +233,16 @@
                       String srcName = tz.nextToken();
   
                       if (srcName.indexOf(":") == -1) {
  -                        //if (request instanceof MultipartHttpServletRequest) {
  -                            Object obj = request.get(srcName);
  -                            mms.addAttachment(obj);
  -                            if (this.getLogger().isDebugEnabled()) {
  -                                getLogger().debug("request-attachment: " + obj);
  -                            }
  -                        //}
  +                        Object obj = request.get(srcName);
  +                        mms.addAttachment(obj);
  +                        if (this.getLogger().isDebugEnabled()) {
  +                            getLogger().debug("request-attachment: " + obj);
  +                        }
                       } else {
  -                        mms.addAttachmentURL(srcName, null, srcName.substring(srcName.lastIndexOf('/') + 1));
  +                        mms.addAttachmentURL(
  +                            srcName,
  +                            null,
  +                            srcName.substring(srcName.lastIndexOf('/') + 1));
                           if (this.getLogger().isDebugEnabled()) {
                               getLogger().debug("sitemap-attachment: " + srcName);
                           }
  @@ -242,7 +261,8 @@
               status.put(Sendmail.STATUS, "success");
   
           } catch (AddressException ae) {
  -            this.getLogger().error("SendmailAction: AddressException: " + ae.getMessage());
  +            this.getLogger().error(
  +                "SendmailAction: AddressException: " + ae.getMessage());
   
               status = new HashMap(2);
               status.put(Sendmail.STATUS, "user-error");
  @@ -250,25 +270,32 @@
   
           } catch (MessagingException me) {
               this.getLogger().error(
  -                "SendmailAction: MessagingException: " + "An error occured while sending email.",
  +                "SendmailAction: MessagingException: "
  +                    + "An error occured while sending email.",
                   me);
   
               // me contains nested exceptions providing insight on the real
               // cause.
               status = new HashMap(2);
               status.put(Sendmail.STATUS, "server-error");
  -            status.put(Sendmail.MESSAGE, "An error occured while sending email: " + me.getMessage());
  +            status.put(
  +                Sendmail.MESSAGE,
  +                "An error occured while sending email: " + me.getMessage());
   
           } catch (Exception e) {
  -            this.getLogger().error("SendmailAction: An exception was thrown while sending email.", e);
  +            this.getLogger().error(
  +                "SendmailAction: An exception was thrown while sending email.",
  +                e);
   
               status = new HashMap(2);
               status.put(Sendmail.STATUS, "server-error");
               status.put(Sendmail.MESSAGE, "An exception was thrown while sending email.");
   
           } finally {
  -            ObjectModelHelper.getRequest(objectModel).setAttribute(Sendmail.REQUEST_ATTRIBUTE, status);
  +            ObjectModelHelper.getRequest(objectModel).setAttribute(
  +                Sendmail.REQUEST_ATTRIBUTE,
  +                status);
           }
  -		return (success ? status : null);
  +        return (success ? status : null);
       }
   }