Search in sources :

Example 1 with SlingHttpServletRequestWrapper

use of org.apache.sling.api.wrappers.SlingHttpServletRequestWrapper in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class ContentFragmentUtils method getComponentExporters.

/**
 * Gets a map of all resources with the resource name as the key and the corresponding {@link ComponentExporter}
 * model as the value.
 *
 * The returned map is ordered as provided by the resourceIterator.
 * Any resource that cannot be adapted to a {@link ComponentExporter} model is omitted from the returned map.
 *
 * @param resourceIterator Iterator of resources for which to get the component exporters.
 * @param modelFactory Model factory service.
 * @param slingHttpServletRequest Current request.
 * @param callerResource The page or template resource that references the experience fragment or content fragment.
 * @return Ordered map of resource names to {@link ComponentExporter} models.
 */
@NotNull
public static LinkedHashMap<String, ComponentExporter> getComponentExporters(@NotNull final Iterator<Resource> resourceIterator, @NotNull final ModelFactory modelFactory, @NotNull final SlingHttpServletRequest slingHttpServletRequest, @NotNull final Resource callerResource) {
    final LinkedHashMap<String, ComponentExporter> componentExporterMap = new LinkedHashMap<>();
    SlingHttpServletRequest wrappedSlingHttpServletRequest = new SlingHttpServletRequestWrapper(slingHttpServletRequest) {

        @Override
        public Object getAttribute(String name) {
            if (ATTR_RESOURCE_CALLER_PATH.equals(name)) {
                String resourceCallerPath = (String) super.getAttribute(ATTR_RESOURCE_CALLER_PATH);
                // that.
                return (resourceCallerPath != null) ? resourceCallerPath : callerResource.getPath();
            }
            return super.getAttribute(name);
        }
    };
    while (resourceIterator.hasNext()) {
        Resource resource = resourceIterator.next();
        ComponentExporter exporter = modelFactory.getModelFromWrappedRequest(wrappedSlingHttpServletRequest, resource, ComponentExporter.class);
        if (exporter != null) {
            String name = resource.getName();
            if (componentExporterMap.put(name, exporter) != null) {
                throw new IllegalStateException(String.format("Duplicate key '%s'", name));
            }
        }
    }
    return componentExporterMap;
}
Also used : ComponentExporter(com.adobe.cq.export.json.ComponentExporter) Resource(org.apache.sling.api.resource.Resource) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) SlingHttpServletRequestWrapper(org.apache.sling.api.wrappers.SlingHttpServletRequestWrapper) LinkedHashMap(java.util.LinkedHashMap) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

ComponentExporter (com.adobe.cq.export.json.ComponentExporter)1 LinkedHashMap (java.util.LinkedHashMap)1 SlingHttpServletRequest (org.apache.sling.api.SlingHttpServletRequest)1 Resource (org.apache.sling.api.resource.Resource)1 SlingHttpServletRequestWrapper (org.apache.sling.api.wrappers.SlingHttpServletRequestWrapper)1 NotNull (org.jetbrains.annotations.NotNull)1