Class AlfrescoService

java.lang.Object
org.saidone.component.BaseComponent
org.saidone.service.AlfrescoService
All Implemented Interfaces:
org.springframework.beans.factory.Aware, org.springframework.context.ApplicationContextAware

@Service public class AlfrescoService extends BaseComponent
Service class for interacting with Alfresco repository nodes and content. Provides methods to retrieve, update, delete, restore nodes and their content, as well as search and process nodes in Alfresco.

Uses Alfresco REST APIs via Feign clients and WebClient for content operations.

  • Constructor Details

    • AlfrescoService

      public AlfrescoService()
  • Method Details

    • init

      @PostConstruct public void init()
      Initializes the AlfrescoService component after dependency injection.

      This method overrides the base initialization to set up the Basic Authentication header used for Alfresco API requests. It constructs the header by encoding the configured username and password in Base64 with UTF-8 charset, then formats it as a standard HTTP Basic Auth header.

      Calls super.init() to perform any initialization defined in the superclass.

      Overrides:
      init in class BaseComponent
    • getGuestHome

      public org.alfresco.core.model.Node getGuestHome()
      Retrieves the "Guest Home" node from Alfresco repository.
      Returns:
      the Guest Home Node or null if not found
    • isAdmin

      public boolean isAdmin(String userId, String password)
      Checks if the specified user has administrative privileges in Alfresco.

      This method attempts to authenticate the user with the given userId and password. If authentication is successful, it retrieves the user's details and checks if the user has admin capabilities.

      Parameters:
      userId - the identifier of the user to check
      password - the password of the user for authentication
      Returns:
      true if the user is authenticated and has admin privileges; false otherwise
    • getNode

      public org.alfresco.core.model.Node getNode(String nodeId)
      Retrieves a node by its identifier.
      Parameters:
      nodeId - the identifier of the node to retrieve
      Returns:
      the Node object
      Throws:
      VaultException - if the node cannot be retrieved
    • getNodeContent

      public InputStream getNodeContent(String nodeId)
      Retrieves the content of a node as an InputStream from the content service using the specified node identifier.

      This method constructs the URL for the node content endpoint, opens a connection, sets the required authorization header, and returns the response stream.

      Parameters:
      nodeId - the unique identifier of the node whose content is to be retrieved
      Returns:
      an InputStream containing the node's content
      Throws:
      RuntimeException - if an I/O error occurs while opening the connection or obtaining the content stream
    • addAspects

      public void addAspects(String nodeId, List<String> additionalAspectNames)
      Adds aspects to a node.
      Parameters:
      nodeId - the identifier of the node to update
      additionalAspectNames - list of aspect names to add
    • removeAspects

      public void removeAspects(String nodeId, List<String> aspectsToRemove)
      Removes specified aspects from a node.
      Parameters:
      nodeId - the identifier of the node to update
      aspectsToRemove - list of aspect names to remove
    • deleteNode

      public void deleteNode(String nodeId)
      Deletes a node from the repository.
      Parameters:
      nodeId - the identifier of the node to delete
    • restoreNode

      public String restoreNode(org.alfresco.core.model.Node node, boolean restorePermissions)
      Restores a previously archived or deleted node into the repository.

      Recreates a node using provided node metadata, including name, type, aspects, properties, and optionally restores the original permissions. Handles the reconstruction of the parent path if the original parent no longer exists, by recreating the necessary path structure or resolving the appropriate parent node. Removes archival aspects before restoration, maintains a reference to the original node, and creates the node in the repository with restored settings.

      Parameters:
      node - the node to be restored, containing all necessary metadata and path information
      restorePermissions - whether to reinstate original permission settings for the node
      Returns:
      the identifier of the newly restored node in the repository
      Throws:
      RuntimeException - if the operation fails due to API errors or missing parent structure
    • restoreNodeContent

      public void restoreNodeContent(String nodeId, NodeContent nodeContent)
      Restores the content of a node by uploading the provided content stream.
      Parameters:
      nodeId - the identifier of the node to update content for
      nodeContent - the NodeContent containing the content stream and metadata
    • computeHash

      public String computeHash(String nodeId, String algorithm)
      Computes a cryptographic hash of the content for the specified node using the provided algorithm.

      This method retrieves the node content from Alfresco, calculates the hash using the specified algorithm via an AnvDigestInputStream, and returns the hash value as a hexadecimal string. The node content is streamed directly through the hash computation to avoid loading the entire content into memory.

      Parameters:
      nodeId - the unique identifier of the node whose content will be hashed
      algorithm - the name of the hash algorithm to use (e.g., "MD5", "SHA-256")
      Returns:
      a hexadecimal string representation of the computed hash
      Throws:
      RuntimeException - if any I/O or algorithm-related exceptions occur during processing (wrapped via @SneakyThrows)
    • searchAndProcess

      public void searchAndProcess(String query, Consumer<String> nodeProcessor)
      Executes a search query and processes each resulting node ID using the provided processor. Processes all pages of results.
      Parameters:
      query - the search query string
      nodeProcessor - a Consumer that processes node IDs
    • searchAndProcess

      public void searchAndProcess(String query, Integer pages, Consumer<String> nodeProcessor)
      Executes a search query and processes each resulting node ID using the provided processor, limiting the number of pages processed.
      Parameters:
      query - the search query string
      pages - maximum number of pages to process; if null or less than 1, all pages are processed
      nodeProcessor - a Consumer that processes node IDs
    • createPathIfNotExists

      public String createPathIfNotExists(String parentId, List<String> path) throws Exception
      Attempts to create a folder structure based on the provided path under the specified parent node. For each element in the path, tries to create a folder with that name inside the current parent. If the folder already exists, retrieves its ID and proceeds to the next path element. If creation and lookup both fail, throws an exception.
      Parameters:
      parentId - the ID of the parent node under which the folder structure should be created
      path - the ordered list of folder names representing the path to create
      Returns:
      the ID of the final node corresponding to the last path element
      Throws:
      Exception - if a folder cannot be created or found at any point in the path
    • nodeExists

      public boolean nodeExists(String nodeId)
      Checks whether a node with the specified ID exists.

      Sends a request to retrieve the node with the given nodeId. If the node is found, the method returns true. If the node does not exist, a FeignException.NotFound is caught and the method returns false.

      Parameters:
      nodeId - the ID of the node to check for existence
      Returns:
      true if the node exists, false otherwise
    • pathExists

      public boolean pathExists(String path)
      Checks if a node exists at the given path within the repository.

      Attempts to retrieve the node using the provided path. If the node is found, the method returns true. If the node does not exist and a FeignException.NotFound is thrown, the method returns false.

      Parameters:
      path - the repository path to check for existence
      Returns:
      true if a node exists at the specified path; false otherwise
    • getErrorKey

      public static String getErrorKey(feign.FeignException e)
      Extracts the error key from a FeignException response body.
      Parameters:
      e - the FeignException thrown by a Feign client
      Returns:
      the error key string extracted from the response