Expand my Community achievements bar.

July 31st AEM Gems Webinar: Elevate your AEM development to master the integration of private GitHub repositories within AEM Cloud Manager.

Touch UI Validate Asset file names on Upload


Level 3

Hi All,


I want to validate asset file names at the time of upload asset in dam.

When uploading new asset in DAM can there be a validation script which will only allow the supported (by AEM and Dynamic Media) characters within the file name. file name only allow these  a-z A-Z - _ 0-9.


I have try one solution but that is not working.



Please could anyone suggest how can i do that.




1 Accepted Solution


Correct answer by
Community Advisor
6 Replies


Community Advisor

Hello @user00181 - 


You can write/customize the below script further as per your use-case : 


package com.yourpackage;

import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import java.io.IOException;
import java.util.regex.Pattern;

public class CustomAssetUploadServlet extends SlingAllMethodsServlet implements Servlet {

    private static final Pattern FILENAME_PATTERN = Pattern.compile("^[a-zA-Z0-9-_]+$");

    protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
        // Get the uploaded file name
        String fileName = request.getRequestPathInfo().getSuffix();

        // Validate the file name against supported characters
        if (!isValidFileName(fileName)) {
            response.sendError(SlingHttpServletResponse.SC_BAD_REQUEST, "Invalid file name. File names can only contain a-z, A-Z, 0-9, hyphen (-), and underscore (_).");

        // Continue with the default asset upload logic
        super.doPost(request, response);

    private boolean isValidFileName(String fileName) {
        return StringUtils.isNotBlank(fileName) && FILENAME_PATTERN.matcher(fileName).matches();



Level 3

How I can call this class at the time of assert upload in Dam.


Community Advisor

@user00181  - 


Develop a custom workflow process in AEM that intercepts the asset upload operation and performs the file name validation.


  • Implement a custom Java class that represents the workflow process step responsible for file name validation. This class should extend the AbstractWorkflowProcess or AbstractAssetWorkflowProcess class provided by AEM.


Correct answer by
Community Advisor


Employee Advisor



o validate asset file names during upload in AEM DAM, develop a custom OSGi bundle implementing AssetUploadNameValidator interface. Override the validate method to check for unsupported characters. Register the bundle as an OSGi service and configure it in AEM's OSGi settings.