package com.ilink.whlsl; /** *

Web based interface to the Wholesale system.


The WhlslServlet needs to receive the XML document stream as an HTTP POST where the xml document is the only piece of the POST

* Copyright: Copyright (c) 2001
* Company: I-Link Incorporated * @author John Muhlestein * @version 1.0 */ import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.ServletConfig; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.ServletException; import*; import java.util.*; import org.xml.sax.SAXException; import java.sql.SQLException; import org.jdom.Document; import org.jdom.Element; import org.jdom.Namespace; import org.jdom.output.XMLOutputter; import org.jdom.JDOMException; public class WhlslServlet extends HttpServlet { private static final String CONTENT_TYPE = "text/xml"; private WhlslXMLProcessor whlslProcessor; private Namespace ns = Namespace.getNamespace("ilink", ""); private PrintWriter output; /**Initialize global variables*/ public void init(ServletConfig config) throws ServletException { super.init(config); } /*Process HTTP Get requests -- this is for testing*/ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); output = response.getWriter(); output.println("You Found it."); output.println("

You Found It!

"); } /**Process the HTTP Post request*/ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType(CONTENT_TYPE); output = response.getWriter(); BufferedReader br = request.getReader(); try { whlslProcessor = new WhlslXMLProcessor(br); whlslProcessor.process(); Document retDoc = whlslProcessor.buildReturnXML(); // Document retDoc = new Document(new Element("test").addContent("Here We Go!")); printResults(retDoc); // printResults(); // printResults("Parameter being passed"); } catch (WhlslException we) { WhlslXMLProcessor.errorDump("servlet WhlslException"); gracefulExit(we); } catch (SQLException sqle) { WhlslXMLProcessor.errorDump("servlet SQLException"); gracefulExit(sqle); } catch (Exception e) { WhlslXMLProcessor.errorDump("servlet Exception"); gracefulExit(e); } } private void gracefulExit(Exception e) { //generate the message to send to the client WhlslXMLProcessor.errorDump("gracefulExit"); Document doc = new Document(new Element("results", ns)); doc.getRootElement() .addContent(new Element("status", ns) .setText("Unable to complete transaction")) .addContent(new Element("errormessage", ns) .setText(e.getMessage() )); try { WhlslXMLProcessor.errorDump("try to printResults"); printResults(doc); } catch (Exception ioe) { String ios = e.toString(); } //close any database connections that may be open // whlslProcessor.closeDBConnection(); //exit the program System.exit(-1); } private void printResults() throws IOException { output.println("No parameter passed. New account made."); output.flush(); } private void printResults(String s) throws IOException { output.println(s); output.flush(); } private void printResults(Document doc) { try{ WhlslXMLProcessor.errorDump("printResults (Document)"); XMLOutputter fmt = new XMLOutputter(" ", true); WhlslXMLProcessor.errorDump("XMLOutputter done"); // fmt.output(doc, output); WhlslXMLProcessor.errorDump(fmt.outputString(doc)); output.print(fmt.outputString(doc)); WhlslXMLProcessor.errorDump("XMLOutputter to response"); output.flush(); WhlslXMLProcessor.errorDump("response flushed"); } catch(Exception e) { WhlslXMLProcessor.errorDump("just plain exception\n"+e.toString()); } } }