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

public final class BrowserApiInstallerextends Object
Registers the browser protocol/file handler that forwards magnet links and torrent files to the I2PSnark browser API (POST /i2psnark/_add).

No shell script is involved: the registered command is the router's own JVM running MagnetHandler with the console URL baked in, so it keeps working across console port or HTTPS changes made before installation.

Linux: writes a .desktop file and registers it in mimeapps.list, which xdg-open, Firefox, and Chromium-based browsers honor.

Windows: writes per-user HKCU\Software\Classes registry entries for the magnet: scheme and the .torrent file type (no admin rights needed).

macOS: not supported (LaunchServices has no CLI scheme registration).

  • Field Summary

    Modifier and Type
    Field
    Description
    static final String
    Base name of the generated XDG .desktop file, also the MIME cache entry key.
    private static final String
    Mime type of the magnet scheme handler.
    private static final String
    Mime type of the torrent files.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
    Not called, the installer class is never instantiated.
  • Method Summary

    Modifier and Type
    Method
    Description
    private static String
    buildCommand(String consoleBaseUrl, boolean uriPlaceholder)
    The command template used by the OS registration, e.g.
    private static String
    The jar (or classes dir) that contains MagnetHandler, resolved from its code source so no shell quoting or path guessing is needed.
    static String
    install(String consoleBaseUrl)
    Register the browser handler pointing at the given console base URL, e.g.
    static String
    installLinux(String consoleBaseUrl, File home)
    Linux registration with an explicit home directory (testable).
    private static String
    installWindows(String consoleBaseUrl)
    Builds the Windows registry script for the handlers.
    private static String
    The java binary that launched this router, or javaw on Windows.
    private static String
    join(List<String> lines)
    Joins the lines with newlines.
    private static void
    updateMimeapps(File file, String... mimes)
    Add or update the two MIME registrations in the [Default Applications] section of mimeapps.list, preserving everything else.
    private static void
    writeFile(File f, String content)
    Writes the text to the file.

    Methods inherited from class Object

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

  • Constructor Details

    • BrowserApiInstaller

      private BrowserApiInstaller()
      Not called, the installer class is never instantiated.
  • Method Details

    • install

      public static String install(String consoleBaseUrl) throws IOException
      Register the browser handler pointing at the given console base URL, e.g. http://127.0.0.1:7657/i2psnark (no trailing slash).
      Parameters:
      consoleBaseUrl - base URL of the I2PSnark webapp
      Returns:
      a human-readable result message
      Throws:
      IOException - on any failure
    • installLinux

      public static String installLinux(String consoleBaseUrl, File home) throws IOException
      Linux registration with an explicit home directory (testable).
      Parameters:
      consoleBaseUrl - base URL of the I2PSnark webapp
      home - the user home dir, e.g. System.getProperty(String)"user.home")
      Returns:
      a human-readable result message
      Throws:
      IOException - on any failure
    • javaBinary

      private static String javaBinary()
      The java binary that launched this router, or javaw on Windows.
      Returns:
      the absolute path to the java executable for this OS
    • handlerClasspath

      private static String handlerClasspath() throws IOException
      The jar (or classes dir) that contains MagnetHandler, resolved from its code source so no shell quoting or path guessing is needed.
      Returns:
      the absolute path of that jar or classes directory
      Throws:
      IOException - if the code source location is missing or is not a usable URI
    • buildCommand

      private static String buildCommand(String consoleBaseUrl, boolean uriPlaceholder) throws IOException
      The command template used by the OS registration, e.g. "java" -cp "i2psnark.jar" org.klomp.snark.MagnetHandler --url "base" %u.
      Parameters:
      consoleBaseUrl - the console URL passed to the handler as --url
      uriPlaceholder - true to append the %u desktop placeholder, false for the "%1" shell argument
      Returns:
      the command template string for the desktop entry
      Throws:
      IOException - if the MagnetHandler classpath cannot be located
    • writeFile

      private static void writeFile(File f, String content) throws IOException
      Writes the text to the file.
      Parameters:
      f - the file to write
      content - the text to write
      Throws:
      IOException - if the writing fails
    • updateMimeapps

      private static void updateMimeapps(File file, String... mimes) throws IOException
      Add or update the two MIME registrations in the [Default Applications] section of mimeapps.list, preserving everything else.
      Parameters:
      file - the mimeapps.list to update, created with its parents if absent
      mimes - the MIME types to point at the desktop entry
      Throws:
      IOException - if the parent directories cannot be created or the file cannot be read
    • join

      private static String join(List<String> lines)
      Joins the lines with newlines.
      Parameters:
      lines - the lines to join
      Returns:
      the joined text
    • installWindows

      private static String installWindows(String consoleBaseUrl) throws IOException
      Builds the Windows registry script for the handlers.
      Parameters:
      consoleBaseUrl - the URL of the console
      Returns:
      the script text
      Throws:
      IOException - if the files cannot be written