View Javadoc

1   /*
2    * Copyright 2007 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.ws.transport.mail;
18  
19  import java.io.IOException;
20  import java.net.URI;
21  import java.util.Properties;
22  import javax.mail.Session;
23  import javax.mail.URLName;
24  import javax.mail.internet.AddressException;
25  import javax.mail.internet.InternetAddress;
26  import javax.mail.internet.MimeMessage;
27  
28  import org.springframework.beans.factory.InitializingBean;
29  import org.springframework.util.Assert;
30  import org.springframework.ws.WebServiceMessageFactory;
31  import org.springframework.ws.transport.WebServiceConnection;
32  import org.springframework.ws.transport.WebServiceMessageSender;
33  import org.springframework.ws.transport.mail.monitor.PollingMonitoringStrategy;
34  import org.springframework.ws.transport.mail.support.MailTransportUtils;
35  
36  /**
37   * {@link WebServiceMessageSender} implementation that uses Mail {@link MimeMessage}s. Requires a {@link
38   * #setTransportUri(String) transport} and {@link #setStoreUri(String) store} URI to be set.
39   * <p/>
40   * Calling {@link WebServiceConnection#receive(WebServiceMessageFactory)} on connections created by this message sender
41   * will result in a blocking call, for the amount of milliseconds specified by the {@link #setReceiveSleepTime(long)
42   * receiveSleepTime} property. This will give the server time to formulate a response message. By default, this propery
43   * is set to 1 minute. For a proper request-response conversation to work, this property value must not be smaller the
44   * {@link PollingMonitoringStrategy#setPollingInterval(long) pollingInterval} property of the server-side message
45   * receiver polling strategy.
46   * <p/>
47   * This message sender supports URI's of the following format: <blockquote> <tt><b>mailto:</b></tt><i>to</i>[<tt><b>?</b></tt><i>param-name</i><tt><b>=</b></tt><i>param-value</i>][<tt><b>&amp;</b></tt><i>param-name</i><tt><b>=</b></tt><i>param-value</i>]*
48   * </blockquote> where the characters <tt><b>:</b></tt>, <tt><b>?</b></tt>, and <tt><b>&amp;</b></tt> stand for
49   * themselves. The <i>to</i> represents a RFC 822 mailbox. Valid <i>param-name</i> include:
50   * <p/>
51   * <blockquote><table> <tr><th><i>param-name</i></th><th><i>Description</i></th></tr>
52   * <tr><td><tt>subject</tt></td><td>The subject of the request message.</td></tr> </table></blockquote>
53   * <p/>
54   * Some examples of email URIs are:
55   * <p/>
56   * <blockquote><tt>mailto:[email protected]</tt><br> <tt>mailto:[email protected]@?subject=SOAP%20Test</tt><br></blockquote>
57   *
58   * @author Arjen Poutsma
59   * @see <a href="http://www.ietf.org/rfc/rfc2368.txt">The mailto URL scheme</a>
60   * @since 1.5.0
61   */
62  public class MailMessageSender implements WebServiceMessageSender, InitializingBean {
63  
64      /**
65       * Default timeout for receive operations. Set to 1000 * 60 milliseconds (i.e. 1 minute).
66       */
67      public static final long DEFAULT_RECEIVE_TIMEOUT = 1000 * 60;
68  
69      private long receiveSleepTime = DEFAULT_RECEIVE_TIMEOUT;
70  
71      private Session session = Session.getInstance(new Properties(), null);
72  
73      private URLName storeUri;
74  
75      private URLName transportUri;
76  
77      private InternetAddress from;
78  
79      /**
80       * Sets the from address to use when sending request messages.
81       */
82      public void setFrom(String from) throws AddressException {
83          this.from = new InternetAddress(from);
84      }
85  
86      /**
87       * Set JavaMail properties for the {@link Session}.
88       * <p/>
89       * A new {@link Session} will be created with those properties. Use either this method or {@link #setSession}, but
90       * not both.
91       * <p/>
92       * Non-default properties in this instance will override given JavaMail properties.
93       */
94      public void setJavaMailProperties(Properties javaMailProperties) {
95          session = Session.getInstance(javaMailProperties, null);
96      }
97  
98      /**
99       * Set the sleep time to use for receive calls, <strong>in milliseconds</strong>. The default is 1000 * 60 ms, that
100      * is 1 minute.
101      */
102     public void setReceiveSleepTime(long receiveSleepTime) {
103         this.receiveSleepTime = receiveSleepTime;
104     }
105 
106     /**
107      * Set the JavaMail <code>Session</code>, possibly pulled from JNDI.
108      * <p/>
109      * Default is a new <code>Session</code> without defaults, that is completely configured via this instance's
110      * properties.
111      * <p/>
112      * If using a pre-configured <code>Session</code>, non-default properties in this instance will override the
113      * settings in the <code>Session</code>.
114      *
115      * @see #setJavaMailProperties
116      */
117     public void setSession(Session session) {
118         Assert.notNull(session, "Session must not be null");
119         this.session = session;
120     }
121 
122     /**
123      * Sets the JavaMail Store URI to be used for retrieving response messages. Typically takes the form of
124      * <code>[imap|pop3]://user:password@host:port/INBOX</code>. Setting this property is required.
125      * <p/>
126      * For example, <code>imap://john:[email protected]/INBOX</code>
127      *
128      * @see Session#getStore(URLName)
129      */
130     public void setStoreUri(String storeUri) {
131         this.storeUri = new URLName(storeUri);
132     }
133 
134     /**
135      * Sets the JavaMail Transport URI to be used for sending response messages. Typically takes the form of
136      * <code>smtp://user:password@host:port</code>. Setting this property is required.
137      * <p/>
138      * For example, <code>smtp://john:[email protected]</code>
139      *
140      * @see Session#getTransport(URLName)
141      */
142     public void setTransportUri(String transportUri) {
143         this.transportUri = new URLName(transportUri);
144     }
145 
146     public void afterPropertiesSet() throws Exception {
147         Assert.notNull(transportUri, "'transportUri' is required");
148         Assert.notNull(storeUri, "'storeUri' is required");
149     }
150 
151     public WebServiceConnection createConnection(URI uri) throws IOException {
152         InternetAddress to = MailTransportUtils.getTo(uri);
153         MailSenderConnection connection =
154                 new MailSenderConnection(session, transportUri, storeUri, to, receiveSleepTime);
155         if (from != null) {
156             connection.setFrom(from);
157         }
158         String subject = MailTransportUtils.getSubject(uri);
159         if (subject != null) {
160             connection.setSubject(subject);
161         }
162         return connection;
163     }
164 
165     public boolean supports(URI uri) {
166         return uri.getScheme().equals(MailTransportConstants.MAIL_URI_SCHEME);
167     }
168 }