package com.etixpert.evolution.gui.servlets;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

/**
* @author Peter Albert
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class ShowError extends HttpServlet {

	public String js = "<SCRIPT LANGUAGE=\"JavaScript\">"+
"<!-- JavaScript code hiding"+
" function simpleLink(origLink) {"+
"parent.mainFrame.location = origLink;"+
"}"+
"	function link(origLink){"+
"		if (top.mainFrame.document.forms.length == 0) {"+
"			simpleLink(origLink);"+
"			return;"+
"		}"+
"		else {"+
"			if (top.mainFrame.document.forms[0].saveState == null) {"+
"				simpleLink(origLink); "+
"				return;"+
"			}"+
"			top.mainFrame.document.forms[0].saveState.value = top.mainFrame.self.location.pathname;"+
"			top.mainFrame.document.forms[0].link.value = origLink;"+
"			top.mainFrame.document.forms[0].submit();"+
"		}"+
"	}"+
"// -->"+
"</SCRIPT>";
	
	public void doGet(HttpServletRequest req, HttpServletResponse res) 
		throws ServletException, IOException {
		
		res.setContentType("text/html");
		PrintWriter out = res.getWriter();
		
		Integer codeObj = (Integer)req.getAttribute("javax.servlet.error.status_code");
		String messageObj = (String)req.getAttribute("javax.servlet.error.message");
		Class typeObj = (Class)req.getAttribute("javax.servlet.error.exception_type");
		Throwable exceptionObj = (Throwable)req.getAttribute("javax.servlet.error.exception");
		String requestURIObj = (String)req.getAttribute("javax.servlet.error.request_uri");
		
		String codeStr = (codeObj == null)?"N/A":codeObj.toString();
		String messageStr = (messageObj == null)?"N/A":messageObj;
		String typeStr = (typeObj == null)?"N/A":typeObj.getName();
		String requestURIStr = (requestURIObj == null)? "N/A":requestURIObj;   
		
		out.println("<html><head><title>Unfortunately an error occured processing a request on Evolution Web GUI</title>"+ js + "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-2\"><link href=\"leftMenu.css\" rel=\"stylesheet\" type=\"text/css\"></head>");
		out.println("<body>Error code: "+codeStr+"<br>");
		out.println("Type: "+typeStr+"<br>");
		out.println("Message: "+messageStr+"<br>");
		
		//can be configured later
		if (true) {
			out.println("Exception: "+exceptionObj.getMessage());
			out.println("Stack-trace:");
			exceptionObj.printStackTrace(out);
			out.println("<br>");
		}
		out.println("<i>Error getting this page: "+requestURIStr+"</i>");
		out.println("</body></html>");		
	}
	
}

