/** * Assert that an object is not <code>null</code> . * <pre class="code">Assert.notNull(clazz);</pre> * @param object the object to check * @throws IllegalArgumentException if the object is <code>null</code> */ public static void notNull(Object object) { notNull(object, "[Assertion failed] - this argument is required; it must not be null"); }
/** * Assert that the provided object is an instance of the provided class. * <pre class="code">Assert.instanceOf(Foo.class, foo);</pre> * @param clazz the required class * @param obj the object to check * @throws IllegalArgumentException if the object is not an instance of clazz * @see Class#isInstance */ public static void isInstanceOf(Class clazz, Object obj) { isInstanceOf(clazz, obj, ""); }
@Override public JwtBuilder signWith(SignatureAlgorithm alg, byte[] secretKeyBytes) throws InvalidKeyException { Assert.notNull(alg, "SignatureAlgorithm cannot be null."); Assert.notEmpty(secretKeyBytes, "secret key byte array cannot be null or empty."); Assert.isTrue(alg.isHmac(), "Key bytes may only be specified for HMAC signatures. If using RSA or Elliptic Curve, use the signWith(SignatureAlgorithm, Key) method instead."); SecretKey key = new SecretKeySpec(secretKeyBytes, alg.getJcaName()); return signWith(key, alg); }
@Override public JwtBuilder signWith(SignatureAlgorithm alg, String base64EncodedSecretKey) throws InvalidKeyException { Assert.hasText(base64EncodedSecretKey, "base64-encoded secret key cannot be null or empty."); Assert.isTrue(alg.isHmac(), "Base64-encoded key bytes may only be specified for HMAC signatures. If using RSA or Elliptic Curve, use the signWith(SignatureAlgorithm, Key) method instead."); byte[] bytes = Decoders.BASE64.decode(base64EncodedSecretKey); return signWith(alg, bytes); }
Assert.hasText(jwt, "JWT String argument cannot be null or empty."); Assert.isTrue(algorithm.isHmac(), "Key bytes can only be specified for HMAC signatures. Please specify a PublicKey or PrivateKey instance."); Assert.notNull(key, "A signing key must be specified if the specified JWT is digitally signed.");
/** * Assert that the given String has valid text content; that is, it must not * be <code>null</code> and must contain at least one non-whitespace character. * <pre class="code">Assert.hasText(name, "'name' must not be empty");</pre> * @param text the String to check * @see Strings#hasText */ public static void hasText(String text) { hasText(text, "[Assertion failed] - this String argument must have text; it must not be null, empty, or blank"); }
public EllipticCurveSignatureValidator(SignatureAlgorithm alg, Key key) { super(alg, key); Assert.isTrue(key instanceof ECPublicKey, EC_PUBLIC_KEY_REQD_MSG); }
/** * Assert that an array has elements; that is, it must not be * <code>null</code> and must have at least one element. * <pre class="code">Assert.notEmpty(array);</pre> * @param array the array to check * @throws IllegalArgumentException if the object array is <code>null</code> or has no elements */ public static void notEmpty(Object[] array) { notEmpty(array, "[Assertion failed] - this array must not be empty: it must contain at least 1 element"); }
/** * Assert a boolean expression, throwing {@link IllegalStateException} * if the test result is <code>false</code>. * <p>Call {@link #isTrue(boolean)} if you wish to * throw {@link IllegalArgumentException} on an assertion failure. * <pre class="code">Assert.state(id == null);</pre> * @param expression a boolean expression * @throws IllegalStateException if the supplied expression is <code>false</code> */ public static void state(boolean expression) { state(expression, "[Assertion failed] - this state invariant must be true"); }
/** * Assert that the given text does not contain the given substring. * <pre class="code">Assert.doesNotContain(name, "rod");</pre> * @param textToSearch the text to search * @param substring the substring to find within the text */ public static void doesNotContain(String textToSearch, String substring) { doesNotContain(textToSearch, substring, "[Assertion failed] - this String argument must not contain the substring [" + substring + "]"); }
/** * Assert that the given String is not empty; that is, * it must not be <code>null</code> and not the empty String. * <pre class="code">Assert.hasLength(name);</pre> * @param text the String to check * @see Strings#hasLength */ public static void hasLength(String text) { hasLength(text, "[Assertion failed] - this String argument must have length; it must not be null or empty"); }
@Override public JwtBuilder signWith(SignatureAlgorithm alg, byte[] secretKey) { Assert.notNull(alg, "SignatureAlgorithm cannot be null."); Assert.notEmpty(secretKey, "secret key byte array cannot be null or empty."); Assert.isTrue(alg.isHmac(), "Key bytes may only be specified for HMAC signatures. If using RSA or Elliptic Curve, use the signWith(SignatureAlgorithm, Key) method instead."); this.algorithm = alg; this.keyBytes = secretKey; return this; }
@Override public Jwt parse(String jwt) throws ExpiredJwtException, MalformedJwtException, SignatureException { Assert.hasText(jwt, "JWT String argument cannot be null or empty."); Assert.isTrue(algorithm.isHmac(), "Key bytes can only be specified for HMAC signatures. Please specify a PublicKey or PrivateKey instance."); Assert.notNull(key, "A signing key must be specified if the specified JWT is digitally signed.");
@Override public JwtParser setSigningKey(String base64EncodedSecretKey) { Assert.hasText(base64EncodedSecretKey, "signing key cannot be null or empty."); this.keyBytes = Decoders.BASE64.decode(base64EncodedSecretKey); return this; }
@Override public JwtBuilder signWith(SignatureAlgorithm alg, String base64EncodedSecretKey) { Assert.hasText(base64EncodedSecretKey, "base64-encoded secret key cannot be null or empty."); Assert.isTrue(alg.isHmac(), "Base64-encoded key bytes may only be specified for HMAC signatures. If using RSA or Elliptic Curve, use the signWith(SignatureAlgorithm, Key) method instead."); byte[] bytes = TextCodec.BASE64.decode(base64EncodedSecretKey); return signWith(alg, bytes); }
/** * Assert a boolean expression, throwing <code>IllegalArgumentException</code> * if the test result is <code>false</code>. * <pre class="code">Assert.isTrue(i > 0);</pre> * @param expression a boolean expression * @throws IllegalArgumentException if expression is <code>false</code> */ public static void isTrue(boolean expression) { isTrue(expression, "[Assertion failed] - this expression must be true"); }
/** * Assert that a Map has entries; that is, it must not be <code>null</code> * and must have at least one entry. * <pre class="code">Assert.notEmpty(map);</pre> * @param map the map to check * @throws IllegalArgumentException if the map is <code>null</code> or has no entries */ public static void notEmpty(Map map) { notEmpty(map, "[Assertion failed] - this map must not be empty; it must contain at least one entry"); }
@SuppressWarnings("unchecked") @Override public Serializer<Object> getInstance() { Serializer<Object> serializer = SERIALIZER.get(); if (serializer == null) { serializer = locate(); Assert.state(serializer != null, "locate() cannot return null."); if (!compareAndSet(serializer)) { serializer = SERIALIZER.get(); } } Assert.state(serializer != null, "serializer cannot be null."); return serializer; }