Package org.gcube.application.shlink
Class ShlinkShortener
java.lang.Object
org.gcube.application.shlink.ShlinkShortener
A URL shortener implementation that uses the Shlink REST API service.
This class provides functionality to create shortened URLs by communicating with a Shlink server instance through its REST API. Shlink is an open-source URL shortener that can be self-hosted.
Usage Example:
ShlinkShortener shortener = new ShlinkShortener();
String shortUrl = shortener.createShortUrl("https://your-shlink-instance.com/rest/v3/short-urls", "your-api-key",
"https://example.com/very-long-url");
Requirements:
- A running Shlink server instance with REST API enabled
- Valid API key with permissions to create short URLs
- Network connectivity to the Shlink server
- Author:
- Francesco Mangiacrapa
- See Also:
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
-
Field Details
-
SHORTENER
The Resurce Name registered in the Information System- See Also:
-
KEY
Property name for API key in the resource- See Also:
-
DOMAIN
- See Also:
-
LOGGER
protected static org.slf4j.Logger LOGGER
-
-
Constructor Details
-
ShlinkShortener
- Throws:
Exception
-
-
Method Details
-
shorten
public String shorten(String longUrl, String[] additionalTags, boolean findIfExists) throws Exception Shorten URL with domain and additional tags.- Parameters:
longUrl- the long urladditionalTags- optional additional tags to associate with the short URL (combined with auto-extracted tags)findIfExists- if true, checks if the short URL already exists before creating a new one- Returns:
- a shorten url
- Throws:
Exception- the exception
-
createShortUrl
Creates a short URL using the Shlink API service. Automatically extracts tags from the URL based on predefined keywords.- Parameters:
shlinkBaseUrl- the base URL of the Shlink serviceshlinkAPIKey- the API key for authenticationlongUrl- the original long URL to be shortened- Returns:
- the shortened URL as a String if successful, null if the operation fails
-