LinkHandlerFactory is the base abstract class for creating LinkHandler instances from URLs or IDs. It provides the core functionality for parsing URLs, extracting IDs, and validating URL acceptance.
Class Overview
Abstract Methods (Must Override)
These methods must be implemented by concrete factory classes:Extract the resource ID from a URL.Parameters:
url(String) - The URL to extract the ID from
ParsingException- If the URL cannot be parsedUnsupportedOperationException- If ID extraction is not supported
Construct a URL from a resource ID.Parameters:
id(String) - The resource ID
ParsingException- If the URL cannot be constructedUnsupportedOperationException- If URL construction is not supported
Determine if this factory can handle the given URL. This is the core validation method.Parameters:
url(String) - The URL to validate
true if this factory can handle the URL, false otherwiseThrows:ParsingException- If an error occurs during validation
Optional Methods
Construct a URL from a resource ID and base URL. Override this if your service requires base URL context.Parameters:
id(String) - The resource IDbaseUrl(String) - The base URL for the service
ParsingException- If the URL cannot be constructedUnsupportedOperationException- If URL construction is not supported
getUrl(id) and ignores the base URLPublic Methods
Build a LinkHandler from a URL. This is the primary method for parsing URLs.Parameters:
url(String) - The URL to parse. Must not be null or empty.
IllegalArgumentException- If URL is null or emptyParsingException- If URL cannot be parsed or is not accepted
- Validates that URL is not null or empty
- Follows Google redirects if present using
Utils.followGoogleRedirectIfNeeded() - Extracts base URL
- Calls
fromUrl(url, baseUrl)with both values
Build a LinkHandler from a URL and base URL. The URL should already be polished (no Google redirects).Parameters:
url(String) - The URL without Google redirects. Must not be null.baseUrl(String) - The base URL extracted from the polished URL
NullPointerException- If URL is nullParsingException- If URL is not accepted or cannot be parsed
- Validates URL via
acceptUrl() - Extracts ID using
getId(url) - Constructs normalized URL using
getUrl(id, baseUrl) - Creates LinkHandler with original URL, normalized URL, and ID
Utils.followGoogleRedirectIfNeeded() if overriding this method, as it should already be done in the single-parameter fromUrl() method.Build a LinkHandler from a resource ID.Parameters:
id(String) - The resource ID. Must not be null.
NullPointerException- If ID is nullParsingException- If URL cannot be constructed from ID
- Constructs URL from ID using
getUrl(id) - Creates LinkHandler where both
originalUrlandurlare the same
Build a LinkHandler from a resource ID and base URL.Parameters:
id(String) - The resource ID. Must not be null.baseUrl(String) - The base URL for the service
NullPointerException- If ID is nullParsingException- If URL cannot be constructed from ID
- Constructs URL from ID and base URL using
getUrl(id, baseUrl) - Creates LinkHandler where both
originalUrlandurlare the same
Test if this factory can handle the given URL. Used for routing URLs to the correct service.Parameters:
url(String) - The URL to test
true if this factory accepts the URLThrows:ParsingException- If an error occurs during validation
- Delegates to
onAcceptUrl(url)for the actual validation logic - When a VIEW_ACTION intent is caught, this determines if the URL should be handled by this service
LinkHandler Class
TheLinkHandler class is the result object created by factory methods:
Immutable data class that holds URL and ID information.Constructors:Fields:
originalUrl(String) - The URL as originally providedurl(String) - The cleaned/normalized URLid(String) - The extracted resource ID
String getOriginalUrl()- Returns the original URLString getUrl()- Returns the normalized URLString getId()- Returns the resource IDString getBaseUrl()- Extracts and returns the base URL (throws ParsingException)
Serializable - Can be serialized for caching or persistenceImplementation Example
Best Practices
- Validate input: Always validate URLs and IDs before processing
- Handle Google redirects: The framework handles this automatically in
fromUrl() - Be specific in
onAcceptUrl(): Ensure your validation is precise to avoid conflicts - Throw meaningful exceptions: Provide clear error messages in ParsingException
- Consider base URLs: Override
getUrl(id, baseUrl)if your service uses different domains - Keep it stateless: Factory instances should be stateless and reusable
See Also
- Link Handler System Overview
- ListLinkHandlerFactory - For filtered lists
- SearchQueryHandlerFactory - For search queries