You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by un...@apache.org on 2005/06/17 13:46:01 UTC

svn commit: r191130 - /cocoon/blocks/mail/trunk/java/org/apache/cocoon/mail/transformation/SendMailTransformer.java

Author: unico
Date: Fri Jun 17 04:45:59 2005
New Revision: 191130

URL: http://svn.apache.org/viewcvs?rev=191130&view=rev
Log:
Make smtp port configurable

Modified:
    cocoon/blocks/mail/trunk/java/org/apache/cocoon/mail/transformation/SendMailTransformer.java

Modified: cocoon/blocks/mail/trunk/java/org/apache/cocoon/mail/transformation/SendMailTransformer.java
URL: http://svn.apache.org/viewcvs/cocoon/blocks/mail/trunk/java/org/apache/cocoon/mail/transformation/SendMailTransformer.java?rev=191130&r1=191129&r2=191130&view=diff
==============================================================================
--- cocoon/blocks/mail/trunk/java/org/apache/cocoon/mail/transformation/SendMailTransformer.java (original)
+++ cocoon/blocks/mail/trunk/java/org/apache/cocoon/mail/transformation/SendMailTransformer.java Fri Jun 17 04:45:59 2005
@@ -188,6 +188,7 @@
     public static final String NAMESPACE                  = "http://apache.org/cocoon/transformation/sendmail";
     public static final String ELEMENT_SENDMAIL           = "sendmail";
     public static final String ELEMENT_SMTPHOST           = "smtphost";
+    public static final String ELEMENT_SMTPPORT           = "smtpport";
     public static final String ELEMENT_MAILFROM           = "from";
     public static final String ELEMENT_MAILTO             = "to";
     public static final String ELEMENT_REPLYTO            = "reply-to";
@@ -215,11 +216,13 @@
     protected static final int MODE_ATTACHMENT         = 6;
     protected static final int MODE_ATTACHMENT_CONTENT = 7;
     protected static final int MODE_REPLY_TO           = 8;
+    protected static final int MODE_SMTPPORT           = 8;
 
     /*
      * constants, related to parameter from request
      */
     public final static String PARAM_SMTPHOST    = "smtphost";
+    public final static String PARAM_SMTPPORT    = "smtpport";
     public final static String PARAM_FROM        = "from";
     public final static String PARAM_TO          = "to";
     public final static String PARAM_REPLY_TO    = "reply-to";
@@ -241,6 +244,7 @@
     protected String               bodyURI;
     protected String               bodyMimeType;
     protected String               mailHost;
+    protected int                  mailPort;
     protected String               fromAddress;
     protected AttachmentDescriptor attachmentDescriptor;
     protected int                  port;
@@ -249,6 +253,7 @@
     protected Message              smtpMessage;
 
     protected String defaultSmtpHost;
+    protected int defaultSmtpPort;
     protected String defaultFromAddress;
 
     protected boolean useExternalRequests = true;
@@ -258,7 +263,7 @@
      * create a new Transformer
      */
     public SendMailTransformer() {
-        super.defaultNamespaceURI = NAMESPACE;
+        this.defaultNamespaceURI = NAMESPACE;
     }
 
     /* (non-Javadoc)
@@ -268,6 +273,7 @@
     throws ConfigurationException {
         super.configure(configuration);
         this.defaultSmtpHost = configuration.getChild("smtphost").getValue("");
+        this.defaultSmtpPort = configuration.getChild("smtpport").getValueAsInteger(25);
         this.defaultFromAddress = configuration.getChild("from").getValue("");
         this.useExternalRequests = configuration.getChild("use-external-requests").getValueAsBoolean(this.useExternalRequests);
     }
@@ -277,17 +283,18 @@
      */
     public void setup(SourceResolver resolver, Map objectModel, String src,
                       Parameters par)
-    throws ProcessingException, SAXException, IOException {
+   throws ProcessingException, SAXException, IOException {
         super.setup(resolver, objectModel, src, par);
 
         this.mailHost    = par.getParameter(PARAM_SMTPHOST, this.defaultSmtpHost);
+        this.mailPort    = par.getParameterAsInteger(PARAM_SMTPPORT, this.defaultSmtpPort);
         this.fromAddress = par.getParameter(PARAM_FROM, this.defaultFromAddress);
         this.port        = this.request.getServerPort();
         this.contextPath = this.request.getContextPath();
         this.sendPartial = par.getParameterAsBoolean(PARAM_SENDPARTIAL, true);
 
         if (getLogger().isDebugEnabled()) {
-            getLogger().debug("Using host " + mailHost + ", from address " + fromAddress);
+            getLogger().debug("Using host " + mailHost + " on port " + mailPort +  ", from address " + fromAddress);
         }
 
         this.attachments = new ArrayList();
@@ -298,7 +305,7 @@
 
   	    this.subject = par.getParameter(PARAM_SUBJECT, null);
    	    this.body = par.getParameter(PARAM_BODY, null);
-    }
+	}
 
     /* (non-Javadoc)
      * @see org.apache.cocoon.transformation.AbstractSAXTransformer#startTransformingElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
@@ -314,6 +321,9 @@
         } else if (name.equals(ELEMENT_SMTPHOST)) {
             startTextRecording();
             this.mode = MODE_SMTPHOST;
+        } else if (name.equals(ELEMENT_SMTPPORT)) {
+            this.startTextRecording();
+            this.mode = MODE_SMTPPORT;
         } else if (name.equals(ELEMENT_MAILFROM)) {
             startTextRecording();
             this.mode = MODE_FROM;
@@ -359,7 +369,7 @@
      * @see org.apache.cocoon.transformation.AbstractSAXTransformer#endTransformingElement(java.lang.String, java.lang.String, java.lang.String)
      */
     public void endTransformingElement(String uri, String name, String raw)
-    throws SAXException, ProcessingException {
+                                throws SAXException, ProcessingException {
         if (name.equals(ELEMENT_SENDMAIL)) {
             if (getLogger().isInfoEnabled()) {
                 getLogger().info("Mail Subject: " + this.subject + "\n" +
@@ -370,6 +380,9 @@
         } else if (name.equals(ELEMENT_SMTPHOST) ) {
             this.mailHost = endTextRecording();
             this.mode     = MODE_NONE;
+        } else if (name.equals(ELEMENT_SMTPPORT) ) {
+            this.mailPort = Integer.parseInt(this.endTextRecording());
+            this.mode     = MODE_NONE;
         } else if (name.equals(ELEMENT_MAILFROM)) {
             this.fromAddress = endTextRecording();
             this.mode        = MODE_NONE;
@@ -429,6 +442,7 @@
         try {
             Properties props = new Properties();
             props.put("mail.smtp.host", this.mailHost);
+            props.put("mail.smtp.port", String.valueOf(this.mailPort));
 
             if (this.subject == null) {
 		        this.ignoreHooksCount++;
@@ -481,7 +495,7 @@
      * @throws Exception
      */
     private void sendMail(List newAddresses, Transport trans)
-    throws Exception {
+                   throws Exception {
         AddressHandler[] iA = new AddressHandler[newAddresses.size()];
 
         for (int i = 0; i < newAddresses.size(); i++) {
@@ -687,7 +701,8 @@
 	    this.body = null;
 	    this.bodyURI = null;
 	    this.mailHost = null;
-	    this.fromAddress = null;
+        this.mailPort = 0;
+        this.fromAddress = null;
 	    this.attachmentDescriptor = null;
 	    this.port = 0;
 	    this.contextPath = null;
@@ -699,7 +714,7 @@
             this.resolver.release(source);
         }
         this.usedSources.clear();
-	    super.recycle();
+ 	    super.recycle();
 	}
 
     static class AttachmentDescriptor {