Class ShlinkShortener

java.lang.Object
org.gcube.application.shlink.ShlinkShortener

public class ShlinkShortener extends Object
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 Details

  • Constructor Details

  • 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 url
      additionalTags - 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

      public String createShortUrl(String shlinkBaseUrl, String shlinkAPIKey, String longUrl)
      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 service
      shlinkAPIKey - the API key for authentication
      longUrl - the original long URL to be shortened
      Returns:
      the shortened URL as a String if successful, null if the operation fails