Sunday 22 November 2015

UTF- 8 Encoding in AEM

Set UTF-8 as Default Encoding : 

If we do not provide any charset encoding,  AEM uses " ISO-8859-1" as default encoding since this is mandated by Servlet API. But most of the times we need "UTF-8" encoding to support special characters and symbols in our multi lingual web sites.

Default Encoding can be configured within the OSGi configuration available http://localhost:4502/system/console/configMgr

AEM 6.1 : Go to Apache Sling Request Parameter Handling . Change the Default Parameter Encoding to "UTF-8"





AEM 5.6 : Go to Apache Sling Main Servlet . Change the Default Parameter Encoding to "UTF-8"




Encoding & Decoding while posting data to Sling Servlet :

Decoding  in the Servlet.

String id = java.net.URLDecoder.decode(request.getParameter("id"), "UTF-8");

Encoding data before posting to a  Servlet

String id= java.net.URLEncoder.encode("cust®", "UTF-8"); 

Set charset encoding in a JSP :


<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib uri="/libs/CFC/resources/jstl/c.tld" prefix="c" %>
<form method="post">
<input name="searchterm" value="<c:out value="${param.searchterm}" />" />
<input type="submit" />

</form>

We can even set encoding using response.setCharacterEncoding("UTF-8")




Querying in AEM

What can we query for ..?






In a relational database one can query against table columns. But in a hierarchical content repository what can we query for.. ?

Well a tree structure has nodes and node can properties as well as child nodes as shown in picture.And every node will have an absolute path which determines its location on the tree structure.

We can create conditions with these things and that is nothing but our query.




Query is simply a set of the conditions alias predicates. Predicates available within AEM are listed below


Query Predicates
Example
Node Name

Wild Card : *
Case Sensitive
nodename=myapp
nodename=myapp* // searches for any or no character after myapp
nodename=myapp? // searches for any character  after myapp

Node Type
type=cq:Page
type=nt:unstructured
Property

Wild card : %
Case Sensitive
property=jcr:title
property.value=catalog
property.operation=equals (default if not provided)

property=jcr:title
property.value=cat% 
property.operation=like
Path
path=/content/myapp/products/catalog/en
Full Text
(Performs a word search in the  entire repository, or within a path if provided)

Wild Card: *
Non Case Sensitive
fulltext=catalog

path=/content/myapp/products
fulltext=cat*
fulltext.relPath=jcr:title

Full text searches all the content, if we need to search for a particular property relPath can be used



How do we Query ? 

AEM provides us Query Builder API to write the queries. Query Builder is very easy to use and it is a wrapper around the actual query language like XPATH, SQL2 etc. 

You just require all of our query parameters put to a map and Java Query Builder API does the rest. 

 Map<String,String> map = new HashMap<String,String> ();
 map.put("path", "
/content/myapp/products");
 map.put("type", "nt:file");

 Query query = builder.createQuery(PredicateGroup.create(map), session);
 SearchResult searchResult   =  query.getResult();
 List<Hit> resultHits   =  searchResult .getHits();


Now we can iterate through the result hits and achieve what we want.


You can find a bit more at Query Builder API