Class UserAgentManager
java.lang.Object
page.codeberg.friedolyn.meta.Downloadable
page.codeberg.friedolyn.configuration.client.UserAgentManager
A utility class to manage a collection of user agents for different clients and platforms. The default user agents
shipped with the application are available statically and can be accessed with
getDefaultUserAgents()
. If a
custom (up-to-date) user-agent collection should be used, it is necessary to create an instance of this class and
import the user agents from a file or JSON string.- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enum
The clients present in the user-agent collection by default.static enum
The platforms used in the user-agent collection by default.static class
AHashMap
in the form of<client, <platform, user-agent>>
that contains the user agents for various clients on different platforms. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate @NonNull Configuration
We need this in order to know where to store theuser-agents
file
.private static UserAgentManager.UserAgentCollection
Contains all the user agents for different clients and platforms that are available in the application by default.private static final @NonNull String
The file name of the default user-agents collection shipped with the application.private static final @NonNull ReentrantLock
A lock to ensure that theuser agents
are not used before they are fully initialised and translated.private static @NonNull Boolean
Will be set totrue
once theuser agents
are fully initialised and translated.private static final @NonNull Preferences
private static final @NonNull String
The file name under which theup-to-date user agents
will be stored once they have been fetched fromthe internet
.private static final @NonNull URL
The URL where an up-to-date user-agent collection can be downloaded from in order to updateuserAgents
.Contains all the user agents for different clients and platforms that are available in the application.private static String
The JSON schema for the user-agent collection. -
Constructor Summary
ConstructorsConstructorDescriptionUserAgentManager
(@NonNull Configuration configuration) Creates a new user-agent manager andtries to import
theuser-agents.json
file from theinvalid reference
Friedolyn directory
-
Method Summary
Modifier and TypeMethodDescriptionboolean
download()
Fetches an up-to-dateuser-agents collection
from the internet
, saves the collection asuser-agents.json
in theinvalid reference
Friedolyn directory
userAgents
field.static @NonNull UserAgentManager.UserAgentCollection
Returns the user-agent collection for different clients and platforms that is shipped with the application.@NonNull UserAgentManager.UserAgentCollection
boolean
Tries to read the user agents from theuser-agents.json
file in theinvalid reference
Friedolyn directory
void
importFromFile
(@NonNull File file) Updates theuser agents
with the contents of the given file.void
importFromJson
(@NonNull String json) Loads the user agents from the given JSON string, validates them against the JSON schema and sets them as the newuser agents
.private static void
Loads the JSON schema for theUserAgentManager.UserAgentCollection
fromuser-agents.schema.json
and imports the default user agents fromuser-agents.default.json
.boolean
wasDownloadedWithin
(@NonNull Duration duration) Checks if theuser agents
weredownloaded
more recently than the specified duration, i.e.Methods inherited from class page.codeberg.friedolyn.meta.Downloadable
wasDownloadedWithin
-
Field Details
-
PREFERENCES
-
configuration
We need this in order to know where to store theuser-agents
file
. -
userAgentsSchema
The JSON schema for the user-agent collection. Expected to be user-agents.schema.json. -
USER_AGENTS_URL
The URL where an up-to-date user-agent collection can be downloaded from in order to updateuserAgents
. Defaults to the Friedolyn data repository. -
USER_AGENTS_FILE_NAME
The file name under which theup-to-date user agents
will be stored once they have been fetched fromthe internet
.- See Also:
-
DEFAULT_USER_AGENTS_FILE_NAME
The file name of the default user-agents collection shipped with the application. The JSON file is expected to be available as a resource in the same package as this class.- See Also:
-
DEFAULT_USER_AGENTS
Contains all the user agents for different clients and platforms that are available in the application by default. This collection is guaranteed to be available but may not be up-to-date. If necessary, it is loaded from thedefault pre-defined JSON file
. -
defaultUserAgentsAreInitialised
Will be set totrue
once theuser agents
are fully initialised and translated. Remainsfalse
until then. -
DEFAULT_USER_AGENTS_LOCK
A lock to ensure that theuser agents
are not used before they are fully initialised and translated. -
userAgents
Contains all the user agents for different clients and platforms that are available in the application.
-
-
Constructor Details
-
UserAgentManager
Creates a new user-agent manager andtries to import
theuser-agents.json
file from theinvalid reference
Friedolyn directory
- Parameters:
configuration
- The configuration that specifies the Friedolyn directory.
-
-
Method Details
-
loadSchemaAndDefaultUserAgentCollection
private static void loadSchemaAndDefaultUserAgentCollection()Loads the JSON schema for theUserAgentManager.UserAgentCollection
fromuser-agents.schema.json
and imports the default user agents fromuser-agents.default.json
. Will callnotifyAll
onDEFAULT_USER_AGENTS_LOCK
once the user agents are fully initialised and translated.- Implementation Note:
- TODO: Verify that this method is thread-safe.
-
importFromConfigurationDirectory
public boolean importFromConfigurationDirectory()Tries to read the user agents from theuser-agents.json
file in theinvalid reference
Friedolyn directory
false
(but not throw an exception).- Returns:
true
if the user agents were successfully imported,false
otherwise.
-
wasDownloadedWithin
Checks if theuser agents
weredownloaded
more recently than the specified duration, i.e. whether the user agents were downloaded within `now - duration`.- Specified by:
wasDownloadedWithin
in classDownloadable
- Parameters:
duration
- The time between the oldest acceptable download date and now.- Returns:
true
if the user agents were downloaded within the specified duration,false
otherwise.
-
download
public boolean download()Fetches an up-to-dateuser-agents collection
from the internet
, saves the collection asuser-agents.json
in theinvalid reference
Friedolyn directory
userAgents
field.- Specified by:
download
in classDownloadable
- Returns:
true
in case of success,false
if anything failed.
-
importFromFile
public void importFromFile(@NonNull @NonNull File file) throws IOException, IllegalArgumentException Updates theuser agents
with the contents of the given file. The file must contain a valid JSON string that matches theuser-agents schema
.- Parameters:
file
- The file to import the user agents from.- Throws:
IOException
- If the file could not be read.IllegalArgumentException
- If the file is not a file, if it does not exist, if the JSON in the file is invalid, if the JSON does not match the schema or if itlacks any of the clients inUserAgentManager.Client
.
-
importFromJson
public void importFromJson(@NonNull @NonNull String json) throws IllegalArgumentException, com.fasterxml.jackson.core.JsonProcessingException Loads the user agents from the given JSON string, validates them against the JSON schema and sets them as the newuser agents
.- Parameters:
json
- The new user agents to load. Must include all the clients inUserAgentManager.Client
, but may contain more.- Throws:
IllegalArgumentException
- If the given JSON is invalid, does not match the schema or lacks any of the clients inUserAgentManager.Client
.com.fasterxml.jackson.core.JsonProcessingException
- SeeObjectMapper.readValue(String, Class)
.
-
getDefaultUserAgents
Returns the user-agent collection for different clients and platforms that is shipped with the application. This means that the user agents are guaranteed to be available but may not be up-to-date. If necessary, they are loaded from the default pre-defined JSON file.- Returns:
- The user agents for different clients and platforms.
- See Also:
- API Note:
- If you have already downloaded/imported a custom
user agents collection
, you should most likely usegetUserAgents()
on the instance that contains them, instead of calling this static method. - Implementation Note:
- TODO: Verify that this method is thread-safe.
-
getUserAgents
- Returns:
- The
user agents
specific to this instance if they are notnull
, otherwise thedefault user agents
shipped with the application. Therefore, it is guaranteed that this method will always return a valid user-agent collection, but it is NOT guaranteed that the user agents are up-to-date. - See Also:
-