1 package org.apache.turbine.services.jsp; 2 3 18 19 import java.io.File ; 20 import java.io.IOException ; 21 22 import javax.servlet.RequestDispatcher ; 23 import javax.servlet.ServletConfig ; 24 import javax.servlet.http.HttpServletRequest ; 25 26 import org.apache.commons.configuration.Configuration; 27 28 import org.apache.commons.lang.StringUtils; 29 30 import org.apache.commons.logging.Log; 31 import org.apache.commons.logging.LogFactory; 32 33 import org.apache.turbine.Turbine; 34 import org.apache.turbine.services.InitializationException; 35 import org.apache.turbine.services.pull.ApplicationTool; 36 import org.apache.turbine.services.pull.tools.TemplateLink; 37 import org.apache.turbine.services.template.BaseTemplateEngineService; 38 import org.apache.turbine.util.RunData; 39 import org.apache.turbine.util.TurbineException; 40 41 51 public class TurbineJspService 52 extends BaseTemplateEngineService 53 implements JspService 54 { 55 56 private String [] templatePaths; 57 58 59 private String [] relativeTemplatePaths; 60 61 62 private int bufferSize; 63 64 65 private static Log log = LogFactory.getLog(TurbineJspService.class); 66 67 75 public void init() 76 throws InitializationException 77 { 78 try 79 { 80 initJsp(); 81 registerConfiguration(JspService.JSP_EXTENSION); 82 setInit(true); 83 } 84 catch (Exception e) 85 { 86 throw new InitializationException( 87 "TurbineJspService failed to initialize", e); 88 } 89 } 90 91 99 public void init(ServletConfig config) 100 throws InitializationException 101 { 102 init(); 103 } 104 105 111 public void addDefaultObjects(RunData data) 112 { 113 HttpServletRequest req = data.getRequest(); 114 115 ApplicationTool templateLink = new TemplateLink(); 121 templateLink.init(data); 122 123 req.setAttribute(LINK, templateLink); 124 req.setAttribute(RUNDATA, data); 125 } 126 127 132 public int getDefaultBufferSize() 133 { 134 return bufferSize; 135 } 136 137 145 public void handleRequest(RunData data, String templateName) 146 throws TurbineException 147 { 148 handleRequest(data, templateName, false); 149 } 150 151 160 public void handleRequest(RunData data, String templateName, boolean isForward) 161 throws TurbineException 162 { 163 164 String relativeTemplateName = getRelativeTemplateName(templateName); 165 166 if (StringUtils.isEmpty(relativeTemplateName)) 167 { 168 throw new TurbineException( 169 "Template " + templateName + " not found in template paths"); 170 } 171 172 RequestDispatcher dispatcher = data.getServletContext() 174 .getRequestDispatcher(relativeTemplateName); 175 176 try 177 { 178 if (isForward) 179 { 180 dispatcher.forward(data.getRequest(), data.getResponse()); 182 } 183 else 184 { 185 data.getOut().flush(); 186 dispatcher.include(data.getRequest(), data.getResponse()); 188 } 189 } 190 catch (Exception e) 191 { 192 try 195 { 196 data.getOut().print("Error encountered processing a template: " 197 + templateName); 198 e.printStackTrace(data.getOut()); 199 } 200 catch (IOException ignored) 201 { 202 } 203 204 throw new TurbineException( 207 "Error encountered processing a template: " + templateName, e); 208 } 209 } 210 211 214 private void initJsp() 215 throws Exception 216 { 217 Configuration config = getConfiguration(); 218 219 relativeTemplatePaths = config.getStringArray(TEMPLATE_PATH_KEY); 222 223 templatePaths = new String [relativeTemplatePaths.length]; 225 for (int i=0; i < relativeTemplatePaths.length; i++) 226 { 227 relativeTemplatePaths[i] = warnAbsolute(relativeTemplatePaths[i]); 228 229 templatePaths[i] = Turbine.getRealPath(relativeTemplatePaths[i]); 230 } 231 232 bufferSize = config.getInt(JspService.BUFFER_SIZE_KEY, 233 JspService.BUFFER_SIZE_DEFAULT); 234 } 235 236 243 public boolean templateExists(String template) 244 { 245 for (int i = 0; i < templatePaths.length; i++) 246 { 247 if (templateExists(templatePaths[i], template)) 248 { 249 return true; 250 } 251 } 252 return false; 253 } 254 255 264 private boolean templateExists(String path, String template) 265 { 266 return new File (path, template).exists(); 267 } 268 269 278 public String getRelativeTemplateName(String template) 279 { 280 template = warnAbsolute(template); 281 282 for (int i = 0; i < templatePaths.length; i++) 286 { 287 if (templateExists(templatePaths[i], template)) 288 { 289 return relativeTemplatePaths[i] + "/" + template; 290 } 291 } 292 return null; 293 } 294 295 301 private String warnAbsolute(String template) 302 { 303 if (template.startsWith("/")) 304 { 305 log.warn("Template " + template 306 + " has a leading /, which is wrong!"); 307 return template.substring(1); 308 } 309 return template; 310 } 311 } 312 | Popular Tags |