Search in sources :

Example 1 with ComponentExporter

use of com.adobe.cq.export.json.ComponentExporter in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class ContentFragmentImplTest method getElementsType.

@Test
void getElementsType() {
    ContentFragment fragment = getModelInstanceUnderTest(CF_TEXT_ONLY);
    final Map<String, ? extends ComponentExporter> elements = fragment.getExportedElements();
    assertNotNull(elements);
    final ComponentExporter mainElement = elements.get("main");
    assertEquals("text/html", mainElement.getExportedType());
}
Also used : DAMContentFragment(com.adobe.cq.wcm.core.components.models.contentfragment.DAMContentFragment) ContentFragment(com.adobe.cq.wcm.core.components.models.contentfragment.ContentFragment) ComponentExporter(com.adobe.cq.export.json.ComponentExporter) Test(org.junit.jupiter.api.Test)

Example 2 with ComponentExporter

use of com.adobe.cq.export.json.ComponentExporter 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)

Example 3 with ComponentExporter

use of com.adobe.cq.export.json.ComponentExporter in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class AbstractContainerImpl method getItemModels.

/**
 * Get the models for the child resources as provided by {@link AbstractContainerImpl#getFilteredChildren()}.
 *
 * @param request The current request.
 * @param modelClass The child model class.
 * @return Map of models wherein the key is the child name, and the value is it's model.
 */
protected Map<String, ComponentExporter> getItemModels(@NotNull final SlingHttpServletRequest request, @NotNull final Class<ComponentExporter> modelClass) {
    Map<String, ComponentExporter> models = new LinkedHashMap<>();
    getFilteredChildren().forEach(child -> {
        ComponentExporter model = modelFactory.getModelFromWrappedRequest(request, child, modelClass);
        if (model != null) {
            models.put(child.getName(), model);
        }
    });
    return models;
}
Also used : ComponentExporter(com.adobe.cq.export.json.ComponentExporter) LinkedHashMap(java.util.LinkedHashMap)

Example 4 with ComponentExporter

use of com.adobe.cq.export.json.ComponentExporter in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class ContentFragmentUtilsTest method getComponentExport.

@Test
public void getComponentExport() {
    // GIVEN
    AemContext slingContext = CoreComponentTestContext.newAemContext();
    slingContext.load().json(this.getClass().getResourceAsStream("foo.json"), "/foo");
    MockSlingHttpServletRequest slingHttpServletRequest = new MockSlingHttpServletRequest(slingContext.bundleContext());
    ComponentExporter componentExporter = new TestComponentExporter();
    ModelFactory modelFactory = Mockito.mock(ModelFactory.class);
    Mockito.when(modelFactory.getModelFromWrappedRequest(Mockito.any(), Mockito.any(), Mockito.eq(ComponentExporter.class))).thenReturn(componentExporter);
    // WHEN
    Map<String, ComponentExporter> exporterMap = ContentFragmentUtils.getComponentExporters(slingContext.resourceResolver().getResource("/foo").listChildren(), modelFactory, slingHttpServletRequest, null);
    // THEN
    Assertions.assertEquals(componentExporter, exporterMap.get("bar"));
    Assertions.assertEquals(componentExporter, exporterMap.get("qux"));
}
Also used : AemContext(io.wcm.testing.mock.aem.junit5.AemContext) MockSlingHttpServletRequest(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest) ComponentExporter(com.adobe.cq.export.json.ComponentExporter) ModelFactory(org.apache.sling.models.factory.ModelFactory) Test(org.junit.jupiter.api.Test)

Aggregations

ComponentExporter (com.adobe.cq.export.json.ComponentExporter)4 LinkedHashMap (java.util.LinkedHashMap)2 Test (org.junit.jupiter.api.Test)2 ContentFragment (com.adobe.cq.wcm.core.components.models.contentfragment.ContentFragment)1 DAMContentFragment (com.adobe.cq.wcm.core.components.models.contentfragment.DAMContentFragment)1 AemContext (io.wcm.testing.mock.aem.junit5.AemContext)1 SlingHttpServletRequest (org.apache.sling.api.SlingHttpServletRequest)1 Resource (org.apache.sling.api.resource.Resource)1 SlingHttpServletRequestWrapper (org.apache.sling.api.wrappers.SlingHttpServletRequestWrapper)1 ModelFactory (org.apache.sling.models.factory.ModelFactory)1 MockSlingHttpServletRequest (org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest)1 NotNull (org.jetbrains.annotations.NotNull)1