Hi @parth3 ,
To enforce uniqueness of a property when creating content fragments via REST in Adobe Experience Manager (AEM), you can leverage custom validation logic. Here's a general approach to implement this rule:
Custom Validation Servlet: Create a custom servlet that intercepts the POST request for creating content fragments. This servlet will be responsible for checking if the unique ID already exists before allowing the creation of a new content fragment.
Validation Logic: In the servlet, implement logic to query the existing content fragments to check if the unique ID already exists. If it does, reject the POST request with an appropriate error message.
Register Servlet: Register the custom servlet with AEM so that it gets invoked when a POST request is made to create a content fragment.
Here's a simplified example of what the servlet might look like:
import org.apache.sling.api.servlets.HttpConstants;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.api.servlets.ServletResolverConstants;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.servlets.ServletResolverConstants;
import org.osgi.service.component.annotations.Component;
import org.osgi.framework.Constants;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import java.io.IOException;
@Component(
service = Servlet.class,
property = {
Constants.SERVICE_DESCRIPTION + "=Custom Content Fragment Creation Validator Servlet",
ServletResolverConstants.SLING_SERVLET_PATHS + "=/bin/contentFragmentValidator",
ServletResolverConstants.SLING_SERVLET_METHODS + "=" + HttpConstants.METHOD_POST
}
)
public class ContentFragmentValidatorServlet extends SlingAllMethodsServlet {
@Override
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
// Get the unique ID from the request
String uniqueId = request.getParameter("uniqueId");
// Check if the unique ID already exists
if (isUniqueIdExists(uniqueId)) {
response.setStatus(HttpServletResponse.SC_CONFLICT); // 409 Conflict
response.getWriter().write("Error: Content Fragment with the provided unique ID already exists.");
} else {
// Proceed with creating the content fragment
// Your code to create content fragment goes here
response.setStatus(HttpServletResponse.SC_CREATED); // 201 Created
response.getWriter().write("Content Fragment created successfully.");
}
}
private boolean isUniqueIdExists(String uniqueId) {
// Logic to check if the unique ID already exists in the repository
// You can use ResourceResolver to query the repository for existing content fragments
// Return true if unique ID exists, false otherwise
}
}
In this example, the servlet intercepts POST requests made to /bin/contentFragmentValidator. It retrieves the unique ID from the request parameters and checks if it already exists in the repository. If the unique ID exists, it returns a 409 Conflict response with an error message. Otherwise, it proceeds with creating the content fragment.
You'll need to customize isUniqueIdExists method to query the repository and check if the unique ID exists. You can use ResourceResolver to perform the query.
Remember to deploy this servlet to your AEM instance and ensure proper error handling and security measures are implemented as per your requirements.