AEM 6460 - Force open a specific metadata schema while editing assets in bulk

Goal


While bulk editing assets (eg. by selecting random assets in search results) if the usecase is to force load a specific schema, try the following sling filter...

In the following example, opening metadata editor http://localhost:4502/mnt/overlay/dam/gui/content/assets/metadataeditor.external.html by selecting multiple assets always shows the schema experience-aem-schema

Solution


Create a Sling filter with the following code....

package com.experienceaem.assets;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.request.RequestParameter;
import org.apache.sling.api.wrappers.SlingHttpServletRequestWrapper;
import org.osgi.framework.Constants;
import org.osgi.service.component.annotations.Component;

import javax.servlet.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

@Component(
        service = Filter.class,
        immediate = true,
        name = "Force the metadata schema editor to load Experience AEM Schema",
        property = {
                Constants.SERVICE_RANKING + ":Integer=-99",
                "sling.filter.scope=COMPONENT",
                "sling.filter.pattern=^(\\/mnt\\/overlay\\/dam\\/gui\\/content\\/assets\\/metadataeditor).*$"
        }
)
public class ForceMetadataSchemaFilter implements Filter {

    private final String FORCED_FORM_TYPE_NAME = "forcedFormTypeName";

    private final String EXPERIENCE_AEM_SCHEMA = "/experience-aem-schema";

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        chain.doFilter(new MetadataSchemaRequestWrapper((SlingHttpServletRequest) request), response);
    }

    @Override
    public void destroy() {
    }

    private class MetadataSchemaRequestWrapper extends SlingHttpServletRequestWrapper {
        public MetadataSchemaRequestWrapper(final SlingHttpServletRequest request) {
            super(request);
        }

        @Override
        public RequestParameter getRequestParameter(String paramName) {
            RequestParameter requestParam = super.getRequestParameter(paramName);

            if(!FORCED_FORM_TYPE_NAME.equals(paramName)){
                return requestParam;
            }

            String[] content = (String[])super.getAttribute("aem.assets.ui.properties.content");

            if(ArrayUtils.isEmpty(content) || (content.length == 1)){
                return requestParam;
            }

            requestParam = new RequestParameter() {
                @Override
                public String getName() {
                    return FORCED_FORM_TYPE_NAME;
                }

                @Override
                public boolean isFormField() {
                    return true;
                }

                @Override
                public String getContentType() {
                    return null;
                }

                @Override
                public long getSize() {
                    return 0;
                }

                @Override
                public byte[] get() {
                    return new byte[0];
                }

                @Override
                public InputStream getInputStream() throws IOException {
                    return null;
                }

                @Override
                public String getFileName() {
                    return null;
                }

                @Override
                public String getString() {
                    return EXPERIENCE_AEM_SCHEMA;
                }

                @Override
                public String getString(String s) throws UnsupportedEncodingException {
                    return EXPERIENCE_AEM_SCHEMA;
                }

                @Override
                public String toString() {
                    return EXPERIENCE_AEM_SCHEMA;
                }
            };

            return requestParam;
        }
    }
}

No comments:

Post a Comment