001/*
002 * acme4j - Java ACME client
003 *
004 * Copyright (C) 2021 Richard "Shred" Körber
005 *   http://acme4j.shredzone.org
006 *
007 * Licensed under the Apache License, Version 2.0 (the "License");
008 * you may not use this file except in compliance with the License.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
013 */
014package org.shredzone.acme4j.smime;
015
016import jakarta.mail.internet.AddressException;
017import jakarta.mail.internet.InternetAddress;
018import org.shredzone.acme4j.Identifier;
019import org.shredzone.acme4j.exception.AcmeProtocolException;
020
021/**
022 * Represents an e-mail identifier.
023 *
024 * @since 2.12
025 */
026public class EmailIdentifier extends Identifier {
027    private static final long serialVersionUID = -1473014167038845395L;
028
029    /**
030     * Type constant for E-Mail identifiers.
031     *
032     * @see <a href="https://datatracker.ietf.org/doc/html/rfc8823">RFC 8823</a>
033     */
034    public static final String TYPE_EMAIL = "email";
035
036    /**
037     * Creates a new {@link EmailIdentifier}.
038     *
039     * @param value
040     *         e-mail address
041     */
042    private EmailIdentifier(String value) {
043        super(TYPE_EMAIL, value);
044    }
045
046    /**
047     * Creates a new email identifier for the given address.
048     *
049     * @param email
050     *         Email address. Must only be the address itself (without personal name).
051     * @return New {@link EmailIdentifier}
052     */
053    public static EmailIdentifier email(String email) {
054        return new EmailIdentifier(email);
055    }
056
057    /**
058     * Creates a new email identifier for the given address.
059     *
060     * @param email
061     *         Email address. Only the address itself is used. The personal name will be
062     *         ignored.
063     * @return New {@link EmailIdentifier}
064     */
065    public static EmailIdentifier email(InternetAddress email) {
066        return email(email.getAddress());
067    }
068
069    /**
070     * Returns the email address.
071     *
072     * @return {@link InternetAddress}
073     * @throws AcmeProtocolException
074     *             if this is not a valid email identifier.
075     */
076    public InternetAddress getEmailAddress() {
077        try {
078            return new InternetAddress(getValue());
079        } catch (AddressException ex) {
080            throw new AcmeProtocolException("bad email address", ex);
081        }
082    }
083
084}