Add method to update registration.

pull/30/head
Richard Körber 2016-07-07 00:08:01 +02:00
parent 3403c69985
commit 1adfb8c9df
3 changed files with 115 additions and 2 deletions

View File

@ -16,9 +16,11 @@ package org.shredzone.acme4j;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URISyntaxException;
import java.security.KeyPair;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
@ -85,6 +87,37 @@ public class Registration extends AcmeResource {
return Collections.unmodifiableList(contacts);
}
/**
* Updates the registration to the current account status.
*/
public void update() throws AcmeException {
LOG.debug("update");
try (Connection conn = getSession().provider().connect()) {
ClaimBuilder claims = new ClaimBuilder();
claims.putResource("reg");
int rc = conn.sendSignedRequest(getLocation(), claims, getSession());
if (rc != HttpURLConnection.HTTP_CREATED && rc != HttpURLConnection.HTTP_ACCEPTED) {
conn.throwAcmeException();
}
Map<String, Object> json = conn.readJsonResponse();
unmarshal(json);
URI location = conn.getLocation();
if (location != null) {
setLocation(conn.getLocation());
}
URI tos = conn.getLink("terms-of-service");
if (tos != null) {
this.agreement = tos;
}
} catch (IOException ex) {
throw new AcmeNetworkException(ex);
}
}
/**
* Authorizes a domain. The domain is associated with this registration.
*
@ -231,6 +264,34 @@ public class Registration extends AcmeResource {
}
}
/**
* Sets registration properties according to the given JSON data.
*
* @param json
* JSON data
*/
@SuppressWarnings("unchecked")
private void unmarshal(Map<String, Object> json) {
if (json.containsKey("agreement")) {
try {
this.agreement = new URI((String) json.get("agreement"));
} catch (ClassCastException | URISyntaxException ex) {
throw new AcmeProtocolException("Illegal agreement URI", ex);
}
}
if (json.containsKey("contact")) {
contacts.clear();
for (Object c : (Collection<Object>) json.get("contact")) {
try {
contacts.add(new URI((String) c));
} catch (ClassCastException | URISyntaxException ex) {
throw new AcmeProtocolException("Illegal contact URI", ex);
}
}
}
}
/**
* Modifies the registration data of the account.
*

View File

@ -48,6 +48,7 @@ public class RegistrationTest {
private URI resourceUri = URI.create("http://example.com/acme/resource");
private URI locationUri = URI.create("http://example.com/acme/registration");
private URI agreementUri = URI.create("http://example.com/agreement.pdf");
/**
* Test getters. Make sure object cannot be modified.
@ -68,6 +69,50 @@ public class RegistrationTest {
}
}
/**
* Test that a registration can be updated.
*/
@Test
public void testUpdateRegistration() throws AcmeException, IOException {
TestableConnectionProvider provider = new TestableConnectionProvider() {
@Override
public int sendSignedRequest(URI uri, ClaimBuilder claims, Session session) {
assertThat(uri, is(locationUri));
assertThat(claims.toString(), sameJSONAs(getJson("updateRegistration")));
assertThat(session, is(notNullValue()));
return HttpURLConnection.HTTP_ACCEPTED;
}
@Override
public Map<String, Object> readJsonResponse() {
return getJsonAsMap("updateRegistrationResponse");
}
@Override
public URI getLocation() {
return locationUri;
}
@Override
public URI getLink(String relation) {
switch(relation) {
case "terms-of-service": return agreementUri;
default: return null;
}
}
};
Registration registration = new Registration(provider.createSession(), locationUri);
registration.update();
assertThat(registration.getLocation(), is(locationUri));
assertThat(registration.getAgreement(), is(agreementUri));
assertThat(registration.getContacts(), hasSize(1));
assertThat(registration.getContacts().get(0), is(URI.create("mailto:foo2@example.com")));
provider.close();
}
/**
* Test that a new {@link Authorization} can be created.
*/

View File

@ -27,6 +27,13 @@ recoverRegistration = \
"base":"https://example.com/acme/some-location",\
"contact":["mailto:foo@example.com"]}
updateRegistration = \
{"resource":"reg"}
updateRegistrationResponse = \
{"agreement":"http://example.com/agreement.pdf",\
"contact":["mailto:foo2@example.com"]}
newAuthorizationRequest = \
{"resource":"new-authz",\
"identifier":{"type":"dns","value":"example.org"}}