01: /*
02: * Copyright 2002-2005 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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.mail.javamail;
18:
19: import java.beans.PropertyEditorSupport;
20:
21: import javax.mail.internet.AddressException;
22: import javax.mail.internet.InternetAddress;
23:
24: import org.springframework.util.StringUtils;
25:
26: /**
27: * Editor for <code>java.mail.internet.InternetAddress</code>,
28: * to directly populate an InternetAddress property.
29: *
30: * <p>Expects the same syntax as InternetAddress's constructor with
31: * a String argument. Converts empty Strings into null values.
32: *
33: * @author Juergen Hoeller
34: * @since 1.2.3
35: * @see javax.mail.internet.InternetAddress
36: */
37: public class InternetAddressEditor extends PropertyEditorSupport {
38:
39: public void setAsText(String text) throws IllegalArgumentException {
40: if (StringUtils.hasText(text)) {
41: try {
42: setValue(new InternetAddress(text));
43: } catch (AddressException ex) {
44: throw new IllegalArgumentException(
45: "Could not parse mail address: "
46: + ex.getMessage());
47: }
48: } else {
49: setValue(null);
50: }
51: }
52:
53: public String getAsText() {
54: InternetAddress value = (InternetAddress) getValue();
55: return (value != null ? value.toUnicodeString() : "");
56: }
57:
58: }
|