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());
}
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;
}
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;
}
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"));
}
Aggregations