java.lang.Object
org.klomp.snark.web.ActionTokens

public final class ActionTokensextends Object
Mints and resolves short, unique action tokens for per-torrent form controls. A token is the shortest prefix of a torrent's base64 info-hash name that no other currently-loaded torrent shares, floored at 4 chars so tokens stay readable and the stale-token match surface stays small. Tokens are recomputed from the live torrent set on every render (stateless), so uniqueness within a render is guaranteed by construction; POST resolution requires exactly one matching torrent, which makes stale or ambiguous tokens degrade to a safe no-op rather than a misdirected action. Replaces 44-char full-hash parameter names, shrinking row markup by ~40 bytes per control.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private static final int
    Base64 of a 32-byte hash is 44 chars including one '=' pad; tokens stop before it.
    private static final int
    Floor for readability/stability: tokens shorter than this are too collision-prone.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
    Utility class, not intended for instantiation.
  • Method Summary

    Modifier and Type
    Method
    Description
    private static int
    lcp(String a, String b)
    Longest common prefix length of two strings.
    static Map<String,String>
    Mints minimal unique prefix tokens for the given names.
    static String
    Resolves a token against candidate names: returns the single name that starts with the token, or null when none or several do.

    Methods inherited from class Object

    clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • MAX_TOKEN_LEN

      private static final int MAX_TOKEN_LEN
      Base64 of a 32-byte hash is 44 chars including one '=' pad; tokens stop before it.
      See Also:
    • MIN_TOKEN_LEN

      private static final int MIN_TOKEN_LEN
      Floor for readability/stability: tokens shorter than this are too collision-prone.
      See Also:
  • Constructor Details

    • ActionTokens

      private ActionTokens()
      Utility class, not intended for instantiation.
  • Method Details

    • mint

      public static Map<String,String> mint(Collection<String> names)
      Mints minimal unique prefix tokens for the given names. Names are sorted once; each name's token length is one past the longest common prefix it shares with either sorted neighbor, so every token is distinct from every other by construction. Identical names cannot occur (one info hash = one torrent).
      Parameters:
      names - base64 info-hash names of all rendered torrents
      Returns:
      map of full name to its token; every input name has an entry
    • resolveUnique

      public static String resolveUnique(String token, Collection<String> names)
      Resolves a token against candidate names: returns the single name that starts with the token, or null when none or several do. The multi-match null return is what makes stale/ambiguous tokens a safe no-op instead of a misdirected action.
      Parameters:
      token - the token extracted from a submitted control name
      names - base64 info-hash names of all currently loaded torrents
      Returns:
      the unique matching name, or null when zero or multiple match
    • lcp

      private static int lcp(String a, String b)
      Longest common prefix length of two strings.
      Parameters:
      a - first string
      b - second string
      Returns:
      number of leading characters shared by both