/*
 * acme4j - Java ACME client
 *
 * Copyright (C) 2017 Richard "Shred" Körber
 *   http://acme4j.shredzone.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 */
package org.shredzone.acme4j.provider.pebble;

import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.shredzone.acme4j.Session;
import org.shredzone.acme4j.exception.AcmeException;
import org.shredzone.acme4j.provider.AbstractAcmeProvider;
import org.shredzone.acme4j.provider.AcmeProvider;
import org.shredzone.acme4j.util.JSON;

/**
 * An {@link AcmeProvider} for <em>Pebble</em>.
 * <p>
 * <a href="https://github.com/letsencrypt/pebble">Pebble</a> is a small ACME test server.
 * This provider can be used to connect to an instance of a Pebble server.
 * <p>
 * {@code "acme://pebble"} connects to a Pebble server running on localhost and listening
 * on the standard port 14000. Using {@code "acme://pebble/other-host:12345"}, it is
 * possible to connect to an external Pebble server on the given {@code other-host} and
 * port. The port is optional, and if omitted, the standard port is used.
 */
public class PebbleAcmeProvider extends AbstractAcmeProvider {

    private static final Pattern HOST_PATTERN = Pattern.compile("^/([^:/]+)(?:\\:(\\d+))?/?$");

    @Override
    public boolean accepts(URI serverUri) {
        return "acme".equals(serverUri.getScheme()) && "pebble".equals(serverUri.getHost());
    }

    @Override
    public URL resolve(URI serverUri) {
        try {
            String path = serverUri.getPath();

            URL baseUrl = new URL("http://localhost:14000/dir");

            if (path != null && !path.isEmpty() && !"/".equals(path)) {
                baseUrl = parsePath(path);
            }

            return baseUrl;
        } catch (MalformedURLException ex) {
            throw new IllegalArgumentException("Bad server URI " + serverUri, ex);
        }
    }

    /**
     * Parses the server URI path and returns the server's base URL.
     *
     * @param path
     *            server URI path
     * @return URL of the server's base
     */
    private URL parsePath(String path) throws MalformedURLException {
        Matcher m = HOST_PATTERN.matcher(path);
        if (m.matches()) {
            String host = m.group(1);
            int port = 14000;
            if (m.group(2) != null) {
                port = Integer.parseInt(m.group(2));
            }
            return new URL("http", host, port, "/dir");
        } else {
            throw new IllegalArgumentException("Invalid Pebble host/port: " + path);
        }
    }

    // TODO PEBBLE: new-reg
    // https://github.com/letsencrypt/pebble/pull/24
    @Override
    public JSON directory(Session session, URI serverUri) throws AcmeException {
        JSON json = super.directory(session, serverUri);
        if (Pebble.workaround()) {
            json = JSON.parse(json.toString().replace("\"new-reg\"", "\"new-account\""));
        }
        return json;
    }

}

@@ -637,98 +637,8 @@
 n;%0A%0A
-import org.shredzone.acme4j.Session;%0Aimport org.shredzone.acme4j.exception.AcmeException;%0A
 impo
@@ -746,47 +746,8 @@
 der;
-%0Aimport org.shredzone.acme4j.util.JSON;
 %0A%0A/*
@@ -2803,402 +2803,6 @@
  %7D%0A%0A
-    // TODO PEBBLE: new-reg%0A    // https://github.com/letsencrypt/pebble/pull/24%0A    @Override%0A    public JSON directory(Session session, URI serverUri) throws AcmeException %7B%0A        JSON json = super.directory(session, serverUri);%0A        if (Pebble.workaround()) %7B%0A            json = JSON.parse(json.toString().replace(%22%5C%22new-reg%5C%22%22, %22%5C%22new-account%5C%22%22));%0A        %7D%0A        return json;%0A    %7D%0A%0A
 %7D%0A

Remove new-reg workaround