- java.lang.Object
-
- org.shredzone.acme4j.connector.DefaultConnection
-
- All Implemented Interfaces:
AutoCloseable
,Connection
public class DefaultConnection extends Object implements Connection
Default implementation ofConnection
. It communicates with the ACME server via HTTP, with a client that is provided by the givenHttpConnector
.
-
-
Field Summary
Fields Modifier and Type Field Description protected HttpClient
httpClient
protected HttpConnector
httpConnector
protected HttpResponse<InputStream>
lastResponse
-
Constructor Summary
Constructors Constructor Description DefaultConnection(HttpConnector httpConnector)
Creates a newDefaultConnection
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
Closes theConnection
, releasing all resources.Optional<ZonedDateTime>
getExpiration()
Returns the expiration date of the resource, if present.Optional<ZonedDateTime>
getLastModified()
Returns the content of the last-modified header, if present.Collection<URL>
getLinks(String relation)
Gets one or more relation links from the header.URL
getLocation()
Gets a location from theLocation
header.Optional<String>
getNonce()
Gets the nonce from the nonce header.Optional<Instant>
getRetryAfter()
Returns the Retry-After header if present.List<X509Certificate>
readCertificates()
Reads a certificate and its chain of issuers.JSON
readJsonResponse()
Reads a server response as JSON object.void
resetNonce(Session session)
Resets the session nonce, by fetching a new one.int
sendCertificateRequest(URL url, Login login)
Sends a signed POST-as-GET request for a certificate resource.int
sendRequest(URL url, Session session, ZonedDateTime ifModifiedSince)
Sends a simple GET request.protected void
sendRequest(Session session, URL url, Consumer<HttpRequest.Builder> body)
Sends a HTTP request via http client.int
sendSignedPostAsGetRequest(URL url, Login login)
Sends a signed POST-as-GET request.int
sendSignedRequest(URL url, JSONBuilder claims, Login login)
Sends a signed POST request.int
sendSignedRequest(URL url, JSONBuilder claims, Session session, KeyPair keypair)
Sends a signed POST request.protected int
sendSignedRequest(URL url, JSONBuilder claims, Session session, KeyPair keypair, URL accountLocation, String accept)
Sends a signed POST request.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.shredzone.acme4j.connector.Connection
handleRetryAfter
-
-
-
-
Field Detail
-
httpConnector
protected final HttpConnector httpConnector
-
httpClient
protected final HttpClient httpClient
-
lastResponse
@Nullable protected HttpResponse<InputStream> lastResponse
-
-
Constructor Detail
-
DefaultConnection
public DefaultConnection(HttpConnector httpConnector)
Creates a newDefaultConnection
.- Parameters:
httpConnector
-HttpConnector
to be used for HTTP connections
-
-
Method Detail
-
resetNonce
public void resetNonce(Session session) throws AcmeException
Description copied from interface:Connection
Resets the session nonce, by fetching a new one.- Specified by:
resetNonce
in interfaceConnection
- Parameters:
session
-Session
instance to fetch a nonce for- Throws:
AcmeException
-
sendRequest
public int sendRequest(URL url, Session session, @Nullable ZonedDateTime ifModifiedSince) throws AcmeException
Description copied from interface:Connection
Sends a simple GET request.If the response code was not HTTP status 200, an
AcmeException
matching the error is raised.- Specified by:
sendRequest
in interfaceConnection
- Parameters:
url
-URL
to send the request to.session
-Session
instance to be used for trackingifModifiedSince
-ZonedDateTime
to be sent as "If-Modified-Since" header, ornull
if this header is not to be used- Returns:
- HTTP status that was returned
- Throws:
AcmeException
-
sendCertificateRequest
public int sendCertificateRequest(URL url, Login login) throws AcmeException
Description copied from interface:Connection
Sends a signed POST-as-GET request for a certificate resource. Requires aLogin
for the session andKeyPair
. TheLogin
account location is sent in a "kid" protected header.If the server does not return a 200 class status code, an
AcmeException
is raised matching the error.- Specified by:
sendCertificateRequest
in interfaceConnection
- Parameters:
url
-URL
to send the request to.login
-Login
instance to be used for signing and tracking.- Returns:
- HTTP 200 class status that was returned
- Throws:
AcmeException
-
sendSignedPostAsGetRequest
public int sendSignedPostAsGetRequest(URL url, Login login) throws AcmeException
Description copied from interface:Connection
Sends a signed POST-as-GET request. Requires aLogin
for the session andKeyPair
. TheLogin
account location is sent in a "kid" protected header.If the server does not return a 200 class status code, an
AcmeException
is raised matching the error.- Specified by:
sendSignedPostAsGetRequest
in interfaceConnection
- Parameters:
url
-URL
to send the request to.login
-Login
instance to be used for signing and tracking.- Returns:
- HTTP 200 class status that was returned
- Throws:
AcmeException
-
sendSignedRequest
public int sendSignedRequest(URL url, JSONBuilder claims, Login login) throws AcmeException
Description copied from interface:Connection
Sends a signed POST request. Requires aLogin
for the session andKeyPair
. TheLogin
account location is sent in a "kid" protected header.If the server does not return a 200 class status code, an
AcmeException
is raised matching the error.- Specified by:
sendSignedRequest
in interfaceConnection
- Parameters:
url
-URL
to send the request to.claims
-JSONBuilder
containing claims.login
-Login
instance to be used for signing and tracking.- Returns:
- HTTP 200 class status that was returned
- Throws:
AcmeException
-
sendSignedRequest
public int sendSignedRequest(URL url, JSONBuilder claims, Session session, KeyPair keypair) throws AcmeException
Description copied from interface:Connection
Sends a signed POST request. Only requires aSession
. TheKeyPair
is sent in a "jwk" protected header field.If the server does not return a 200 class status code, an
AcmeException
is raised matching the error.- Specified by:
sendSignedRequest
in interfaceConnection
- Parameters:
url
-URL
to send the request to.claims
-JSONBuilder
containing claims.session
-Session
instance to be used for tracking.keypair
-KeyPair
to be used for signing.- Returns:
- HTTP 200 class status that was returned
- Throws:
AcmeException
-
readJsonResponse
public JSON readJsonResponse() throws AcmeException
Description copied from interface:Connection
Reads a server response as JSON object.- Specified by:
readJsonResponse
in interfaceConnection
- Returns:
- The JSON response.
- Throws:
AcmeException
-
readCertificates
public List<X509Certificate> readCertificates() throws AcmeException
Description copied from interface:Connection
Reads a certificate and its chain of issuers.- Specified by:
readCertificates
in interfaceConnection
- Returns:
- List of X.509 certificate and chain that was read.
- Throws:
AcmeException
-
getNonce
public Optional<String> getNonce()
Description copied from interface:Connection
Gets the nonce from the nonce header.- Specified by:
getNonce
in interfaceConnection
- Returns:
- Base64 encoded nonce, or empty if no nonce header was set
-
getLocation
public URL getLocation()
Description copied from interface:Connection
Gets a location from theLocation
header.Relative links are resolved against the last request's URL.
- Specified by:
getLocation
in interfaceConnection
- Returns:
- Location
URL
-
getLastModified
public Optional<ZonedDateTime> getLastModified()
Description copied from interface:Connection
Returns the content of the last-modified header, if present.- Specified by:
getLastModified
in interfaceConnection
- Returns:
- Date in the Last-Modified header, or empty if the server did not provide this information.
-
getExpiration
public Optional<ZonedDateTime> getExpiration()
Description copied from interface:Connection
Returns the expiration date of the resource, if present.- Specified by:
getExpiration
in interfaceConnection
- Returns:
- Expiration date, either from the Cache-Control or Expires header. If empty, the server did not provide an expiration date, or forbid caching.
-
getLinks
public Collection<URL> getLinks(String relation)
Description copied from interface:Connection
Gets one or more relation links from the header. The result is expected to be a URL.Relative links are resolved against the last request's URL.
- Specified by:
getLinks
in interfaceConnection
- Parameters:
relation
- Link relation- Returns:
- Collection of links. Empty if there was no such relation.
-
close
public void close()
Description copied from interface:Connection
Closes theConnection
, releasing all resources.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceConnection
-
sendRequest
protected void sendRequest(Session session, URL url, Consumer<HttpRequest.Builder> body) throws IOException
Sends a HTTP request via http client. This is the central method to be used for sending. It will create aHttpRequest
by using the request builder, configure commnon headers, and then send the request viaHttpClient
.- Parameters:
session
-Session
to be used for sendingurl
- TargetURL
body
- Callback that completes theHttpRequest.Builder
with the request body (e.g. HTTP method, request body, more headers).- Throws:
IOException
-
sendSignedRequest
protected int sendSignedRequest(URL url, @Nullable JSONBuilder claims, Session session, KeyPair keypair, @Nullable URL accountLocation, String accept) throws AcmeException
Sends a signed POST request.- Parameters:
url
-URL
to send the request to.claims
-JSONBuilder
containing claims.null
for POST-as-GET request.session
-Session
instance to be used for signing and trackingkeypair
-KeyPair
to be used for signingaccountLocation
- If set, the account location is set as "kid" header. Ifnull
, the public key is set as "jwk" header.accept
- Accept header- Returns:
- HTTP 200 class status that was returned
- Throws:
AcmeException
-
getRetryAfter
public Optional<Instant> getRetryAfter()
Description copied from interface:Connection
Returns the Retry-After header if present.- Specified by:
getRetryAfter
in interfaceConnection
-
-