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 enumThe clients present in the user-agent collection by default.static enumThe platforms used in the user-agent collection by default.static classAHashMapin the form of<client, <platform, user-agent>>that contains the user agents for various clients on different platforms. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate @NonNull ConfigurationWe need this in order to know where to store theuser-agentsfile.private static UserAgentManager.UserAgentCollectionContains all the user agents for different clients and platforms that are available in the application by default.private static final @NonNull StringThe file name of the default user-agents collection shipped with the application.private static final @NonNull ReentrantLockA lock to ensure that theuser agentsare not used before they are fully initialised and translated.private static @NonNull BooleanWill be set totrueonce theuser agentsare fully initialised and translated.private static final @NonNull Preferencesprivate static final @NonNull StringThe file name under which theup-to-date user agentswill be stored once they have been fetched fromthe internet.private static final @NonNull URLThe 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 StringThe JSON schema for the user-agent collection. -
Constructor Summary
ConstructorsConstructorDescriptionUserAgentManager(@NonNull Configuration configuration) Creates a new user-agent manager andtries to importtheuser-agents.jsonfile from thespecified in the given configuration.invalid reference
Friedolyn directory -
Method Summary
Modifier and TypeMethodDescriptionbooleandownload()Fetches an up-to-dateuser-agents collectionfrom the internet, saves the collection asuser-agents.jsonin theand imports it into theinvalid reference
Friedolyn directoryuserAgentsfield.static @NonNull UserAgentManager.UserAgentCollectionReturns the user-agent collection for different clients and platforms that is shipped with the application.@NonNull UserAgentManager.UserAgentCollectionbooleanTries to read the user agents from theuser-agents.jsonfile in the.invalid reference
Friedolyn directoryvoidimportFromFile(@NonNull File file) Updates theuser agentswith the contents of the given file.voidimportFromJson(@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 voidLoads the JSON schema for theUserAgentManager.UserAgentCollectionfromuser-agents.schema.jsonand imports the default user agents fromuser-agents.default.json.booleanwasDownloadedWithin(@NonNull Duration duration) Checks if theuser agentsweredownloadedmore 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-agentsfile. -
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 agentswill 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 totrueonce theuser agentsare fully initialised and translated. Remainsfalseuntil then. -
DEFAULT_USER_AGENTS_LOCK
A lock to ensure that theuser agentsare 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 importtheuser-agents.jsonfile from thespecified in the given configuration.invalid 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.UserAgentCollectionfromuser-agents.schema.jsonand imports the default user agents fromuser-agents.default.json. Will callnotifyAllonDEFAULT_USER_AGENTS_LOCKonce 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.jsonfile in the. It is expected that the file might not exist, in which case the method will just returninvalid reference
Friedolyn directoryfalse(but not throw an exception).- Returns:
trueif the user agents were successfully imported,falseotherwise.
-
wasDownloadedWithin
Checks if theuser agentsweredownloadedmore recently than the specified duration, i.e. whether the user agents were downloaded within `now - duration`.- Specified by:
wasDownloadedWithinin classDownloadable- Parameters:
duration- The time between the oldest acceptable download date and now.- Returns:
trueif the user agents were downloaded within the specified duration,falseotherwise.
-
download
public boolean download()Fetches an up-to-dateuser-agents collectionfrom the internet, saves the collection asuser-agents.jsonin theand imports it into theinvalid reference
Friedolyn directoryuserAgentsfield.- Specified by:
downloadin classDownloadable- Returns:
truein case of success,falseif anything failed.
-
importFromFile
public void importFromFile(@NonNull @NonNull File file) throws IOException, IllegalArgumentException Updates theuser agentswith 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 agentsspecific to this instance if they are notnull, otherwise thedefault user agentsshipped 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:
-