Class BlobKeys

java.lang.Object
org.mockserver.state.BlobKeys

public final class BlobKeys extends Object
Key conventions shared by the object-store backed BlobStore implementations that apply a blobStoreKeyPrefix (S3, GCS, Azure); forPersistedFile(BlobStore, java.nio.file.Path) additionally governs the key for EVERY store other than FilesystemBlobStore.

Object stores are NOT filesystems: their keys are opaque strings in which / is only a display convention. Two shapes that a filesystem tolerates cause trouble as object names, and only one of them is fatal:

  • a DOUBLED // — FATAL. Produced whenever a prefix that already ends in / is concatenated with a key that begins with one. MinIO rejects it outright with HTTP 400 "Object name contains unsupported characters", so the write never lands;
  • a LEADING / — accepted, but undesirable. It is a legal byte in an S3 object name and get round-trips the very same key, so writes and reads DO work; the cost is that /a/b is a distinct name from a/b and most path-like tools cannot browse to it. When the leading / came from an absolute local path, as the persisted document's key once did, the object was also named after the writing container's filesystem layout, so an instance that resolved that path differently looked under a different name and silently found nothing.
These helpers normalise both away so that prefix + key always yields a single-separator, leading-slash-free, portable object name whether the configured blobStoreKeyPrefix is empty, ends in /, ends without one, or (mistakenly) starts with one.
  • Method Details

    • join

      public static String join(String keyPrefix, String key)
      Joins a configured key prefix and a blob key into an object-store key with EXACTLY one separator between them, no leading separator, and no repeated separators anywhere.

      Examples (prefix, key) → result:

         ("",             "expectations.json") → "expectations.json"
         ("mockserver",   "expectations.json") → "mockserver/expectations.json"
         ("mockserver/",  "expectations.json") → "mockserver/expectations.json"
         ("/mockserver/", "/expectations.json") → "mockserver/expectations.json"
         ("mockserver/",  "")                  → "mockserver/"
       
      The empty-key case keeps the trailing separator on purpose: the only caller that passes an empty key is list(""), which must scope the listing to the prefix rather than to every key that merely starts with the same characters.
      Parameters:
      keyPrefix - the configured blobStoreKeyPrefix (may be null or empty)
      key - the blob key (may be null or empty)
      Returns:
      a valid object-store key
    • stripPrefix

      public static String stripPrefix(String keyPrefix, String storeKey)
      Reverses join(String, String), recovering the NORMALISED blob key from an object-store key. It is an inverse only modulo that normalisation: stripPrefix(p, join(p, "/x.json")) returns x.json, not /x.json. Keys that do not carry the prefix are returned unchanged, matching the tolerant behaviour of the previous startsWith-based implementations.
      Parameters:
      keyPrefix - the configured blobStoreKeyPrefix (may be null or empty)
      storeKey - the object-store key as returned by the cloud SDK
      Returns:
      the blob key without the prefix
    • forPersistedFile

      public static String forPersistedFile(BlobStore blobStore, Path filePath)
      Derives the object-store key under which a persisted document is stored.

      For the FilesystemBlobStore the key IS the file path, so the absolute path is kept — the store resolves it back to the very file the pre-blob-store direct-I/O implementation wrote.

      For every other store the key is the FILE NAME alone. Embedding the absolute local path in a cloud object name produced an invalid key (a leading /, doubled up to // by any prefix ending in a separator, which MinIO rejects with HTTP 400) and tied the object name to the writing container's filesystem layout, so an instance started from a different directory silently restored nothing.

      Parameters:
      blobStore - the store the key is destined for
      filePath - the configured local persistence path
      Returns:
      the key to use with blobStore
    • normalize

      public static String normalize(String key)
      Normalizes a key: drops any leading separator and collapses runs of separators into one. A trailing separator is preserved because callers use it to scope a list to a logical folder.
      Parameters:
      key - the key to normalize (may be null)
      Returns:
      the normalized key, never null
    • normalizePrefix

      public static String normalizePrefix(String keyPrefix)
      Normalizes a prefix: as normalize(String) but also drops any trailing separator, so callers can append exactly one themselves.
      Parameters:
      keyPrefix - the prefix to normalize (may be null)
      Returns:
      the normalized prefix, never null and never ending in a separator