mirror of https://github.com/shred/acme4j
Use URI instead of plain string parsing
parent
1d56065495
commit
1e9855bf05
|
@ -13,6 +13,8 @@
|
||||||
*/
|
*/
|
||||||
package org.shredzone.acme4j;
|
package org.shredzone.acme4j;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
import java.net.URISyntaxException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.ServiceLoader;
|
import java.util.ServiceLoader;
|
||||||
|
@ -45,6 +47,23 @@ public final class AcmeClientFactory {
|
||||||
* @return {@link AcmeClient} for communication with the server
|
* @return {@link AcmeClient} for communication with the server
|
||||||
*/
|
*/
|
||||||
public static AcmeClient connect(String serverUri) throws AcmeException {
|
public static AcmeClient connect(String serverUri) throws AcmeException {
|
||||||
|
try {
|
||||||
|
return connect(new URI(serverUri));
|
||||||
|
} catch (URISyntaxException ex) {
|
||||||
|
throw new IllegalArgumentException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Connects to an ACME server and provides an {@link AcmeClient} for communication.
|
||||||
|
*
|
||||||
|
* @param serverUri
|
||||||
|
* URI of the ACME server. This can either be a http/https URI to the
|
||||||
|
* server's directory service, or a special acme URI for specific
|
||||||
|
* implementations.
|
||||||
|
* @return {@link AcmeClient} for communication with the server
|
||||||
|
*/
|
||||||
|
public static AcmeClient connect(URI serverUri) throws AcmeException {
|
||||||
List<AcmeClientProvider> candidates = new ArrayList<>();
|
List<AcmeClientProvider> candidates = new ArrayList<>();
|
||||||
for (AcmeClientProvider acp : ServiceLoader.load(AcmeClientProvider.class)) {
|
for (AcmeClientProvider acp : ServiceLoader.load(AcmeClientProvider.class)) {
|
||||||
if (acp.accepts(serverUri)) {
|
if (acp.accepts(serverUri)) {
|
||||||
|
|
|
@ -41,7 +41,7 @@ public interface AcmeClientProvider {
|
||||||
* @return {@code true} if this provider accepts the server URI, {@code false}
|
* @return {@code true} if this provider accepts the server URI, {@code false}
|
||||||
* otherwise
|
* otherwise
|
||||||
*/
|
*/
|
||||||
boolean accepts(String serverUri);
|
boolean accepts(URI serverUri);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Connects to an {@link AcmeClient} for communication with the ACME server.
|
* Connects to an {@link AcmeClient} for communication with the ACME server.
|
||||||
|
@ -50,7 +50,7 @@ public interface AcmeClientProvider {
|
||||||
* Server URI to connect to
|
* Server URI to connect to
|
||||||
* @return {@link AcmeClient} connected to the server
|
* @return {@link AcmeClient} connected to the server
|
||||||
*/
|
*/
|
||||||
AcmeClient connect(String serverUri);
|
AcmeClient connect(URI serverUri);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a {@link Challenge} instance that is able to respond to the challenge of
|
* Creates a {@link Challenge} instance that is able to respond to the challenge of
|
||||||
|
|
|
@ -14,7 +14,6 @@
|
||||||
package org.shredzone.acme4j.provider;
|
package org.shredzone.acme4j.provider;
|
||||||
|
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.net.URISyntaxException;
|
|
||||||
|
|
||||||
import org.shredzone.acme4j.AcmeClient;
|
import org.shredzone.acme4j.AcmeClient;
|
||||||
import org.shredzone.acme4j.impl.GenericAcmeClient;
|
import org.shredzone.acme4j.impl.GenericAcmeClient;
|
||||||
|
@ -30,22 +29,18 @@ import org.shredzone.acme4j.impl.GenericAcmeClient;
|
||||||
public class GenericAcmeClientProvider extends AbstractAcmeClientProvider {
|
public class GenericAcmeClientProvider extends AbstractAcmeClientProvider {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean accepts(String serverUri) {
|
public boolean accepts(URI serverUri) {
|
||||||
return serverUri.startsWith("http://") || serverUri.startsWith("https://");
|
return "http".equals(serverUri.getScheme())
|
||||||
|
|| "https".equals(serverUri.getScheme());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public AcmeClient connect(String serverUri) {
|
public AcmeClient connect(URI serverUri) {
|
||||||
if (!accepts(serverUri)) {
|
if (!accepts(serverUri)) {
|
||||||
throw new IllegalArgumentException("This provider does not accept " + serverUri);
|
throw new IllegalArgumentException("This provider does not accept " + serverUri);
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
return new GenericAcmeClient(this, serverUri);
|
||||||
URI directoryUri = new URI(serverUri);
|
|
||||||
return new GenericAcmeClient(this, directoryUri);
|
|
||||||
} catch (URISyntaxException ex) {
|
|
||||||
throw new IllegalArgumentException(serverUri, ex);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -51,25 +51,25 @@ public class LetsEncryptAcmeClientProvider extends AbstractAcmeClientProvider {
|
||||||
private SSLSocketFactory sslSocketFactory;
|
private SSLSocketFactory sslSocketFactory;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean accepts(String serverUri) {
|
public boolean accepts(URI serverUri) {
|
||||||
return serverUri.startsWith("acme://letsencrypt.org");
|
return "acme".equals(serverUri.getScheme())
|
||||||
|
&& "letsencrypt.org".equals(serverUri.getHost());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public AcmeClient connect(String serverUri) {
|
public AcmeClient connect(URI serverUri) {
|
||||||
|
if (accepts(serverUri)) {
|
||||||
|
throw new IllegalArgumentException("Unknown URI " + serverUri);
|
||||||
|
}
|
||||||
|
|
||||||
|
String path = serverUri.getPath();
|
||||||
String directoryUri;
|
String directoryUri;
|
||||||
switch (serverUri) {
|
if (path == null || "v01".equals(path)) {
|
||||||
case "acme://letsencrypt.org/staging":
|
directoryUri = V01_DIRECTORY_URI;
|
||||||
directoryUri = STAGING_DIRECTORY_URI;
|
} else if ("staging".equals(path)) {
|
||||||
break;
|
directoryUri = STAGING_DIRECTORY_URI;
|
||||||
|
} else {
|
||||||
case "acme://letsencrypt.org/v01":
|
throw new IllegalArgumentException("Unknown URI " + serverUri);
|
||||||
case "acme://letsencrypt.org":
|
|
||||||
directoryUri = V01_DIRECTORY_URI;
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
throw new IllegalArgumentException("Unknown URI " + serverUri);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
Loading…
Reference in New Issue