001/*
002 * acme4j - Java ACME client
003 *
004 * Copyright (C) 2015 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.exception;
015
016import java.io.Serial;
017import java.net.URL;
018import java.time.Instant;
019import java.util.Collection;
020import java.util.Collections;
021import java.util.Optional;
022
023import edu.umd.cs.findbugs.annotations.Nullable;
024import org.shredzone.acme4j.Problem;
025
026/**
027 * A rate limit was exceeded. If provided by the server, it also includes the earliest
028 * time at which a new attempt will be accepted, and a reference to a document that
029 * further explains the rate limit that was exceeded.
030 */
031public class AcmeRateLimitedException extends AcmeServerException {
032    @Serial
033    private static final long serialVersionUID = 4150484059796413069L;
034
035    private final @Nullable Instant retryAfter;
036    private final Collection<URL> documents;
037
038    /**
039     * Creates a new {@link AcmeRateLimitedException}.
040     *
041     * @param problem
042     *         {@link Problem} that caused the exception
043     * @param retryAfter
044     *         The instant of time that the request is expected to succeed again, may be
045     *         {@code null} if not known
046     * @param documents
047     *         URLs pointing to documents about the rate limit that was hit, may be
048     *         {@code null} if not known
049     */
050    public AcmeRateLimitedException(Problem problem, @Nullable Instant retryAfter,
051                @Nullable Collection<URL> documents) {
052        super(problem);
053        this.retryAfter = retryAfter;
054        this.documents = documents != null ? documents : Collections.emptyList();
055    }
056
057    /**
058     * Returns the instant of time the request is expected to succeed again. Empty
059     * if this moment is not known.
060     */
061    public Optional<Instant> getRetryAfter() {
062        return Optional.ofNullable(retryAfter);
063    }
064
065    /**
066     * Collection of URLs pointing to documents about the rate limit that was hit.
067     * Empty if the server did not provide such URLs.
068     */
069    public Collection<URL> getDocuments() {
070        return Collections.unmodifiableCollection(documents);
071    }
072
073}