The XMLRequestServlet Class
//: XMLRequestServlet.java
package com.owensum.everest.servlet.xml;
import com.owensum.everest.xml.*;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.w3c.dom.*;
/**
* XMLRequestServlet.java
*
* @author Jeng Shih
* @version 1.0
*/
public class XMLRequestServlet extends HttpServlet
{
/**
* The doGet method.
*/
public void doGet( HttpServletRequest req, HttpServletResponse res )
throws ServletException, IOException
{
doPost( req, res );
}
/**
* The doPost method.
*/
public void doPost( HttpServletRequest req, HttpServletResponse res )
throws ServletException, IOException
{
String step = req.getParameter( "step" );
int seq = ( step != null ) ? Integer.parseInt( step ) : 0;
if ( seq == 0 ) new RequestStep0( req, res ).execute();
if ( seq == 1 ) new RequestStep1( req, res ).execute();
if ( seq == 2 ) new RequestStep2( req, res ).execute();
if ( seq == 3 ) new RequestStep3( req, res ).execute();
}
public String outputDocument( Document doc, boolean indenting, boolean preserveSpace )
throws IOException
{
PipedInputStream pis = new PipedInputStream();
PipedOutputStream pos = new PipedOutputStream( pis );
XMLDocumentStream request = new XMLDocumentStream( doc, pos );
request.setIndenting( indenting );
request.setPreserveSpace( preserveSpace );
Thread thread = new Thread( request );
thread.start();
return StreamToString( pis );
}
public String StreamToString( InputStream is )
{
ByteArrayOutputStream bout = new ByteArrayOutputStream();
try {
while ( true ) {
byte[] b = new byte[512];
int i = is.read( b );
if ( i == -1 ) break;
bout.write( b, 0, i );
}
}
catch ( IOException e ) {
System.out.println( "Couldn't read from PipedInputStream." );
}
return bout.toString();
}
/**
* RequestStepBase
*
*/
class RequestStepBase
{
protected HttpServletRequest _req;
protected HttpServletResponse _res;
public RequestStepBase( HttpServletRequest req, HttpServletResponse res )
{
_req = req;
_res = res;
}
public void execute()
throws ServletException, IOException
{
doLogic();
doHTML();
}
public void doLogic() throws ServletException, IOException
{
}
public void doHTML() throws ServletException, IOException
{
_res.setContentType( "text/html" );
PrintWriter out = _res.getWriter();
out.println( "" );
out.println( "XML Request Input" );
out.println( "" );
out.println( "This is the HTML output from the RequestStepBase class." );
out.println( "" );
}
public void doError( String msg ) throws IOException
{
_res.setContentType( "text/html" );
PrintWriter out = _res.getWriter();
out.println( "" );
out.println( "XML Request Error" );
out.println( "" );
out.println( "" + msg + "
" );
out.println( "" );
}
}
/**
* RequestStep0
*
*/
class RequestStep0 extends RequestStepBase
{
public RequestStep0( HttpServletRequest req, HttpServletResponse res )
{
super( req, res );
}
public void doHTML() throws ServletException, IOException
{
_res.setContentType( "text/html" );
PrintWriter out = _res.getWriter();
out.println( "" );
out.println( "XML Transactions" );
out.println( "" );
out.println( "XML Transactions
" );
out.println( "
" );
out.println( "
" );
out.println( "" );
out.println( "" );
}
}
/**
* RequestStep1
*
*/
class RequestStep1 extends RequestStepBase
{
private InsuranceSvcRqCreate _app;
public RequestStep1( HttpServletRequest req, HttpServletResponse res )
{
super( req, res );
}
public void doLogic() throws ServletException, IOException
{
String[] reqNames = _req.getParameterValues( "requestName" );
if ( reqNames == null ) {
doError( "No requests have been selected!" );
return;
}
Vector args = new Vector( Arrays.asList( reqNames ) );
_app = new InsuranceSvcRqCreate( args );
HttpSession session = _req.getSession( true );
session.putValue( "InsuranceSvcRqCreate", _app );
}
public void doHTML() throws ServletException, IOException
{
if ( _app.inputRequired() ) {
_res.setContentType( "text/html" );
PrintWriter out = _res.getWriter();
out.println( "" );
out.println( "XML Request Input" );
out.println( "" );
out.println( "" );
out.println( "" );
}
else {
new RequestStep2( _req, _res ).execute();
}
}
}
/**
* RequestStep2
*
*/
class RequestStep2 extends RequestStepBase
{
private InsuranceSvcRqCreate _app;
private Document _doc;
private String _xmlreq;
private String _xmlreq1;
public RequestStep2( HttpServletRequest req, HttpServletResponse res )
{
super( req, res );
}
public void doLogic() throws ServletException, IOException
{
HttpSession session = _req.getSession( true );
_app = (InsuranceSvcRqCreate) session.getValue( "InsuranceSvcRqCreate" );
if ( _app.inputRequired() ) {
_app.getInputParams( _req );
}
_doc = createRequestDocument();
_xmlreq = outputDocument( _doc, true, true ); // to be displayed in the textarea
_xmlreq1 = outputDocument( _doc, true, false ); // disregard all spaces outside the XML tags
}
public void doHTML() throws ServletException, IOException
{
_res.setContentType( "text/html" );
PrintWriter out = _res.getWriter();
out.println( "" );
out.println( "XML Request Confirmation" );
out.println( "" );
out.println( "" );
out.println( "Request
" );
out.println( "" );
out.println( "" );
}
public Document createRequestDocument()
throws ServletException
{
Document doc = null;
try {
doc = (Document) _app.create();
}
catch ( Exception e ) {
System.out.println( "Couldn't create a document for the request." );
throw new ServletException( e.getMessage() );
}
return doc;
}
}
/**
* RequestStep3
*
*/
class RequestStep3 extends RequestStepBase
{
private String _xmlres;
public RequestStep3( HttpServletRequest req, HttpServletResponse res )
{
super( req, res );
}
public void doLogic() throws ServletException, IOException
{
String xmlreq = _req.getParameter( "request" );
ByteArrayInputStream in = new ByteArrayInputStream( xmlreq.getBytes() );
Document doc = createResponseDocument( in );
_xmlres = outputDocument( doc, true, false );
}
public void doHTML() throws ServletException, IOException
{
_res.setContentType( "text/html" );
PrintWriter out = _res.getWriter();
out.println( "" );
out.println( "XML Response" );
out.println( "" );
out.println( "" );
out.println( "" + "Response" + "
" );
out.println( "" );
out.println( "" );
}
public Document createResponseDocument( InputStream is ) throws ServletException
{
Document resXml = null;
try {
// get the request document
Document reqXml = XMLService.service().createDocument( is );
// process the request
resXml = XMLService.service().processRequest( reqXml );
}
catch ( Exception e ) {
throw new ServletException( e.getMessage() );
}
return resXml;
}
}
}