Thursday 7 November 2013

Using the Assembler to connect to Lifestyle Endeca Application

In my earlier posts, I have detailed on how to create a new Endeca Appication 'Lifestyle' based on discover-data deployment template and new reference web application based on discover-electronics reference application.

I shall now detail how to connect to Endeca Application using an Assembler

CREATE A NEW SERVER INSTANCE


  • Windows>Preferences (Expand server and select Runtime Environments) and click ADD
  • Select Apache Tomcat and provide the tomcat installation directory path (C:\Endeca\ToolsAndFrameworks\3.1.2\server\apache-tomcat-6.0.32)
  • Once done, create a new server using the 'Servers' tab and provide the server name as 'lifestyleWebServer'
  • Start the 'lifestyleWebServer' and confirm it starts up successfully using the console tab

CREATE A NEW WEB APPLICATION

  • Create a new dynamic web project 'Lifestyle'
  • Add the JARs in the directory (C:\Endeca\ToolsAndFrameworks\3.1.2\reference\discover-electronics-authoring\WEB-INF\lib) to the Web App using Java Build Path. Also click on the 'Order and Export' tab and select the JARs that we have just added
  • Right-click on Web App, select properties -> Deployment Assembly. Click Add and select the Java Built Path Entries and Click Next. Select all the entries and finish
  • Importing classes from discover-authoring to Lifestyle web app
    • Lifestyle>Web Content>WEB-INF and choose import from file system. Navigate to directory (C:\Endeca\ToolsAndFrameworks\3.1.2\reference\discover-electronics-authoring\WEB-INF\classes)
    • Select the 'classes' folder, and check the two options (Override existing resources and create top-level folfers)
  • Configure Assembler
    • Copy the files (assembler.properties, assembler-context.xml, endeca-url-config.xml,perf-logging-config.xml,urlrewrite.xml and web.xml) from the directory (C:\Endeca\ToolsAndFrameworks\3.1.2\reference\discover-electronics-authoring\WEB-INF) to a local directory (C:\Endeca|Lab\AssemblerFiles) and do the below modifications
    • Replace the web-app xmlns entry in web.xml as (<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">)
    • Ensure that you are pointing to the correct application and mdex host by verifying the configured values in assembler.properties
    • Search for 'endeca-seo-url-config.xml' entry in assembler-context.xml and replace it with 'endeca-url-config.xml'
    • Search for 'urlFormatter' entry in assembler-context.xml and replace the text ref="seoUrlFormatter" to ref="basicUrlFormatter"
    • Lifestyle> Web Content>WEB-INF and choose import from file system. Navigate to directory (C:\Endeca|Lab\AssemblerFiles)
    • Select all the files, but ensure that 'Create top-level folder is not selected'
INVOKE ASSEMBLER USING A JSP
  • Right click on WEB-INF directory and create a new folder 'services'
  • Create a new JSP file 'lifestyleAssemble.jsp'
  • Remove all the HTML content and replace the same with below
<%@page import="com.endeca.infront.assembler.Assembler"%>
<%@page import="com.endeca.infront.assembler.AssemblerFactory"%>
<%@page import="com.endeca.infront.assembler.ContentItem"%>
<%@page import="com.endeca.infront.cartridge.ContentInclude"%>
<%@page import="com.endeca.infront.serialization.JsonSerializer"%>
<%@page import="com.endeca.infront.serialization.XmlSerializer"%>
<%@page import="com.endeca.infront.navigation.UserState"%>
<%@page import="com.endeca.mobile.services.detection.DeviceManager"%>
<%@page import="org.springframework.web.context.WebApplicationContext"%>
<%@page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%@page import="java.util.Properties"%>
<%@page import="java.util.LinkedHashMap"%>
<%@page import="java.util.Map"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>

<%@page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<%
WebApplicationContext webappCtx = WebApplicationContextUtils
.getRequiredWebApplicationContext(application);
AssemblerFactory assemblerFactory = (AssemblerFactory) webappCtx
.getBean("assemblerFactory");
Assembler assembler = assemblerFactory.createAssembler();
ContentItem contentItem = new ContentInclude("/pages/browse");
ContentItem responseContentItem = assembler.assemble(contentItem);

String format = request.getParameter("format");
if ("json".equals(format)) {
response.setHeader("content-type", "application/json");
new JsonSerializer(response.getWriter())
.write(responseContentItem);
} else if ("xml".equals(format)) {
response.setHeader("content-type", "application/xml");
new XmlSerializer(response.getWriter())
.write(responseContentItem);
}
%>
  • Search for the servlet entry 'assemble' in web.xml and change the jsp-file entry to 'lifestyleAssemble.jsp'
DEPLOY THE LIFESTYLE WEB APPLICATION USING LIFESTYLEWEBSERVER TOMCAT INSTANCE

  • Click on the servers tab, right click on 'lifestyleWebserver' and choose Add and Remove. Add the 'Lifestyle' web app 
  • Restart the tomcat server instance
  • Navigate to the URL (http://localhost:8080/Lifestyle/browse?format=json') from browser and you should see the JSON reponse for the browse page as configured in Endeca Experience Manager rendered using the Assembler