1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
40
41
42
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
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
83
84
85
86
87
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
102
103
104
105
106
107 public static void closeFolder(Folder folder) {
108 closeFolder(folder, false);
109 }
110
111
112
113
114
115
116
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
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
173
174
175
176
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 }