<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="/struts-tags" prefix="ww" %>
<%@ page import="com.atlassian.crowd.util.SystemInfoHelper" %>
<%@ page import="com.atlassian.crowd.util.SystemInfoHelperImpl" %>
<%@ page import="org.apache.log4j.Logger" %>
<%@ page import="java.io.PrintWriter" %>
<%@ page import="java.io.StringWriter" %>
<%@ page import="org.apache.commons.lang3.StringEscapeUtils" %>
<html>
<head>
    <title>Oops - an error has occurred</title>
    <meta name="decorator" content="atl.general"/>
    <%
        Exception exception = (Exception) request.getAttribute("exception");
        SystemInfoHelper systemInfoHelper = new SystemInfoHelperImpl();
        String exCause = null;
        String ex = null;
        if (exception != null)
        {
            Throwable cause = exception;
            if (exception instanceof ServletException)
            {
                Throwable rootCause = ((ServletException) exception).getRootCause();
                if (rootCause != null)
                {
                    cause = rootCause;
                }
            }
            //log exception to the log files, so that it gets captured somewhere.
            Logger.getLogger("500ErrorPage").error("Exception caught in 500 page " + cause.getMessage(), cause);
            exCause = cause.toString();
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            cause.printStackTrace(pw);
            ex = sw.toString();
        }
    %>
</head>
<body>
<div class="content">
    <ww:i18n name="com.atlassian.crowd.console.action.BaseAction">
        <h1>
            <ww:text name="system.error.label"/>
        </h1>
        <p>
            <ww:text name="system.error.message.1.description"/>
        </p>
        <p>
            <ww:text name="system.error.message.2.description">
                <ww:param name="value0" value="'<a href=https://support.atlassian.com/>'"/>
                <ww:param name="value1" value="'</a>'"/>
            </ww:text>
        </p>
        <ul>
            <li><ww:text name="system.error.message.3.description"/></li>
            <li><ww:text name="system.error.message.4.description"/></li>
            <li><ww:text name="system.error.message.5.description"/></li>
        </ul>

        <h2>
            <ww:text name="system.error.error.information.label"/>
        </h2>
        <h3>
            <ww:text name="system.error.cause.label"/>:
        </h3>
        <%= StringEscapeUtils.escapeHtml4(exCause) %>
        <ww:if test="exception != null">
            <h3>
                <ww:text name="system.error.stack.trace.label"/>:
            </h3>
            <code id="stacktrace"><%= StringEscapeUtils.escapeHtml4(ex) %>
            </code>
        </ww:if>
        <ww:else>
            <code>
                <%= StringEscapeUtils.escapeHtml4((String) request.getAttribute("javax.servlet.error.message")) %>
            </code>
        </ww:else>
        <h2>
            <ww:text name="system.error.system.information.label"/>
        </h2>
        <h3>
            <ww:text name="referer.label"/>:
        </h3>
        <%= request.getHeader("Referer") != null ? StringEscapeUtils.escapeHtml4(request.getHeader("Referer")) : "Unknown" %>


        <h3>
            <ww:text name="system.error.build.information.label"/>:
        </h3>
        <strong><ww:text name="systeminfo.version.label"/>:</strong>
        <%= StringEscapeUtils.escapeHtml4(systemInfoHelper.getCrowdVersion()) %>
        <h3>
            <ww:text name="system.error.memory.information.label"/>:
        </h3>
        <strong><ww:text name="systeminfo.totalmemory.label"/>:</strong>
        <%= StringEscapeUtils.escapeHtml4("" + systemInfoHelper.getTotalMemory()) %> MB<br/>
        <strong><ww:text name="systeminfo.freememory.label"/>:</strong>
        <%= StringEscapeUtils.escapeHtml4("" + systemInfoHelper.getFreeMemory()) %> MB<br/>
        <strong><ww:text name="systeminfo.usedmemory.label"/>:</strong>
        <%= StringEscapeUtils.escapeHtml4("" + systemInfoHelper.getUsedMemory()) %> MB
        <h2>
            <ww:text name="system.error.request.information.label"/>:
        </h2>

        <h3>
            <ww:text name="system.error.request.attributes.label"/>:
        </h3>
        <ul>
            <ww:iterator value="#request">
                <li>
                    <strong><ww:property value="key" escapeHtml="true"/>:</strong>
                    <ww:if test="key == @com.atlassian.crowd.integration.Constants@COOKIE_TOKEN_KEY">
                        ********
                    </ww:if>
                    <ww:else>
                        <ww:property value="value" escapeHtml="true"/>
                    </ww:else>
                </li>
            </ww:iterator>
        </ul>
        <h3>
            <ww:text name="system.error.request.parameters.label"/>:
        </h3>
        <ul>
            <ww:iterator value="#parameters">
                <li>
                    <strong><ww:property value="key" escapeHtml="true"/>:</strong>
                    <ww:property value="value" escapeHtml="true"/>
                </li>
            </ww:iterator>
        </ul>
    </ww:i18n>
</div>
</body>
</html>
