Use URI instead of plain string parsing

pull/17/merge
Richard Körber 2015-12-10 23:39:25 +01:00
parent 1d56065495
commit 1e9855bf05
4 changed files with 41 additions and 27 deletions

View File

@ -13,6 +13,8 @@
*/
package org.shredzone.acme4j;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.ServiceLoader;
@ -45,6 +47,23 @@ public final class AcmeClientFactory {
* @return {@link AcmeClient} for communication with the server
*/
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<>();
for (AcmeClientProvider acp : ServiceLoader.load(AcmeClientProvider.class)) {
if (acp.accepts(serverUri)) {

View File

@ -41,7 +41,7 @@ public interface AcmeClientProvider {
* @return {@code true} if this provider accepts the server URI, {@code false}
* otherwise
*/
boolean accepts(String serverUri);
boolean accepts(URI serverUri);
/**
* Connects to an {@link AcmeClient} for communication with the ACME server.
@ -50,7 +50,7 @@ public interface AcmeClientProvider {
* Server URI to connect to
* @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

View File

@ -14,7 +14,6 @@
package org.shredzone.acme4j.provider;
import java.net.URI;
import java.net.URISyntaxException;
import org.shredzone.acme4j.AcmeClient;
import org.shredzone.acme4j.impl.GenericAcmeClient;
@ -30,22 +29,18 @@ import org.shredzone.acme4j.impl.GenericAcmeClient;
public class GenericAcmeClientProvider extends AbstractAcmeClientProvider {
@Override
public boolean accepts(String serverUri) {
return serverUri.startsWith("http://") || serverUri.startsWith("https://");
public boolean accepts(URI serverUri) {
return "http".equals(serverUri.getScheme())
|| "https".equals(serverUri.getScheme());
}
@Override
public AcmeClient connect(String serverUri) {
public AcmeClient connect(URI serverUri) {
if (!accepts(serverUri)) {
throw new IllegalArgumentException("This provider does not accept " + serverUri);
}
try {
URI directoryUri = new URI(serverUri);
return new GenericAcmeClient(this, directoryUri);
} catch (URISyntaxException ex) {
throw new IllegalArgumentException(serverUri, ex);
}
return new GenericAcmeClient(this, serverUri);
}
}

View File

@ -51,25 +51,25 @@ public class LetsEncryptAcmeClientProvider extends AbstractAcmeClientProvider {
private SSLSocketFactory sslSocketFactory;
@Override
public boolean accepts(String serverUri) {
return serverUri.startsWith("acme://letsencrypt.org");
public boolean accepts(URI serverUri) {
return "acme".equals(serverUri.getScheme())
&& "letsencrypt.org".equals(serverUri.getHost());
}
@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;
switch (serverUri) {
case "acme://letsencrypt.org/staging":
directoryUri = STAGING_DIRECTORY_URI;
break;
case "acme://letsencrypt.org/v01":
case "acme://letsencrypt.org":
directoryUri = V01_DIRECTORY_URI;
break;
default:
throw new IllegalArgumentException("Unknown URI " + serverUri);
if (path == null || "v01".equals(path)) {
directoryUri = V01_DIRECTORY_URI;
} else if ("staging".equals(path)) {
directoryUri = STAGING_DIRECTORY_URI;
} else {
throw new IllegalArgumentException("Unknown URI " + serverUri);
}
try {