Friday, December 22, 2017

Approach to 301 redirect the internal URL's to dynamic SEO friendly URL - Adobe Experience Manager

Approach to 301 redirect the internal URL's to dynamic SEO friendly URL - Adobe Experience Manager

This post will explain the approach to 301 redirect the internal URL's to dynamic SEO friendly URL.

We had a scenario to display the SEO friendly URL to the user instead of displaying the internal URL for the PDP pages in eCommerce website - The SEO friendly URL is formed dynamically based on the product title and format from Hybris.

The internal product URL is - /en/test/pdp/book.html?id=123(Internal Publisher page that handle the request based on the product id)
New SEO friendly URL - /en/test/Sample_Book/book/pdp.html?id=123(Dynamic URL and should be rewritten to internal URL to process by publisher )

When ever user access the SEO specific URL(/en/test/Sample_Book/book/pdp.html?id=123) in the browser the user should be redirected to internal URL(/en/test/pdp/book.html?id=123) without changing the Browser URL.

Also, the user should be 301 redirected to SEO friendly URL while accessing the internal URL directly from the browser.

Redirect the internal URL to new Dynamic URL:


As the new URL is dynamic based on the product title and product format` from Hybris, the dispatcher rules cant be defined to handle the redirect from internal URL to new SEO friendly dynamic URL.

The alternative solution is define a Request filter that will form the dynamic URL by fetching the product tile from Hybris and 301 redirect to new URL.

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;

import org.apache.felix.scr.annotations.sling.SlingFilter;
import org.apache.felix.scr.annotations.sling.SlingFilterScope;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;

@SlingFilter(label = "301 URL redirector", description = "301 URL redirector", metatype = true, generateComponent = true, // True if you want to leverage activate/deactivate
generateService = true, order = 0, scope = SlingFilterScope.REQUEST)
public class Redirect301HandlerFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {

final SlingHttpServletRequest slingRequest = (SlingHttpServletRequest) request;
final SlingHttpServletResponse slingResponse = (SlingHttpServletResponse) response;
String requestPath= slingRequest.getRequestURI();
String pid = slingRequest.getParameter("pid");
if (!requestPath.contains("/pdp/") || slingRequest.getParameter("isInternalRedirect")!=null) {
chain.doFilter(request, response);
return;
}

String title = "";// Get the product tile and product format from Hybris based on the pid
String format = "";//// Get the product tile and product format from Hybris based on the pid

String newURL = slingRequest.getScheme() + "://" + slingRequest.getServerName() + ":"+ slingRequest.getServerPort() + "/en/test/" + title + "/" + format + "/pdp.html?pid=" + pid;

slingResponse.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
slingResponse.setHeader("Location", newURL);
}

@Override
public void destroy() {
// TODO Auto-generated method stub
}
}

Rewrite the SEO friendly URL to internal URL:


The SEO friendly URL is not recognized by publisher so rewrite the SEO friendly URL to internal URL in dispatcher without changing browser URL.The below post provides the steps to rewrite the URL to internal URL without changing the browser URL.

https://www.albinsblog.com/2017/11/rewrite-rules-with-ptpass-through-flag-notwork-in-aem.html

Add a additional parameter isInternalRedirect while rewriting the SEO URL to internal URL from Dispatcher to avoid the redirect loop

RewriteCond %{QUERY_STRING} ^id=(.*) [NC]
RewriteRule ^/en/test/(.*)/(.*)/pdp.html$ /en/test/pdp/$2.html?isInternalRedirect=true&id=%1 [PT,L]

Monday, December 18, 2017

How to get the Inherited Properties in Adobe Experience Manager Servlet's or Filter's

How to get the Inherited Properties in Adobe Experience Manager Servlet's or Filter's

This post will explain the approach to get the inherited properties in AEM servlet's or Filter's.

Sample Filter - Retrieves the inherited page properties and component properties:


HierarchyNodeInheritanceValueMap - This will help to retrieve the inherited properties from parent pages.

While accessing the property prop1 from /content/site/en/test/jcr:content - /content/site/en/test/jcr:content/@prop1,the system searches in the following paths

/content/site/en/test/jcr:content/@prop1
/content/site/en/jcr:content/@prop1
/content/site/jcr:content/@prop1

If the prop1 is located in any of the above paths then the corresponding values is returned, if not it will return null value.

ComponentInheritanceValueMap - This will help to retrieve the inherited properties from parent components.

While accessing the prop2 property from /content/site/en/test/jcr:content/T01_region4/titletext_e8d0 - /content/site/en/test/jcr:content/T01_region4/titletext_e8d0/@prop2,the system searches in the following paths

/content/site/en/test/jcr:content/T01_region4/titletext_e8d0/@prop2
/content/site/en/test/jcr:content/T01_region4/@prop2
/content/site/en/test/jcr:content/@prop2

If the prop2 is located in any of the above paths then the corresponding values is returned, if not it will return null value.

import java.io.IOException;

import javax.servlet.*;
import javax.servlet.http.HttpServletResponse;

import org.apache.felix.scr.annotations.*;
import org.apache.felix.scr.annotations.sling.SlingFilter;
import org.apache.felix.scr.annotations.sling.SlingFilterScope;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;

import com.day.cq.commons.inherit.ComponentInheritanceValueMap;
import com.day.cq.commons.inherit.HierarchyNodeInheritanceValueMap;
import com.day.cq.commons.inherit.InheritanceValueMap;

@SlingFilter(label = "301 URL redirector", description = "301 URL redirector", metatype = true, generateComponent = true,
generateService = true, order = 0, scope = SlingFilterScope.REQUEST)
public class Redirect301HandlerFilter implements Filter {

@Override
public void init(FilterConfig filterConfig) throws ServletException {

}

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

final SlingHttpServletRequest slingRequest = (SlingHttpServletRequest) request;
final SlingHttpServletResponse slingResponse = (SlingHttpServletResponse) response;
String resourcePathURL = slingRequest.getResource().getPath();

ResourceResolver resourceResolver = slingRequest.getResourceResolver();
String resourceContentPath = resourcePathURL.split(".html")[0] + "/jcr:content";
Resource res = resourceResolver.getResource(resourceContentPath);

InheritanceValueMap inheritedProp = new HierarchyNodeInheritanceValueMap(res);
String prop1 = inheritedProp.getInherited("prop1", String.class);

res = resourceResolver.getResource(resourceContentPath+"/T01_region4/titletext_e8d0");

InheritanceValueMap inheritedPropCom = new ComponentInheritanceValueMap(res);
String prop2 = inheritedPropCom.getInherited("prop2", String.class);

chain.doFilter(slingRequest, slingResponse);

}

@Override
public void destroy() {
// TODO Auto-generated method stub

}

}

Thursday, December 14, 2017

com.day.cq.searchpromote.Search ParseError at [row,col]:[1,1] Message: Content is not allowed in prolog - Adobe Search and Promote

com.day.cq.searchpromote.Search ParseError at [row,col]:[1,1] Message: Content is not allowed in prolog - Adobe Search and Promote

I was getting the below exception while searching the documents from Search&Promote in Adobe Experience Manager website.

14.12.2017 17:49:43.040 *ERROR* [10.204.139.58 [1513273782091] GET /content/test/en/search.html HTTP/1.1] com.day.cq.searchpromote.Search ParseError at [row,col]:[1,1]
Message: Content is not allowed in prolog.
javax.xml.stream.XMLStreamException: ParseError at [row,col]:[1,1]
Message: Content is not allowed in prolog.
        at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.next(XMLStreamReaderImpl.java:596)
        at com.sun.xml.internal.stream.XMLEventReaderImpl.nextEvent(XMLEventReaderImpl.java:83)
        at com.day.cq.searchpromote.xml.result.ResultParser.getNextEvent(ResultParser.java:61)
        at com.day.cq.searchpromote.xml.result.ResultParser.parse(ResultParser.java:48)
        at com.day.cq.searchpromote.Search.parseResults(Search.java:526)
        at com.day.cq.searchpromote.Search.<init>(Search.java:154)
        at com.tr.openweb.ecommerce.sly.search.SearchInitializer.activate(SearchInitializer.java:69)
        at com.tr.openweb.ecommerce.sly.search.SearchBarPage.activate(SearchBarPage.java:19)
        at com.adobe.cq.sightly.WCMUsePojo.init(WCMUsePojo.java:84)
        at org.apache.sling.scripting.sightly.impl.engine.extension.use.JavaUseProvider.provide(JavaUseProvider.java:119)
        at org.apache.sling.scripting.sightly.impl.engine.extension.use.UseRuntimeExtension.call(UseRuntimeExtension.java:84)
        at org.apache.sling.scripting.sightly.impl.engine.runtime.RenderContextImpl.call(RenderContextImpl.java:66)
        at org.apache.sling.scripting.sightly.apps.test.components.search.searchBarPage.SightlyJava_searchBarPage.render(SightlyJava_searchBarPage.java:44

I was not able to identify any configuration issues with AEM  or Search&Promote.

It looks to be the issue was caused due to the renaming of existing Search-Promote cloud configuration in AEM, the renaming of the configuration created some internal issue and due that the search was failing with parsing error, the issue got resolved after deleting and recreating the Search-Promote cloud configuration.