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 java.io.Serial;
017
018import jakarta.mail.internet.AddressException;
019import jakarta.mail.internet.InternetAddress;
020import org.shredzone.acme4j.Identifier;
021import org.shredzone.acme4j.exception.AcmeProtocolException;
022
023/**
024 * Represents an e-mail identifier.
025 *
026 * @since 2.12
027 */
028public class EmailIdentifier extends Identifier {
029    @Serial
030    private static final long serialVersionUID = -1473014167038845395L;
031
032    /**
033     * Type constant for E-Mail identifiers.
034     *
035     * @see <a href="https://datatracker.ietf.org/doc/html/rfc8823">RFC 8823</a>
036     */
037    public static final String TYPE_EMAIL = "email";
038
039    /**
040     * Creates a new {@link EmailIdentifier}.
041     *
042     * @param value
043     *         e-mail address
044     */
045    private EmailIdentifier(String value) {
046        super(TYPE_EMAIL, value);
047    }
048
049    /**
050     * Creates a new email identifier for the given address.
051     *
052     * @param email
053     *         Email address. Must only be the address itself (without personal name).
054     * @return New {@link EmailIdentifier}
055     */
056    public static EmailIdentifier email(String email) {
057        return new EmailIdentifier(email);
058    }
059
060    /**
061     * Creates a new email identifier for the given address.
062     *
063     * @param email
064     *         Email address. Only the address itself is used. The personal name will be
065     *         ignored.
066     * @return New {@link EmailIdentifier}
067     */
068    public static EmailIdentifier email(InternetAddress email) {
069        return email(email.getAddress());
070    }
071
072    /**
073     * Returns the email address.
074     *
075     * @return {@link InternetAddress}
076     * @throws AcmeProtocolException
077     *             if this is not a valid email identifier.
078     */
079    public InternetAddress getEmailAddress() {
080        try {
081            return new InternetAddress(getValue());
082        } catch (AddressException ex) {
083            throw new AcmeProtocolException("bad email address", ex);
084        }
085    }
086
087}