From 8168e6efc707e5342c2a899c1818e0da8a8a2ab9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20K=C3=B6rber?= Date: Mon, 21 Dec 2015 00:28:39 +0100 Subject: [PATCH] Extract Status enum. Make Authorization use it. --- .../org/shredzone/acme4j/Authorization.java | 6 +- .../java/org/shredzone/acme4j/Status.java | 55 +++++++++++++++++++ .../shredzone/acme4j/challenge/Challenge.java | 8 +-- .../acme4j/challenge/GenericChallenge.java | 4 +- .../acme4j/impl/AbstractAcmeClient.java | 3 +- .../shredzone/acme4j/AuthorizationTest.java | 4 +- .../java/org/shredzone/acme4j/StatusTest.java | 45 +++++++++++++++ .../acme4j/challenge/DnsChallengeTest.java | 2 +- .../challenge/GenericChallengeTest.java | 4 +- .../acme4j/challenge/HttpChallengeTest.java | 2 +- .../acme4j/impl/AbstractAcmeClientTest.java | 6 +- .../java/org/shredzone/acme4j/ClientTest.java | 5 +- 12 files changed, 119 insertions(+), 25 deletions(-) create mode 100644 acme4j-client/src/main/java/org/shredzone/acme4j/Status.java create mode 100644 acme4j-client/src/test/java/org/shredzone/acme4j/StatusTest.java diff --git a/acme4j-client/src/main/java/org/shredzone/acme4j/Authorization.java b/acme4j-client/src/main/java/org/shredzone/acme4j/Authorization.java index 5fdb1f0b..b2724a77 100644 --- a/acme4j-client/src/main/java/org/shredzone/acme4j/Authorization.java +++ b/acme4j-client/src/main/java/org/shredzone/acme4j/Authorization.java @@ -32,7 +32,7 @@ public class Authorization implements Serializable { private URI location; private String domain; - private String status; + private Status status; private String expires; private List challenges; private List> combinations; @@ -82,14 +82,14 @@ public class Authorization implements Serializable { /** * Gets the authorization status. */ - public String getStatus() { + public Status getStatus() { return status; } /** * Sets the authorization status. */ - public void setStatus(String status) { + public void setStatus(Status status) { this.status = status; } diff --git a/acme4j-client/src/main/java/org/shredzone/acme4j/Status.java b/acme4j-client/src/main/java/org/shredzone/acme4j/Status.java new file mode 100644 index 00000000..69fdd19a --- /dev/null +++ b/acme4j-client/src/main/java/org/shredzone/acme4j/Status.java @@ -0,0 +1,55 @@ +/* + * acme4j - Java ACME client + * + * Copyright (C) 2015 Richard "Shred" Körber + * http://acme4j.shredzone.org + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + */ +package org.shredzone.acme4j; + +/** + * Status codes of challenges and authorizations. + * + * @author Richard "Shred" Körber + */ +public enum Status { + + PENDING, PROCESSING, VALID, INVALID, REVOKED, UNKNOWN; + + /** + * Parses the string and returns a corresponding Status object. + * + * @param str + * String to parse + * @return {@link Status} matching the string, or {@link Status#UNKNOWN} if there was + * no match + */ + public static Status parse(String str) { + try { + return valueOf(str.toUpperCase()); + } catch (IllegalArgumentException ex) { + return Status.UNKNOWN; + } + } + + /** + * Parses the string and returns a corresponding Status object. + * + * @param str + * String to parse + * @param def + * Default Status if str is {@code null} + * @return {@link Status} matching the string, or {@link Status#UNKNOWN} if there was + * no match, or {@code def} if the str was {@code null} + */ + public static Status parse(String str, Status def) { + return (str != null ? parse(str) : def); + } + +} diff --git a/acme4j-client/src/main/java/org/shredzone/acme4j/challenge/Challenge.java b/acme4j-client/src/main/java/org/shredzone/acme4j/challenge/Challenge.java index 73f66035..5bd0b2f6 100644 --- a/acme4j-client/src/main/java/org/shredzone/acme4j/challenge/Challenge.java +++ b/acme4j-client/src/main/java/org/shredzone/acme4j/challenge/Challenge.java @@ -18,6 +18,7 @@ import java.net.URI; import java.util.Map; import org.shredzone.acme4j.Account; +import org.shredzone.acme4j.Status; import org.shredzone.acme4j.util.ClaimBuilder; /** @@ -27,13 +28,6 @@ import org.shredzone.acme4j.util.ClaimBuilder; */ public interface Challenge extends Serializable { - /** - * Challenge status enumeration. - */ - public enum Status { - PENDING, PROCESSING, VALID, INVALID, REVOKED, UNKNOWN; - } - /** * Returns the challenge type by name (e.g. "http-01"). */ diff --git a/acme4j-client/src/main/java/org/shredzone/acme4j/challenge/GenericChallenge.java b/acme4j-client/src/main/java/org/shredzone/acme4j/challenge/GenericChallenge.java index cee4bca1..8abe97c4 100644 --- a/acme4j-client/src/main/java/org/shredzone/acme4j/challenge/GenericChallenge.java +++ b/acme4j-client/src/main/java/org/shredzone/acme4j/challenge/GenericChallenge.java @@ -30,6 +30,7 @@ import org.jose4j.jwk.JsonWebKey; import org.jose4j.jwk.JsonWebKey.OutputControlLevel; import org.jose4j.lang.JoseException; import org.shredzone.acme4j.Account; +import org.shredzone.acme4j.Status; import org.shredzone.acme4j.util.ClaimBuilder; /** @@ -58,8 +59,7 @@ public class GenericChallenge implements Challenge { @Override public Status getStatus() { - String status = get(KEY_STATUS); - return (status != null ? Status.valueOf(status.toUpperCase()) : Status.PENDING); + return Status.parse((String) get(KEY_STATUS), Status.PENDING); } @Override diff --git a/acme4j-client/src/main/java/org/shredzone/acme4j/impl/AbstractAcmeClient.java b/acme4j-client/src/main/java/org/shredzone/acme4j/impl/AbstractAcmeClient.java index 934b2059..62b8fcd6 100644 --- a/acme4j-client/src/main/java/org/shredzone/acme4j/impl/AbstractAcmeClient.java +++ b/acme4j-client/src/main/java/org/shredzone/acme4j/impl/AbstractAcmeClient.java @@ -26,6 +26,7 @@ import org.shredzone.acme4j.Account; import org.shredzone.acme4j.AcmeClient; import org.shredzone.acme4j.Authorization; import org.shredzone.acme4j.Registration; +import org.shredzone.acme4j.Status; import org.shredzone.acme4j.challenge.Challenge; import org.shredzone.acme4j.connector.Connection; import org.shredzone.acme4j.connector.Resource; @@ -373,7 +374,7 @@ public abstract class AbstractAcmeClient implements AcmeClient { */ @SuppressWarnings("unchecked") private void unmarshalAuthorization(Map json, Authorization auth) { - auth.setStatus((String) json.get("status")); + auth.setStatus(Status.parse((String) json.get("status"), Status.PENDING)); auth.setExpires((String) json.get("expires")); Map identifier = (Map) json.get("identifier"); diff --git a/acme4j-client/src/test/java/org/shredzone/acme4j/AuthorizationTest.java b/acme4j-client/src/test/java/org/shredzone/acme4j/AuthorizationTest.java index 6c8d8e0b..e61b166e 100644 --- a/acme4j-client/src/test/java/org/shredzone/acme4j/AuthorizationTest.java +++ b/acme4j-client/src/test/java/org/shredzone/acme4j/AuthorizationTest.java @@ -80,13 +80,13 @@ public class AuthorizationTest { assertThat(auth.getCombinations(), is(nullValue())); auth.setDomain("example.com"); - auth.setStatus("invalid"); + auth.setStatus(Status.INVALID); auth.setExpires("2015-12-12T17:19:36.336785823Z"); auth.setChallenges(authorization.getChallenges()); auth.setCombinations(authorization.getCombinations()); assertThat(auth.getDomain(), is("example.com")); - assertThat(auth.getStatus(), is("invalid")); + assertThat(auth.getStatus(), is(Status.INVALID)); assertThat(auth.getExpires(), is("2015-12-12T17:19:36.336785823Z")); assertThat(auth.getChallenges(), is(sameInstance(authorization.getChallenges()))); assertThat(auth.getCombinations(), is(sameInstance(authorization.getCombinations()))); diff --git a/acme4j-client/src/test/java/org/shredzone/acme4j/StatusTest.java b/acme4j-client/src/test/java/org/shredzone/acme4j/StatusTest.java new file mode 100644 index 00000000..77722898 --- /dev/null +++ b/acme4j-client/src/test/java/org/shredzone/acme4j/StatusTest.java @@ -0,0 +1,45 @@ +/* + * acme4j - Java ACME client + * + * Copyright (C) 2015 Richard "Shred" Körber + * http://acme4j.shredzone.org + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + */ +package org.shredzone.acme4j; + +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; + +import org.junit.Test; + +/** + * Unit tests for {@link Status} enumeration. + * + * @author Richard "Shred" Körber + */ +public class StatusTest { + + /** + * Test that invoking {@link Status#parse(String)} gives the correct status. + */ + @Test + public void testParse() { + for (Status s : Status.values()) { + Status parsed = Status.parse(s.name().toLowerCase()); + assertThat(parsed, is(s)); + } + + // unknown status returns UNKNOWN + assertThat(Status.parse("foo"), is(Status.UNKNOWN)); + + // null returns default value + assertThat(Status.parse(null, Status.PROCESSING), is(Status.PROCESSING)); + } + +} diff --git a/acme4j-client/src/test/java/org/shredzone/acme4j/challenge/DnsChallengeTest.java b/acme4j-client/src/test/java/org/shredzone/acme4j/challenge/DnsChallengeTest.java index bad3d9c4..30bb0e2e 100644 --- a/acme4j-client/src/test/java/org/shredzone/acme4j/challenge/DnsChallengeTest.java +++ b/acme4j-client/src/test/java/org/shredzone/acme4j/challenge/DnsChallengeTest.java @@ -22,7 +22,7 @@ import java.security.KeyPair; import org.junit.Test; import org.shredzone.acme4j.Account; -import org.shredzone.acme4j.challenge.Challenge.Status; +import org.shredzone.acme4j.Status; import org.shredzone.acme4j.util.ClaimBuilder; import org.shredzone.acme4j.util.TestUtils; diff --git a/acme4j-client/src/test/java/org/shredzone/acme4j/challenge/GenericChallengeTest.java b/acme4j-client/src/test/java/org/shredzone/acme4j/challenge/GenericChallengeTest.java index 53153726..ec38784e 100644 --- a/acme4j-client/src/test/java/org/shredzone/acme4j/challenge/GenericChallengeTest.java +++ b/acme4j-client/src/test/java/org/shredzone/acme4j/challenge/GenericChallengeTest.java @@ -32,7 +32,7 @@ import org.jose4j.jwk.JsonWebKey; import org.jose4j.jwk.JsonWebKey.OutputControlLevel; import org.jose4j.lang.JoseException; import org.junit.Test; -import org.shredzone.acme4j.challenge.Challenge.Status; +import org.shredzone.acme4j.Status; import org.shredzone.acme4j.util.ClaimBuilder; import org.shredzone.acme4j.util.TestUtils; @@ -164,7 +164,7 @@ public class GenericChallengeTest { assertThat(testChallenge, not(sameInstance((Challenge) originalChallenge))); assertThat(testChallenge, is(instanceOf(HttpChallenge.class))); assertThat(testChallenge.getType(), is(HttpChallenge.TYPE)); - assertThat(testChallenge.getStatus(), is(Challenge.Status.PENDING)); + assertThat(testChallenge.getStatus(), is(Status.PENDING)); assertThat(((HttpChallenge )testChallenge).getToken(), is("rSoI9JpyvFi-ltdnBW0W1DjKstzG7cHixjzcOjwzAEQ")); } diff --git a/acme4j-client/src/test/java/org/shredzone/acme4j/challenge/HttpChallengeTest.java b/acme4j-client/src/test/java/org/shredzone/acme4j/challenge/HttpChallengeTest.java index 1e5b9337..0e7d2be1 100644 --- a/acme4j-client/src/test/java/org/shredzone/acme4j/challenge/HttpChallengeTest.java +++ b/acme4j-client/src/test/java/org/shredzone/acme4j/challenge/HttpChallengeTest.java @@ -22,7 +22,7 @@ import java.security.KeyPair; import org.junit.Test; import org.shredzone.acme4j.Account; -import org.shredzone.acme4j.challenge.Challenge.Status; +import org.shredzone.acme4j.Status; import org.shredzone.acme4j.util.ClaimBuilder; import org.shredzone.acme4j.util.TestUtils; diff --git a/acme4j-client/src/test/java/org/shredzone/acme4j/impl/AbstractAcmeClientTest.java b/acme4j-client/src/test/java/org/shredzone/acme4j/impl/AbstractAcmeClientTest.java index c215f6e6..b17b3d7f 100644 --- a/acme4j-client/src/test/java/org/shredzone/acme4j/impl/AbstractAcmeClientTest.java +++ b/acme4j-client/src/test/java/org/shredzone/acme4j/impl/AbstractAcmeClientTest.java @@ -31,8 +31,8 @@ import org.junit.Test; import org.shredzone.acme4j.Account; import org.shredzone.acme4j.Authorization; import org.shredzone.acme4j.Registration; +import org.shredzone.acme4j.Status; import org.shredzone.acme4j.challenge.Challenge; -import org.shredzone.acme4j.challenge.Challenge.Status; import org.shredzone.acme4j.challenge.DnsChallenge; import org.shredzone.acme4j.challenge.GenericChallenge; import org.shredzone.acme4j.challenge.HttpChallenge; @@ -186,7 +186,7 @@ public class AbstractAcmeClientTest { client.newAuthorization(testAccount, auth); assertThat(auth.getDomain(), is("example.org")); - assertThat(auth.getStatus(), is("pending")); + assertThat(auth.getStatus(), is(Status.PENDING)); assertThat(auth.getExpires(), is(nullValue())); assertThat(auth.getLocation(), is(locationUri)); @@ -230,7 +230,7 @@ public class AbstractAcmeClientTest { client.updateAuthorization(auth); assertThat(auth.getDomain(), is("example.org")); - assertThat(auth.getStatus(), is("valid")); + assertThat(auth.getStatus(), is(Status.VALID)); assertThat(auth.getExpires(), is("2015-03-01")); assertThat(auth.getLocation(), is(locationUri)); diff --git a/acme4j-example/src/main/java/org/shredzone/acme4j/ClientTest.java b/acme4j-example/src/main/java/org/shredzone/acme4j/ClientTest.java index fb873f49..5a205200 100644 --- a/acme4j-example/src/main/java/org/shredzone/acme4j/ClientTest.java +++ b/acme4j-example/src/main/java/org/shredzone/acme4j/ClientTest.java @@ -26,7 +26,6 @@ import java.util.Collection; import javax.swing.JOptionPane; -import org.shredzone.acme4j.challenge.Challenge; import org.shredzone.acme4j.challenge.HttpChallenge; import org.shredzone.acme4j.exception.AcmeConflictException; import org.shredzone.acme4j.exception.AcmeException; @@ -157,8 +156,8 @@ public class ClientTest { // Poll for the challenge to complete int attempts = 10; - while (challenge.getStatus() != Challenge.Status.VALID && attempts-- > 0) { - if (challenge.getStatus() == Challenge.Status.INVALID) { + while (challenge.getStatus() != Status.VALID && attempts-- > 0) { + if (challenge.getStatus() == Status.INVALID) { LOG.error("Challenge failed... Giving up."); return; }