Tuesday, March 3, 2015

How to Customize/Configure the 404 error handler for multi sites in AEM/Adobe CQ5?

How to Customize/Configure the 404 error handler for multi sites in AEM/Adobe CQ5?

This post will explain how to configure the 404 error handler in multi site scenario for Adobe Experience Manager(AEM) - This will configure different 404 pages for different sites..

If the error handler is configured for first time then copy /libs/sling/servlet/errorhandler to /apps/sling/servlet (create the folder structure before copying )

Modify /apps/sling/servlet/errorhandler/404.jsp file to modify the 404 error handling rules.

<%  
//setting response code as 404
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
try {
    String uri = request.getRequestURI();
         
    if(uri.matches("(/content/Sample/en/)(.*)"))
    {
        pageContext.include("/content/Sample/en/404.html");
    } else if(uri.matches("(/content/Sample/es/)(.*)"))
    {
        pageContext.include("/content/Sample/es/404.html");
    } else if(uri.matches("(/en/)(.*)"))
    {
        pageContext.include("/content/Sample/en/404.html");
    }
    else if(uri.matches("(/es/)(.*)"))
    {
        pageContext.include("/content/Sample/es/404.html");
    } else
    {
        pageContext.include("/content/Sample/en/404.html");
    }

} catch (Exception e) {

%>
        Page Not Found
<%
}

%>

The conditions can be added to handle different sites, if the sites are running on different virtual host names then get the server name from the request
(request.getServerName()) and add the condition based on the server name.

This will redirect the user to site specific 404 pages.


How to find the Email Group Id in Eloqua?

 How to find the Email Group Id in Eloqua? 

This post will explain  how to find the Email Group Id in Eloqua

There will be different approach to find the Email Group id. Here. This post explains the approach used by me.
Use any of the REST client(here i am using Advance Rest Client)

Provide the below URL - https://secure.p03.eloqua.com/API/REST/1.0/assets/email/groups

Select the Http Method as GET

Provide the Authorization header

Authorization: Basic xxxxxxxxxxx

Replace xxxxxxxxxxx with base64 encoded string of companyName\userName:password

Send the request

The response will have all the Email Group id details - id and name of the Emial group can be found in the response.

{
  elements: [2]
  0:  {
         type: "ContactEmailSubscription"
         contactId: "1"
         emailGroup: {
             type: "EmailGroup"
             id: "1"
            depth: "minimal"
            description: ""
            name: "Sample1"
            permissions: "fullControl"
            updatedAt: "1423758721"
           updatedBy: "1"
        }
       isSubscribed: "true"
       updatedAt: "1423219686"
   }
  1:  {
         type: "ContactEmailSubscription"
         contactId: "1"
         emailGroup: {
             type: "EmailGroup"
             id: "2"
            depth: "minimal"
            description: ""
            name: "Sample2"
            permissions: "fullControl"
            updatedAt: "1423758721"
           updatedBy: "1"
        }
       isSubscribed: "true"
       updatedAt: "1423219686"
   }
  page: 1
 pageSize: 1000
 total: 2
}



Sunday, March 1, 2015

How to get the child Pages of a root Page through Java API -AEM/ Adobe CQ5

How to get the child Pages of a root Page through Java API - AEM/Adobe CQ5

This post will explain how to get the child Pages of a root Page through Java API in Adobe Experience Manager(AEM)

public static PageFilter getPageFilter() {
PageFilter pf = new PageFilter();
return pf;
}

public static Iterator<Page> getPageIterator(Page page){
Iterator<Page> children = page. listChildren(getPageFilter());
return children;
}

Filter can be changed to restrict child pages e.g Get the child pages created only with the template "/apps/sample/templates/samplePage"

public static PageFilter getPageFilter() {
PageFilter pf = new PageFilter() {
public boolean includes(Page p) {
ValueMap props = p.getProperties();
String templatePath = props.get("cq:template",String.class);
if("/apps/sample/templates/samplePage".equals(templatePath))
{
return true;
} else
{
return false;
}
}
};
return pf;
}


How to find a Page has child Pages thorough Java API - AEM/Adobe CQ5

How to find a Page has child Pages thorough Java API - AEM/Adobe CQ5

This post will explain how to find a Page has child Pages thorough Java API in Adobe Experience Manager(AEM)

public static boolean hasPageChildren(Page rootPage) {
          boolean isTrue = false;
          if (rootPage != null&& rootPage.listChildren(getPageFilter()).hasNext()) {
       isTrue = true;
          }
        return isTrue;
}

public static PageFilter getPageFilter() {
PageFilter pf = new PageFilter();
return pf;
}


How to get the published date of a page through Java API - Adobe Experience Manager(AEM)

How to get the published date of a page through Java API - Adobe Experience Manager(AEM)

This post will explain how to get the published date of a page through Java API in Adobe Experience Manager(AEM)

public static Date getPublishDate(Page page) {
Date newDate = null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd", Locale.getDefault());
if (page != null) {
String publishedDate =  page.getProperties().get("publisheddate", "");
if(!"".equals(publishedDate)){
try {
newDate = sdf.parse(publishedDate);
} catch (ParseException e) {

}
}
}
return newDate;
}


How to send the email through Java API in Eloqua

How to send the email through Java API in Eloqua

This post explains how to send the email through Eloqua API.

Create the required model classes:

public class EmailTestDeployment 

    public String contactId ;
    public String sendFromUserId ;
    public Email email;
    public String name;
    public String type;       

}

public class Email 
{
public int emailGroupId;
public RawHtmlContent htmlContent; 
public int id; 
public boolean isPlainTextEditable; 
public String name; 
public String plainText; 
public boolean sendPlainTextOnly; 
public String subject; 

}

public class RawHtmlContent 
{
    public String type;
    public String html;

}

RestClient class to connect to Eloqua:


import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class RestClient
{
    private String authToken;
    private String baseUrl;       
    public RestClient(String user, String password, String url)
    {
       baseUrl = url;             
       String authString = user + ":" + password;
      authToken = "Basic " + javax.xml.bind.DatatypeConverter.printBase64Binary(authString.getBytes());           
    }             
                          
    public String execute(String uri, String method, String body)  throws Exception
    {
       String response ="";
       try
       {           
          URL url = new URL(baseUrl + uri);
          HttpURLConnection conn = (HttpURLConnection) url.openConnection();                         
          conn.setInstanceFollowRedirects(false);
          conn.setRequestMethod(method.toString());
          conn.setRequestProperty("Content-Type", "application/json");
          conn.setRequestProperty("Accept", "application/json");
          System.out.println(authToken);
          conn.setRequestProperty("Authorization", authToken);         
                  
          if (method == "POST" || method == "PUT")
          {
              if(null != body){
                  conn.setDoOutput(true);
                  final OutputStream os = conn.getOutputStream();
                  os.write(body.getBytes());
                  os.flush();
                  os.close();
               }
           }
                      
           InputStream is = conn.getInputStream();
           BufferedReader rd = new BufferedReader(new InputStreamReader( is));
           String line; 
           while ((line = rd.readLine()) != null)
           {
               response += line;
           }           
           rd.close();
           conn.disconnect();
         }
         catch (Exception e)
         {
            throw e;
         }
       return response;
      }

}



How to find the id of the existing contact in Eloqua

How to find the id of the existing contact in Eloqua

This post will explain How to find the id of the existing contact in Eloqua

Login to Eloqua
Click on Contacts in main page
Search by entering *, this will return all the contacts.
Open the particular contact.
Select Field Details tab and select All Contact Fields in the drop down.


In the Eloqua Contact ID field ignore CTHOM and all the zeros, consider the last digits as contact id(e.g 109)