mirror of https://github.com/shred/acme4j
Use functional interface for ResourceIterator
parent
c1b677f310
commit
eedc2ae68c
|
@ -125,12 +125,7 @@ public class Registration extends AcmeResource {
|
||||||
public Iterator<Authorization> getAuthorizations() throws AcmeException {
|
public Iterator<Authorization> getAuthorizations() throws AcmeException {
|
||||||
LOG.debug("getAuthorizations");
|
LOG.debug("getAuthorizations");
|
||||||
load();
|
load();
|
||||||
return new ResourceIterator<Authorization>(getSession(), KEY_AUTHORIZATIONS, authorizations) {
|
return new ResourceIterator<>(getSession(), KEY_AUTHORIZATIONS, authorizations, Authorization::bind);
|
||||||
@Override
|
|
||||||
protected Authorization create(Session session, URI uri) {
|
|
||||||
return Authorization.bind(session, uri);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -147,12 +142,7 @@ public class Registration extends AcmeResource {
|
||||||
public Iterator<Certificate> getCertificates() throws AcmeException {
|
public Iterator<Certificate> getCertificates() throws AcmeException {
|
||||||
LOG.debug("getCertificates");
|
LOG.debug("getCertificates");
|
||||||
load();
|
load();
|
||||||
return new ResourceIterator<Certificate>(getSession(), KEY_CERTIFICATES, certificates) {
|
return new ResourceIterator<>(getSession(), KEY_CERTIFICATES, certificates, Certificate::bind);
|
||||||
@Override
|
|
||||||
protected Certificate create(Session session, URI uri) {
|
|
||||||
return Certificate.bind(session, uri);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -20,6 +20,7 @@ import java.util.Deque;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.NoSuchElementException;
|
import java.util.NoSuchElementException;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
import java.util.function.BiFunction;
|
||||||
|
|
||||||
import org.shredzone.acme4j.AcmeResource;
|
import org.shredzone.acme4j.AcmeResource;
|
||||||
import org.shredzone.acme4j.Session;
|
import org.shredzone.acme4j.Session;
|
||||||
|
@ -34,11 +35,12 @@ import org.shredzone.acme4j.util.JSON;
|
||||||
* @param <T>
|
* @param <T>
|
||||||
* {@link AcmeResource} type to iterate over
|
* {@link AcmeResource} type to iterate over
|
||||||
*/
|
*/
|
||||||
public abstract class ResourceIterator<T extends AcmeResource> implements Iterator<T> {
|
public class ResourceIterator<T extends AcmeResource> implements Iterator<T> {
|
||||||
|
|
||||||
private final Session session;
|
private final Session session;
|
||||||
private final String field;
|
private final String field;
|
||||||
private final Deque<URI> uriList = new ArrayDeque<>();
|
private final Deque<URI> uriList = new ArrayDeque<>();
|
||||||
|
private final BiFunction<Session, URI, T> creator;
|
||||||
private boolean eol = false;
|
private boolean eol = false;
|
||||||
private URI nextUri;
|
private URI nextUri;
|
||||||
|
|
||||||
|
@ -51,11 +53,15 @@ public abstract class ResourceIterator<T extends AcmeResource> implements Iterat
|
||||||
* Field name to be used in the JSON response
|
* Field name to be used in the JSON response
|
||||||
* @param start
|
* @param start
|
||||||
* URI of the first JSON array, may be {@code null} for an empty iterator
|
* URI of the first JSON array, may be {@code null} for an empty iterator
|
||||||
|
* @param creator
|
||||||
|
* Creator for an {@link AcmeResource} that is bound to the given
|
||||||
|
* {@link Session} and {@link URI}.
|
||||||
*/
|
*/
|
||||||
public ResourceIterator(Session session, String field, URI start) {
|
public ResourceIterator(Session session, String field, URI start, BiFunction<Session, URI, T> creator) {
|
||||||
this.session = Objects.requireNonNull(session, "session");
|
this.session = Objects.requireNonNull(session, "session");
|
||||||
this.field = Objects.requireNonNull(field, "field");
|
this.field = Objects.requireNonNull(field, "field");
|
||||||
this.nextUri = start;
|
this.nextUri = start;
|
||||||
|
this.creator = Objects.requireNonNull(creator, "creator");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -101,7 +107,7 @@ public abstract class ResourceIterator<T extends AcmeResource> implements Iterat
|
||||||
throw new NoSuchElementException("no more " + field);
|
throw new NoSuchElementException("no more " + field);
|
||||||
}
|
}
|
||||||
|
|
||||||
return create(session, next);
|
return creator.apply(session, next);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -112,18 +118,6 @@ public abstract class ResourceIterator<T extends AcmeResource> implements Iterat
|
||||||
throw new UnsupportedOperationException("cannot remove " + field);
|
throw new UnsupportedOperationException("cannot remove " + field);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a new {@link AcmeResource} object by binding it to the {@link Session} and
|
|
||||||
* using the given {@link URI}.
|
|
||||||
*
|
|
||||||
* @param session
|
|
||||||
* {@link Session} to bind the object to
|
|
||||||
* @param uri
|
|
||||||
* {@link URI} of the resource
|
|
||||||
* @return Created object
|
|
||||||
*/
|
|
||||||
protected abstract T create(Session session, URI uri);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fetches the next batch of URIs. Handles exceptions. Does nothing if there is no
|
* Fetches the next batch of URIs. Handles exceptions. Does nothing if there is no
|
||||||
* URI of the next batch.
|
* URI of the next batch.
|
||||||
|
|
|
@ -31,8 +31,8 @@ import org.shredzone.acme4j.Authorization;
|
||||||
import org.shredzone.acme4j.Session;
|
import org.shredzone.acme4j.Session;
|
||||||
import org.shredzone.acme4j.exception.AcmeException;
|
import org.shredzone.acme4j.exception.AcmeException;
|
||||||
import org.shredzone.acme4j.provider.TestableConnectionProvider;
|
import org.shredzone.acme4j.provider.TestableConnectionProvider;
|
||||||
import org.shredzone.acme4j.util.JSONBuilder;
|
|
||||||
import org.shredzone.acme4j.util.JSON;
|
import org.shredzone.acme4j.util.JSON;
|
||||||
|
import org.shredzone.acme4j.util.JSONBuilder;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unit test for {@link ResourceIterator}.
|
* Unit test for {@link ResourceIterator}.
|
||||||
|
@ -167,12 +167,7 @@ public class ResourceIteratorTest {
|
||||||
|
|
||||||
provider.close();
|
provider.close();
|
||||||
|
|
||||||
return new ResourceIterator<Authorization>(session, TYPE, first) {
|
return new ResourceIterator<>(session, TYPE, first, Authorization::bind);
|
||||||
@Override
|
|
||||||
protected Authorization create(Session session, URI uri) {
|
|
||||||
return Authorization.bind(session, uri);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue