/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *
 *    https://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 */
package org.grails.web.errors;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.codehaus.groovy.control.CompilationFailedException;
import org.codehaus.groovy.runtime.InvokerInvocationException;

import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.ServletContextAware;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;

import grails.config.Config;
import grails.config.Settings;
import grails.core.GrailsApplication;
import grails.core.support.GrailsApplicationAware;
import grails.util.Environment;
import grails.web.mapping.UrlMappingInfo;
import grails.web.mapping.UrlMappingsHolder;
import grails.web.mapping.exceptions.UrlMappingException;
import grails.web.mvc.GrailsResponseMutator;
import org.apache.grails.core.GrailsBootstrapRegistryInitializer;
import org.grails.core.exceptions.GrailsRuntimeException;
import org.grails.exceptions.ExceptionUtils;
import org.grails.exceptions.reporting.DefaultStackTraceFilterer;
import org.grails.exceptions.reporting.StackTraceFilterer;
import org.grails.web.mapping.DefaultUrlMappingInfo;
import org.grails.web.mapping.UrlMappingUtils;
import org.grails.web.servlet.mvc.exceptions.GrailsMVCException;
import org.grails.web.util.GrailsApplicationAttributes;
import org.grails.web.util.WebUtils;

/**
 * Wraps any runtime exceptions with a GrailsWrappedException instance.
 *
 * @author Graeme Rocher
 */
public class GrailsExceptionResolver extends SimpleMappingExceptionResolver implements ServletContextAware, GrailsApplicationAware {

    public static final String EXCEPTION_ATTRIBUTE = WebUtils.EXCEPTION_ATTRIBUTE;

    protected static final Log LOG = LogFactory.getLog(GrailsExceptionResolver.class);
    protected static final String LINE_SEPARATOR = System.getProperty("line.separator");

    protected ServletContext servletContext;
    protected GrailsApplication grailsApplication;
    protected StackTraceFilterer stackFilterer;
    protected AuditorAwareLookup auditorAwareLookup;
    private volatile boolean logFlagsResolved;
    private boolean logFullStackTrace;
    private boolean logAuditor;
    private boolean logRemoteAddr;

    /* (non-Javadoc)
     * @see org.springframework.web.servlet.handler.SimpleMappingExceptionResolver#resolveException(jakarta.servlet.http.HttpServletRequest, jakarta.servlet.http.HttpServletResponse, java.lang.Object, java.lang.Exception)
     */
    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response,
                                         Object handler, Exception ex) {
        // don't reuse cached controller attribute
        request.removeAttribute(GrailsApplicationAttributes.GRAILS_CONTROLLER_CLASS_AVAILABLE);

        ex = findWrappedException(ex);

        logFullStackTraceIfEnabled(ex);

        filterStackTrace(ex);

        ModelAndView mv = super.resolveException(request, response, handler, ex);

        setStatus(request, response, mv, ex);

        logStackTrace(ex, request);

        UrlMappingsHolder urlMappings = lookupUrlMappings();
        if (urlMappings != null) {
            mv = resolveViewOrForward(ex, urlMappings, request, response, mv);
        }

        return mv;
    }

    public String getRequestLogMessage(Throwable e, HttpServletRequest request) {
        Throwable cause = getRootCause(e);
        String exceptionName = cause.getClass().getSimpleName();
        return getRequestLogMessage(exceptionName, request, cause.getMessage());
    }

    public String getRequestLogMessage(HttpServletRequest request) {
        return getRequestLogMessage("Exception", request, null);
    }

    public void setServletContext(ServletContext servletContext) {

        this.servletContext = servletContext;
    }

    public void setGrailsApplication(GrailsApplication grailsApplication) {
        this.grailsApplication = grailsApplication;
        createStackFilterer();
        this.auditorAwareLookup = new AuditorAwareLookup(grailsApplication.getMainContext());
    }

    /**
     * Obtains the root cause of the given exception
     * @param ex The exception
     * @return The root cause
     */
    public static Throwable getRootCause(Throwable ex) {
        return ExceptionUtils.getRootCause(ex);
    }

    public static int extractLineNumber(CompilationFailedException e) {
        return ExceptionUtils.extractLineNumber(e);
    }

    public static RuntimeException getFirstRuntimeException(Throwable e) {
        return ExceptionUtils.getFirstRuntimeException(e);
    }

    protected void filterStackTrace(Exception e) {
        stackFilterer.filter(e, true);
    }

    protected void setStatus(HttpServletRequest request, HttpServletResponse response, ModelAndView mv, Exception e) {
        response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        // expose the servlet 2.3 specs status code request attribute as 500
        request.setAttribute(WebUtils.ERROR_STATUS_CODE_ATTRIBUTE, HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        final GrailsWrappedRuntimeException gwre = new GrailsWrappedRuntimeException(servletContext, e);
        mv.addObject(WebUtils.ERROR_EXCEPTION_ATTRIBUTE, gwre);
        mv.addObject(WebUtils.EXCEPTION_ATTRIBUTE, gwre);
    }

    protected UrlMappingsHolder lookupUrlMappings() {
        try {
            return UrlMappingUtils.lookupUrlMappings(servletContext);
        }
        catch (Exception ignored) {
            // ignore, no app ctx in this case.
            return null;
        }
    }

    Map extractRequestParamsWithUrlMappingHolder(UrlMappingsHolder urlMappings, HttpServletRequest request) {
        Map params = new HashMap();
        try {
            UrlMappingInfo requestInfo = urlMappings.match(request.getRequestURI());
            if (requestInfo != null) {
                params.putAll(UrlMappingUtils.findAllParamsNotInUrlMappingKeywords(requestInfo.getParameters()));
            }
        } catch (UrlMappingException ulrMappingException) {
            logger.debug("Could not find urlMapping which matches: " + request.getRequestURI());
        }
        return params;
    }

    protected ModelAndView resolveViewOrForward(Exception ex, UrlMappingsHolder urlMappings, HttpServletRequest request,
            HttpServletResponse response, ModelAndView mv) {

        UrlMappingInfo info = matchStatusCode(ex, urlMappings);

        if (info != null) {
            Map params = extractRequestParamsWithUrlMappingHolder(urlMappings, request);
            if (params != null && !params.isEmpty()) {
                Map infoParams = info.getParameters();
                if (infoParams != null) {
                    params.putAll(info.getParameters());
                }
                info = new DefaultUrlMappingInfo(info, params, grailsApplication);
            }
        }

        try {
            if (info != null && info.getViewName() != null) {
                resolveView(request, info, mv);
            }
            else if (info != null && info.getControllerName() != null) {
                String uri = determineUri(request);
                if (!response.isCommitted()) {
                    if (response instanceof GrailsResponseMutator) {
                        // prevent further mutation of the request since an error page needs rendered instead
                        ((GrailsResponseMutator) response).deactivateResponseMutator();
                    }
                    forwardRequest(info, request, response, mv, uri);
                    // return an empty ModelAndView since the error handler has been processed
                    return new ModelAndView();
                }
            }
            return mv;
        }
        catch (Exception e) {
            LOG.error("Unable to render errors view: " + e.getMessage(), e);
            throw new GrailsRuntimeException(e);
        }
    }

    protected void forwardRequest(UrlMappingInfo info, HttpServletRequest request, HttpServletResponse response,
            ModelAndView mv, String uri) throws ServletException, IOException {
        info.configure(WebUtils.retrieveGrailsWebRequest());
        String forwardUrl = UrlMappingUtils.forwardRequestForUrlMappingInfo(
                request, response, info, mv.getModel(), true);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Matched URI [" + uri + "] to URL mapping [" + info +
                    "], forwarding to [" + forwardUrl + "] with response [" + response.getClass() + "]");
        }
    }

    protected String determineUri(HttpServletRequest request) {
        String uri = (String) request.getAttribute(WebUtils.FORWARD_REQUEST_URI_ATTRIBUTE);
        if (uri == null) {
            uri = request.getRequestURI();
        }
        return uri;
    }

    protected void resolveView(HttpServletRequest request, UrlMappingInfo info, ModelAndView mv) throws Exception {
        ViewResolver viewResolver = WebUtils.lookupViewResolver(servletContext);
        View v = UrlMappingUtils.resolveView(request, info, info.getViewName(), viewResolver);
        if (v != null) {
            mv.setView(v);
        }
    }

    protected UrlMappingInfo matchStatusCode(Exception ex, UrlMappingsHolder urlMappings) {
        UrlMappingInfo info = urlMappings.matchStatusCode(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, ex);
        if (info == null) {
            info = urlMappings.matchStatusCode(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
                    getRootCause(ex));
        }
        if (info == null) {
            info = urlMappings.matchStatusCode(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        }
        return info;
    }

    protected void logStackTrace(Exception e, HttpServletRequest request) {
        LOG.error(getRequestLogMessage(e, request), e);
    }

    /**
     * When the {@code grails.exceptionresolver.logFullStackTrace} property is enabled,
     * emits the unfiltered stack trace to the dedicated {@code StackTrace} logger.
     * Must be invoked <em>before</em> {@link #filterStackTrace(Exception)} — once the
     * filterer calls {@code setStackTrace(clean)}, the original frames are gone and
     * this method can only log the already-trimmed trace.
     */
    protected void logFullStackTraceIfEnabled(Exception e) {
        if (shouldLogFullStackTrace()) {
            DefaultStackTraceFilterer.STACK_LOG.error(StackTraceFilterer.FULL_STACK_TRACE_MESSAGE, e);
        }
    }

    protected boolean shouldLogFullStackTrace() {
        resolveLogFlags();
        return logFullStackTrace;
    }

    protected boolean shouldLogAuditor() {
        resolveLogFlags();
        return logAuditor;
    }

    protected boolean shouldLogRemoteAddr() {
        resolveLogFlags();
        return logRemoteAddr;
    }

    /**
     * Resolves the three {@code shouldLog*} flags from the application config once, on first use.
     * Config values do not change at runtime, so each {@link Config#getProperty} lookup is paid once
     * rather than per resolved exception. Subclasses that override the {@code shouldLog*} predicates
     * never reach this method.
     */
    private void resolveLogFlags() {
        if (logFlagsResolved) {
            return;
        }
        synchronized (this) {
            if (logFlagsResolved) {
                return;
            }
            Config config = grailsApplication != null ? grailsApplication.getConfig() : null;
            if (config != null) {
                logFullStackTrace = config.getProperty(Settings.SETTING_LOG_FULL_STACKTRACE, Boolean.class, false);
                logAuditor = config.getProperty(Settings.SETTING_LOG_AUDITOR, Boolean.class, false);
                logRemoteAddr = config.getProperty(Settings.SETTING_LOG_REMOTE_ADDR, Boolean.class, false);
            }
            logFlagsResolved = true;
        }
    }

    /**
     * Resolves the client address to include in the exception log headline. The default
     * returns {@link HttpServletRequest#getRemoteAddr()} — the container's view of the
     * TCP peer, which reflects forwarded-header handling only when the servlet container
     * is configured to trust a proxy chain (for example Spring Boot's
     * {@code server.forward-headers-strategy}). Subclasses can override this to apply a
     * different resolution strategy; the returned value (null or empty to omit) is
     * appended verbatim as {@code ip: <value>}.
     */
    protected String resolveRemoteAddr(HttpServletRequest request) {
        return request.getRemoteAddr();
    }

    /**
     * Appends optional per-request context (remote address, current auditor) to the log
     * headline as a single parenthesised clause — e.g. {@code (ip: 1.2.3.4, user: alice)}.
     * Emits nothing when no tokens apply, so the baseline headline format is unchanged.
     */
    protected void appendRequestContext(StringBuilder sb, HttpServletRequest request) {
        List<String> tokens = new ArrayList<>(2);
        if (shouldLogRemoteAddr()) {
            String remoteAddr = resolveRemoteAddr(request);
            if (remoteAddr != null && !remoteAddr.isEmpty()) {
                tokens.add("ip: " + remoteAddr);
            }
        }
        if (shouldLogAuditor() && auditorAwareLookup != null) {
            auditorAwareLookup.getCurrentAuditor().ifPresent(auditor ->
                tokens.add("user: " + auditor)
            );
        }
        if (!tokens.isEmpty()) {
            sb.append(" (").append(String.join(", ", tokens)).append(")");
        }
    }

    protected Exception findWrappedException(Exception e) {
        if ((e instanceof InvokerInvocationException) || (e instanceof GrailsMVCException)) {
            Throwable t = getRootCause(e);
            if (t instanceof Exception) {
                e = (Exception) t;
            }
        }
        return e;
    }

    protected String getRequestLogMessage(String exceptionName, HttpServletRequest request, String message) {
        StringBuilder sb = new StringBuilder();

        sb.append(exceptionName)
          .append(" occurred when processing request: ")
          .append("[").append(request.getMethod().toUpperCase()).append("] ");

        if (request.getAttribute(WebUtils.FORWARD_REQUEST_URI_ATTRIBUTE) != null) {
            sb.append(request.getAttribute(WebUtils.FORWARD_REQUEST_URI_ATTRIBUTE));
        } else {
            sb.append(request.getRequestURI());
        }

        appendRequestContext(sb, request);

        Config config = grailsApplication != null ? grailsApplication.getConfig() : null;
        final boolean shouldLogRequestParameters = config != null ? config.getProperty(Settings.SETTING_LOG_REQUEST_PARAMETERS, Boolean.class, Environment.getCurrent() == Environment.DEVELOPMENT) : false;

        if (shouldLogRequestParameters) {
            Enumeration<String> params = request.getParameterNames();

            if (params.hasMoreElements()) {
                String param;
                String values[];
                int i;

                sb.append(" - parameters:");
                @SuppressWarnings("unchecked")
                List<String> blackList = (config.getProperty(Settings.SETTING_EXCEPTION_RESOLVER_PARAM_EXCLUDES, List.class, Collections.emptyList()));

                if (blackList == null) {
                    blackList = Collections.emptyList();
                }
                while (params.hasMoreElements()) {
                    param = params.nextElement();
                    values = request.getParameterValues(param);

                    if (values != null) {
                        for (i = 0; i < values.length; i++) {
                            sb.append(LINE_SEPARATOR).append(param).append(": ");

                            if (isExcludedRequestParameter(param, blackList)) {
                                sb.append("***");
                            } else {
                                sb.append(values[i]);
                            }
                        }
                    }
                }
            }
        }

        sb.append(LINE_SEPARATOR);
        if (message != null) {
            sb.append(message).append(". ");
        }
        sb.append("Stacktrace follows:");

        return sb.toString();
    }

    protected boolean isExcludedRequestParameter(String parameterName, List<String> excludedParameterNames) {
        if (parameterName == null || excludedParameterNames == null) {
            return false;
        }
        for (String excludedParameterName : excludedParameterNames) {
            if (excludedParameterName != null && parameterName.equalsIgnoreCase(excludedParameterName)) {
                return true;
            }
        }
        return false;
    }

    protected void createStackFilterer() {
        StackTraceFilterer promoted = resolvePromotedStackTraceFilterer();
        if (promoted != null) {
            stackFilterer = promoted;
            return;
        }
        try {
            Class filtererClass = grailsApplication.getConfig().getProperty(Settings.SETTING_LOGGING_STACKTRACE_FILTER_CLASS, Class.class, DefaultStackTraceFilterer.class);
            stackFilterer = BeanUtils.instantiateClass(filtererClass, StackTraceFilterer.class);
        }
        catch (Throwable t) {
            logger.error("Problem instantiating StackTracePrinter class, using default: " + t.getMessage());
            stackFilterer = new DefaultStackTraceFilterer();
        }
        applyLogFullStackTraceOnFilter();
    }

    /**
     * Looks up the {@link StackTraceFilterer} that
     * {@link GrailsBootstrapRegistryInitializer} promoted to the {@code ApplicationContext} during
     * bootstrap, so this resolver reuses that instance instead of instantiating a second copy from
     * config. Returns {@code null} when the bean cannot be obtained — no such bean (e.g. a
     * {@code GrailsApplication} wired up outside the normal Spring Boot bootstrap sequence), or a
     * bean of an incompatible type registered under the same name — in which case
     * {@link #createStackFilterer()} falls back to its own construction. A filterer
     * misconfiguration must never fail the context, so no {@code BeansException} escapes here.
     *
     * <p>Note that an application registering its own bean definition named
     * {@link GrailsBootstrapRegistryInitializer#STACK_TRACE_FILTERER_BEAN_NAME} replaces the
     * promoted singleton: this resolver then uses the application's bean, while the static filterer
     * already installed in {@code GrailsUtil} at bootstrap remains the config-resolved one. Replace
     * the bean only when that split is intended; otherwise configure
     * {@code grails.logging.stackTraceFiltererClass} so both consume the same instance.
     */
    protected StackTraceFilterer resolvePromotedStackTraceFilterer() {
        ApplicationContext context = grailsApplication.getMainContext();
        if (context == null) {
            return null;
        }
        try {
            return context.getBean(GrailsBootstrapRegistryInitializer.STACK_TRACE_FILTERER_BEAN_NAME,
                    StackTraceFilterer.class);
        }
        catch (BeansException e) {
            return null;
        }
    }

    /**
     * Propagates {@code grails.exceptionresolver.logFullStackTraceOnFilter} to the
     * filterer instance when it is a {@link DefaultStackTraceFilterer} (or subclass
     * thereof). Custom {@link StackTraceFilterer} implementations that do not extend
     * the default are responsible for their own logging policy.
     */
    protected void applyLogFullStackTraceOnFilter() {
        if (stackFilterer instanceof DefaultStackTraceFilterer) {
            Config config = grailsApplication != null ? grailsApplication.getConfig() : null;
            boolean logOnFilter = config == null ?
                true :
                config.getProperty(Settings.SETTING_LOG_FULL_STACKTRACE_ON_FILTER, Boolean.class, true);
            ((DefaultStackTraceFilterer) stackFilterer).setLogFullStackTraceOnFilter(logOnFilter);
        }
    }
}
