001/*
002 * acme4j - Java ACME client
003 *
004 * Copyright (C) 2018 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;
015
016import java.net.URL;
017import java.util.Objects;
018
019import edu.umd.cs.findbugs.annotations.Nullable;
020import org.shredzone.acme4j.connector.Connection;
021import org.shredzone.acme4j.exception.AcmeException;
022import org.shredzone.acme4j.exception.AcmeLazyLoadingException;
023import org.shredzone.acme4j.exception.AcmeRetryAfterException;
024import org.shredzone.acme4j.toolbox.JSON;
025import org.slf4j.Logger;
026import org.slf4j.LoggerFactory;
027
028/**
029 * An ACME resource that stores its state in a JSON structure.
030 */
031public abstract class AcmeJsonResource extends AcmeResource {
032    private static final long serialVersionUID = -5060364275766082345L;
033    private static final Logger LOG = LoggerFactory.getLogger(AcmeJsonResource.class);
034
035    private @Nullable JSON data = null;
036
037    /**
038     * Create a new {@link AcmeJsonResource}.
039     *
040     * @param login
041     *            {@link Login} the resource is bound with
042     * @param location
043     *            Location {@link URL} of this resource
044     */
045    protected AcmeJsonResource(Login login, URL location) {
046        super(login, location);
047    }
048
049    /**
050     * Returns the JSON representation of the resource data.
051     * <p>
052     * If there is no data, {@link #update()} is invoked to fetch it from the server.
053     * <p>
054     * This method can be used to read proprietary data from the resources.
055     *
056     * @return Resource data, as {@link JSON}.
057     */
058    public JSON getJSON() {
059        if (data == null) {
060            try {
061                update();
062            } catch (AcmeRetryAfterException ex) {
063                // ignore... The object was still updated.
064                LOG.debug("Retry-After", ex);
065            } catch (AcmeException ex) {
066                throw new AcmeLazyLoadingException(this, ex);
067            }
068        }
069        return data;
070    }
071
072    /**
073     * Sets the JSON representation of the resource data.
074     *
075     * @param data
076     *            New {@link JSON} data, must not be {@code null}.
077     */
078    protected void setJSON(JSON data) {
079        this.data = Objects.requireNonNull(data, "data");
080    }
081
082    /**
083     * Checks if this resource is valid.
084     *
085     * @return {@code true} if the resource state has been loaded from the server. If
086     *         {@code false}, {@link #getJSON()} would implicitly call {@link #update()}
087     *         to fetch the current state from the server.
088     */
089    protected boolean isValid() {
090        return data != null;
091    }
092
093    /**
094     * Invalidates the state of this resource. Enforces an {@link #update()} when
095     * {@link #getJSON()} is invoked.
096     */
097    protected void invalidate() {
098        data = null;
099    }
100
101    /**
102     * Updates this resource, by fetching the current resource data from the server.
103     *
104     * @throws AcmeException
105     *             if the resource could not be fetched.
106     * @throws AcmeRetryAfterException
107     *             the resource is still being processed, and the server returned an
108     *             estimated date when the process will be completed. If you are polling
109     *             for the resource to complete, you should wait for the date given in
110     *             {@link AcmeRetryAfterException#getRetryAfter()}. Note that the status
111     *             of the resource is updated even if this exception was thrown.
112     */
113    public void update() throws AcmeException {
114        String resourceType = getClass().getSimpleName();
115        LOG.debug("update {}", resourceType);
116        try (Connection conn = getSession().connect()) {
117            conn.sendSignedPostAsGetRequest(getLocation(), getLogin());
118            setJSON(conn.readJsonResponse());
119            conn.handleRetryAfter(resourceType + " is not completed yet");
120        }
121    }
122
123}