Remove boilerplate code for parameter null checks

pull/30/head
Richard Körber 2016-12-16 01:19:25 +01:00
parent ce125da4aa
commit 232a243e92
12 changed files with 66 additions and 74 deletions

View File

@ -13,6 +13,8 @@
*/ */
package org.shredzone.acme4j; package org.shredzone.acme4j;
import static org.shredzone.acme4j.util.AcmeUtils.assertNotNull;
import java.io.Serializable; import java.io.Serializable;
import java.net.URI; import java.net.URI;
@ -50,10 +52,7 @@ public abstract class AcmeResource implements Serializable {
* Sets a new {@link Session}. * Sets a new {@link Session}.
*/ */
protected void setSession(Session session) { protected void setSession(Session session) {
if (session == null) { assertNotNull(session, "session");
throw new NullPointerException("session must not be null");
}
this.session = session; this.session = session;
} }
@ -61,10 +60,7 @@ public abstract class AcmeResource implements Serializable {
* Sets the resource's location. * Sets the resource's location.
*/ */
protected void setLocation(URI location) { protected void setLocation(URI location) {
if (location == null) { assertNotNull(location, "location");
throw new NullPointerException("location must not be null");
}
this.location = location; this.location = location;
} }

View File

@ -36,6 +36,7 @@ import org.shredzone.acme4j.connector.ResourceIterator;
import org.shredzone.acme4j.exception.AcmeException; import org.shredzone.acme4j.exception.AcmeException;
import org.shredzone.acme4j.exception.AcmeProtocolException; import org.shredzone.acme4j.exception.AcmeProtocolException;
import org.shredzone.acme4j.exception.AcmeRetryAfterException; import org.shredzone.acme4j.exception.AcmeRetryAfterException;
import org.shredzone.acme4j.util.AcmeUtils;
import org.shredzone.acme4j.util.ClaimBuilder; import org.shredzone.acme4j.util.ClaimBuilder;
import org.shredzone.acme4j.util.DomainUtils; import org.shredzone.acme4j.util.DomainUtils;
import org.shredzone.acme4j.util.SignatureUtils; import org.shredzone.acme4j.util.SignatureUtils;
@ -176,8 +177,9 @@ public class Registration extends AcmeResource {
* @return {@link Authorization} object for this domain * @return {@link Authorization} object for this domain
*/ */
public Authorization authorizeDomain(String domain) throws AcmeException { public Authorization authorizeDomain(String domain) throws AcmeException {
if (domain == null || domain.isEmpty()) { AcmeUtils.assertNotNull(domain, "domain");
throw new NullPointerException("domain must not be empty or null"); if (domain.isEmpty()) {
throw new IllegalArgumentException("domain must not be empty");
} }
LOG.debug("authorizeDomain {}", domain); LOG.debug("authorizeDomain {}", domain);
@ -229,9 +231,7 @@ public class Registration extends AcmeResource {
*/ */
public Certificate requestCertificate(byte[] csr, Date notBefore, Date notAfter) public Certificate requestCertificate(byte[] csr, Date notBefore, Date notAfter)
throws AcmeException { throws AcmeException {
if (csr == null) { AcmeUtils.assertNotNull(csr, "csr");
throw new NullPointerException("csr must not be null");
}
LOG.debug("requestCertificate"); LOG.debug("requestCertificate");
try (Connection conn = getSession().provider().connect()) { try (Connection conn = getSession().provider().connect()) {
@ -269,9 +269,7 @@ public class Registration extends AcmeResource {
* new {@link KeyPair} to be used for identifying this account * new {@link KeyPair} to be used for identifying this account
*/ */
public void changeKey(KeyPair newKeyPair) throws AcmeException { public void changeKey(KeyPair newKeyPair) throws AcmeException {
if (newKeyPair == null) { AcmeUtils.assertNotNull(newKeyPair, "newKeyPair");
throw new NullPointerException("newKeyPair must not be null");
}
if (Arrays.equals(getSession().getKeyPair().getPrivate().getEncoded(), if (Arrays.equals(getSession().getKeyPair().getPrivate().getEncoded(),
newKeyPair.getPrivate().getEncoded())) { newKeyPair.getPrivate().getEncoded())) {
throw new IllegalArgumentException("newKeyPair must actually be a new key pair"); throw new IllegalArgumentException("newKeyPair must actually be a new key pair");

View File

@ -30,6 +30,7 @@ import org.shredzone.acme4j.connector.Resource;
import org.shredzone.acme4j.exception.AcmeException; import org.shredzone.acme4j.exception.AcmeException;
import org.shredzone.acme4j.exception.AcmeProtocolException; import org.shredzone.acme4j.exception.AcmeProtocolException;
import org.shredzone.acme4j.provider.AcmeProvider; 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 * A session stores the ACME server URI and the account's key pair. It also tracks
@ -71,13 +72,8 @@ public class Session {
* {@link KeyPair} of the ACME account * {@link KeyPair} of the ACME account
*/ */
public Session(URI serverUri, KeyPair keyPair) { public Session(URI serverUri, KeyPair keyPair) {
if (serverUri == null) { AcmeUtils.assertNotNull(serverUri, "serverUri");
throw new NullPointerException("serverUri must not be null"); AcmeUtils.assertNotNull(keyPair, "keyPair");
}
if (keyPair == null) {
throw new NullPointerException("keypair must not be null");
}
this.serverUri = serverUri; this.serverUri = serverUri;
this.keyPair = keyPair; this.keyPair = keyPair;
@ -170,9 +166,7 @@ public class Session {
* @return {@link Challenge} instance * @return {@link Challenge} instance
*/ */
public Challenge createChallenge(Map<String, Object> data) { public Challenge createChallenge(Map<String, Object> data) {
if (data == null) { AcmeUtils.assertNotNull(data, "data");
throw new NullPointerException("data must not be null");
}
String type = (String) data.get("type"); String type = (String) data.get("type");
if (type == null || type.isEmpty()) { if (type == null || type.isEmpty()) {
@ -200,9 +194,7 @@ public class Session {
* @return {@link URI}, or {@code null} if the server does not offer that resource * @return {@link URI}, or {@code null} if the server does not offer that resource
*/ */
public URI resourceUri(Resource resource) throws AcmeException { public URI resourceUri(Resource resource) throws AcmeException {
if (resource == null) { AcmeUtils.assertNotNull(resource, "resource");
throw new NullPointerException("resource must not be null");
}
readDirectory(); readDirectory();
return resourceMap.get(resource); return resourceMap.get(resource);
} }

View File

@ -31,6 +31,7 @@ import org.shredzone.acme4j.connector.Connection;
import org.shredzone.acme4j.exception.AcmeException; import org.shredzone.acme4j.exception.AcmeException;
import org.shredzone.acme4j.exception.AcmeProtocolException; import org.shredzone.acme4j.exception.AcmeProtocolException;
import org.shredzone.acme4j.exception.AcmeRetryAfterException; import org.shredzone.acme4j.exception.AcmeRetryAfterException;
import org.shredzone.acme4j.util.AcmeUtils;
import org.shredzone.acme4j.util.ClaimBuilder; import org.shredzone.acme4j.util.ClaimBuilder;
import org.shredzone.acme4j.util.TimestampParser; import org.shredzone.acme4j.util.TimestampParser;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -67,12 +68,8 @@ public class Challenge extends AcmeResource {
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static <T extends Challenge> T bind(Session session, URI location) throws AcmeException { public static <T extends Challenge> T bind(Session session, URI location) throws AcmeException {
if (session == null) { AcmeUtils.assertNotNull(session, "session");
throw new NullPointerException("session must not be null"); AcmeUtils.assertNotNull(location, "location");
}
if (location == null) {
throw new NullPointerException("location must not be null");
}
LOG.debug("bind"); LOG.debug("bind");
try (Connection conn = session.provider().connect()) { try (Connection conn = session.provider().connect()) {

View File

@ -49,6 +49,7 @@ import org.shredzone.acme4j.exception.AcmeProtocolException;
import org.shredzone.acme4j.exception.AcmeRateLimitExceededException; import org.shredzone.acme4j.exception.AcmeRateLimitExceededException;
import org.shredzone.acme4j.exception.AcmeServerException; import org.shredzone.acme4j.exception.AcmeServerException;
import org.shredzone.acme4j.exception.AcmeUnauthorizedException; import org.shredzone.acme4j.exception.AcmeUnauthorizedException;
import org.shredzone.acme4j.util.AcmeUtils;
import org.shredzone.acme4j.util.ClaimBuilder; import org.shredzone.acme4j.util.ClaimBuilder;
import org.shredzone.acme4j.util.SignatureUtils; import org.shredzone.acme4j.util.SignatureUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -71,21 +72,14 @@ public class DefaultConnection implements Connection {
protected HttpURLConnection conn; protected HttpURLConnection conn;
public DefaultConnection(HttpConnector httpConnector) { public DefaultConnection(HttpConnector httpConnector) {
if (httpConnector == null) { AcmeUtils.assertNotNull(httpConnector, "httpConnector");
throw new NullPointerException("httpConnector must not be null");
}
this.httpConnector = httpConnector; this.httpConnector = httpConnector;
} }
@Override @Override
public void sendRequest(URI uri, Session session) throws AcmeException { public void sendRequest(URI uri, Session session) throws AcmeException {
if (uri == null) { AcmeUtils.assertNotNull(uri, "uri");
throw new NullPointerException("uri must not be null"); AcmeUtils.assertNotNull(session, "session");
}
if (session == null) {
throw new NullPointerException("session must not be null");
}
assertConnectionIsClosed(); assertConnectionIsClosed();
LOG.debug("GET {}", uri); LOG.debug("GET {}", uri);
@ -107,15 +101,9 @@ public class DefaultConnection implements Connection {
@Override @Override
public void sendSignedRequest(URI uri, ClaimBuilder claims, Session session) throws AcmeException { public void sendSignedRequest(URI uri, ClaimBuilder claims, Session session) throws AcmeException {
if (uri == null) { AcmeUtils.assertNotNull(uri, "uri");
throw new NullPointerException("uri must not be null"); AcmeUtils.assertNotNull(claims, "claims");
} AcmeUtils.assertNotNull(session, "session");
if (claims == null) {
throw new NullPointerException("claims must not be null");
}
if (session == null) {
throw new NullPointerException("session must not be null");
}
assertConnectionIsClosed(); assertConnectionIsClosed();
try { try {

View File

@ -14,6 +14,7 @@
package org.shredzone.acme4j.exception; package org.shredzone.acme4j.exception;
import org.shredzone.acme4j.connector.DefaultConnection; 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 * An exception that is thrown when the ACME server returned an error. It contains
@ -35,9 +36,7 @@ public class AcmeServerException extends AcmeException {
*/ */
public AcmeServerException(String type, String detail) { public AcmeServerException(String type, String detail) {
super(detail); super(detail);
if (type == null) { AcmeUtils.assertNotNull(type, "type");
throw new IllegalArgumentException("Error type must not be null");
}
this.type = type; this.type = type;
} }

View File

@ -27,6 +27,7 @@ import org.shredzone.acme4j.connector.Connection;
import org.shredzone.acme4j.connector.DefaultConnection; import org.shredzone.acme4j.connector.DefaultConnection;
import org.shredzone.acme4j.connector.HttpConnector; import org.shredzone.acme4j.connector.HttpConnector;
import org.shredzone.acme4j.exception.AcmeException; import org.shredzone.acme4j.exception.AcmeException;
import org.shredzone.acme4j.util.AcmeUtils;
/** /**
* Abstract implementation of {@link AcmeProvider}. It consists of a challenge * Abstract implementation of {@link AcmeProvider}. It consists of a challenge
@ -58,11 +59,9 @@ public abstract class AbstractAcmeProvider implements AcmeProvider {
@Override @Override
@SuppressWarnings("deprecation") // must still provide deprecated challenges @SuppressWarnings("deprecation") // must still provide deprecated challenges
public Challenge createChallenge(Session session, String type) { public Challenge createChallenge(Session session, String type) {
if (session == null) { AcmeUtils.assertNotNull(session, "session");
throw new NullPointerException("session must not be null"); AcmeUtils.assertNotNull(type, "type");
} if (type.isEmpty()) {
if (type == null || type.isEmpty()) {
throw new IllegalArgumentException("no type given"); throw new IllegalArgumentException("no type given");
} }

View File

@ -78,4 +78,19 @@ public final class AcmeUtils {
return Base64Url.encode(data); 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");
}
}
} }

View File

@ -56,10 +56,7 @@ public class ClaimBuilder {
* @return {@code this} * @return {@code this}
*/ */
public ClaimBuilder put(String key, Object value) { public ClaimBuilder put(String key, Object value) {
if (key == null) { AcmeUtils.assertNotNull(key, "key");
throw new NullPointerException("key must not be null");
}
data.put(key, value); data.put(key, value);
return this; return this;
} }
@ -144,9 +141,7 @@ public class ClaimBuilder {
* @return {@code this} * @return {@code this}
*/ */
public ClaimBuilder putKey(String key, PublicKey publickey) { public ClaimBuilder putKey(String key, PublicKey publickey) {
if (publickey == null) { AcmeUtils.assertNotNull(publickey, "publickey");
throw new NullPointerException("publickey must not be null");
}
try { try {
final PublicJsonWebKey jwk = PublicJsonWebKey.Factory.newPublicJwk(publickey); final PublicJsonWebKey jwk = PublicJsonWebKey.Factory.newPublicJwk(publickey);

View File

@ -165,7 +165,7 @@ public class AbstractAcmeProviderTest {
try { try {
provider.createChallenge(session, (String) null); provider.createChallenge(session, (String) null);
fail("null was accepted"); fail("null was accepted");
} catch (IllegalArgumentException ex) { } catch (NullPointerException ex) {
// expected // expected
} }

View File

@ -14,7 +14,7 @@
package org.shredzone.acme4j.util; package org.shredzone.acme4j.util;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat; import static org.junit.Assert.*;
import static org.shredzone.acme4j.util.AcmeUtils.*; import static org.shredzone.acme4j.util.AcmeUtils.*;
import javax.xml.bind.DatatypeConverter; import javax.xml.bind.DatatypeConverter;
@ -54,4 +54,19 @@ public class AcmeUtilsTest {
assertThat(base64UrlEncode, is("w6uP8Tcg6K2QR905Rms8iXTlksL6OD1KOWBxTK7wxPI")); 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
}
}
} }

View File

@ -145,12 +145,10 @@ public class CSRBuilder {
* {@link KeyPair} to sign the CSR with * {@link KeyPair} to sign the CSR with
*/ */
public void sign(KeyPair keypair) throws IOException { public void sign(KeyPair keypair) throws IOException {
AcmeUtils.assertNotNull(keypair, "keypair");
if (namelist.isEmpty()) { if (namelist.isEmpty()) {
throw new IllegalStateException("No domain was set"); throw new IllegalStateException("No domain was set");
} }
if (keypair == null) {
throw new IllegalArgumentException("keypair must not be null");
}
try { try {
GeneralName[] gns = new GeneralName[namelist.size()]; GeneralName[] gns = new GeneralName[namelist.size()];