<%@page contentType="text/html;charset=UTF-8" pageEncoding="ISO-8859-1" isErrorPage="true" session="true"
%><%@page import="java.io.*"
%><%@page import="java.util.Enumeration"
%><%@page import="com.fujitsu.iflow.model.util.QuickForm"
%><%request.setCharacterEncoding("UTF-8");request.getParameterNames(); //Need dummy call for WL9.1
%>
<!-- ======================== EXCEPTION ====================== -->
<!-- <%
if (exception != null) out.write(exception.toString());
%> -->
<html>
<head><title>Interstage Business Process Manager | Error report</title>
<jsp:include page="StyleSheet.jsp" flush="true"/>
<script language="javascript" src="styles/clockscripts.js"></script>
<%
    String timeOfException = (new java.util.Date()).toString();
%>
<script>
function showTrace(wform)
{
    settings=
    "toolbar=no,location=no,directories=no,"+
    "status=no,menubar=no,scrollbars=yes,"+
    "resizable=yes,width=400,height=300";

    MyNewWindow=window.open("","Error",settings);
    MyNewWindow.document.write(wform.exc.value);
}
</script>
</head>

<body class="bodyClass" onLoad="goforit();">
<center>
<table class="tableClass" cellspacing="0" cellpadding="0" border="0" width="100%">
    <tr>
        <td colspan="3" align="right">
            <jsp:include page="WorkItemHeader.jsp" flush="true"/>
        </td>
    </tr>
</table>

<center>
<form name="temp" method="post" action="Login.jsp">
<table cellpadding="10" width="80%" border="0">
<tr>
    <td>
        <h3><font color="#990000" class="arialfonts">
		<!-- point 1-->
<%
    out.flush();
    boolean showTraceButton = false;
    String message  = (String)request.getAttribute("message");
    String nextPage = (String)request.getAttribute("nextPage");
    String nextLink = (String)request.getAttribute("nextLink");
    String userName = (String)session.getAttribute("userName");
    if (userName == null) {
        userName = "Unknown-User";
    }
    String hostName = (String)session.getAttribute("hostName");
    if (hostName == null) {
        hostName = "Unknown-Host";
    }
    String protocol = (String)session.getAttribute("protocol");
    if (protocol == null) {
        protocol = "Unknown-Protocol";
    }
    out.write("<!-- point 2-->");
    if(request.getParameter("message") != null)
    {
      out.write(request.getParameter("message"));
      out.flush();
    }

    if (exception != null) {

%>
        An exception has occurred and therefore your request cannot
        be completed.<br><!-- point 2b -->
    <% out.flush(); %>
        The exception's message is:
    <% out.flush(); %>
        <h4><font color="#0000ff">
            <% deparenthesize(out, exception); %>
            </font></h4>
    <% out.flush(); %>
        Time: <%= timeOfException %><br>
    <% out.flush(); %>
        Logged in as: <%= userName %><br>
    <% out.flush(); %>
        Host: <%= hostName %>
              <%= protocol %><br>
    <% out.flush(); %>
        To see the stack trace, press the 'Trace' button below.
<%
        out.flush();
        StringWriter sw = new StringWriter();
        PrintWriter w = new PrintWriter(sw);
        sw.write("<UL>");
        if (exception.getClass() != null) {
            sw.write("<LI>The exception object was of class <B>");
            QuickForm.writeHtml(sw, exception.getClass().getName());
            sw.write("</B><br>");
        }
        sw.write("<LI>The exception's message is <B>");
        QuickForm.writeHtml(sw, exception.toString());
        sw.write("</B><br>");
        sw.write("<LI>Time:");
        sw.write(           timeOfException);
        sw.write("<LI>The stack trace is as follows:");
        sw.write("</UL><PRE>");
        exception.printStackTrace(w);  //note, if the stack trace contains left or right angle brackets this will cause garbage in the final window
        sw.write("</PRE><br>");
        showTraceButton = true;
%>
        <input type="hidden" name="exc" value="<% QuickForm.writeHtml(out, sw.toString()); %>">
<%
        w.close();
    }
    else if ( message != null || message.length() > 0 ) {
        out.write(message);
    }
    else {
        out.write("There is no exception or error message to display!");
    }
    out.write("<!-- point 2b -->");
    out.flush();
%>
        </font></h3><!-- point 3-->
    </td>
</tr>
<tr align="center">
    <td>
<%
    if (nextPage != null && nextPage.length() > 0) {
        if (nextLink == null || nextLink.length() == 0) {
            nextLink = "Continue";
        }
%>
        <input type="button" class="submitClass"
               onClick=window.location.href="<%=nextPage%>"
               value="<%=nextLink%>">
<%
    }
    if (showTraceButton) {
%>
        <input value="Trace" type="button" onClick="showTrace(this.form)"
               class="submitClass">
<%
    }
    out.flush();
%>
    </td>
</tr>
</table>

</form>
</center>
</body>
</html>
<%
    out.flush();
%>


<%!
    // converts a string with multiple nested parenthises into
    // a nested hierarchy of lists ... much easier to read
    public void deparenthesize(JspWriter out, Throwable e)
        throws Exception
    {
        String input = e.toString();
        try {
            // put the original string in the page as a comment, just for comparison
            out.write("\n<!-- ");
            QuickForm.writeHtml(out, input);
            out.write("-->\n");
            out.flush();

            // now print it as a set of ul statements
            int pos = 0;
            int leftover=1;
            int last = input.length();
            out.write("\n<ul>");
            while (pos < last) {
                int spos = input.indexOf('(', pos);
                int epos = input.indexOf(')', pos);
                int jpos = input.indexOf("nested exception is:", pos);
                int kpos = input.indexOf("because:", pos);
                int npos = last;
                int option = 0;
                int skip = 0;
                if (spos >= 0 && spos < npos) {
                    npos = spos;
                    option = 1;
                    skip = 1;
                }
                if (epos >= 0 && epos < npos) {
                    npos = epos;
                    option = 2;
                    skip = 1;
                }
                if (jpos >= 0 && jpos < npos) {
                    npos = jpos;
                    option = 3;
                    skip = 20;
                }
                if (kpos >= 0 && kpos < npos) {
                    npos = kpos;
                    option = 3;
                    skip = 8;
                }

                // if the character at the position is the searched char,
                // then both pos and npos will be the same
                if (npos > pos) {
                    out.write("<li>");
                    QuickForm.writeHtml(out, input.substring(pos, npos));
                }
                pos = npos + skip;
                switch (option) {
                case 3:
                    leftover++;
                    // continue to next block
                case 1:
                    out.write("\n<ul>");
                    break;
                case 2:
                    out.write("\n</ul>");
                    break;
                case 0:
                    break;
                }

                // skip any following white space to avoid empty outline entries
                while (pos < last && input.charAt(pos) == ' ') {
                    pos++;
                }
            }
            while (leftover > 0) {
                out.write("\n</ul>");
                leftover--;
            }
        }
        catch (Exception ee) {
            out.write("<!-- deparenthesize got this error:");
            out.write(ee.toString());
            out.write("-->");
        }
    }
%>
