001    /*
002     * Jpkg - Java library and tools for operating system package creation.
003     *
004     * Copyright (c) 2007 Three Rings Design, Inc.
005     * All rights reserved.
006     *
007     * Redistribution and use in source and binary forms, with or without
008     * modification, are permitted provided that the following conditions
009     * are met:
010     * 1. Redistributions of source code must retain the above copyright
011     *    notice, this list of conditions and the following disclaimer.
012     * 2. Redistributions in binary form must reproduce the above copyright
013     *    notice, this list of conditions and the following disclaimer in the
014     *    documentation and/or other materials provided with the distribution.
015     * 3. Neither the name of the copyright owner nor the names of contributors
016     *    may be used to endorse or promote products derived from this software
017     *    without specific prior written permission.
018     *
019     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
020     * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
021     * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
022     * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
023     * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
024     * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
025     * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
026     * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
027     * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
028     * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
029     * POSSIBILITY OF SUCH DAMAGE.
030     */
031    package com.threerings.jpkg.debian;
032    
033    import javax.mail.Address;
034    import javax.mail.internet.AddressException;
035    import javax.mail.internet.InternetAddress;
036    
037    /**
038     * Holds and parses the Debian package maintainer.
039     * @see <a href="http://www.debian.org/doc/debian-policy/ch-controlfields.html#s-f-Maintainer">Debian Policy Manual</a>
040     */
041    public class PackageMaintainer
042        implements ControlFileData
043    {
044        /**
045         * Construct a new PackageMaintainer. The address will be parsed and an exception thrown
046         * if it is not a valid RFC822 address. The maintainer's name will also be verified to not
047         * contain a period.
048         * @throws ControlDataInvalidException
049         */
050        public PackageMaintainer (String name, String address)
051            throws ControlDataInvalidException
052        {
053            _name = validateName(name);
054            _address = validateEmail(address);
055        }
056    
057        // from ControlFileData
058        public String getField ()
059        {
060            return "Maintainer";
061        }
062    
063        // from ControlFileData
064        public String getFieldValue ()
065        {
066            return _name + " <" + _address.toString() + ">";
067        }
068    
069        /**
070         * Validates the maintainer's name. If it contains a period, throw an exception.
071         * From: <a href="http://www.debian.org/doc/debian-policy/ch-controlfields.html#s-f-Maintainer">Debian Policy Manual</a>
072         * "If the maintainer's name contains a full stop then the whole field will not work directly
073         * as an email address due to a misfeature in the syntax specified in RFC822; a program using
074         * this field as an address must check for this and correct the problem if necessary
075         * (for example by putting the name in round brackets and moving it to the end, and bringing
076         * the email address forward)."
077         */
078        private String validateName (String name)
079            throws ControlDataInvalidException
080        {
081            if (name.contains(".")) {
082                throw new ControlDataInvalidException(
083                    "Maintainer name may not contain a period. name=[" + name + "].");
084            }
085    
086            return name;
087        }
088    
089        /**
090         * Validate the email address is RFC822 complaint.
091         * @return the String email converted into an Address.
092         */
093        private Address validateEmail (String address)
094            throws ControlDataInvalidException
095        {
096            try {
097                // true == strict parsing
098                return new InternetAddress(address, true);
099    
100            } catch (final AddressException ae) {
101                throw new ControlDataInvalidException(
102                    "Email is not a valid RFC822 address. email=[" + address + "]", ae);
103            }
104        }
105    
106        /** The maintainer's name. */
107        private final String _name;
108    
109        /** The maintainer's RFC822 formatted email address. */
110        private final Address _address;
111    }