/**Connects servlet API to MDSAX */
/* Simon St.Laurent 3/2/99 to 3/25/99 */
/* Copyright 1999 Simon St.Laurent */
/* Please see BSD-style license at http://www.simonstl.com/projects/mdservlet/license.txt for information on reuse and distribution. */
/* Version 0.1 */

import java.io.*;
import java.util.*;
import java.net.URL;
import javax.servlet.*;
import javax.servlet.http.*;
import org.xml.sax.*;
import com.jxml.mdsax.MDModule;

public class MDServlet extends HttpServlet { 
	protected PrintWriter out;
	protected Boolean useBaseURLforQueries;
	protected String contentType;
	protected String baseURL;
	protected String appBootURL;
	protected String contextURL;
	protected String appURL;

	protected Parser parser;

	protected String appBootURLParamName;
	protected String contextURLParamName;
	protected String appURLParamName;
	protected String returnName;

	protected Boolean acceptAppBootURL;
	protected Boolean acceptContextURL;
	protected Boolean acceptAppURL;
	protected Boolean acceptGet;
	protected Boolean acceptPost;

public void init(ServletConfig config) throws ServletException {
	super.init(config);

	contentType=getInitParameter("contentType");
	if (contentType==null) {contentType="text/xml";}
	baseURL=getInitParameter("baseURL");

	appBootURL=baseURL+getInitParameter("appBootURL");
	contextURL=baseURL+getInitParameter("contextURL");
	appURL=baseURL+getInitParameter("appURL");

	appBootURLParamName=getInitParameter("appBootURLParamName");
	contextURLParamName=getInitParameter("contextURLParamName");
	appURLParamName=getInitParameter("appURLParamName");

	returnName=getInitParameter("returnName");
	if (returnName==null) {returnName="result";}

	acceptAppBootURL=Boolean.valueOf(getInitParameter("acceptAppBootURL"));
	acceptContextURL=Boolean.valueOf(getInitParameter("acceptContextURL"));
	acceptAppURL=Boolean.valueOf(getInitParameter("acceptAppURL"));
	acceptGet=Boolean.valueOf(getInitParameter("acceptGet"));
	acceptPost=Boolean.valueOf(getInitParameter("acceptPost"));
	useBaseURLforQueries=Boolean.valueOf(getInitParameter("useBaseURLforQueries"));

	//other params to come?
}


/** Handles HTTP GET requests.  Really checks to make sure the server should be accepting GET requests (i.e., acceptGet is true) and then passes to process.(req,res)*/

public void doGet (HttpServletRequest req, HttpServletResponse res) 
	  throws IOException, ServletException {
	if (acceptGet.booleanValue()) {

		process(req,res);

	} else {
		res.sendError(res.SC_FORBIDDEN);
	}
}	


/** Handles HTTP POST requests.  Really checks to make sure the server should be accepting POST requests (i.e., acceptPost is true) and then passes to process(req,res).*/

public void doPost (HttpServletRequest req, HttpServletResponse res) 
	  throws IOException, ServletException {
	if (acceptPost.booleanValue()) {

		process(req,res);

	} else {
		res.sendError(res.SC_FORBIDDEN);
	}
}	


public void process (HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException  {

	String param; 

	//Get parameters sent by POST or GET
	//Remember - only use parameters if allowed!

//get bootURL
	if (acceptAppBootURL.booleanValue()) {
	    param=req.getParameter(appBootURLParamName);
	    if (param != null) {
		if (useBaseURLforQueries.booleanValue()) {
			appBootURL=baseURL+param;
		} else {
			appBootURL=param;

		}
	    }
	}

//get contextURL
	if (acceptContextURL.booleanValue()) {
	    param=req.getParameter(contextURLParamName);
	    if (param != null) {
		if (useBaseURLforQueries.booleanValue()) {
			contextURL=baseURL+param;
		} else {
			contextURL=param;

		}
	    }
	}

//get appURL
	if (acceptAppURL.booleanValue()) {
	    param=req.getParameter(appURLParamName);

	    if (param != null) {
		if (useBaseURLforQueries.booleanValue()) {
			appURL=baseURL+param;
		} else {
			appURL=param;

		}
	    } else {
		//use path info, if available.

		if ((req.getPathInfo() != null) && (req.getPathInfo() != "/")) {
			String temp;
			URL tempURL;
			temp=req.getPathTranslated();
			tempURL=new URL("file","",temp);
			appURL=tempURL.toString();
		}
	    }
	}


	// Set content type 
	res.setContentType(contentType);

	// Get Request OutputStream 
    	out = res.getWriter();

	//  Write response
	try {
		
	MDModule module=new MDModule(appBootURL, contextURL, appURL, null, null);

	String result=module.getResult("after").toString();
	
	out.println(result);
/*
		out.println(baseURL);
		out.println(appBootURL);
		out.println(contextURL);
		out.println(appURL);

		out.println(acceptAppBootURL);
		out.println(acceptContextURL);
		out.println(acceptAppURL);
		out.println(acceptGet);
		out.println(acceptPost);
*/
	} catch (SAXParseException pe)
		{
			System.out.println("");
			MDModule.showSAXParseException(pe);
			System.out.println("");
		} catch (Exception e)
		{
			e.printStackTrace();
		}
	}

}