Class AlfrescoService
- All Implemented Interfaces:
org.springframework.beans.factory.Aware
,org.springframework.context.ApplicationContextAware
Uses Alfresco REST APIs via Feign clients and WebClient for content operations.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
addAspects
(String nodeId, List<String> additionalAspectNames) Adds aspects to a node.computeHash
(String nodeId, String algorithm) Computes a cryptographic hash of the content for the specified node using the provided algorithm.createPathIfNotExists
(String parentId, List<String> path) Attempts to create a folder structure based on the provided path under the specified parent node.void
deleteNode
(String nodeId) Deletes a node from the repository.static String
getErrorKey
(feign.FeignException e) Extracts the error key from a FeignException response body.org.alfresco.core.model.Node
Retrieves the "Guest Home" node from Alfresco repository.org.alfresco.core.model.Node
Retrieves a node by its identifier.getNodeContent
(String nodeId) Retrieves the content of a node as an InputStream from the content service using the specified node identifier.void
init()
Initializes the AlfrescoService component after dependency injection.boolean
Checks if the specified user has administrative privileges in Alfresco.boolean
nodeExists
(String nodeId) Checks whether a node with the specified ID exists.boolean
pathExists
(String path) Checks if a node exists at the given path within the repository.void
removeAspects
(String nodeId, List<String> aspectsToRemove) Removes specified aspects from a node.restoreNode
(org.alfresco.core.model.Node node, boolean restorePermissions) Restores a previously archived or deleted node into the repository.void
restoreNodeContent
(String nodeId, NodeContent nodeContent) Restores the content of a node by uploading the provided content stream.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.void
searchAndProcess
(String query, Consumer<String> nodeProcessor) Executes a search query and processes each resulting node ID using the provided processor.Methods inherited from class org.saidone.component.BaseComponent
setApplicationContext, shutDown, stop
-
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 classBaseComponent
-
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
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 checkpassword
- the password of the user for authentication- Returns:
true
if the user is authenticated and has admin privileges;false
otherwise
-
getNode
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
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
Adds aspects to a node.- Parameters:
nodeId
- the identifier of the node to updateadditionalAspectNames
- list of aspect names to add
-
removeAspects
Removes specified aspects from a node.- Parameters:
nodeId
- the identifier of the node to updateaspectsToRemove
- list of aspect names to remove
-
deleteNode
Deletes a node from the repository.- Parameters:
nodeId
- the identifier of the node to delete
-
restoreNode
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 informationrestorePermissions
- 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
Restores the content of a node by uploading the provided content stream.- Parameters:
nodeId
- the identifier of the node to update content fornodeContent
- theNodeContent
containing the content stream and metadata
-
computeHash
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 hashedalgorithm
- 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
Executes a search query and processes each resulting node ID using the provided processor. Processes all pages of results.- Parameters:
query
- the search query stringnodeProcessor
- aConsumer
that processes node IDs
-
searchAndProcess
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 stringpages
- maximum number of pages to process; if null or less than 1, all pages are processednodeProcessor
- aConsumer
that processes node IDs
-
createPathIfNotExists
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 createdpath
- 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
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 returnstrue
. If the node does not exist, aFeignException.NotFound
is caught and the method returnsfalse
.- Parameters:
nodeId
- the ID of the node to check for existence- Returns:
true
if the node exists,false
otherwise
-
pathExists
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 aFeignException.NotFound
is thrown, the method returnsfalse
.- Parameters:
path
- the repository path to check for existence- Returns:
true
if a node exists at the specified path;false
otherwise
-
getErrorKey
Extracts the error key from a FeignException response body.- Parameters:
e
- theFeignException
thrown by a Feign client- Returns:
- the error key string extracted from the response
-