Jerry Jacob's Blog

Web Service Integration using JXPath

Posted in Java/ J2EE by Jerry Jacob on August 5, 2009

I was successful in integrating two web services using the JXPath API from Apache. My task was to generate a letter using the data obtained from a third party web service. I had to call another web service to get the letter template and the metadata that’s needed for printing the letter. Since these services were in no way related, I had to create a file; mapping the data items in our web service to the actual data in third party web service (key-value). Using this mapping file along with the JXPath API made the integration a piece of cake.

Now let me explain how the JXPath works. Consider this example:

String city = (String)JXPathContext.newContext(vendor).

This XPath expression is equivalent to the following Java code:

String city = null;
Collection locations = vendor.getLocations();
Iterator it = locations.iterator();
while (it.hasNext()){
    Location location = (Location);
    String zipCode = location.getAddress().getZipCode();
    if (zipCode.equals("90210")){
      city = location.getAddress().getCity();

In my mapping file, I have my values stored as XPath strings. These XPath values were used to fetch the actual data from the third party web service using the JXPath API.


Querying the ‘IBM Content Manager On Demand’ using ODWEK API

Posted in Java/ J2EE by Jerry Jacob on July 23, 2009

The IBM Content Manager OnDemand Web Enablement Kit (ODWEK) API’s provide a development environment for client access to Content Manager OnDemand (OD) servers. This API provide access to Servers, Folders, Criteria, and Hits, making it easy for Web application developers to logon, search and retrieve OD content. Here is the code that I used to query the CMOD folder with a specific criteria.

private int validateClaimInfo(final String ssnNumber, final String cDate) {
	final int port = 2345;
	final ODServer odServer = new ODServer(new ODConfig());
	try {
		//Logon to specified server
		odServer.setServerName(this.cmodProps.getString("cmod.server") .trim());
		//Search Folder
		return this.isExistingClaim(odServer, ssnNumber, cDate);
	} catch (final ODException e) {
		if (e.getErrorId() == 2107) {
			this.logger.error("OnDemand server " + odServer.getServerName()  + " is alive but: " + e.getErrorMsg());
		} else {
			this.logger.error(" Error code: " + e.getErrorId()+ "Encountered error: " + e.getErrorMsg());
  	} catch (final Exception e) {
	} finally {
		try {
   		} catch (final Exception e) {
  	return 0;
private int isExistingClaim(final ODServer odServer, final String ssnNumber, final String cDate) {
	int isExisting = 0;
	try {
		//Opening the folder specified in the property file...
		final ODFolder folder = odServer.openFolder(this.cmodProps.getString("claimings.folder").trim());
		//Getting specified criteria...
		final ODCriteria odCriteriaSsn = folder.getCriteria(this.cmodProps.getString("criteria.ssn").trim());
		//Setting operand...
		final ODCriteria odCriteriaCDate = folder.getCriteria(this.cmodProps.getString("criteria.cdate").trim());
		this.logger.debug("Search results size: "+ folder.searchCountHits());
		if (folder.searchCountHits() > 0) {
			isExisting = 1;
	} catch (final ODException e) {
		this.logger.error("ODException: " + e.getMessage(), e);
	} catch (final Exception e) {
		this.logger.error("Exception: " + e.getMessage(), e);
	return isExisting;

Create PDF and RTF reports using Jasper API

Posted in Java/ J2EE by Jerry Jacob on July 22, 2009

Using Japer, you can create reports in various formats like CSV file, HTML file, PDF file, RTF file, Excel file, XML file etc. First of all, the report needs to be designed using a report designer. I was using the open source report designing tool called, iReport. You can produce a report using the data from a database, parameters, variable expressions etc. If you are generating the report from a custom class file (DAO), include it in the class path of iReport. Using the iReport designing tool, you’ll be able to create a report as a JRXML/XML file; compile the report creating a new file with extension, .jasper called as ‘Jasper file’. We will be using this Jasper file in our java code.

Download the Jasper API with all its dependent jar’s and invoke the report using the API. Here is a sample to generate PDF and RTF reports. Generating other formats are very similar to this. Here I am using a custom datasource inplemented from JRDataSource for generating the reports. Since I was writing a web service to generate reports, I wasn’t rendering the report but returning the report in byte array. The client progran would use the FileOutputStream to render the report from byte array or could directly feed the byte array to the OutputStream of the HttpServletResponse.

public byte[] buildRtfResume(final String ssnNumber) {
     ResumeEngineImpl.log.debug("Building RTF Resume for " + ssnNumber);
     byte[] rtfResume = null;
     try {
          final Applicant applicant = this.getApplicantBySsn(ssnNumber);
          final JasperPrint jasperPrint = this.renderCV(applicant);
          final JRRtfExporter rtfExporter = new JRRtfExporter();
          final ByteArrayOutputStream rtfStream = new ByteArrayOutputStream();
          rtfResume = rtfStream.toByteArray();
     } catch (final JRException e) {
          ResumeEngineImpl.log.error(e.getMessage(), e);
     } catch (final RuntimeException e) {
          ResumeEngineImpl.log.error(e.getMessage(), e);
     return rtfResume;
public byte[] buildPdfResume(final String ssnNumber) {
     ResumeEngineImpl.log.debug("Building PDF Resume for " + ssnNumber);
     byte[] pdfResume = null;
     try {
          final Applicant applicant = this.getApplicantBySsn(ssnNumber);
          final JasperPrint jasperPrint = this.renderCV(applicant);
          pdfResume = JasperExportManager.exportReportToPdf(jasperPrint);
     } catch (final JRException e) {
          ResumeEngineImpl.log.error(e.getMessage(), e);
     } catch (final RuntimeException e) {
          ResumeEngineImpl.log.error(e.getMessage(), e);
     return pdfResume;

JasperPrint object represents the output, which can be viewed, printed or exported to many different formats.JasperFillManager class is used for report filling process. It takes three arguments to fill a report; Compiled report design, Parameters and Datasources.

public JasperPrint renderCV(Applicant applicant){
     final JasperReport jasperReport = (JasperReport) JRLoader.loadObject(this.getClass().getClassLoader().getResourceAsStream("jerry/jacob/reports/resume.jasper"));
     final JasperReport empHistSubReport = (JasperReport) JRLoader.loadObject(this.getClass().getClassLoader().getResourceAsStream("jerry/jacob/reports/EmploymentSubReport.jasper"));
     final JasperReport edHistSubReport = (JasperReport) JRLoader.loadObject(this.getClass().getClassLoader().getResourceAsStream("jerry/jacob/reports/EducationSubReport.jasper"));
     final EmpHistDataSource empHistDataSource = new EmpHistDataSource(empHists);
     final EducationHistDataSource edHistDataSource = new EducationHistDataSource(eduHists);
     final Map parameters = new HashMap();
     // add sub reports to map
     parameters.put("EmploymentSubReport", empHistSubReport);
     parameters.put("EducationSubReport", edHistSubReport);
     // add data sources to map
     parameters.put("EmploymentDataSource", empHistDataSource);
     parameters.put("EducationDataSource", edHistDataSource);
     return JasperFillManager.fillReport(jasperReport, parameters,applicantDataSource);

Making your Spring application, ‘Context Aware’

Posted in Java/ J2EE by Jerry Jacob on July 20, 2009

Suppose you have some java classes which are unable (or you don’t want it) to be wired to the Spring application context; for example, an auto generated web service client class! But you do want to use the dependency injection feature of Spring to get some of the other beans injected in to this class. How can we make this happen? One way to achieve this would be to use the ApplicationContextAware interface provided by Spring.

Create a class named ApplicationContext which implements  ApplicationContextAware. The inherited method, ‘setApplicationContext(…)’ will get called during the creation of this bean, providing the reference to the context. Our program should store this for a later interaction with the context.

package jerry.jacob;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class ApplicationContextProvider implements ApplicationContextAware {
    private static ApplicationContext ctx = null;
    public static ApplicationContext getApplicationContext() {         
        return ctx;    
    public void setApplicationContext(ApplicationContext ctx) throws BeansException {
        // Assign the ApplicationContext into a static method
        this.ctx = ctx;

Initialize the new bean in applicationContext.xml

<bean id="applicationContextProvider" class="jerry.jacob.ApplicationContextProvider"></bean>

Now you can call the static method, getApplicationContext(), from any class in your application, to get assess to the Spring ApplicationContext.