April 7, 2020
Estimated Post Reading Time ~

Overview of AEM Fiddle Tool

Why AEM Fiddle?
AEM Fid­dle is a read–eval–print loop (REPL) devel­op­ment tool provided by ACS AEM Tools with­in the AEM envi­ron­ment. The intent of AEM Fiddle is to provide an accessible, developer-friendly environment for experimenting on AEM APIs and doing POCs tasks, etc.
Usually when one would like to explore/experiment on AEM APIs or if one would like to manipulate content in the JCR, call OSGi services, or execute arbitrary code using the Sling, or JCR APIs, etc., they need to write the code, build the bundle and then deploy it to see the results. But, using the AEM Fiddle, one can write JSP/Java/HTML/JS/ESP/ECMA script in the tool’s console and directly hit the “Run Code” option to see the results. That is, AEM Fiddle lets you write code, execute it, and immediately see the results!

Use cases: The following are a few use-cases where AEM Fiddle comes handy and is very much helpful.

As discussed above, when one is new to AEM and would like to explore various AEM APIs such as node API, they can write simply write some sample piece of code within the tool’s console and hit on “Run Code” option which will execute their piece of code.
If one would like to fire a query using query builder in order to update some properties on various nodes, they can just write their logic in the tool’s console and hit on “Run Code” option and can see that properties will get updated immediately as soon as the Run Code option is clicked.
Download and install: You can down­load this tool here. Once it is downloaded, it can be installed through the CRX Package Manager.
After installing the package,

Hit http://<host-name>:<port-no>/aem/start.html
Click on Tools icon and select “ACS AEM Tools” option.
Select “AEM Fiddle” from a list of available ACS AEM Tools.



AEM Fiddle console will be displayed as soon as one clicks on the AEM Fiddle option as described in the above steps.


One can switch between various languages as shown in the above screenshot by clicking on “+” icon on the top right corner of the screen.
For example, select Java. Write some sample code to create a node in AEM and set a property to it and click on “Run code” to see immediate results as shown in the below screenshot.

Verify result: Nodes will get created and also property will get set to the node as shown in the below screenshot.



Sample code: package apps.acs_002dtools.components.aemfiddle.fiddle;

import com.day.cq.dam.api.*;
import com.day.cq.search.*;
import com.day.cq.search.PredicateGroup;
import com.day.cq.search.Query;
import com.day.cq.search.QueryBuilder;
import com.day.cq.search.result.Hit;
import com.day.cq.search.result.SearchResult;
import com.day.cq.wcm.api.*;
import org.apache.felix.scr.annotations.*;
import org.apache.sling.api.*;
import org.apache.sling.api.resource.*;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.api.scripting.*;
import org.apache.sling.api.servlets.*;
import org.apache.sling.jcr.api.SlingRepository;
import org.osgi.framework.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.jcr.*;
import java.io.IOException;
import java.util.*;

public class fiddle extends SlingAllMethodsServlet {

private ResourceResolverFactory resolverFactory;
private SlingRepository repository;
private QueryBuilder builder;
private Session adminSession = null;
SlingScriptHelper scriptHelper = null;

private static final Logger LOG = LoggerFactory.getLogger(fiddle.class);

@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException {
SlingBindings bindings = (SlingBindings) request.getAttribute(SlingBindings.class.getName());
scriptHelper = bindings.getSling();
setReference();
addNodeAndSetProperty();
response.getWriter().println(“Hello from” + request.getResource().getPath());
}

public void addNodeAndSetProperty() {
try {
// Create a Session
LOG.info(“Inside doGet..!!”);
// adminSession = repository.loginService(null, null);
Map < String, Object > paramMap = new HashMap < String, Object > ();
ResourceResolver resourceResolver = resolverFactory.getAdministrativeResourceResolver(paramMap);
adminSession = resourceResolver.adaptTo(Session.class);

// Create a node that represents the root node
Node root = adminSession.getRootNode();

// Store content
Node adobe = root.addNode(“adobe”);
Node day = adobe.addNode(“cq”);
day.setProperty(“message”, “Adobe Experience Manager is part of the Adobe Digital Marketing Suite!”);

// Retrieve content
Node node = root.getNode(“adobe / cq”);
LOG.info(“Node path: {}”, node.getPath());
LOG.info(“getProperty: {}”, node.getProperty(“message”).getString());

// Save the session changes and log out
adminSession.save();
adminSession.logout();
} catch (Exception e) {
LOG.error(“Exception::“, e);
}
//add finally to close session
finally {
if (adminSession != null && adminSession.isLive()) {
adminSession.logout();
}
}
}

public void setReference() {
BundleContext bundleContext = FrameworkUtil.getBundle(SlingAllMethodsServlet.class).getBundleContext();
resolverFactory = scriptHelper.getService(ResourceResolverFactory.class);
repository = scriptHelper.getService(SlingRepository.class);
builder = scriptHelper.getService(QueryBuilder.class);
}

}


FYI: Code is taken from https://helpx.adobe.com/experience-manager/using/programmatically-accessing-cq-content-using.html

NOTE:
  • Fiddle can be saved for further reference. To save fiddle, select a hamburger icon on the top right corner of the screen and give a name to the fiddle. Click on “Save” to save the fiddle.
  • If you are using AEM 6.5, please try to install ACS AEM Tools v1.0.2 or later that version.
Source: https://aem.adobemarketingclub.com/aem-fiddle/


By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.