View Javadoc

1   /*
2    * Copyright 2005-2010 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.support;
18  
19  import java.net.URI;
20  import java.net.URISyntaxException;
21  import java.util.regex.Matcher;
22  import java.util.regex.Pattern;
23  import javax.mail.Folder;
24  import javax.mail.MessagingException;
25  import javax.mail.Service;
26  import javax.mail.Store;
27  import javax.mail.Transport;
28  import javax.mail.URLName;
29  import javax.mail.internet.AddressException;
30  import javax.mail.internet.InternetAddress;
31  
32  import org.springframework.util.StringUtils;
33  import org.springframework.ws.transport.mail.MailTransportConstants;
34  
35  import org.apache.commons.logging.Log;
36  import org.apache.commons.logging.LogFactory;
37  
38  /**
39   * Collection of utility methods to work with Mail transports.
40   *
41   * @author Arjen Poutsma
42   * @since 1.5.0
43   */
44  public abstract class MailTransportUtils {
45  
46      private static final Pattern TO_PATTERN = Pattern.compile("^([^\\?]+)");
47  
48      private static final Pattern SUBJECT_PATTERN = Pattern.compile("subject=([^\\&]+)");
49  
50      private static final Log logger = LogFactory.getLog(MailTransportUtils.class);
51  
52      private MailTransportUtils() {
53      }
54  
55      public static InternetAddress getTo(URI uri) {
56          Matcher matcher = TO_PATTERN.matcher(uri.getSchemeSpecificPart());
57          if (matcher.find()) {
58              for (int i = 1; i <= matcher.groupCount(); i++) {
59                  String group = matcher.group(i);
60                  if (group != null) {
61                      try {
62                          return new InternetAddress(group);
63                      }
64                      catch (AddressException e) {
65                          // try next group
66                      }
67                  }
68              }
69          }
70          return null;
71      }
72  
73      public static String getSubject(URI uri) {
74          Matcher matcher = SUBJECT_PATTERN.matcher(uri.getSchemeSpecificPart());
75          if (matcher.find()) {
76              return matcher.group(1);
77          }
78          return null;
79      }
80  
81      /**
82       * Close the given JavaMail Service and ignore any thrown exception. This is useful for typical <code>finally</code>
83       * blocks in manual JavaMail code.
84       *
85       * @param service the JavaMail Service to close (may be <code>null</code>)
86       * @see Transport
87       * @see Store
88       */
89      public static void closeService(Service service) {
90          if (service != null) {
91              try {
92                  service.close();
93              }
94              catch (MessagingException ex) {
95                  logger.debug("Could not close JavaMail Service", ex);
96              }
97          }
98      }
99  
100     /**
101      * Close the given JavaMail Folder and ignore any thrown exception. This is useful for typical <code>finally</code>
102      * blocks in manual JavaMail code.
103      *
104      * @param folder the JavaMail Folder to close (may be <code>null</code>)
105      */
106 
107     public static void closeFolder(Folder folder) {
108         closeFolder(folder, false);
109     }
110 
111     /**
112      * Close the given JavaMail Folder and ignore any thrown exception. This is useful for typical <code>finally</code>
113      * blocks in manual JavaMail code.
114      *
115      * @param folder  the JavaMail Folder to close (may be <code>null</code>)
116      * @param expunge whether all deleted messages should be expunged from the folder
117      */
118     public static void closeFolder(Folder folder, boolean expunge) {
119         if (folder != null && folder.isOpen()) {
120             try {
121                 folder.close(expunge);
122             }
123             catch (MessagingException ex) {
124                 logger.debug("Could not close JavaMail Folder", ex);
125             }
126         }
127     }
128 
129     /** Returns a string representation of the given [email protected] URLName}, where the password has been protected. */
130     public static String toPasswordProtectedString(URLName name) {
131         String protocol = name.getProtocol();
132         String username = name.getUsername();
133         String password = name.getPassword();
134         String host = name.getHost();
135         int port = name.getPort();
136         String file = name.getFile();
137         String ref = name.getRef();
138         StringBuilder tempURL = new StringBuilder();
139         if (protocol != null) {
140             tempURL.append(protocol).append(':');
141         }
142 
143         if (StringUtils.hasLength(username) || StringUtils.hasLength(host)) {
144             tempURL.append("//");
145             if (StringUtils.hasLength(username)) {
146                 tempURL.append(username);
147                 if (StringUtils.hasLength(password)) {
148                     tempURL.append(":*****");
149                 }
150                 tempURL.append("@");
151             }
152             if (StringUtils.hasLength(host)) {
153                 tempURL.append(host);
154             }
155             if (port != -1) {
156                 tempURL.append(':').append(Integer.toString(port));
157             }
158             if (StringUtils.hasLength(file)) {
159                 tempURL.append('/');
160             }
161         }
162         if (StringUtils.hasLength(file)) {
163             tempURL.append(file);
164         }
165         if (StringUtils.hasLength(ref)) {
166             tempURL.append('#').append(ref);
167         }
168         return tempURL.toString();
169     }
170 
171     /**
172      * Converts the given internet address into a <code>mailto</code> URI.
173      *
174      * @param to      the To: address
175      * @param subject the subject, may be <code>null</code>
176      * @return a mailto URI
177      */
178     public static URI toUri(InternetAddress to, String subject) throws URISyntaxException {
179         if (StringUtils.hasLength(subject)) {
180             return new URI(MailTransportConstants.MAIL_URI_SCHEME, to.getAddress() + "?subject=" + subject, null);
181         }
182         else {
183             return new URI(MailTransportConstants.MAIL_URI_SCHEME, to.getAddress(), null);
184         }
185     }
186 
187 
188 }