blob: ea5f707293f60669f9d05eaee02cea10e2aabc6e [file] [log] [blame]
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang=""><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../jacoco-resources/report.gif" type="image/gif"/><title>TurbineJspService.java</title><link rel="stylesheet" href="../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">Apache Turbine</a> &gt; <a href="index.source.html" class="el_package">org.apache.turbine.services.jsp</a> &gt; <span class="el_source">TurbineJspService.java</span></div><h1>TurbineJspService.java</h1><pre class="source lang-java linenums">package org.apache.turbine.services.jsp;
/*
* 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
* &quot;License&quot;); 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
* &quot;AS IS&quot; 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.
*/
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.configuration2.Configuration;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.turbine.Turbine;
import org.apache.turbine.pipeline.PipelineData;
import org.apache.turbine.services.InitializationException;
import org.apache.turbine.services.pull.ApplicationTool;
import org.apache.turbine.services.pull.tools.TemplateLink;
import org.apache.turbine.services.template.BaseTemplateEngineService;
import org.apache.turbine.util.RunData;
import org.apache.turbine.util.TurbineException;
/**
* This is a Service that can process JSP templates from within a Turbine
* screen.
*
* @author &lt;a href=&quot;mailto:john.mcnally@clearink.com&quot;&gt;John D. McNally&lt;/a&gt;
* @author &lt;a href=&quot;mailto:jvanzyl@apache.org&quot;&gt;Jason van Zyl&lt;/a&gt;
* @author &lt;a href=&quot;mailto:dlr@finemaltcoding.com&quot;&gt;Daniel Rall&lt;/a&gt;
* @author &lt;a href=&quot;mailto:hps@intermeta.de&quot;&gt;Henning P. Schmiedehausen&lt;/a&gt;
*/
<span class="nc" id="L53">public class TurbineJspService</span>
extends BaseTemplateEngineService
implements JspService
{
/** The base path[s] prepended to filenames given in arguments */
private String[] templatePaths;
/** The relative path[s] prepended to filenames */
private String[] relativeTemplatePaths;
/** The buffer size for the output stream. */
private int bufferSize;
/** Logging */
<span class="nc" id="L67"> private static Logger log = LogManager.getLogger(TurbineJspService.class);</span>
/**
* Load all configured components and initialize them. This is
* a zero parameter variant which queries the Turbine Servlet
* for its config.
*
* @throws InitializationException Something went wrong in the init
* stage
*/
@Override
public void init()
throws InitializationException
{
try
{
<span class="nc" id="L83"> initJsp();</span>
<span class="nc" id="L84"> registerConfiguration(JspService.JSP_EXTENSION);</span>
<span class="nc" id="L85"> setInit(true);</span>
}
<span class="nc" id="L87"> catch (Exception e)</span>
{
<span class="nc" id="L89"> throw new InitializationException(</span>
&quot;TurbineJspService failed to initialize&quot;, e);
<span class="nc" id="L91"> }</span>
<span class="nc" id="L92"> }</span>
/**
* Adds some convenience objects to the request. For example an instance
* of TemplateLink which can be used to generate links to other templates.
*
* @param pipelineData the Turbine PipelineData object
*/
@Override
public void addDefaultObjects(PipelineData pipelineData)
{
<span class="nc" id="L103"> HttpServletRequest req = pipelineData.get(Turbine.class, HttpServletRequest.class);</span>
//
// This is a place where an Application Pull Tool is used
// in a regular Java Context. We have no Pull Service with the
// Jsp Paging stuff, but we can run our Application Tool by Hand:
//
<span class="nc" id="L110"> ApplicationTool templateLink = new TemplateLink();</span>
<span class="nc" id="L111"> templateLink.init(pipelineData);</span>
<span class="nc" id="L113"> req.setAttribute(LINK, templateLink);</span>
<span class="nc" id="L114"> req.setAttribute(PIPELINE_DATA, pipelineData);</span>
<span class="nc" id="L115"> }</span>
/**
* Returns the default buffer size of the JspService
*
* @return The default buffer size.
*/
@Override
public int getDefaultBufferSize()
{
<span class="nc" id="L125"> return bufferSize;</span>
}
/**
* executes the JSP given by templateName.
*
* @param pipelineData A PipelineData Object
* @param templateName The template to execute
* @param isForward whether to perform a forward or include.
*
* @throws TurbineException If a problem occurred while executing the JSP
*/
@Override
public void handleRequest(PipelineData pipelineData, String templateName, boolean isForward)
throws TurbineException
{
<span class="nc bnc" id="L141" title="All 2 branches missed."> if(!(pipelineData instanceof RunData))</span>
{
<span class="nc" id="L143"> throw new RuntimeException(&quot;Can't cast to rundata from pipeline data.&quot;);</span>
}
<span class="nc" id="L146"> RunData data = (RunData)pipelineData;</span>
/** template name with relative path */
<span class="nc" id="L149"> String relativeTemplateName = getRelativeTemplateName(templateName);</span>
<span class="nc bnc" id="L151" title="All 2 branches missed."> if (StringUtils.isEmpty(relativeTemplateName))</span>
{
<span class="nc" id="L153"> throw new TurbineException(</span>
&quot;Template &quot; + templateName + &quot; not found in template paths&quot;);
}
// get the RequestDispatcher for the JSP
<span class="nc" id="L158"> RequestDispatcher dispatcher = data.getServletContext()</span>
<span class="nc" id="L159"> .getRequestDispatcher(relativeTemplateName);</span>
try
{
<span class="nc bnc" id="L163" title="All 2 branches missed."> if (isForward)</span>
{
// forward the request to the JSP
<span class="nc" id="L166"> dispatcher.forward(data.getRequest(), data.getResponse());</span>
}
else
{
<span class="nc" id="L170"> data.getResponse().getWriter().flush();</span>
// include the JSP
<span class="nc" id="L172"> dispatcher.include(data.getRequest(), data.getResponse());</span>
}
}
<span class="nc" id="L175"> catch (Exception e)</span>
{
// Let's try hard to send the error message to the browser, to speed up debugging
try
{
<span class="nc" id="L180"> data.getResponse().getWriter().print(&quot;Error encountered processing a template: &quot;</span>
+ templateName);
<span class="nc" id="L182"> e.printStackTrace(data.getResponse().getWriter());</span>
}
<span class="nc" id="L184"> catch (IOException ignored)</span>
{
// ignore
<span class="nc" id="L187"> }</span>
// pass the exception to the caller according to the general
// contract for templating services in Turbine
<span class="nc" id="L191"> throw new TurbineException(</span>
&quot;Error encountered processing a template: &quot; + templateName, e);
<span class="nc" id="L193"> }</span>
<span class="nc" id="L194"> }</span>
/**
* executes the JSP given by templateName.
*
* @param pipelineData A PipelineData Object
* @param templateName The template to execute
*
* @throws TurbineException If a problem occurred while executing the JSP
*/
@Override
public void handleRequest(PipelineData pipelineData, String templateName)
throws TurbineException
{
<span class="nc" id="L208"> handleRequest(pipelineData, templateName, false);</span>
<span class="nc" id="L209"> }</span>
/**
* This method sets up the template cache.
*/
private void initJsp()
throws Exception
{
<span class="nc" id="L217"> Configuration config = getConfiguration();</span>
// Set relative paths from config.
// Needed for jakarta.servlet.RequestDispatcher
<span class="nc" id="L221"> relativeTemplatePaths = config.getStringArray(TEMPLATE_PATH_KEY);</span>
// Use Turbine Servlet to translate the template paths.
<span class="nc" id="L224"> templatePaths = new String [relativeTemplatePaths.length];</span>
<span class="nc bnc" id="L225" title="All 2 branches missed."> for (int i=0; i &lt; relativeTemplatePaths.length; i++)</span>
{
<span class="nc" id="L227"> relativeTemplatePaths[i] = warnAbsolute(relativeTemplatePaths[i]);</span>
<span class="nc" id="L229"> templatePaths[i] = Turbine.getRealPath(relativeTemplatePaths[i]);</span>
}
<span class="nc" id="L232"> bufferSize = config.getInt(JspService.BUFFER_SIZE_KEY,</span>
JspService.BUFFER_SIZE_DEFAULT);
<span class="nc" id="L234"> }</span>
/**
* Determine whether a given template is available on the
* configured template pathes.
*
* @param template The name of the requested Template
* @return True if the template is available.
*/
@Override
public boolean templateExists(String template)
{
<span class="nc" id="L246"> return Arrays.stream(templatePaths).anyMatch(templatePath -&gt; templateExists(templatePath, template));</span>
}
/**
* Determine whether a given template exists on the supplied
* template path. This service ATM only supports file based
* templates so it simply checks for file existence.
*
* @param path The absolute (file system) template path
* @param template The name of the requested Template
* @return True if the template is available.
*/
private boolean templateExists(String path, String template)
{
<span class="nc" id="L260"> return new File(path, template).exists();</span>
}
/**
* Searches for a template in the default.template path[s] and
* returns the template name with a relative path which is
* required by &lt;a href=&quot;http://java.sun.com/products/servlet/2.3/javadoc/jakarta/servlet/ServletContext.html#getRequestDispatcher(java.lang.String)&quot;&gt;
* jakarta.servlet.RequestDispatcher&lt;/a&gt;
*
* @param template the name of the template
* @return String
*/
@Override
public String getRelativeTemplateName(String template)
{
<span class="nc" id="L275"> String relativeTemplate = warnAbsolute(template);</span>
// Find which template path the template is in
// We have a 1:1 match between relative and absolute
// pathes so we can use the index for translation.
<span class="nc bnc" id="L280" title="All 2 branches missed."> for (int i = 0; i &lt; templatePaths.length; i++)</span>
{
<span class="nc bnc" id="L282" title="All 2 branches missed."> if (templateExists(templatePaths[i], relativeTemplate))</span>
{
<span class="nc" id="L284"> return relativeTemplatePaths[i] + &quot;/&quot; + relativeTemplate;</span>
}
}
<span class="nc" id="L287"> return null;</span>
}
/**
* Warn if a template name or path starts with &quot;/&quot;.
*
* @param template The template to test
* @return The template name with a leading / stripped off
*/
private String warnAbsolute(String template)
{
<span class="nc bnc" id="L298" title="All 2 branches missed."> if (template.startsWith(&quot;/&quot;))</span>
{
<span class="nc" id="L300"> log.warn(&quot;Template {} has a leading /, which is wrong!&quot;, template);</span>
<span class="nc" id="L301"> return template.substring(1);</span>
}
<span class="nc" id="L303"> return template;</span>
}
}
</pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.12.202403310830</span></div></body></html>