Search in sources :

Example 1 with StringTemplateResource

use of org.thymeleaf.templateresource.StringTemplateResource in project thymeleaf-tests by thymeleaf.

the class BareHtmlEngineTest method check.

private static void check(final String input, final String output, final Set<String> blockSelectors) throws Exception {
    final String templateName = "test";
    final StringWriter writer = new StringWriter();
    final ITemplateHandler handler = new OutputTemplateHandler(writer);
    PARSER.parseStandalone(TEMPLATE_ENGINE_CONFIGURATION, templateName, templateName, blockSelectors, new StringTemplateResource(input), TemplateMode.HTML, false, handler);
    Assert.assertEquals("Test failed for file: " + templateName, output, writer.toString());
}
Also used : StringTemplateResource(org.thymeleaf.templateresource.StringTemplateResource) StringWriter(java.io.StringWriter)

Example 2 with StringTemplateResource

use of org.thymeleaf.templateresource.StringTemplateResource in project thymeleaf-tests by thymeleaf.

the class TestTemplateResolver method resolveTemplate.

public TemplateResolution resolveTemplate(final IEngineConfiguration configuration, final String ownerTemplate, final String template, final Map<String, Object> templateResolutionAttributes) {
    final int placeholderPos = this.template.indexOf("{%%}");
    final String resource = this.template.substring(0, placeholderPos) + template + this.template.substring(placeholderPos + 4);
    final ITemplateResource templateResource = new StringTemplateResource(resource);
    final TemplateResolution templateResolution = new TemplateResolution(templateResource, // For the sake of these tests, considering resource existence verified is fine
    true, TemplateMode.HTML, false, new NonCacheableCacheEntryValidity());
    return templateResolution;
}
Also used : StringTemplateResource(org.thymeleaf.templateresource.StringTemplateResource) TemplateResolution(org.thymeleaf.templateresolver.TemplateResolution) ITemplateResource(org.thymeleaf.templateresource.ITemplateResource) NonCacheableCacheEntryValidity(org.thymeleaf.cache.NonCacheableCacheEntryValidity)

Example 3 with StringTemplateResource

use of org.thymeleaf.templateresource.StringTemplateResource in project cas by apereo.

the class RestfulUrlTemplateResolver method computeTemplateResource.

@Override
protected ITemplateResource computeTemplateResource(final IEngineConfiguration configuration, final String ownerTemplate, final String template, final String resourceName, final String characterEncoding, final Map<String, Object> templateResolutionAttributes) {
    val rest = casProperties.getView().getRest();
    val request = HttpRequestUtils.getHttpServletRequestFromRequestAttributes();
    val themeName = this.themeResolver.resolveThemeName(request);
    val headers = new LinkedHashMap<String, Object>();
    headers.put("owner", ownerTemplate);
    headers.put("template", template);
    headers.put("resource", resourceName);
    if (StringUtils.isNotBlank(themeName)) {
        headers.put("theme", themeName);
    }
    headers.put("locale", request.getLocale().getCountry());
    headers.putAll(HttpRequestUtils.getRequestHeaders(request));
    headers.putAll(rest.getHeaders());
    HttpResponse response = null;
    try {
        val exec = HttpUtils.HttpExecutionRequest.builder().basicAuthPassword(rest.getBasicAuthPassword()).basicAuthUsername(rest.getBasicAuthUsername()).method(HttpMethod.valueOf(rest.getMethod().toUpperCase().trim())).url(rest.getUrl()).headers(headers).build();
        response = HttpUtils.execute(exec);
        val statusCode = response.getStatusLine().getStatusCode();
        if (HttpStatus.valueOf(statusCode).is2xxSuccessful()) {
            val result = IOUtils.toString(response.getEntity().getContent(), StandardCharsets.UTF_8);
            return new StringTemplateResource(result);
        }
    } catch (final Exception e) {
        LoggingUtils.error(LOGGER, e);
    } finally {
        HttpUtils.close(response);
    }
    return super.computeTemplateResource(configuration, ownerTemplate, template, resourceName, characterEncoding, templateResolutionAttributes);
}
Also used : lombok.val(lombok.val) StringTemplateResource(org.thymeleaf.templateresource.StringTemplateResource) HttpResponse(org.apache.http.HttpResponse) LinkedHashMap(java.util.LinkedHashMap)

Example 4 with StringTemplateResource

use of org.thymeleaf.templateresource.StringTemplateResource in project thymeleaf-tests by thymeleaf.

the class HtmlBlockSelectorMarkupHandlerTest method check.

private static void check(final HTMLTemplateParser parser, final IEngineConfiguration templateEngineContext, final String templateName, final String input, final String output, final Set<String> blockSelectors) throws Exception {
    final StringWriter writer = new StringWriter();
    final ITemplateHandler handler = new OutputTemplateHandler(writer);
    parser.parseStandalone(templateEngineContext, templateName, templateName, blockSelectors, new StringTemplateResource(input), TemplateMode.HTML, false, handler);
    assertEquals("Test failed for file: " + templateName, output, writer.toString());
}
Also used : ITemplateHandler(org.thymeleaf.engine.ITemplateHandler) StringTemplateResource(org.thymeleaf.templateresource.StringTemplateResource) StringWriter(java.io.StringWriter) OutputTemplateHandler(org.thymeleaf.engine.OutputTemplateHandler)

Aggregations

StringTemplateResource (org.thymeleaf.templateresource.StringTemplateResource)4 StringWriter (java.io.StringWriter)2 LinkedHashMap (java.util.LinkedHashMap)1 lombok.val (lombok.val)1 HttpResponse (org.apache.http.HttpResponse)1 NonCacheableCacheEntryValidity (org.thymeleaf.cache.NonCacheableCacheEntryValidity)1 ITemplateHandler (org.thymeleaf.engine.ITemplateHandler)1 OutputTemplateHandler (org.thymeleaf.engine.OutputTemplateHandler)1 TemplateResolution (org.thymeleaf.templateresolver.TemplateResolution)1 ITemplateResource (org.thymeleaf.templateresource.ITemplateResource)1