AEM Endpoint for Creating a Node in DAM
Here's a Java implementation for an AEM endpoint that will create a simple node in the DAM when called. This uses Sling servlets and the AEM JCR API.
package com.example.aem.core.servlets;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.servlets.annotations.SlingServletPaths;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import java.io.IOException;
@Component(service = Servlet.class)
@SlingServletPaths(value = {"/api/v1/myEndpoint"})
public class MyEndpointServlet extends SlingAllMethodsServlet {
private static final Logger LOG = LoggerFactory.getLogger(MyEndpointServlet.class);
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
private void processRequest(SlingHttpServletRequest request, SlingHttpServletResponse response)
throws IOException {
try {
// Get the JCR session
Session session = request.getResourceResolver().adaptTo(Session.class);
if (session == null) {
response.sendError(SlingHttpServletResponse.SC_INTERNAL_SERVER_ERROR,
"Could not get JCR session");
return;
}
// Define the path where the node will be created (arbitrary location under DAM)
String parentPath = "/content/dam/my-folder"; // Change this to your desired path
String nodeName = "my-created-node-" + System.currentTimeMillis();
// Create the node
Node parentNode = session.getNode(parentPath);
Node newNode = parentNode.addNode(nodeName, "nt:unstructured");
// Add some properties to the node
newNode.setProperty("jcr:title", "My Created Node");
newNode.setProperty("description", "This node was created via API endpoint");
newNode.setProperty("createdBy", "API Endpoint");
// Save the changes
session.save();
LOG.info("Successfully created node at: {}", newNode.getPath());
// Return success response
response.setContentType("application/json");
response.getWriter().write(
String.format("{\"status\":\"success\", \"path\":\"%s\"}", newNode.getPath())
);
} catch (RepositoryException e) {
LOG.error("Error creating node in DAM", e);
response.sendError(SlingHttpServletResponse.SC_INTERNAL_SERVER_ERROR,
"Error creating node: " + e.getMessage());
}
}
}
Important Notes:
-
Path Configuration: The code uses /content/dam/my-folder as the parent path. You should change this to your desired location in the DAM.
-
Permissions: The user making the request needs appropriate permissions to create nodes in the specified location.
-
Node Type: The example uses nt:unstructured as the node type. You might want to use a more specific node type depending on your requirements.
-
Error Handling: The code includes basic error handling, but you might want to enhance it based on your specific needs.
-
Authentication: This endpoint will be accessible to authenticated users. If you need to restrict access further, you should add appropriate authentication checks.
-
Dependencies: Make sure your project has the necessary AEM dependencies in the POM file.
-
Testing: After deploying this servlet, you can test it by making a GET or POST request to /api/v1/myEndpoint.
Would you like me to modify any specific part of this implementation or add additional functionality?