/* * @(#)FunBooks.java * * Copyright 2002 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms * */ package com.sun.bookprovider; import javax.xml.messaging.*; import java.util.*; import javax.xml.soap.*; import javax.servlet.*; import javax.servlet.http.*; import javax.xml.transform.*; import javax.xml.transform.stream.*; import javax.xml.transform.dom.*; import org.w3c.dom.*; import java.sql.*; import java.io.*; import java.net.*; import javax.xml.parsers.*; import javax.activation.*; import javax.naming.*; /** * FunBooks.java follows JAXM Synchronous invocation model. * BookClient of EmployeePortal uses call method of JAXM to send synchronous * message to this provider. * This class does the following * 1) gets the request * 2) extract the query criteria and query value from the request. * 3) checks the inventory for the books matching the criteria * 4) sends the response as a SOAP attachment. * 5) also sends any promotion information as another attachment. * @author Bhakti Mehta and Ramesh Mandava * */ public class FunBooks extends JAXMServlet implements ReqRespListener { static MessageFactory fac = null; FileOutputStream fos ; Hashtable bookHash = new Hashtable(); Vector bookInfoVector = new Vector(); static { try { fac = MessageFactory.newInstance(); } catch (Exception ex) { ex.printStackTrace(); } }; public void init(ServletConfig servletConfig) throws ServletException { super.init(servletConfig); } public void executeQuery( String queryType, String queryVal ) { try { /* * This method looks for books matching the query type * and query value by parsing the funbooksinventory.xml */ DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse(new File("funbooksinventory.xml")); org.w3c.dom.Node parentNode = null; NodeList childList=null; Vector parentNodeVector = new Vector(); Book bookInfo; NodeList nl = doc.getElementsByTagName(queryType ); /* * This means that there are no matching values */ if (nl.getLength()==0){ System.out.println("No matching query results"); DocumentBuilderFactory docBFac=DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docBFac.newDocumentBuilder(); Document document = docBuilder.newDocument(); Element books = document.createElement("books"); document.appendChild(books); DOMSource domSource= new DOMSource(document); TransformerFactory tfactory = TransformerFactory.newInstance(); Transformer transformer = tfactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT,"yes"); /* * The file to be sent will be an empty file */ FileOutputStream fileOutputStream = new FileOutputStream ("../webapps/ROOT/funbooks.xml"); transformer.transform(domSource, new StreamResult(fos)); fileOutputStream.close(); }else { bookInfoVector = new Vector(); for (int i = 0;i=0 ){ System.out.println("Query Matched"+ queryList.item(0).getNodeValue()); parentNode = nl.item(i).getParentNode(); /* * There is match for the query * Now this gets all other values from the parent * such as bookName,bookAuthor,bookPrice etc. * */ String bookISBN=""; String bookAuthor=""; String bookName=""; String bookPrice=""; String bookQuantity=""; String bookDescription=""; childList = parentNode.getChildNodes(); for (int j = 0;j