Thursday, July 4, 2019

How to design the AEM website using Editable Template and Layout Container

How to design the AEM website using Editable Template and Layout Container

This video explains How to design the AEM website using Editable Template and Layout Container




Tuesday, June 25, 2019

How to add a custom thumbnail to AEM(Adobe Experience Manager) content packages?

This post explains the steps to add a custom thumbnail to AEM(Adobe Experience Manager) content packages.

Create an image named thumbnail.png -the recommended dimensions are 64 x 64 pixels.

Create a new folder named definition under: <ui.apps or ui.content>/src/main/content/META-INF/vault/definition

Add the thumbnail to the definition folder.

Inside the definition folder, add a file named .content.xml. Populate it with the following:

 <?xml version="1.0" encoding="UTF-8"?>
 <jcr:root xmlns:vlt="http://www.day.com/jcr/vault/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0" xmlns:nt="http://www.jcp.org/jcr/nt/1.0">
     <thumbnail.png/>
 </jcr:root>

Custom-thumbnail-aem-content-packages

 Build&deploy the package - mvn clean install -PautoInstallPackage

 Verify the custom thumbnail from package manager.

Custom-thumbnail-aem-content-packages



Saturday, June 22, 2019

Error extracting plugin descriptor: 'Goal: * already exists in the plugin descriptor for prefix: *

 Error extracting plugin descriptor: 'Goal: validate already exists in the plugin descriptor for prefix: test

I was getting the below error while installing the Maven plugin - mvn clean install

maven-plugin-error

org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-plugin-plugin:3.2:descriptor (default-descriptor) on project test: Error extracting plugin descriptor: 'Goal: validate already exists in the plugin descriptor for prefix: test
Existing implementation is: test.MyMojo
Conflicting implementation is: test.MyMojo'
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:213)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:154)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:146)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:51)

The configurations are below

MyMojo.java

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

/**
 * Goal which validate the content.
 * @goal validate
 * @phase VERIFY
 */

@Mojo (name="validate", defaultPhase=LifecyclePhase.VERIFY, requiresProject=false )
public class MyMojo extends AbstractMojo
{
    @Parameter (property="message", defaultValue="Default Message")
    private String message;

    public void execute() throws MojoExecutionException
    {
        System.console().writer().println(message);
    }
}

Pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>test</groupId>
  <artifactId>test</artifactId>
  <packaging>maven-plugin</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>test Maven Mojo</name>
  <url>http://maven.apache.org</url>
  
    <properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>
  
  <dependencies>
    <dependency>
      <groupId>org.apache.maven</groupId>
      <artifactId>maven-plugin-api</artifactId>
      <version>3.0</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  <dependency>
      <groupId>org.apache.maven.plugin-tools</groupId>
      <artifactId>maven-plugin-annotations</artifactId>
      <version>3.4</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>org.apache.maven</groupId>
      <artifactId>maven-core</artifactId>
      <version>3.2.5</version>
      <scope>provided</scope>
    </dependency>
  </dependencies>
</project>

After analysis, the root cause is @goal validate in the Java annotation and the @Mojo setting (name="validate") generating the same goal - validate and failing with the duplicate exception.

/**
 * Goal which validate the content.
 * @goal validate
 * @phase VERIFY
 */

@Mojo (name="validate", defaultPhase=LifecyclePhase.VERIFY, requiresProject=false )

The deployment is success after removing the @goal validate from the java annotation.

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

/**
 * Goal which validate the content.
 * @phase VERIFY
 */

@Mojo (name="validate", defaultPhase=LifecyclePhase.VERIFY, requiresProject=false )
public class MyMojo extends AbstractMojo
{
    @Parameter (property="message", defaultValue="Default Message")
    private String message;

    public void execute() throws MojoExecutionException
    {
        System.console().writer().println(message);
    }
}



Friday, June 21, 2019

How to expose Restful Services through JAX-RS(Jersey) in AEM

How to expose Restful Services through JAX-RS(Jersey) in AEM


JAX-RS is a specification that provides portable API’s for developing, exposing and accessing web applications designed and implemented in compliance with principals of REST architectural style.

-RS has annotations to bind specific URI patterns and HTTP methods to individual methods of your Java code. AEM is driven based on REST principals but there is no direct support for creating Restful services.

There are multiple implementations for JAX-RS specifications, this tutorial explains exposing REST based services in AEM through Jersey.

Install JAX-RS Jersey modules


As a first step, install the JAX-RS Jersey connector to AEM server

The connector modules can be downloaded from the following URL — https://search.maven.org/search?q=g:com.eclipsesource.jaxrs, the required modules are jersey-all, publisher and provider-gson


jax-rs-OSGI-modules

Install the modules through OSGI console —


Ensure the bundles are active

AEM-OSGI-console-bundle-installation
AEM-OSGI-console-bundle-installation

JAX-RS Component


Create a component with JAX-RS annotations.

package jaxrsservice.core.restservices;

import javax.ws.rs.Path;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.io.IOException; 

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; 

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

@Component(service=RESTProductAPI.class)
@Path("/products")
public class RESTProductAPI {

@GET
@Path("/{catagroy}/{title}/p/{code : \\d{5}}")
@Produces({MediaType.TEXT_PLAIN})
public String getProductDetails(@Context HttpServletRequest request, @Context HttpServletResponse response,@PathParam("catagroy") String catagroy,@PathParam("title") String title,@PathParam("code") String code) {

return "code="+code+";catagroy="+catagroy+";title="+title; 

}

@POST
@Path("/create")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Product createProduct(Product product) {
product.setResult("Product Created");
product.setId("1");
return product;

}
}

Some of the important annotation used to define the component

@POST/@GET — HTTP Method supported by the method, the other HTTP methods can be used based on the requirement
 
@Path — URL path, static or dynamic regex based URL’s can be defined
 
@Produces — The output produced by the method e.g MediaType.APPLICATION_JSON and MediaType.TEXT_PLAIN etc
 
@Consumes — The input consumed by the method e.g MediaType.APPLICATION_JSON and MediaType.APPLICATION_FORM_URLENCODED etc

POJO class to map JSON input/Output

package jaxrsservice.core.restservices;

public class Product {

private String id;
private String name;
private String title;
private String category;
private String desc;
private String result;

public void setId(String id)
{
this.id=id;
}
public void setName(String name)
{
this.name=name;
}
public void setTitle(String title)
{
this.title=title;
}
public void setCategory(String category)
{
this.category=category;
}
public void setDesc(String desc)
{
this.desc=desc;
}

public void setResult(String result)
{
this.result=result;
}

public String getId()
{
return id;
}
public String getName()
{
return name;
}
public String getTitle()
{
return title;
}
public String getCategory()
{
return category;
}
public String getDesc()
{
return desc;
}

public String getResult()
{
return result;
}

}

Maven dependency to support JAX-RS annotations

<dependency>
 <groupId>javax.ws.rs</groupId>
 <artifactId>javax.ws.rs-api</artifactId>
 <version>2.0</version>
 </dependency>

Sample Code Repository


Deploy the component as a core module(execute mvn clean install -PautoInstallBundle from the core folder) and ensure the module is active

Now the service is ready to accept the requests and ready for testing


This will provide the following response - code=12345;catagroy=catagroyTest;title=sampleTitle

createProduct — http://localhost:4502/services/products/create (POST)

Tested through Postman

Sample Input:

{
“name”:”testName”,
“title”:”titleTest”,
“catagory”:”catagoryTest”,
“desc”:”descTest”
}

Sample Output:

{
“id”: “1”,
“name”: “testName”,
“title”: “titleTest”,
“desc”: “descTest”,
“result”: “Product Created”
}


postman-testing-post-service




By using JAX-RS(Jersey) OSGI modules the REST API’s can be exposed in AEM, JAX-RS provides more flexibility to define the REST services — handling different input/output types, mapping of inputs/outputs to/from POJO’s. Allows to define more dynamic URL paths to the REST services. AEM built on REST principles so by default supports the REST based services but JAX-RS can be used based on the use cases.


Saturday, June 15, 2019

AEM OAuth Server Functionalities - Deep Dive

AEM OAuth Server Functionalities - Deep Dive

This video explains how to use Adobe Granite OAuth Server functionalities to grant the resource access to external clients