mirror of https://github.com/shred/acme4j
Use standard method for null tests
parent
25b00313b2
commit
5565eba9eb
|
@ -13,10 +13,9 @@
|
|||
*/
|
||||
package org.shredzone.acme4j;
|
||||
|
||||
import static org.shredzone.acme4j.util.AcmeUtils.assertNotNull;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.net.URI;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* A generic ACME resource.
|
||||
|
@ -52,16 +51,14 @@ public abstract class AcmeResource implements Serializable {
|
|||
* Sets a new {@link Session}.
|
||||
*/
|
||||
protected void setSession(Session session) {
|
||||
assertNotNull(session, "session");
|
||||
this.session = session;
|
||||
this.session = Objects.requireNonNull(session, "session");
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the resource's location.
|
||||
*/
|
||||
protected void setLocation(URI location) {
|
||||
assertNotNull(location, "location");
|
||||
this.location = location;
|
||||
this.location = Objects.requireNonNull(location, "location");
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -28,6 +28,7 @@ import java.util.Date;
|
|||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
import org.jose4j.jwk.PublicJsonWebKey;
|
||||
import org.jose4j.jws.JsonWebSignature;
|
||||
|
@ -38,7 +39,6 @@ import org.shredzone.acme4j.connector.ResourceIterator;
|
|||
import org.shredzone.acme4j.exception.AcmeException;
|
||||
import org.shredzone.acme4j.exception.AcmeProtocolException;
|
||||
import org.shredzone.acme4j.exception.AcmeRetryAfterException;
|
||||
import org.shredzone.acme4j.util.AcmeUtils;
|
||||
import org.shredzone.acme4j.util.ClaimBuilder;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
@ -177,7 +177,7 @@ public class Registration extends AcmeResource {
|
|||
* @return {@link Authorization} object for this domain
|
||||
*/
|
||||
public Authorization authorizeDomain(String domain) throws AcmeException {
|
||||
AcmeUtils.assertNotNull(domain, "domain");
|
||||
Objects.requireNonNull(domain, "domain");
|
||||
if (domain.isEmpty()) {
|
||||
throw new IllegalArgumentException("domain must not be empty");
|
||||
}
|
||||
|
@ -231,7 +231,7 @@ public class Registration extends AcmeResource {
|
|||
*/
|
||||
public Certificate requestCertificate(byte[] csr, Date notBefore, Date notAfter)
|
||||
throws AcmeException {
|
||||
AcmeUtils.assertNotNull(csr, "csr");
|
||||
Objects.requireNonNull(csr, "csr");
|
||||
|
||||
LOG.debug("requestCertificate");
|
||||
try (Connection conn = getSession().provider().connect()) {
|
||||
|
@ -269,7 +269,7 @@ public class Registration extends AcmeResource {
|
|||
* new {@link KeyPair} to be used for identifying this account
|
||||
*/
|
||||
public void changeKey(KeyPair newKeyPair) throws AcmeException {
|
||||
AcmeUtils.assertNotNull(newKeyPair, "newKeyPair");
|
||||
Objects.requireNonNull(newKeyPair, "newKeyPair");
|
||||
if (Arrays.equals(getSession().getKeyPair().getPrivate().getEncoded(),
|
||||
newKeyPair.getPrivate().getEncoded())) {
|
||||
throw new IllegalArgumentException("newKeyPair must actually be a new key pair");
|
||||
|
|
|
@ -22,6 +22,7 @@ import java.util.EnumMap;
|
|||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.ServiceLoader;
|
||||
|
||||
import org.shredzone.acme4j.challenge.Challenge;
|
||||
|
@ -30,7 +31,6 @@ import org.shredzone.acme4j.connector.Resource;
|
|||
import org.shredzone.acme4j.exception.AcmeException;
|
||||
import org.shredzone.acme4j.exception.AcmeProtocolException;
|
||||
import org.shredzone.acme4j.provider.AcmeProvider;
|
||||
import org.shredzone.acme4j.util.AcmeUtils;
|
||||
|
||||
/**
|
||||
* A session stores the ACME server URI and the account's key pair. It also tracks
|
||||
|
@ -72,11 +72,8 @@ public class Session {
|
|||
* {@link KeyPair} of the ACME account
|
||||
*/
|
||||
public Session(URI serverUri, KeyPair keyPair) {
|
||||
AcmeUtils.assertNotNull(serverUri, "serverUri");
|
||||
AcmeUtils.assertNotNull(keyPair, "keyPair");
|
||||
|
||||
this.serverUri = serverUri;
|
||||
this.keyPair = keyPair;
|
||||
this.serverUri = Objects.requireNonNull(serverUri, "serverUri");
|
||||
this.keyPair = Objects.requireNonNull(keyPair, "keyPair");
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -166,7 +163,7 @@ public class Session {
|
|||
* @return {@link Challenge} instance
|
||||
*/
|
||||
public Challenge createChallenge(Map<String, Object> data) {
|
||||
AcmeUtils.assertNotNull(data, "data");
|
||||
Objects.requireNonNull(data, "data");
|
||||
|
||||
String type = (String) data.get("type");
|
||||
if (type == null || type.isEmpty()) {
|
||||
|
@ -194,9 +191,8 @@ public class Session {
|
|||
* @return {@link URI}, or {@code null} if the server does not offer that resource
|
||||
*/
|
||||
public URI resourceUri(Resource resource) throws AcmeException {
|
||||
AcmeUtils.assertNotNull(resource, "resource");
|
||||
readDirectory();
|
||||
return resourceMap.get(resource);
|
||||
return resourceMap.get(Objects.requireNonNull(resource, "resource"));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -25,6 +25,7 @@ import java.net.URL;
|
|||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
import org.jose4j.json.JsonUtil;
|
||||
import org.jose4j.lang.JoseException;
|
||||
|
@ -35,7 +36,6 @@ import org.shredzone.acme4j.connector.Connection;
|
|||
import org.shredzone.acme4j.exception.AcmeException;
|
||||
import org.shredzone.acme4j.exception.AcmeProtocolException;
|
||||
import org.shredzone.acme4j.exception.AcmeRetryAfterException;
|
||||
import org.shredzone.acme4j.util.AcmeUtils;
|
||||
import org.shredzone.acme4j.util.ClaimBuilder;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
@ -71,8 +71,8 @@ public class Challenge extends AcmeResource {
|
|||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T extends Challenge> T bind(Session session, URI location) throws AcmeException {
|
||||
AcmeUtils.assertNotNull(session, "session");
|
||||
AcmeUtils.assertNotNull(location, "location");
|
||||
Objects.requireNonNull(session, "session");
|
||||
Objects.requireNonNull(location, "location");
|
||||
|
||||
LOG.debug("bind");
|
||||
try (Connection conn = session.provider().connect()) {
|
||||
|
|
|
@ -34,6 +34,7 @@ import java.util.Collection;
|
|||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
|
@ -51,7 +52,6 @@ import org.shredzone.acme4j.exception.AcmeProtocolException;
|
|||
import org.shredzone.acme4j.exception.AcmeRateLimitExceededException;
|
||||
import org.shredzone.acme4j.exception.AcmeServerException;
|
||||
import org.shredzone.acme4j.exception.AcmeUnauthorizedException;
|
||||
import org.shredzone.acme4j.util.AcmeUtils;
|
||||
import org.shredzone.acme4j.util.ClaimBuilder;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
@ -73,14 +73,13 @@ public class DefaultConnection implements Connection {
|
|||
protected HttpURLConnection conn;
|
||||
|
||||
public DefaultConnection(HttpConnector httpConnector) {
|
||||
AcmeUtils.assertNotNull(httpConnector, "httpConnector");
|
||||
this.httpConnector = httpConnector;
|
||||
this.httpConnector = Objects.requireNonNull(httpConnector, "httpConnector");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendRequest(URI uri, Session session) throws AcmeException {
|
||||
AcmeUtils.assertNotNull(uri, "uri");
|
||||
AcmeUtils.assertNotNull(session, "session");
|
||||
Objects.requireNonNull(uri, "uri");
|
||||
Objects.requireNonNull(session, "session");
|
||||
assertConnectionIsClosed();
|
||||
|
||||
LOG.debug("GET {}", uri);
|
||||
|
@ -102,9 +101,9 @@ public class DefaultConnection implements Connection {
|
|||
|
||||
@Override
|
||||
public void sendSignedRequest(URI uri, ClaimBuilder claims, Session session) throws AcmeException {
|
||||
AcmeUtils.assertNotNull(uri, "uri");
|
||||
AcmeUtils.assertNotNull(claims, "claims");
|
||||
AcmeUtils.assertNotNull(session, "session");
|
||||
Objects.requireNonNull(uri, "uri");
|
||||
Objects.requireNonNull(claims, "claims");
|
||||
Objects.requireNonNull(session, "session");
|
||||
assertConnectionIsClosed();
|
||||
|
||||
try {
|
||||
|
|
|
@ -13,8 +13,9 @@
|
|||
*/
|
||||
package org.shredzone.acme4j.exception;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
import org.shredzone.acme4j.connector.DefaultConnection;
|
||||
import org.shredzone.acme4j.util.AcmeUtils;
|
||||
|
||||
/**
|
||||
* An exception that is thrown when the ACME server returned an error. It contains
|
||||
|
@ -36,8 +37,7 @@ public class AcmeServerException extends AcmeException {
|
|||
*/
|
||||
public AcmeServerException(String type, String detail) {
|
||||
super(detail);
|
||||
AcmeUtils.assertNotNull(type, "type");
|
||||
this.type = type;
|
||||
this.type = Objects.requireNonNull(type, "type");
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -16,6 +16,7 @@ package org.shredzone.acme4j.provider;
|
|||
import java.net.HttpURLConnection;
|
||||
import java.net.URI;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
import org.shredzone.acme4j.Session;
|
||||
import org.shredzone.acme4j.challenge.Challenge;
|
||||
|
@ -27,7 +28,6 @@ import org.shredzone.acme4j.connector.Connection;
|
|||
import org.shredzone.acme4j.connector.DefaultConnection;
|
||||
import org.shredzone.acme4j.connector.HttpConnector;
|
||||
import org.shredzone.acme4j.exception.AcmeException;
|
||||
import org.shredzone.acme4j.util.AcmeUtils;
|
||||
|
||||
/**
|
||||
* Abstract implementation of {@link AcmeProvider}. It consists of a challenge
|
||||
|
@ -59,8 +59,8 @@ public abstract class AbstractAcmeProvider implements AcmeProvider {
|
|||
@Override
|
||||
@SuppressWarnings("deprecation") // must still provide deprecated challenges
|
||||
public Challenge createChallenge(Session session, String type) {
|
||||
AcmeUtils.assertNotNull(session, "session");
|
||||
AcmeUtils.assertNotNull(type, "type");
|
||||
Objects.requireNonNull(session, "session");
|
||||
Objects.requireNonNull(type, "type");
|
||||
if (type.isEmpty()) {
|
||||
throw new IllegalArgumentException("no type given");
|
||||
}
|
||||
|
|
|
@ -99,21 +99,6 @@ public final class AcmeUtils {
|
|||
return Base64Url.encode(data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the given value is not {@code null}. Otherwise a
|
||||
* {@link NullPointerException} is thrown.
|
||||
*
|
||||
* @param value
|
||||
* Value to test
|
||||
* @param name
|
||||
* Name of the parameter
|
||||
*/
|
||||
public static void assertNotNull(Object value, String name) {
|
||||
if (value == null) {
|
||||
throw new NullPointerException(name + " must not be null");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* ASCII encodes a domain name.
|
||||
* <p>
|
||||
|
|
|
@ -21,6 +21,7 @@ import java.text.SimpleDateFormat;
|
|||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.TimeZone;
|
||||
import java.util.TreeMap;
|
||||
|
||||
|
@ -56,8 +57,7 @@ public class ClaimBuilder {
|
|||
* @return {@code this}
|
||||
*/
|
||||
public ClaimBuilder put(String key, Object value) {
|
||||
AcmeUtils.assertNotNull(key, "key");
|
||||
data.put(key, value);
|
||||
data.put(Objects.requireNonNull(key, "key"), value);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
@ -141,7 +141,7 @@ public class ClaimBuilder {
|
|||
* @return {@code this}
|
||||
*/
|
||||
public ClaimBuilder putKey(String key, PublicKey publickey) {
|
||||
AcmeUtils.assertNotNull(publickey, "publickey");
|
||||
Objects.requireNonNull(publickey, "publickey");
|
||||
|
||||
try {
|
||||
final PublicJsonWebKey jwk = PublicJsonWebKey.Factory.newPublicJwk(publickey);
|
||||
|
|
|
@ -85,21 +85,6 @@ public class AcmeUtilsTest {
|
|||
assertThat(base64UrlEncode, is("w6uP8Tcg6K2QR905Rms8iXTlksL6OD1KOWBxTK7wxPI"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that {@code null} check works properly.
|
||||
*/
|
||||
@Test
|
||||
public void testAssertNotNull() {
|
||||
AcmeUtils.assertNotNull(new Object(), "foo");
|
||||
|
||||
try {
|
||||
AcmeUtils.assertNotNull(null, "bar");
|
||||
fail("null was accepted");
|
||||
} catch (NullPointerException ex) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test ACE conversion.
|
||||
*/
|
||||
|
|
|
@ -25,6 +25,7 @@ import java.security.interfaces.ECKey;
|
|||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
|
||||
import org.bouncycastle.asn1.x500.X500Name;
|
||||
|
@ -147,7 +148,7 @@ public class CSRBuilder {
|
|||
* {@link KeyPair} to sign the CSR with
|
||||
*/
|
||||
public void sign(KeyPair keypair) throws IOException {
|
||||
AcmeUtils.assertNotNull(keypair, "keypair");
|
||||
Objects.requireNonNull(keypair, "keypair");
|
||||
if (namelist.isEmpty()) {
|
||||
throw new IllegalStateException("No domain was set");
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue