----- Original Message ----- 
From: "Christian Haul" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Monday, March 15, 2004 8:39 PM
Subject: Re: sendmail:attachment dynamic parameter?


> julien bloit wrote:
> > Hi,
> >
> > I use cocoon 2.1.3 and having a little trouble to send mail with dynamic
> > content, using the sendmail logicsheet.
> > Here's an xsp snippet that works :
> >
> > <mail:send-mail>
> >        <mail:charset>ISO-8859-1</mail:charset>
> >        <mail:smtphost><xsp:expr>smtphost</xsp:expr></mail:smtphost>
> >        <mail:from>[EMAIL PROTECTED]</mail:from>
> >        <mail:to><xsp:expr>email</xsp:expr></mail:to>
> >        <mail:subject>shop : your command</mail:subject>
> >        <mail:body><xsp:expr>mailBody</xsp:expr></mail:body>
> >        <mail:attachment mime-type="text/xml" name="commande.xml">
> >             <mail:param name="url">cocoon:/recap_cmd/24</mail:param>
> >        </mail:attachment>
> >       </mail:send-mail>
> >
> > but I need the attachment 'url' parameter to be dynamic , i.e. like this
:
> >
> > <mail:param
> > name="url">cocoon:/recap_cmd/<xsp:expr><util:get-sitemap-parameter
> > name="cmd_id"/></xsp:expr></mail:param>
> >
> > which doesn't work (although pulling in the sitemap parameter works
> > elsewhere in the xsp).
> >
> > Has someone got any clue on how I could fix this?
>
> AFAIK this should work. Could you check the generated source or even
> better, check with an XSL debugger (or just apply the sendmail.xsl
> manually)?
>
> Chris.
>

Chris, I've solved the probleme using a work-around  ( cf
http://marc.theaimsgroup.com/?l=xml-cocoon-users&m=107936193009378&w=2) but,
finding out why what should work doesn't work might help :-).

So here's the complete xsp page , and the generated source follows up.
Thanks for your help.
(I've put 2 differents attachments for comparison) :

XSP :
<?xml version="1.0" encoding="UTF-8"?>
<xsp:page language="java" xmlns:xsp="http://apache.org/xsp";
xmlns:xsp-request="http://apache.org/xsp/request/2.0";
xmlns:esql="http://apache.org/cocoon/SQL/v2";
xmlns:mail="http://apache.org/cocoon/sendmail/1.0";
xmlns:util="http://apache.org/xsp/util/2.0";>
 <dummy>
  <xsp:logic>
   String smtphost = <util:get-sitemap-parameter name="smtp"/>;
   String mailBody = <util:get-sitemap-parameter name="body"/>;

  </xsp:logic>
  <!-- recuperer l'email de l'utilisateur passant la commande -->
  <esql:connection>
   <esql:pool>dvdadmin</esql:pool>
   <esql:execute-query>
    <esql:query>select email from users, commandes where commande_id =
<util:get-sitemap-parameter name="cmd_id"/> and commandes.user_id =
users.user_id;</esql:query>
    <esql:results>
     <esql:row-results>
      <xsp:logic>String email = <esql:get-string column="email"/>;
     String attach_url_xml = "cocoon:/recap_cmd/";
     String attach_url_tab = "cocoon:/cmd_format_tab/";
     attach_url_xml = attach_url_xml.concat(<util:get-sitemap-parameter
name="cmd_id"/>);
     attach_url_tab = attach_url_tab.concat(<util:get-sitemap-parameter
name="cmd_id"/>);
     </xsp:logic>
      <mail:send-mail>
          <mail:charset>ISO-8859-1</mail:charset>
          <mail:smtphost><xsp:expr>smtphost</xsp:expr></mail:smtphost>
          <mail:from>[EMAIL PROTECTED]</mail:from>
          <mail:to><xsp:expr>email</xsp:expr></mail:to>
          <mail:subject>DVD a la carte : Votre commande</mail:subject>
          <mail:body><xsp:expr>mailBody</xsp:expr></mail:body>
          <mail:attachment mime-type="text/xml" name="commande.xml">
              <mail:param
name="url"><xsp:expr>attach_url_xml</xsp:expr></mail:param>
          </mail:attachment>
          <mail:attachment mime-type="text/tab-separated-value"
name="commande.tab">
              <mail:param
name="url">cocoon:/recap_cmd/<xsp:expr><util:get-sitemap-parameter
name="cmd_id"/></xsp:expr></mail:param>
           </mail:attachment>
    </mail:send-mail>
      <cmd_id>
       <xsp:expr>
        <util:get-sitemap-parameter name="cmd_id"/>
       </xsp:expr>
      </cmd_id>
     </esql:row-results>
    </esql:results>
   </esql:execute-query>
  </esql:connection>
 </dummy>
</xsp:page>




GENERATED SOURCE :


    package org.apache.cocoon.www.dvdcarte.xsp;

    import java.io.File;
    import java.io.IOException;
    import java.io.StringReader;
    //import java.net.*;
    import java.util.Date;
    import java.util.List;
    import java.util.Stack;

    //import org.w3c.dom.*;
    import org.xml.sax.InputSource;
    import org.xml.sax.SAXException;
    import org.xml.sax.helpers.AttributesImpl;

    //import org.apache.avalon.framework.*;
    import org.apache.avalon.framework.component.Component;
    import org.apache.avalon.framework.component.ComponentException;
    import org.apache.avalon.framework.component.ComponentManager;
    import org.apache.avalon.framework.component.ComponentSelector;
    import org.apache.avalon.framework.context.Context;
    //import org.apache.avalon.framework.util.*;

    import org.apache.cocoon.Constants;
    import org.apache.cocoon.ProcessingException;
    import org.apache.cocoon.generation.Generator;
    //import org.apache.cocoon.util.*;

    import org.apache.cocoon.components.language.markup.xsp.XSPGenerator;
    import org.apache.cocoon.components.language.markup.xsp.XSPObjectHelper;
    import
org.apache.cocoon.components.language.markup.xsp.XSPRequestHelper;
    import
org.apache.cocoon.components.language.markup.xsp.XSPResponseHelper;
    import
org.apache.cocoon.components.language.markup.xsp.XSPSessionHelper;

    /* User Imports */

      import java.net.URL;

      import java.util.Date;

      import java.io.FileReader;

      import java.text.SimpleDateFormat;

      import org.apache.cocoon.components.language.markup.xsp.XSPUtil;

      import java.sql.DriverManager;

      import java.sql.Connection;

      import java.sql.Statement;

      import java.sql.PreparedStatement;

      import java.sql.CallableStatement;

      import java.sql.ResultSet;

      import java.sql.ResultSetMetaData;

      import java.sql.Struct;

      import java.sql.SQLException;

      import java.sql.Clob;

      import java.sql.Blob;

      import java.text.SimpleDateFormat;

      import java.text.DecimalFormat;

      import java.io.StringWriter;

      import java.io.PrintWriter;

      import java.io.BufferedInputStream;

      import java.io.InputStream;

      import java.util.Set;

      import java.util.List;

      import java.util.Iterator;

      import java.util.ListIterator;

      import java.math.BigDecimal;

      import java.sql.Struct;

      import java.sql.Types;

      import org.apache.cocoon.components.language.markup.xsp.EsqlHelper;

      import
org.apache.cocoon.components.language.markup.xsp.AbstractEsqlQuery;

      import
org.apache.cocoon.components.language.markup.xsp.AbstractEsqlConnection;

      import
org.apache.cocoon.components.language.markup.xsp.Cocoon2EsqlConnection;

      import org.apache.cocoon.components.language.markup.xsp.XSPUtil;

      import org.apache.avalon.excalibur.datasource.DataSourceComponent;


    /**
     * Generated by XSP. Edit at your own risk, :-)
     */
    public class sendCmdMail2_xsp extends XSPGenerator {

        // Files this XSP depends on
        private static File[] _dependentFiles = new File[] {

            };

        // Initialize attributes used by modifiedSince() (see
AbstractServerPage)
        {
            this.dateCreated = 1079429418575L;
            this.dependencies = _dependentFiles;
        }

        /* Built-in parameters available for use */
        // context    - org.apache.cocoon.environment.Context
        // request    - org.apache.cocoon.environment.Request
        // response   - org.apache.cocoon.environment.Response
        // parameters - parameters defined in the sitemap
        // objectModel- java.util.Map
        // resolver   - org.apache.cocoon.environment.SourceResolver

        /* User Class Declarations */
        private Stack _esql_connections = new Stack();
    private Cocoon2EsqlConnection _esql_connection = null;
    private Stack _esql_queries = new Stack();
    private AbstractEsqlQuery _esql_query = null;
    private SQLException _esql_exception = null;
    private StringWriter _esql_exception_writer = null;

            private static ComponentSelector _esql_selector = null;

            protected ComponentSelector _esql_get_selector() throws
org.apache.avalon.framework.component.ComponentException {
              if (_esql_selector == null) {
                try {
                  _esql_selector = (ComponentSelector)
manager.lookup(DataSourceComponent.ROLE + "Selector");
                } catch (ComponentException cme) {
                  getLogger().error("Could not look up the datasource
component", cme);
                }
              }
              return _esql_selector;
            }


            protected void _esql_printObject ( Object obj, AttributesImpl
xspAttr) throws SAXException
            {
               if ( obj instanceof List) {
           ListIterator j=((List)obj).listIterator();


    this.contentHandler.startElement(
      "",
      "sql-list",
      "sql-list",
      xspAttr
    );

    xspAttr.clear();


                       while (j.hasNext()){


    xspAttr.addAttribute(
      "",
      "pos",
      "pos",
      "CDATA",
      String.valueOf(j.nextIndex())
    );


    this.contentHandler.startElement(
      "",
      "sql-list-item",
      "sql-list-item",
      xspAttr
    );

    xspAttr.clear();

    this._esql_printObject(j.next(),xspAttr);

    this.contentHandler.endElement(
      "",
      "sql-list-item",
      "sql-list-item");



                       };


    this.contentHandler.endElement(
      "",
      "sql-list",
      "sql-list");



               } else if ( obj instanceof Set ) {
            Iterator j=((Set)obj).iterator();


    this.contentHandler.startElement(
      "",
      "sql-set",
      "sql-set",
      xspAttr
    );

    xspAttr.clear();


                        while (j.hasNext()){


    this.contentHandler.startElement(
      "",
      "sql-set-item",
      "sql-set-item",
      xspAttr
    );

    xspAttr.clear();

    this._esql_printObject(j.next(),xspAttr);

    this.contentHandler.endElement(
      "",
      "sql-set-item",
      "sql-set-item");



                        };


    this.contentHandler.endElement(
      "",
      "sql-set",
      "sql-set");



           } else {


        XSPObjectHelper.xspExpr(contentHandler, obj);
      ;
           }
        }


        /**
         * Generate XML data.
         */
        public void generate() throws SAXException, IOException,
ProcessingException {



            this.contentHandler.startDocument();
            AttributesImpl xspAttr = new AttributesImpl();



          this.contentHandler.startPrefixMapping(
            "xml",
            "http://www.w3.org/XML/1998/namespace";
          );

          this.contentHandler.startPrefixMapping(
            "xsp",
            "http://apache.org/xsp";
          );

          this.contentHandler.startPrefixMapping(
            "util",
            "http://apache.org/xsp/util/2.0";
          );

        this.contentHandler.startPrefixMapping(
          "xspdoc",
          "http://apache.org/cocoon/XSPDoc/v1";
        );

        this.contentHandler.startPrefixMapping(
          "esql",
          "http://apache.org/cocoon/SQL/v2";
        );

        this.contentHandler.startPrefixMapping(
          "xsp-request",
          "http://apache.org/xsp/request/2.0";
        );

        this.contentHandler.startPrefixMapping(
          "mail",
          "http://apache.org/cocoon/sendmail/1.0";
        );


    this.contentHandler.startElement(
      "",
      "dummy",
      "dummy",
      xspAttr
    );
    xspAttr.clear();


        this.characters("\n\t\t");

   String smtphost =

        ((parameters.getParameter("smtp", null)))
      ;
   String mailBody =

        ((parameters.getParameter("body", null)))
      ;


        this.characters("\n\t\t");

        this.characters("\n\t\t");

    if (_esql_connection != null) {
      _esql_connections.push(_esql_connection);
    }
    try {

          try {
            _esql_connection = new Cocoon2EsqlConnection(
(DataSourceComponent) _esql_get_selector().select(String.valueOf(
      ""


                + "dvdadmin"
              )) );
            setupLogger(_esql_connection);


          } catch (Exception _esql_exception_N1001C) {
            getLogger().error("Could not get the
datasource",_esql_exception_N1001C);
            throw new RuntimeException("Could not get the datasource
"+_esql_exception_N1001C);
          }

      try {
        if ("false".equalsIgnoreCase(String.valueOf(""))) {
          if (_esql_connection.getAutoCommit()) {
            _esql_connection.setAutoCommit(false);
          }
        } else {
          if (!_esql_connection.getAutoCommit()) {
            _esql_connection.setAutoCommit(true);
          }
        }
      } catch (Exception _esql_exception_N1001C) {
        // do NOT: throw new RuntimeException("Error setting connection
autocommit");
      }

        this.characters("\n\t\t\t");

        this.characters("\n\t\t\t");

    if (_esql_query != null) {
      _esql_queries.push(_esql_query);
    }

    _esql_query = _esql_connection.createQuery(
             String.valueOf(""),
             String.valueOf(
      ""


                + "select email from users, commandes where commande_id = "


                +

        ((parameters.getParameter("cmd_id", null)))


                + " and commandes.user_id =  users.user_id;"
              )
      );



    try {

          _esql_query.prepareStatement();
          _esql_query.execute();

      if (getLogger().isDebugEnabled()) {
        getLogger().debug("esql query: " + _esql_query.getQueryString());
      }


  do {
     if (_esql_query.hasResultSet()) {
        _esql_query.getResultRows();
        if (_esql_query.nextRow()) {
           switch (_esql_query.getQueryResultsCount()) {

             case 1:
             default:
        this.characters("\n\t\t\t\t\t");

    do {

        this.characters("\n\t\t\t\t\t\t");
      String email =

        (
      EsqlHelper.getAscii(_esql_query.getResultSet(),"email","")
    )
      ;
     String attach_url_xml = "cocoon:/recap_cmd/";
     String attach_url_tab = "cocoon:/cmd_format_tab/";
     attach_url_xml = attach_url_xml.concat(

        ((parameters.getParameter("cmd_id", null)))
      );
     attach_url_tab = attach_url_tab.concat(

        ((parameters.getParameter("cmd_id", null)))
      );

        this.characters("\n\t\t\t\t\t\t");

       { // sendmail
         org.apache.cocoon.mail.MailMessageSender _sendmail_mms =
           new org.apache.cocoon.mail.MailMessageSender(String.valueOf(
          ""


                    +

        (smtphost)
      ));

         _sendmail_mms.setTo(String.valueOf(
          ""


                    +

        (email)
      ));
         _sendmail_mms.setSubject(String.valueOf(
          ""


                    + "DVD a la carte : Votre commande"
                  ));
         _sendmail_mms.setFrom(String.valueOf(
          ""


                    + "[EMAIL PROTECTED]"
                  ));

         _sendmail_mms.setCharset(String.valueOf(
          ""


                    + "ISO-8859-1"
                  ));
         _sendmail_mms.setBody(String.valueOf(
          ""


                    +

        (mailBody)
      ));


        _sendmail_mms.addAttachmentURL(


        (attach_url_xml)

          ,"text/xml"
          ,"commande.xml"
        );

        _sendmail_mms.addAttachmentURL(
        "cocoon:/recap_cmd/"
          ,"text/tab-separated-value"
          ,"commande.tab"
        );


         if(_sendmail_mms.sendIt(resolver)){

         } else {

              if (_sendmail_mms.getException() instanceof
                  javax.mail.internet.AddressException) {

    xspAttr.addAttribute(
      "",
      "type",
      "type",
      "CDATA",
      "user"
    );


    this.contentHandler.startElement(
      "",
      "error",
      "error",
      xspAttr
    );
    xspAttr.clear();


        this.characters("One of the given email address(es) is invalid.");


    this.contentHandler.endElement(
      "",
      "error",
      "error"
    );


              } else if (_sendmail_mms.getException() instanceof
                  javax.mail.MessagingException) {

    xspAttr.addAttribute(
      "",
      "type",
      "type",
      "CDATA",
      "server"
    );


    this.contentHandler.startElement(
      "",
      "error",
      "error",
      xspAttr
    );
    xspAttr.clear();


        this.characters("An error occured while sending email.");


    this.contentHandler.endElement(
      "",
      "error",
      "error"
    );


              }

         }
       }// sendmail

        this.characters("\n\t\t\t\t\t\t");


    this.contentHandler.startElement(
      "",
      "cmd_id",
      "cmd_id",
      xspAttr
    );
    xspAttr.clear();


        this.characters("\n\t\t\t\t\t\t\t");


        XSPObjectHelper.xspExpr(contentHandler,
        (parameters.getParameter("cmd_id", null))
       );

        this.characters("\n\t\t\t\t\t\t");


    this.contentHandler.endElement(
      "",
      "cmd_id",
      "cmd_id"
    );


        this.characters("\n\t\t\t\t\t");

  //checking out early?
    if (_esql_query.getMaxRows() != -1 && _esql_query.getCurrentRow() -
_esql_query.getSkipRows() == _esql_query.getMaxRows()) {
      _esql_query.setKeepGoing( false );
    } else { //if not, advance normally
      _esql_query.setKeepGoing( _esql_query.nextRow() );
    }

    } while ( _esql_query.keepGoing() );

    if (_esql_query.getSkipRows() > 0 ) {

    }

    if (_esql_query.nextRow()) {

    }

        this.characters("\n\t\t\t\t");

                 break;

           }
        } else {
           switch (_esql_query.getUpdateResultsCount()) {

           }
        }
        _esql_query.getResultSet().close();
     } else {
        if (_esql_query.getUpdateCount() > 0) {
           switch (_esql_query.getUpdateResultsCount()) {

           }
        } else {
           switch (_esql_query.getUpdateResultsCount()) {

           }
        }
     }
   } while(_esql_connection.getMultipleResults() &&
_esql_query.getMoreResults());


      _esql_query.getPreparedStatement().close();

    } catch (SQLException _esql_exception_N10024) {

          try {
            if (!_esql_connection.getAutoCommit()) {
              _esql_connection.rollback();
            }
          } catch (Exception _esql_exception_N10024_2) {}
          throw new RuntimeException("Error executing statement: " +
_esql_query.getQueryString() + ": "+_esql_exception_N10024);

    } finally {
        _esql_query.cleanUp();
    }
    if (_esql_queries.empty()) {
      _esql_query = null;
    } else {
      _esql_query = (AbstractEsqlQuery)_esql_queries.pop();
    }

        this.characters("\n\t\t");

    }
    catch (SQLException _esql_exception_N1001C) {
      getLogger().error("",_esql_exception_N1001C);
    }
    finally {
      try {
        if(!_esql_connection.getAutoCommit()) {
          _esql_connection.commit();
        }
        _esql_connection.close();
        if (_esql_connections.empty()) {
          _esql_connection = null;
        } else {
          _esql_connection = (Cocoon2EsqlConnection)_esql_connections.pop();
        }
      } catch (Exception _esql_exception_N1001C) {}
    }

        this.characters("\n\t");


    this.contentHandler.endElement(
      "",
      "dummy",
      "dummy"
    );


      this.contentHandler.endPrefixMapping(
        "xspdoc"
      );

      this.contentHandler.endPrefixMapping(
        "esql"
      );

      this.contentHandler.endPrefixMapping(
        "xsp-request"
      );

      this.contentHandler.endPrefixMapping(
        "mail"
      );

      this.contentHandler.endPrefixMapping(
        "xml"
      );

      this.contentHandler.endPrefixMapping(
        "xsp"
      );

      this.contentHandler.endPrefixMapping(
        "util"
      );


            this.contentHandler.endDocument();



        }
    }








> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to