Saturday, July 30, 2022

Cloud Configs for CIF- Store View

 You can setup multi store views in Magneto and connect with AEM sites. Follow CIF-Add-On and create factory configurations for GraphQL. So let's start.

1) Create OSGI Configs for multi stores. for eg : you have 2 stores default and mystore then create 2 OSGI configs as per run-modes

In ui.config package , create json configs like below

com.adobe.cq.commerce.graphql.client.impl.GraphqlClientImpl~default.cfg.json


{
"url": "https://localhost.magento2brand1.com/graphql",
"identifier" : "default",
"httpMethod": "POST",
"allowHttpProtocol": "true",
"connectionTimeout": 1000000,
"requestPoolTimeout": 500000,
"socketTimeout": 1000000
}

com.adobe.cq.commerce.graphql.client.impl.GraphqlClientImpl~mystore.cfg.json

{
"url": "https://localhost.magento2brand2.com/graphql",
"identifier" : "mystore",
"httpMethod": "POST",
"allowHttpProtocol": "true",
"connectionTimeout": 1000000,
"requestPoolTimeout": 500000,
"socketTimeout": 1000000
}

Follow below steps to do CIF Cloud configurations.

Step-1:

If you already have created config folder under /conf folder from codebase then its fine. Otherwise follow below steps to create folder inside /conf

    • Open author instance http://localhost:4502/aem/start.html
    • Tools > General > Configuration Browser
Configuration Browser-AEM
Configuration Browser-AEM


Click Create and below pop-up opens

Create Configuration-AEM
Create Configuration-AEM


Enter title of the project for site-1 and Check Cloud Configurations(mandate for cloud configs) and other options for editable templates enablement check editable templates and so on..
Project Configuration
Project Configuration

Repeat step-1 for site-2


Step-2: Go to Tools>Cloud Services>CIF Configuration

CIF Configuration-AEM
CIF Configuration-AEM




Select your project created in Step-1

Configure Commerce Config
Configure Commerce Config



Click Create and select Commerce Configuration. It will create project specific commerce configuration.

Now Select/check commerce config created under <MyProject> and open properties. It will open below config window

GraphQL Store Configuration
GraphQL Store Configuration

Title- Same name of the configuration created for commerce(default). You can change this title.

 

GraphQL Client * - Select store as identifier from drop down. You have 2 OSGI configs. One is "default" and second is "mystore". Select store view as per your requirement for one of the site.

 


 

Store View- You can enter same name as in identifier or skip as it is optional.


Catalog Root identifier- default value

 

Language- Add language of the store view. In my case it is for “en” so selected  English(India)

Click Save and close.

Repeat step-2 for site-2

Step-3

Now open your site hierarchy. Go to http://localhost:4502/aem/start.html

  • Select Sites. Select your first website
  • Open properties
  • Click Advance Tab
  • Select cloud configuration from Cloud Configuration section(saved in step-2). It should be under /conf
Configuration at page properties
Configuration at page properties


Cloud config path under /conf
  • Save & Close.

Do same configuration for site-2

Now open your Product Listing Page(PLP) or detail page (PDP) to check your realtime data coming from magento store views.


Happy Coding!


Thursday, July 7, 2022

Distributed Log Tracing

 In Springboot framework, Distributed Log Tracing is possible via Sleuth and Zipkin. In this blog we would be focusing for Sleuth and Zipkin and why these are required.


So lets start!


Monday, July 4, 2022

OSGI Annotations

  In last blog we discussed how to register a servlet in AEM. Below are the    OSGI       annotations used in servlet.

  • @Component annotation - This OSGi annotation signifies that the class is a Service component and is not processed at runtime by a Service Component Runtime (SCR) implementation. It must be processed by tools and used to add a Component Description to the bundle.

  • service property - This signifies the types under which to register this Component as a service. In our case, the component is registered under the Servlet type. 

  • Constants.SERVICE_DESCRIPTION - Defines standard names for the OSGi environment system properties, service properties, and Manifest header attribute keys. In our case, we are defining the description of our servlet.

  • sling.servlet.methods - Defines which methods this servlet will use. In our case, we are using the HTTP GET method.

  • sling.servlet.resourceTypes - This property defines how are we accessing the servlet. There are two ways - via resource types and via paths. We will discuss this in the next section.

  • sling.servlet.extensions - The request URL extensions supported by the servlet for requests. The property value must either be a single String, an array of Strings or a Vector of Strings. This property is only considered for the registration with sling.servlet.resourceTypes.
Happy learning!

 


Sling Servlets in AEM

 

What are Servlets?

 

Java Servlets are programs that run on a Web or Application server and act as a middle layer between a requests coming from a Web browser or other HTTP client and databases or applications on the HTTP server.

 

A Servlet is a class used to extend the capabilities of servers that host applications accessed by means of a request-response programming model. For such applications, Servlet technology defines HTTP-specific servlet classes.

 

All servlets must implement the Servlet interface, which defines life-cycle methods.
When implementing a generic service, we can use or extend the GenericServlet class provided with the Java Servlet API. The HttpServlet class provides methods, such as doGet() and doPost(), for handling HTTP-specific services.

 

Servlet Registration

 

For a Servlet registered as an OSGi service to be used by the Sling Servlet Resolver, either one or both of the sling.servlet.paths or the sling.servlet.resourceTypes service reference properties must be set. If neither is set, the Servlet service is ignored. 

A Sling servlet can be registered in two ways - 

1.    Using Resource Types - Using this way, we use the sling:resourceType property of the node. For this, we need to hit the path in the browser for which the sling:resourceType is the given one.

2.    Using Paths - Using this way, we can directly use the path specified in the request and our servlet will be executed.

 

Types of Servlets

There are two types of servlets in Sling which are nothing but the classes we need to extend while creating our servlet.

  • SlingSafeMethodsServlet - If we want to use only the read-only methods then we use this. This base class is actually just a better implementation of the Servlet API HttpServlet class which accounts for extensibility. 
    Supported Methods:
  •     GET
  •     HEAD
  •     Options
  •     etc

  • SlingAllMethodsServlet - If we want to use methods that write as well, then we use this. This class extends the SlingSafeMethodsServlet by support for the below methods.
Supported Methods:

  • POST
  • PUT
  • DELETE

 For Annotations refer here.

Sling Servlets

-Using Resource Path
import lombok.extern.slf4j.Slf4j;
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.SlingSafeMethodsServlet;
import org.osgi.framework.Constants;
import org.osgi.service.component.annotations.Component;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import java.io.IOException;

@Slf4j
@Component(service = Servlet.class, property = {Constants.SERVICE_DESCRIPTION + "=Test Service",
"sling.servlet.methods=" + HttpConstants.METHOD_GET, "sling.servlet.paths=" + "/bin/testget"})
public class SlingServletResourceType extends SlingSafeMethodsServlet {

private static final long serialVersionUID = 1L;

@Override
protected void doGet(final SlingHttpServletRequest request,
final SlingHttpServletResponse response) throws ServletException, IOException {
String responseString = "response json";
response.getWriter().println(responseString);
}
}
Deploy code on AEM

How to call?

http://<host>:<port>/bin/testget

host : localhost

port: 4502 for author

-Using Resource Type-

import com.day.cq.commons.jcr.JcrConstants;
import lombok.extern.slf4j.Slf4j;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.osgi.framework.Constants;
import org.osgi.service.component.annotations.Component;

import javax.servlet.Servlet;
import java.io.IOException;

@Slf4j
@Component(service = Servlet.class, property = {Constants.SERVICE_DESCRIPTION + "=Test Service",
"sling.servlet.resourceTypes="+ "myproject/components/structure/page",
"sling.servlet.extensions=" + "txt"})
class SlingServletResourceTypeExample extends SlingSafeMethodsServlet {

private static final long serialVersionUID = 1L;

@Override
protected void doGet(final SlingHttpServletRequest request,
final SlingHttpServletResponse response) throws IOException {
final Resource resource = request.getResource();
response.setContentType("text/plain");
response.getWriter().write("Title = " + resource.getValueMap().get(JcrConstants.JCR_TITLE));
}
}


Happy Coding!

ElasticSearch vs Apache SolrCloud

  Elasticsearch and Apache SolrCloud are both powerful, distributed search engines built on top of Apache Lucene. Both Elasticsearch and S...