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.provider.letsencrypt;
015
016import java.net.MalformedURLException;
017import java.net.URI;
018import java.net.URL;
019
020import org.shredzone.acme4j.exception.AcmeProtocolException;
021import org.shredzone.acme4j.provider.AbstractAcmeProvider;
022import org.shredzone.acme4j.provider.AcmeProvider;
023
024/**
025 * An {@link AcmeProvider} for <em>Let's Encrypt</em>.
026 * <p>
027 * The {@code serverUri} is {@code "acme://letsencrypt.org"} for the production server,
028 * and {@code "acme://letsencrypt.org/staging"} for a testing server.
029 * <p>
030 * If you want to use <em>Let's Encrypt</em>, always prefer to use this provider.
031 *
032 * @see <a href="https://letsencrypt.org/">Let's Encrypt</a>
033 */
034public class LetsEncryptAcmeProvider extends AbstractAcmeProvider {
035
036    private static final String V01_DIRECTORY_URL = "https://acme-v01.api.letsencrypt.org/directory";
037    private static final String STAGING_DIRECTORY_URL = "https://acme-staging.api.letsencrypt.org/directory";
038
039    @Override
040    public boolean accepts(URI serverUri) {
041        return "acme".equals(serverUri.getScheme())
042                && "letsencrypt.org".equals(serverUri.getHost());
043    }
044
045    @Override
046    public URL resolve(URI serverUri) {
047        String path = serverUri.getPath();
048        String directoryUrl;
049        if (path == null || "".equals(path) || "/".equals(path) || "/v01".equals(path)) {
050            directoryUrl = V01_DIRECTORY_URL;
051        } else if ("/staging".equals(path)) {
052            directoryUrl = STAGING_DIRECTORY_URL;
053        } else {
054            throw new IllegalArgumentException("Unknown URI " + serverUri);
055        }
056
057        try {
058            return new URL(directoryUrl);
059        } catch (MalformedURLException ex) {
060            throw new AcmeProtocolException(directoryUrl, ex);
061        }
062    }
063
064}