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 java.io.*;
import java.util.*;
import org.xml.sax.SAXException;
import java.sql.SQLException;
import org.jdom.Document;
import org.jdom.input.SAXBuilder;
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", "http://www.i-link.net");
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();
/* Joe Bowbee suggestion */
byte[] buf = new byte[request.getContentLength()];
new DataInputStream(request.getInputStream()).readFully(buf);
InputStream in = new ByteArrayInputStream(buf);
try {
SAXBuilder builder = new SAXBuilder();
WhlslXMLProcessor.errorDump("building the document");
Document doc = builder.build(in);
/* Vladimir Grishchenko suggestion
Document doc = builder.build(new BufferedInputStream(request.getInputStream()));
*/
WhlslXMLProcessor.errorDump("Document built");
/* Application Code not part of the build
whlslProcessor = new WhlslXMLProcessor(doc);
whlslProcessor.process();
Document retDoc = whlslProcessor.buildReturnXML();
printResults(retDoc);
*/
printResults(new String(buf));
// }
/* catch (WhlslException we) {
gracefulExit(we);
} catch (SQLException sqle) {
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{
XMLOutputter fmt = new XMLOutputter(" ", true);
// fmt.output(doc, output);
output.print(fmt.outputString(doc));
output.flush();
}
catch(Exception e)
{
// WhlslXMLProcessor.errorDump("just plain exception\n"+e.toString());
System.out.println(e.getMessage());
}
}
}