Bind on RegistrationBuilder.create

pull/30/head
Richard Körber 2016-07-21 21:32:22 +02:00
parent 434b349d20
commit 13c2ba9169
5 changed files with 11 additions and 30 deletions

View File

@ -34,29 +34,8 @@ import org.slf4j.LoggerFactory;
public class RegistrationBuilder {
private static final Logger LOG = LoggerFactory.getLogger(RegistrationBuilder.class);
private final Session session;
private List<URI> contacts = new ArrayList<>();
/**
* Creates a new instance of {@link RegistrationBuilder} and binds it to the
* {@link Session}.
*
* @param session
* {@link Session} to be used
*/
public static RegistrationBuilder bind(Session session) {
return new RegistrationBuilder(session);
}
/**
* Creates a new {@link RegistrationBuilder}.
*
* @param session {@link Session} to bind to
*/
private RegistrationBuilder(Session session) {
this.session = session;
}
/**
* Add a contact URI to the list of contacts.
*
@ -86,13 +65,15 @@ public class RegistrationBuilder {
/**
* Creates a new account.
*
* @param session
* {@link Session} to be used for registration
* @return {@link Registration} referring to the new account
* @throws AcmeConflictException
* if there is already an account for the connection's key pair.
* {@link AcmeConflictException#getLocation()} contains the registration's
* location URI.
*/
public Registration create() throws AcmeException {
public Registration create(Session session) throws AcmeException {
LOG.debug("create");
try (Connection conn = session.provider().connect()) {

View File

@ -65,10 +65,10 @@ public class RegistrationBuilderTest {
provider.putTestResource(Resource.NEW_REG, resourceUri);
RegistrationBuilder builder = RegistrationBuilder.bind(provider.createSession());
RegistrationBuilder builder = new RegistrationBuilder();
builder.addContact("mailto:foo@example.com");
Registration registration = builder.create();
Registration registration = builder.create(provider.createSession());
assertThat(registration.getLocation(), is(locationUri));
assertThat(registration.getAgreement(), is(agreementUri));

View File

@ -87,7 +87,7 @@ public class ClientTest {
// Register a new user
Registration reg = null;
try {
reg = RegistrationBuilder.bind(session).create();
reg = new RegistrationBuilder().create(session);
LOG.info("Registered a new user, URI: " + reg.getLocation());
} catch (AcmeConflictException ex) {
reg = Registration.bind(session, ex.getLocation());

View File

@ -28,7 +28,7 @@ You must know your account's location URI. Use a `RegistrationBuilder` if you do
Registration registration;
try {
// Try to create a new Registration...
registration = RegistrationBuilder.bind(session).create();
registration = new RegistrationBuilder().create(session);
} catch (AcmeConflictException ex) {
// It failed because your key was already registered.
// Retrieve the registration location URI from the exception.

View File

@ -2,13 +2,13 @@
If it is the first time you connect to the ACME server, you need to register your account key.
To do so, bind a `RegistrationBuilder`, optionally add some contact information, then invoke `create()`. If the account was successfully created, you will get a `Registration` object in return. Invoking its `getLocation()` method will return the location URI of your account. You should store it somewhere, because you will need it later. Unlike your key pair, the location is a public information that does not need security precautions.
To do so, create a `RegistrationBuilder`, optionally add some contact information, then invoke `create()`. If the account was successfully created, you will get a `Registration` object in return. Invoking its `getLocation()` method will return the location URI of your account. You should store it somewhere, because you will need it later. Unlike your key pair, the location is a public information that does not need security precautions.
```java
RegistrationBuilder builder = RegistrationBuilder.bind(session);
RegistrationBuilder builder = new RegistrationBuilder();
builder.addContact("mailto:acme@example.com");
Registration registration = builder.create();
Registration registration = builder.create(session);
URI accountLocationUri = registration.getLocation();
```
@ -20,7 +20,7 @@ The following example will create a new `Registration` and restore an existing `
```java
Registration registration;
try {
registration = RegistrationBuilder.bind(session).create();
registration = new RegistrationBuilder().create(session);
} catch (AcmeConflictException ex) {
registration = Registration.bind(session, ex.getLocation());
}