001/*
002 * acme4j - Java ACME client
003 *
004 * Copyright (C) 2016 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.MalformedURLException;
017import java.net.URI;
018import java.net.URL;
019
020import edu.umd.cs.findbugs.annotations.Nullable;
021import org.shredzone.acme4j.Problem;
022
023/**
024 * An exception that is thrown when the user is required to take action as indicated.
025 * <p>
026 * Usually this exception is thrown when the terms of service have changed, and the CA
027 * requires an agreement to the new terms before proceeding.
028 */
029public class AcmeUserActionRequiredException extends AcmeServerException {
030    private static final long serialVersionUID = 7719055447283858352L;
031
032    private final @Nullable URI tosUri;
033
034    /**
035     * Creates a new {@link AcmeUserActionRequiredException}.
036     *
037     * @param problem
038     *            {@link Problem} that caused the exception
039     * @param tosUri
040     *            {@link URI} of the terms-of-service document to accept
041     */
042    public AcmeUserActionRequiredException(Problem problem, @Nullable URI tosUri) {
043        super(problem);
044        this.tosUri = tosUri;
045    }
046
047    /**
048     * Returns the {@link URI} of the terms-of-service document to accept, or {@code null}
049     * if the server did not provide a link to such a document.
050     */
051    @Nullable
052    public URI getTermsOfServiceUri() {
053        return tosUri;
054    }
055
056    /**
057     * Returns the {@link URL} of a document that gives instructions on the actions to
058     * be taken by a human.
059     */
060    public URL getInstance() {
061        URI instance = getProblem().getInstance();
062
063        if (instance == null) {
064            throw new AcmeProtocolException("Instance URL required, but missing.");
065        }
066
067        try {
068            return instance.toURL();
069        } catch (MalformedURLException ex) {
070            throw new AcmeProtocolException("Bad instance URL: " + instance, ex);
071        }
072    }
073
074}