001    /*
002     * Copyright 2010 the original author or authors.
003     * 
004     *  Licensed under the Apache License, Version 2.0 (the "License");
005     *  you may not use this file except in compliance with the License.
006     *  You may obtain a copy of the License at
007     *
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     *
010     *  Unless required by applicable law or agreed to in writing, software
011     *  distributed under the License is distributed on an "AS IS" BASIS,
012     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     *  See the License for the specific language governing permissions and
014     *  limitations under the License.
015     */
016    package com.hs.mail.smtp.message;
017    
018    import java.io.Serializable;
019    
020    import javax.mail.Address;
021    import javax.mail.internet.InternetAddress;
022    import javax.mail.internet.ParseException;
023    
024    import com.hs.mail.smtp.SmtpException;
025    
026    /**
027     * 
028     * @author Won Chul Doh
029     * @since Jun 1, 2010
030     *
031     */
032    public class MailAddress implements Serializable, Comparable {
033    
034            private static final long serialVersionUID = -3308891400855394336L;
035    
036            private String address;
037            private transient boolean strict;
038            private transient String user;
039            private transient String host;
040    
041            public MailAddress() {
042                    this.address = "";
043            }
044            
045            public MailAddress(Address address) {
046                    this.address = ((InternetAddress) address).getAddress();
047            }
048    
049            public MailAddress(String address, boolean strict) throws SmtpException {
050                    this.strict = strict;
051                    if (strict) {
052                            try {
053                                    this.address = parse(address);
054                            } catch (ParseException e) {
055                                    throw new SmtpException("501 5.1.7 " + e.getMessage());
056                            }
057                    } else {
058                            this.address = address;
059                    }
060            }
061            
062            public MailAddress(String address) throws SmtpException {
063                    this(address, true);
064            }
065            
066            public boolean isStrict() {
067                    return strict;
068            }
069    
070            public String getMailbox() {
071                    return address;
072            }
073            
074            protected void setMailbox(String address) {
075                    this.address = address;
076            }
077    
078            public String getUser() {
079                    if (user == null) {
080                            int index = address.lastIndexOf('@');
081                            user = (index != -1) ? address.substring(0, index) : address;
082                    }
083                    return user;
084            }
085    
086        public String getHost() {
087                    if (host == null) {
088                            int index = address.lastIndexOf('@');
089                            if (index != -1) {
090                                    host = address.substring(index + 1).toLowerCase();
091                                    if (host.charAt(0) == '[')
092                                            host = host.substring(1, host.length() - 1);
093                            }
094                    }
095                    return host;
096            }
097            
098            public InternetAddress toInternetAddress() {
099                    try {
100                            return new InternetAddress(address);
101                    } catch (javax.mail.internet.AddressException ae) {
102                            // impossible really
103                            return null;
104                    }
105            }
106            
107            /**
108             * Strip source routing, according to RFC-2821 it is an allowed approach to
109             * handle mails containing RFC-821 source-route information.
110             */
111            private static String stripSourceRoute(String address) {
112                    String s = address;
113                    int index = address.indexOf(':');
114                    if (index != -1) {
115                            s = address.substring(index + 1);
116                    }
117                    return s;
118            }
119    
120            private static String parse(String address) throws ParseException {
121                    String s = address;
122                    if (!s.startsWith("<") || !s.endsWith(">")) {
123                            throw new ParseException("Address did not start and end with < >");
124                    }
125                    // Strip < and >
126                    s = s.substring(1, s.length() - 1);
127                    // Test if mail address has source routing information (RFC-821) and
128                    // get rid of it!!
129                    s = stripSourceRoute(s);
130                    // When the strict flag is set true, InternetAddress checks that the
131                    // address is a valid "mailbox" per RFC822.
132                    new InternetAddress(s, true);
133                    return s;
134            }
135    
136            public int compareTo(Object o) {
137                    if (o instanceof MailAddress)
138                            return address.compareTo(((MailAddress) o).getMailbox());
139                    else if (o instanceof InternetAddress)
140                            return address.compareTo(((InternetAddress) o).getAddress());
141                    else if (o instanceof String)
142                            return address.compareTo((String) o);
143                    else
144                            return -1;
145            }
146    
147    }