Class WebSeedClient
- java.lang.Object
-
- io.github.pr0methean.betterrandom.seed.WebSeedClient
-
- All Implemented Interfaces:
SeedGenerator
,Serializable
- Direct Known Subclasses:
AnuQuantumSeedClient
,RandomDotOrgAnonymousClient
,RandomDotOrgApi2Client
public abstract class WebSeedClient extends Object implements SeedGenerator
ASeedGenerator
that is a client for a Web random-number service. Contains many methods for parsing JSON responses.- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description protected static Clock
CLOCK
Measures the retry delay.protected Instant
earliestNextAttempt
The earliest time we'll try again if there's been a previous IOE, or when the server requests throttling.protected static org.json.simple.parser.JSONParser
JSON_PARSER
Made available to parse JSON responses.protected Lock
lock
Held while downloading, so that two requests to the same server won't be pending at the same time.protected String
userAgent
The value for the HTTP User-Agent header.-
Fields inherited from interface io.github.pr0methean.betterrandom.seed.SeedGenerator
EMPTY_SEED
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
WebSeedClient()
protected
WebSeedClient(WebSeedClientConfiguration webSeedClientConfiguration)
protected
WebSeedClient(Proxy proxy, SSLSocketFactory socketFactory, boolean useRetryDelay)
Deprecated.Use one of the other overloads, which allow specifying the delay before retry.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected static <T> T
checkedGetObject(org.json.simple.JSONObject parent, String key, Class<T> outputClass)
Reads a field value from a JSON object and checks that it is the correct type.protected static int
divideRoundingUp(int dividend, int divisor)
protected abstract void
downloadBytes(HttpURLConnection connection, byte[] seed, int offset, int length)
Performs a single request for random bytes.boolean
equals(Object o)
void
generateSeed(byte[] seed)
Generates a seed value for a random number generator in an existing array.protected abstract URL
getConnectionUrl(int numBytes)
protected abstract int
getMaxRequestSize()
Returns the maximum number of bytes that can be obtained with one request to the service.protected Proxy
getProxy()
The proxy to use with this server, or null to use the JVM default.protected static BufferedReader
getResponseReader(HttpURLConnection connection)
Creates aBufferedReader
reading the response from the givenHttpURLConnection
as UTF-8.protected long
getRetryDelayMs()
Wait this many milliseconds before trying again after an IOException.protected SSLSocketFactory
getSocketFactory()
The SSLSocketFactory to use with this server.int
hashCode()
boolean
isWorthTrying()
Returns true if we cannot determine quickly (i.e.protected static int
modRange1ToM(int dividend, int modulus)
protected HttpsURLConnection
openConnection(URL url)
Opens anHttpsURLConnection
that will make a GET request to the given URL using this seed generator's currentProxy
,SeedGenerator
and User-Agent string, with the headerContent-Type: application/json
.protected static org.json.simple.JSONObject
parseJsonResponse(HttpURLConnection connection)
Parses the response from the givenHttpURLConnection
as UTF-8 encoded JSON.-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface io.github.pr0methean.betterrandom.seed.SeedGenerator
generateSeed
-
-
-
-
Field Detail
-
CLOCK
protected static final Clock CLOCK
Measures the retry delay. A ten-second delay might become either nothing or an hour if we used local time during the start or end of Daylight Saving Time, but it's fine if we occasionally wait 9 or 11 seconds instead of 10 because of a leap-second adjustment. See Tom Scott's video about the various considerations involved in this choice of clock.
-
JSON_PARSER
protected static final org.json.simple.parser.JSONParser JSON_PARSER
Made available to parse JSON responses.
-
lock
protected final Lock lock
Held while downloading, so that two requests to the same server won't be pending at the same time.
-
earliestNextAttempt
protected volatile Instant earliestNextAttempt
The earliest time we'll try again if there's been a previous IOE, or when the server requests throttling.
-
userAgent
protected final String userAgent
The value for the HTTP User-Agent header.
-
-
Constructor Detail
-
WebSeedClient
@Deprecated protected WebSeedClient(@Nullable Proxy proxy, @Nullable SSLSocketFactory socketFactory, boolean useRetryDelay)
Deprecated.Use one of the other overloads, which allow specifying the delay before retry.- Parameters:
proxy
- the proxy to use with this server, or null to use the JVM defaultsocketFactory
- the socket factory, or null for the JVM defaultuseRetryDelay
- whether to wait 10 seconds before trying again after an IOException
-
WebSeedClient
protected WebSeedClient(WebSeedClientConfiguration webSeedClientConfiguration)
- Parameters:
webSeedClientConfiguration
- configuration
-
WebSeedClient
protected WebSeedClient()
-
-
Method Detail
-
checkedGetObject
protected static <T> T checkedGetObject(org.json.simple.JSONObject parent, String key, Class<T> outputClass)
Reads a field value from a JSON object and checks that it is the correct type.- Type Parameters:
T
- the type ofoutputClass
- Parameters:
parent
- the JSON object to retrieve a field fromkey
- the field nameoutputClass
- the object class that we expect the value to be- Returns:
- the field value
- Throws:
SeedException
- if the field is missing or the wrong type
-
getResponseReader
protected static BufferedReader getResponseReader(HttpURLConnection connection) throws IOException
Creates aBufferedReader
reading the response from the givenHttpURLConnection
as UTF-8. The connection must be open and all request properties must be set before this reader is used.- Parameters:
connection
- the connection to read the response from- Returns:
- a BufferedReader reading the response
- Throws:
IOException
- if thrown byURLConnection.getInputStream()
-
parseJsonResponse
protected static org.json.simple.JSONObject parseJsonResponse(HttpURLConnection connection) throws IOException
Parses the response from the givenHttpURLConnection
as UTF-8 encoded JSON.- Parameters:
connection
- the connection to parse the response from- Returns:
- the response as a
JSONObject
- Throws:
IOException
- if thrown byURLConnection.getInputStream()
-
getMaxRequestSize
protected abstract int getMaxRequestSize()
Returns the maximum number of bytes that can be obtained with one request to the service. When a seed larger than this is needed, it is obtained using multiple requests.- Returns:
- the maximum number of bytes per request
-
openConnection
protected HttpsURLConnection openConnection(URL url) throws IOException
Opens anHttpsURLConnection
that will make a GET request to the given URL using this seed generator's currentProxy
,SeedGenerator
and User-Agent string, with the headerContent-Type: application/json
.- Parameters:
url
- the URL to connect to- Returns:
- a connection to the URL
- Throws:
IOException
- if thrown byURL.openConnection()
orURL.openConnection(Proxy)
-
getConnectionUrl
protected abstract URL getConnectionUrl(int numBytes)
-
downloadBytes
protected abstract void downloadBytes(HttpURLConnection connection, byte[] seed, int offset, int length) throws IOException
Performs a single request for random bytes.- Parameters:
connection
- the connection to download fromseed
- the array to save them tooffset
- the first index to save them to in the arraylength
- the number of bytes to download- Throws:
IOException
- if a connection error occursSeedException
- if a malformed response is received
-
generateSeed
public void generateSeed(byte[] seed) throws SeedException
Description copied from interface:SeedGenerator
Generates a seed value for a random number generator in an existing array.- Specified by:
generateSeed
in interfaceSeedGenerator
- Parameters:
seed
- The array that is to be populated with the seed.- Throws:
SeedException
- If a seed cannot be generated for any reason.
-
divideRoundingUp
protected static int divideRoundingUp(int dividend, int divisor)
-
modRange1ToM
protected static int modRange1ToM(int dividend, int modulus)
-
isWorthTrying
public boolean isWorthTrying()
Description copied from interface:SeedGenerator
Returns true if we cannot determine quickly (i.e. without I/O calls) that this SeedGenerator would throw aSeedException
ifSeedGenerator.generateSeed(int)
orSeedGenerator.generateSeed(byte[])
were being called right now.- Specified by:
isWorthTrying
in interfaceSeedGenerator
- Returns:
- true if this SeedGenerator will get as far as an I/O call or other slow operation in attempting to generate a seed immediately.
-
getProxy
@Nullable protected Proxy getProxy()
The proxy to use with this server, or null to use the JVM default.
-
getSocketFactory
@Nullable protected SSLSocketFactory getSocketFactory()
The SSLSocketFactory to use with this server.
-
getRetryDelayMs
protected long getRetryDelayMs()
Wait this many milliseconds before trying again after an IOException.
-
-