A Simple Example of Sling Pipes in AEM


With Sling Pipes it's very easy to update the AEM content. It is a tool for executing CURD operations over resources in AEM repository. Hope this will be helpful.

Plumber is OSGi service and it has newPipe method in which we need to pass resourceResolver. After that we can call several pipes or methods to achieve our objective. Some of them are-

echo(path) - to retrieve the root resource of the path.

write(config) - to write/update properties of a resource.

run() - to execute the code.


Use case: To update property of a node using Sling Pipe OSGi service.


 Dependencies required- 

1.  Sling Query (org.apache.sling.query-4.0.0)- 

Maven Repository: org.apache.sling » org.apache.sling.query » 4.0.0 (mvnrepository.com)

2, Sling Pipes (org.apache.sling.pipes-3.1.0)- 

Maven Repository: org.apache.sling » org.apache.sling.pipes » 3.1.0 (mvnrepository.com)

Either install the bundle of respective dependency in console or add them in the project's pom.xml


System user - 

1. Create a system user from http://localhost:4502/crx/explorer/index.jsp

2. Add the configuration in  Apache sling Service User Mapper Service Amendment 


Example - customprojec.core:slingpipeservice=pipesystemuser

3. Provide permission for the content path to system user


Service-

public interface SlingPipesExamples {

public void pipeExample();

}

Implementation-


import java.util.HashMap;

import java.util.Map;

import org.apache.sling.api.resource.LoginException;

import org.apache.sling.api.resource.ResourceResolver;

import org.apache.sling.api.resource.ResourceResolverFactory;

import org.osgi.service.component.annotations.Component;

import org.osgi.service.component.annotations.Reference;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.apache.sling.pipes.ExecutionResult;

import org.apache.sling.pipes.Plumber;

@Component(service = SlingPipesExamples.class, immediate = true)

public class SlingPipeExample implements SlingPipesExamples {

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

   @Reference

ResourceResolverFactory factory;

    //method to create resource resolver

    public ResourceResolver createResolver() {

Map<String, Object> map = new HashMap<>();

map.put(ResourceResolverFactory.SUBSERVICE, "testSystemUser");

ResourceResolver resolver = null;

try {

resolver = factory.getServiceResourceResolver(map);

} catch (LoginException e) {

LOG.info("excepion occurred" + e);

}

return resolver;

}


@Reference

    Plumber plumber;

    @Override

    public void pipeExample() {

        try 

        {       

    ResourceResolver resolver = createResolver();          

            LOG.info("getting pipe created");


            ExecutionResult result = plumber.newPipe(resolver)

            .echo("/content/we-retail")

            .run();     

            LOG.info("printing result"+ result.toString());

           

            //updae property - imageRotate of image node

            String path = "/content/we-retail/jcr:content/image";

            plumber.newPipe(resolver)

            .echo(path)

            .write("imageRotate","testval2")

            .run();

            LOG.info("update operation done");

        } catch ( Exception e) {

        LOG.error("Exception during creating service resource resolver ", e);

        }

    }

}


Servlet-

import java.io.IOException;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.HttpConstants;
import org.apache.sling.api.servlets.ServletResolverConstants;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.osgi.framework.Constants;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.customproject.core.services.CustomProjectService;

@Component(service = Servlet.class, immediate = true, property = { Constants.SERVICE_DESCRIPTION + "=Custom Servlet",
ServletResolverConstants.SLING_SERVLET_PATHS + "=/bin/customservlet",
ServletResolverConstants.SLING_SERVLET_METHODS + "=" + HttpConstants.METHOD_GET })
public class CustomServlet extends SlingSafeMethodsServlet {

/**
*/
private static final long serialVersionUID = 1L;
@Reference
CustomProjectService cps;
private static final Logger LOG = LoggerFactory.getLogger(CustomServlet.class);

@Override
protected void doGet(final SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {

LOG.info("In CustomServlet doGet method");
//sling pipe method called
cps.pipeExample();
response.setContentType("application/json");
response.getWriter().write("servlet call successful");
}
}


Result-


References-












Comments

Popular posts from this blog

Use cases of ModifiableValueMap in AEM

Run modes in AEM