diff --git a/acme4j-client/src/main/java/org/shredzone/acme4j/RegistrationBuilder.java b/acme4j-client/src/main/java/org/shredzone/acme4j/RegistrationBuilder.java index dbbecf50..c3a12754 100644 --- a/acme4j-client/src/main/java/org/shredzone/acme4j/RegistrationBuilder.java +++ b/acme4j-client/src/main/java/org/shredzone/acme4j/RegistrationBuilder.java @@ -105,16 +105,11 @@ public class RegistrationBuilder { } int rc = conn.sendSignedRequest(session.resourceUri(Resource.NEW_REG), claims, session); - if (rc != HttpURLConnection.HTTP_CREATED && rc != HttpURLConnection.HTTP_CONFLICT) { + if (rc != HttpURLConnection.HTTP_CREATED) { conn.throwAcmeException(); } URI location = conn.getLocation(); - - if (rc == HttpURLConnection.HTTP_CONFLICT) { - throw new AcmeConflictException("Account is already registered", location); - } - URI tos = conn.getLink("terms-of-service"); return new Registration(session, location, tos); diff --git a/acme4j-client/src/main/java/org/shredzone/acme4j/connector/DefaultConnection.java b/acme4j-client/src/main/java/org/shredzone/acme4j/connector/DefaultConnection.java index d5298c41..efb1bdd8 100644 --- a/acme4j-client/src/main/java/org/shredzone/acme4j/connector/DefaultConnection.java +++ b/acme4j-client/src/main/java/org/shredzone/acme4j/connector/DefaultConnection.java @@ -40,6 +40,7 @@ import org.jose4j.jwk.PublicJsonWebKey; import org.jose4j.jws.JsonWebSignature; import org.jose4j.lang.JoseException; import org.shredzone.acme4j.Session; +import org.shredzone.acme4j.exception.AcmeConflictException; import org.shredzone.acme4j.exception.AcmeException; import org.shredzone.acme4j.exception.AcmeProtocolException; import org.shredzone.acme4j.exception.AcmeRateLimitExceededException; @@ -335,6 +336,10 @@ public class DefaultConnection implements Connection { detail = "general problem"; } + if (conn.getResponseCode() == HttpURLConnection.HTTP_CONFLICT) { + throw new AcmeConflictException(detail, getLocation()); + } + if (type == null) { throw new AcmeException(detail); } diff --git a/acme4j-client/src/test/java/org/shredzone/acme4j/connector/DefaultConnectionTest.java b/acme4j-client/src/test/java/org/shredzone/acme4j/connector/DefaultConnectionTest.java index 146b4625..c6686a3b 100644 --- a/acme4j-client/src/test/java/org/shredzone/acme4j/connector/DefaultConnectionTest.java +++ b/acme4j-client/src/test/java/org/shredzone/acme4j/connector/DefaultConnectionTest.java @@ -170,7 +170,7 @@ public class DefaultConnectionTest { */ @Test public void testGetLink() throws Exception { - Map> headers = new HashMap>(); + Map> headers = new HashMap<>(); headers.put("Content-Type", Arrays.asList("application/json")); headers.put("Location", Arrays.asList("https://example.com/acme/reg/asdf")); headers.put("Link", Arrays.asList( @@ -267,7 +267,7 @@ public class DefaultConnectionTest { } verify(mockUrlConnection, atLeastOnce()).getHeaderField("Content-Type"); - verify(mockUrlConnection).getResponseCode(); + verify(mockUrlConnection, atLeastOnce()).getResponseCode(); verify(mockUrlConnection).getErrorStream(); verifyNoMoreInteractions(mockUrlConnection); } @@ -276,14 +276,16 @@ public class DefaultConnectionTest { * Test if an {@link AcmeServerException} is thrown on another problem. */ @Test - public void testOtherThrowException() { + public void testOtherThrowException() throws IOException { when(mockUrlConnection.getHeaderField("Content-Type")) .thenReturn("application/problem+json"); + when(mockUrlConnection.getResponseCode()) + .thenReturn(HttpURLConnection.HTTP_INTERNAL_ERROR); try (DefaultConnection conn = new DefaultConnection(mockHttpConnection) { @Override public Map readJsonResponse() { - Map result = new HashMap(); + Map result = new HashMap<>(); result.put("type", "urn:zombie:error:apocalypse"); result.put("detail", "Zombie apocalypse in progress"); return result; @@ -301,6 +303,7 @@ public class DefaultConnectionTest { } verify(mockUrlConnection).getHeaderField("Content-Type"); + verify(mockUrlConnection, atLeastOnce()).getResponseCode(); verifyNoMoreInteractions(mockUrlConnection); } @@ -308,14 +311,16 @@ public class DefaultConnectionTest { * Test if an {@link AcmeException} is thrown if there is no error type. */ @Test - public void testNoTypeThrowException() { + public void testNoTypeThrowException() throws IOException { when(mockUrlConnection.getHeaderField("Content-Type")) .thenReturn("application/problem+json"); + when(mockUrlConnection.getResponseCode()) + .thenReturn(HttpURLConnection.HTTP_INTERNAL_ERROR); try (DefaultConnection conn = new DefaultConnection(mockHttpConnection) { @Override public Map readJsonResponse() { - return new HashMap(); + return new HashMap<>(); }; }) { conn.conn = mockUrlConnection; @@ -328,6 +333,7 @@ public class DefaultConnectionTest { } verify(mockUrlConnection).getHeaderField("Content-Type"); + verify(mockUrlConnection, atLeastOnce()).getResponseCode(); verifyNoMoreInteractions(mockUrlConnection); }