Extract Status enum. Make Authorization use it.

pull/17/merge
Richard Körber 2015-12-21 00:28:39 +01:00
parent 41dabd0cfd
commit 8168e6efc7
12 changed files with 119 additions and 25 deletions

View File

@ -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<Challenge> challenges;
private List<List<Challenge>> 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;
}

View File

@ -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);
}
}

View File

@ -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").
*/

View File

@ -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

View File

@ -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<String, Object> 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<String, Object> identifier = (Map<String, Object>) json.get("identifier");

View File

@ -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())));

View File

@ -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));
}
}

View File

@ -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;

View File

@ -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"));
}

View File

@ -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;

View File

@ -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));

View File

@ -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;
}