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.net.URL;
017import java.time.Instant;
018import java.util.Collection;
019import java.util.Collections;
020
021import edu.umd.cs.findbugs.annotations.Nullable;
022import org.shredzone.acme4j.Problem;
023
024/**
025 * An exception that is thrown when a rate limit was exceeded.
026 */
027public class AcmeRateLimitedException extends AcmeServerException {
028    private static final long serialVersionUID = 4150484059796413069L;
029
030    private final @Nullable Instant retryAfter;
031    private final @Nullable Collection<URL> documents;
032
033    /**
034     * Creates a new {@link AcmeRateLimitedException}.
035     *
036     * @param problem
037     *            {@link Problem} that caused the exception
038     * @param retryAfter
039     *            The moment the request is expected to succeed again, may be {@code null}
040     *            if not known
041     * @param documents
042     *            URLs pointing to documents about the rate limit that was hit
043     */
044    public AcmeRateLimitedException(Problem problem, @Nullable Instant retryAfter,
045                @Nullable Collection<URL> documents) {
046        super(problem);
047        this.retryAfter = retryAfter;
048        this.documents =
049                documents != null ? Collections.unmodifiableCollection(documents) : null;
050    }
051
052    /**
053     * Returns the moment the request is expected to succeed again. {@code null} if this
054     * moment is not known.
055     */
056    @Nullable
057    public Instant getRetryAfter() {
058        return retryAfter;
059    }
060
061    /**
062     * Collection of URLs pointing to documents about the rate limit that was hit.
063     * {@code null} if the server did not provide such URLs.
064     */
065    @Nullable
066    public Collection<URL> getDocuments() {
067        return documents;
068    }
069
070}