All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ninja.diagnostics.DiagnosticErrorRenderer Maven / Gradle / Ivy

/**
 * Copyright (C) the original author or authors.
 *
 * Licensed 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
 *
 *     http://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 ninja.diagnostics;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.net.URI;
import java.net.URL;
import java.util.List;
import java.util.Map;

import ninja.Context;
import ninja.Cookie;
import ninja.Result;
import ninja.Route;
import ninja.exceptions.InternalServerErrorException;
import ninja.utils.ResponseStreams;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringEscapeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Utility class for rendering DiagnosticError instances as
 * a Result.  Does not rely on any 3rd party rendering library to permit
 * rendering exceptions in the case where a template engine fails!
 * 
 * @author Joe Lauer (https://twitter.com/jjlauer)
 * @author Fizzed, Inc. (http://fizzed.com)
 */
public class DiagnosticErrorRenderer {
    private static final Logger logger = LoggerFactory.getLogger(DiagnosticErrorRenderer.class);
    
    private final StringBuilder s;
    
    private DiagnosticErrorRenderer() {
        s = new StringBuilder();
    }
    
    public String render() {
        return s.toString();
    }
    
    static public void tryToRender(Context context, Result result, DiagnosticError diagnosticError, boolean throwInternalServerExceptionOnError) {
        try {
            
            DiagnosticErrorRenderer errorRenderer
                = build(context, result, diagnosticError);
            
            errorRenderer.renderResult(
                context,
                result);
            
        } catch (IOException e) {
            // fallback to ninja system-wide error handler?
            if (throwInternalServerExceptionOnError) {
                throw new InternalServerErrorException(e);
            } else {
                logger.error("Something is really fishy. Unable to render diagnostic error", e);
            }
        }
        
    }
    
    public void renderResult(Context context, Result result) throws IOException {
        String out = render();

        // set context response content type
        result.contentType("text/html");
        result.charset("utf-8");
        
        ResponseStreams responseStreams = context.finalizeHeaders(result);
        try (Writer w = responseStreams.getWriter()) {
            w.write(out);
            w.flush();
            w.close();
        }
    }
    
    static public DiagnosticErrorRenderer build(Context context,
                                                Result result,
                                                DiagnosticError diagnosticError) throws IOException {
        
        Result underlyingResult = diagnosticError.getUnderlyingResult();
        
        return new DiagnosticErrorRenderer()
            .appendHeader(
                    context,
                    result,
                    diagnosticError.getTitle())
                
            .appendTabsBegin(new String[] { "Exception", "Context", "Request", "Response" })
                
            .appendTabBegin(0)    
            .appendSourceSnippet(
                    diagnosticError.getSourceLocation(),
                    diagnosticError.getSourceLines(),
                    diagnosticError.getLineNumberOfSourceLines(),
                    diagnosticError.getLineNumberOfError())
            .appendThrowable(
                    diagnosticError.getThrowable()) 
            .appendTabEnd()
                
            .appendTabBegin(1)
            .appendContext(context)
            .appendTabEnd()
            
            .appendTabBegin(2)
            .appendRequest(context)
            .appendTabEnd()
            
            .appendTabBegin(3)
            .appendResponse(underlyingResult)
            .appendTabEnd()     
            
            .appendTabsEnd()
            .appendFooter();
    }
    
    private DiagnosticErrorRenderer appendHeader(Context context,
                                        Result result,
                                        String title) throws IOException {
        
        String headerTemplate = getResource("diagnostic_header.html");
        String styleTemplate = getResource("diagnostic.css");
        
        // simple token replacement
        headerTemplate = headerTemplate.replace("${TITLE}", escape(title));
        headerTemplate = headerTemplate.replace("${STYLE}", escape(styleTemplate));
        
        s.append(headerTemplate);
        
        if (result != null) {
            s.append("    

\n"); if (result.getStatusCode() != 200) { s.append ("Status code ").append(result.getStatusCode()); } s.append(" for request '").append(context.getMethod()).append(" ").append(context.getRequestPath()).append("'\n"); // append info about the route itself if (context.getRoute() != null) { Route route = context.getRoute(); s.append("
In controller method '").append(route.getControllerClass().getCanonicalName()).append(".").append(route.getControllerMethod().getName()).append("'\n"); } s.append("

\n"); } return this; } private DiagnosticErrorRenderer appendTabsBegin(String[] names) throws IOException { s.append("
\n"); s.append(" \n"); s.append("
\n"); return this; } private DiagnosticErrorRenderer appendTabsEnd() throws IOException { s.append("
\n"); s.append("
\n"); return this; } private DiagnosticErrorRenderer appendTabBegin(int index) throws IOException { s.append("
\n"); return this; } private DiagnosticErrorRenderer appendTabEnd() throws IOException { s.append("
\n"); return this; } private DiagnosticErrorRenderer appendFooter() throws IOException { // embed jquery s.append(""); // diagnostic javascript s.append(""); // footer body -> html tags s.append(getResource("diagnostic_footer.html")); return this; } private DiagnosticErrorRenderer appendContext(Context context) throws IOException { s.append("
\n"); s.append("

Route

\n"); if (context.getRoute() != null) { Route route = context.getRoute(); appendNameValue(s, "Http method", route.getHttpMethod()); appendNameValue(s, "Controller method", route.getControllerClass().getCanonicalName() + "." + route.getControllerMethod().getName() + "()"); StringBuilder params = new StringBuilder(); for (Class type : route.getControllerMethod().getParameterTypes()) { if (params.length() > 0) { params.append(", "); } params.append(type.getCanonicalName()); } appendNameValue(s, "Controller parameters", params.toString()); } else { appendNoValues(s); } s.append("

Session

\n"); if (context.getSession() != null && !context.getSession().getData().isEmpty()) { for (Map.Entry sessionEntry : context.getSession().getData().entrySet()) { appendNameValue(s, sessionEntry.getKey(), sessionEntry.getValue()); } } else { appendNoValues(s); } s.append("

Flash

\n"); if (context.getFlashScope() != null && !context.getFlashScope().getCurrentFlashCookieData().isEmpty()) { for (Map.Entry sessionEntry : context.getFlashScope().getCurrentFlashCookieData().entrySet()) { appendNameValue(s, sessionEntry.getKey(), sessionEntry.getValue()); } } else { appendNoValues(s); } s.append("

Attributes

\n"); Map attributes = context.getAttributes(); if (attributes != null && !attributes.isEmpty()) { for (Map.Entry entry : attributes.entrySet()) { appendNameValue(s, entry.getKey(), (entry.getValue() != null ? entry.getValue().toString() : "null")); } } else { appendNoValues(s); } List cookies = context.getCookies(); if (cookies == null || cookies.isEmpty()) { s.append("

Cookies

\n"); appendNoValues(s); } else { for (Cookie cookie : context.getCookies()) { s.append("

Cookie: ").append(cookie.getName()).append("

\n"); appendNameValue(s, "Value", cookie.getValue()); appendNameValue(s, "Path", cookie.getPath()); appendNameValue(s, "Domain", cookie.getDomain()); appendNameValue(s, "HTTP only", cookie.isHttpOnly()+""); appendNameValue(s, "Secure", cookie.isSecure()+""); appendNameValue(s, "Max age", cookie.getMaxAge()+""); appendNameValue(s, "Comment", cookie.getComment()); } } s.append("
\n"); return this; } private DiagnosticErrorRenderer appendRequest(Context context) throws IOException { s.append("
\n"); s.append("

Request

\n"); appendNameValue(s, "Context path", context.getContextPath()); appendNameValue(s, "Hostname", context.getHostname()); appendNameValue(s, "Method", context.getMethod()); appendNameValue(s, "Remote address", context.getRemoteAddr()); appendNameValue(s, "Content type", context.getRequestContentType()); appendNameValue(s, "Path", context.getRequestPath()); appendNameValue(s, "Scheme", context.getScheme()); s.append("

Parameters

\n"); Map parameters = context.getParameters(); if (parameters != null && !parameters.isEmpty()) { for (Map.Entry entry : parameters.entrySet()) { for (String value : entry.getValue()) { appendNameValue(s, entry.getKey(), value); } } } else { appendNoValues(s); } s.append("

Headers

\n"); Map> headers = context.getHeaders(); if (headers != null && !headers.isEmpty()) { for (Map.Entry> entry : headers.entrySet()) { for (String value : entry.getValue()) { appendNameValue(s, entry.getKey(), value); } } } else { appendNoValues(s); } s.append("
\n"); return this; } private DiagnosticErrorRenderer appendResponse(Result result) throws IOException { s.append("
\n"); s.append("

Application Result

\n"); if (result == null) { // request did not get far enough along in processing to actually // have a response for us to debug appendNoValues(s, "Application failure before a result was created"); return this; } appendNameValue(s, "Template", result.getTemplate()); appendNameValue(s, "Charset", result.getCharset()); appendNameValue(s, "Content type", result.getContentType()); appendNameValue(s, "Status code", result.getStatusCode()+""); List supportedContentTypes = result.supportedContentTypes(); if (supportedContentTypes == null || supportedContentTypes.isEmpty()) { appendNameValue(s, "Supported content types", "None set"); } else { for (int i = 0; i < supportedContentTypes.size(); i++) { appendNameValue(s, "Supported content type #" + i, supportedContentTypes.get(i)); } } appendNameValue(s, "Fallback content type", result.fallbackContentType().orElse("None set")); appendNameValue(s, "Json View", (result.getJsonView() != null ? result.getJsonView().getClass().getCanonicalName() : "None")); s.append("

Renderable

\n"); Object renderable = result.getRenderable(); // only rendering exceptions would have the renderable actually set // to something other than a DiagnosticError if (renderable == null || renderable instanceof DiagnosticError) { appendNoValues(s); } else if (renderable instanceof Map) { Map map = (Map)renderable; for (Map.Entry entry : map.entrySet()) { appendNameValue(s, entry.getKey(), (entry.getValue() != null ? entry.getValue().toString() : "null")); } } else { appendNameValue(s, "Class of", renderable.getClass().getCanonicalName()); } s.append("

Headers

\n"); Map headers = result.getHeaders(); if (headers != null && !headers.isEmpty()) { for (Map.Entry entry : headers.entrySet()) { appendNameValue(s, entry.getKey(), entry.getValue()); } } else { appendNoValues(s); } s.append("
\n"); return this; } private void appendNameValue(StringBuilder sb, String name, String value) throws IOException { sb.append("
");
        sb.append(escape(name));
        sb.append("");
        sb.append(escape(value));
        sb.append("
"); } private void appendNoValues(StringBuilder sb) throws IOException { appendNoValues(sb, "No values"); } private void appendNoValues(StringBuilder sb, String title) throws IOException { sb.append("
").append(escape(title)).append("

"); } private DiagnosticErrorRenderer appendSourceSnippet(URI sourceLocation, List sourceLines, int lineNumberOfSourceLines, int lineNumberOfError) { if (sourceLocation != null) { s.append("

").append(escape(sourceLocation.toString())).append("

\n"); } if (sourceLines != null) { s.append("
\n"); for (int i = 0; i < sourceLines.size(); i++) { s.append("
");
                
                int lineNumber = lineNumberOfSourceLines + i;
                
                // line of error?
                String cssClass = (lineNumber == lineNumberOfError ? "line error" : "line info");

                s.append("").append(lineNumber).append("");
                s.append("")
                        .append(escape(sourceLines.get(i)))
                        .append("");
                s.append("
"); } s.append("
\n"); } return this; } private DiagnosticErrorRenderer appendThrowable(Throwable throwable) throws IOException { s.append("

Stack Trace

"); if (throwable != null) { s.append("
").append(escape(throwableStackTraceToString(throwable))).append("
\n"); } else { appendNoValues(s, "Result was not triggered by an exception"); } return this; } private String throwableStackTraceToString(Throwable throwable) { StringWriter sw = new StringWriter(); try (PrintWriter pw = new PrintWriter(sw)) { throwable.printStackTrace(pw); } return sw.toString(); } private String escape(String value) { return StringEscapeUtils.escapeHtml4(value); } private String getResource(String resourceName) throws IOException { URL url = getClass().getResource(resourceName); if (url == null) { throw new IOException("Unable to find diagnostic resource: " + resourceName); } return IOUtils.toString(url); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy