Search in sources :

Example 1 with TemplateEngineException

use of org.thymeleaf.exceptions.TemplateEngineException in project thymeleaf by thymeleaf.

the class TemplateEngine method processThrottled.

public final IThrottledTemplateProcessor processThrottled(final TemplateSpec templateSpec, final IContext context) {
    if (!this.initialized) {
        initialize();
    }
    final IThrottledTemplateProcessor throttledTemplateProcessor;
    try {
        Validate.notNull(templateSpec, "Template Specification cannot be null");
        Validate.notNull(context, "Context cannot be null");
        if (logger.isTraceEnabled()) {
            logger.trace("[THYMELEAF][{}] STARTING PREPARATION OF THROTTLED TEMPLATE \"{}\" WITH LOCALE {}", new Object[] { TemplateEngine.threadIndex(), templateSpec, context.getLocale() });
        }
        final long startNanos = System.nanoTime();
        final TemplateManager templateManager = this.configuration.getTemplateManager();
        throttledTemplateProcessor = templateManager.parseAndProcessThrottled(templateSpec, context);
        final long endNanos = System.nanoTime();
        if (logger.isTraceEnabled()) {
            logger.trace("[THYMELEAF][{}] FINISHED PREPARATION OF THROTTLED TEMPLATE \"{}\" WITH LOCALE {}", new Object[] { TemplateEngine.threadIndex(), templateSpec, context.getLocale() });
        }
        if (timerLogger.isTraceEnabled()) {
            final BigDecimal elapsed = BigDecimal.valueOf(endNanos - startNanos);
            final BigDecimal elapsedMs = elapsed.divide(BigDecimal.valueOf(NANOS_IN_SECOND), RoundingMode.HALF_UP);
            timerLogger.trace("[THYMELEAF][{}][{}][{}][{}][{}] TEMPLATE \"{}\" WITH LOCALE {} PREPARED FOR THROTTLED PROCESSING IN {} nanoseconds (approx. {}ms)", new Object[] { TemplateEngine.threadIndex(), LoggingUtils.loggifyTemplateName(templateSpec.getTemplate()), context.getLocale(), elapsed, elapsedMs, templateSpec, context.getLocale(), elapsed, elapsedMs });
        }
    } catch (final TemplateOutputException e) {
        // We log the exception just in case higher levels do not end up logging it (e.g. they could simply display traces in the browser
        logger.error(String.format("[THYMELEAF][%s] Exception preparing throttled template \"%s\": %s", new Object[] { TemplateEngine.threadIndex(), templateSpec, e.getMessage() }), e);
        throw e;
    } catch (final TemplateEngineException e) {
        // We log the exception just in case higher levels do not end up logging it (e.g. they could simply display traces in the browser
        logger.error(String.format("[THYMELEAF][%s] Exception preparing throttled template \"%s\": %s", new Object[] { TemplateEngine.threadIndex(), templateSpec, e.getMessage() }), e);
        throw e;
    } catch (final RuntimeException e) {
        // We log the exception just in case higher levels do not end up logging it (e.g. they could simply display traces in the browser
        logger.error(String.format("[THYMELEAF][%s] Exception preparing throttled template \"%s\": %s", new Object[] { TemplateEngine.threadIndex(), templateSpec, e.getMessage() }), e);
        throw new TemplateProcessingException("Exception preparing throttled template", templateSpec.toString(), e);
    }
    return throttledTemplateProcessor;
}
Also used : TemplateManager(org.thymeleaf.engine.TemplateManager) TemplateOutputException(org.thymeleaf.exceptions.TemplateOutputException) TemplateProcessingException(org.thymeleaf.exceptions.TemplateProcessingException) TemplateEngineException(org.thymeleaf.exceptions.TemplateEngineException) BigDecimal(java.math.BigDecimal)

Example 2 with TemplateEngineException

use of org.thymeleaf.exceptions.TemplateEngineException in project thymeleaf by thymeleaf.

the class TemplateEngine method process.

public final void process(final TemplateSpec templateSpec, final IContext context, final Writer writer) {
    if (!this.initialized) {
        initialize();
    }
    try {
        Validate.notNull(templateSpec, "Template Specification cannot be null");
        Validate.notNull(context, "Context cannot be null");
        Validate.notNull(writer, "Writer cannot be null");
        if (logger.isTraceEnabled()) {
            logger.trace("[THYMELEAF][{}] STARTING PROCESS OF TEMPLATE \"{}\" WITH LOCALE {}", new Object[] { TemplateEngine.threadIndex(), templateSpec, context.getLocale() });
        }
        final long startNanos = System.nanoTime();
        final TemplateManager templateManager = this.configuration.getTemplateManager();
        templateManager.parseAndProcess(templateSpec, context, writer);
        final long endNanos = System.nanoTime();
        if (logger.isTraceEnabled()) {
            logger.trace("[THYMELEAF][{}] FINISHED PROCESS AND OUTPUT OF TEMPLATE \"{}\" WITH LOCALE {}", new Object[] { TemplateEngine.threadIndex(), templateSpec, context.getLocale() });
        }
        if (timerLogger.isTraceEnabled()) {
            final BigDecimal elapsed = BigDecimal.valueOf(endNanos - startNanos);
            final BigDecimal elapsedMs = elapsed.divide(BigDecimal.valueOf(NANOS_IN_SECOND), RoundingMode.HALF_UP);
            timerLogger.trace("[THYMELEAF][{}][{}][{}][{}][{}] TEMPLATE \"{}\" WITH LOCALE {} PROCESSED IN {} nanoseconds (approx. {}ms)", new Object[] { TemplateEngine.threadIndex(), LoggingUtils.loggifyTemplateName(templateSpec.getTemplate()), context.getLocale(), elapsed, elapsedMs, templateSpec, context.getLocale(), elapsed, elapsedMs });
        }
        /*
             * Finally, flush the writer in order to make sure that everything has been written to output
             */
        try {
            writer.flush();
        } catch (final IOException e) {
            throw new TemplateOutputException("An error happened while flushing output writer", templateSpec.getTemplate(), -1, -1, e);
        }
    } catch (final TemplateOutputException e) {
        // We log the exception just in case higher levels do not end up logging it (e.g. they could simply display traces in the browser
        logger.error(String.format("[THYMELEAF][%s] Exception processing template \"%s\": %s", new Object[] { TemplateEngine.threadIndex(), templateSpec, e.getMessage() }), e);
        throw e;
    } catch (final TemplateEngineException e) {
        // We log the exception just in case higher levels do not end up logging it (e.g. they could simply display traces in the browser
        logger.error(String.format("[THYMELEAF][%s] Exception processing template \"%s\": %s", new Object[] { TemplateEngine.threadIndex(), templateSpec, e.getMessage() }), e);
        throw e;
    } catch (final RuntimeException e) {
        // We log the exception just in case higher levels do not end up logging it (e.g. they could simply display traces in the browser
        logger.error(String.format("[THYMELEAF][%s] Exception processing template \"%s\": %s", new Object[] { TemplateEngine.threadIndex(), templateSpec, e.getMessage() }), e);
        throw new TemplateProcessingException("Exception processing template", templateSpec.toString(), e);
    }
}
Also used : TemplateManager(org.thymeleaf.engine.TemplateManager) TemplateOutputException(org.thymeleaf.exceptions.TemplateOutputException) TemplateProcessingException(org.thymeleaf.exceptions.TemplateProcessingException) TemplateEngineException(org.thymeleaf.exceptions.TemplateEngineException) IOException(java.io.IOException) BigDecimal(java.math.BigDecimal)

Example 3 with TemplateEngineException

use of org.thymeleaf.exceptions.TemplateEngineException in project thymeleaf by thymeleaf.

the class ThrottledTemplateProcessor method process.

private int process(final int maxOutput, final String outputType) {
    int writtenCount = 0;
    try {
        if (this.allProcessingFinished || maxOutput == 0) {
            // No bytes written
            return 0;
        }
        if (logger.isTraceEnabled()) {
            logger.trace("[THYMELEAF][{}] Starting throttled process (limit:{} {}) of template \"{}\" with locale {}", new Object[] { TemplateEngine.threadIndex(), Integer.valueOf(maxOutput), outputType, this.templateSpec, this.context.getLocale() });
        }
        final long startNanos = System.nanoTime();
        // Save the initial written count so that we can know at the end how many bytes were written
        final int initialWrittenCount = this.writer.getWrittenCount();
        // Set the new limit for the writer (might provoke overflow being processed)
        this.writer.allow(maxOutput);
        // Maybe by processing all overflow we just finished
        if (!computeFinish() && !this.writer.isStopped()) {
            if (this.flowController.processorTemplateHandlerPending) {
                this.processorTemplateHandler.handlePending();
            }
            if (!computeFinish() && !this.writer.isStopped()) {
                this.offset += this.templateModel.process(this.templateHandler, this.offset, this.flowController);
                if (this.offset == this.templateModel.size()) {
                    EngineContextManager.disposeEngineContext(this.context);
                    this.eventProcessingFinished = true;
                    computeFinish();
                }
            }
        }
        final long endNanos = System.nanoTime();
        /*
             * Finally, flush the writer in order to make sure that everything has been written to output
             */
        try {
            this.writer.flush();
        } catch (final IOException e) {
            throw new TemplateOutputException("An error happened while flushing output writer", templateSpec.getTemplate(), -1, -1, e);
        }
        writtenCount = this.writer.getWrittenCount() - initialWrittenCount;
        if (logger.isTraceEnabled()) {
            logger.trace("[THYMELEAF][{}] Finished throttled process (limit:{} {}, output: {} {}) of template \"{}\" with locale {}", new Object[] { TemplateEngine.threadIndex(), Integer.valueOf(maxOutput), outputType, Integer.valueOf(writtenCount), outputType, this.templateSpec, this.context.getLocale() });
        }
        if (timerLogger.isTraceEnabled()) {
            final BigDecimal elapsed = BigDecimal.valueOf(endNanos - startNanos);
            final BigDecimal elapsedMs = elapsed.divide(BigDecimal.valueOf(NANOS_IN_SECOND), RoundingMode.HALF_UP);
            timerLogger.trace("[THYMELEAF][{}][{}][{}][{}][{}] TEMPLATE \"{}\" WITH LOCALE {} PROCESSED (THROTTLED, LIMIT:{} {}, OUTPUT: {} {}) IN {} nanoseconds (approx. {}ms)", new Object[] { TemplateEngine.threadIndex(), LoggingUtils.loggifyTemplateName(this.templateSpec.getTemplate()), this.context.getLocale(), elapsed, elapsedMs, this.templateSpec, this.context.getLocale(), Integer.valueOf(maxOutput), outputType, Integer.valueOf(writtenCount), outputType, elapsed, elapsedMs });
        }
    } catch (final TemplateOutputException e) {
        this.eventProcessingFinished = true;
        this.allProcessingFinished = true;
        // We log the exception just in case higher levels do not end up logging it (e.g. they could simply display traces in the browser
        logger.error(String.format("[THYMELEAF][%s] Exception processing throttled template \"%s\": %s", new Object[] { TemplateEngine.threadIndex(), this.templateSpec, e.getMessage() }), e);
        throw e;
    } catch (final TemplateEngineException e) {
        this.eventProcessingFinished = true;
        this.allProcessingFinished = true;
        // We log the exception just in case higher levels do not end up logging it (e.g. they could simply display traces in the browser
        logger.error(String.format("[THYMELEAF][%s] Exception processing throttled template \"%s\": %s", new Object[] { TemplateEngine.threadIndex(), this.templateSpec, e.getMessage() }), e);
        throw e;
    } catch (final Exception e) {
        this.eventProcessingFinished = true;
        this.allProcessingFinished = true;
        // We log the exception just in case higher levels do not end up logging it (e.g. they could simply display traces in the browser
        logger.error(String.format("[THYMELEAF][%s] Exception processing throttled template \"%s\": %s", new Object[] { TemplateEngine.threadIndex(), this.templateSpec, e.getMessage() }), e);
        throw new TemplateProcessingException("Exception processing throttled template", this.templateSpec.toString(), e);
    }
    reportFinish(outputType);
    return writtenCount;
}
Also used : TemplateOutputException(org.thymeleaf.exceptions.TemplateOutputException) TemplateProcessingException(org.thymeleaf.exceptions.TemplateProcessingException) TemplateEngineException(org.thymeleaf.exceptions.TemplateEngineException) IOException(java.io.IOException) BigDecimal(java.math.BigDecimal) TemplateProcessingException(org.thymeleaf.exceptions.TemplateProcessingException) IOException(java.io.IOException) TemplateOutputException(org.thymeleaf.exceptions.TemplateOutputException) TemplateEngineException(org.thymeleaf.exceptions.TemplateEngineException)

Aggregations

BigDecimal (java.math.BigDecimal)3 TemplateEngineException (org.thymeleaf.exceptions.TemplateEngineException)3 TemplateOutputException (org.thymeleaf.exceptions.TemplateOutputException)3 TemplateProcessingException (org.thymeleaf.exceptions.TemplateProcessingException)3 IOException (java.io.IOException)2 TemplateManager (org.thymeleaf.engine.TemplateManager)2