1 package org.kohsuke.stapler.jelly; 2 3 import org.apache.commons.jelly.JellyContext; 4 import org.apache.commons.jelly.JellyException; 5 import org.apache.commons.jelly.JellyTagException; 6 import org.apache.commons.jelly.Script; 7 import org.apache.commons.jelly.XMLOutput; 8 import org.kohsuke.stapler.AbstractTearOff; 9 import org.kohsuke.stapler.MetaClass; 10 import org.kohsuke.stapler.StaplerRequest; 11 import org.kohsuke.stapler.StaplerResponse; 12 13 import javax.servlet.RequestDispatcher ; 14 import javax.servlet.ServletContext ; 15 import javax.servlet.ServletException ; 16 import java.io.FilterOutputStream ; 17 import java.io.IOException ; 18 import java.io.OutputStream ; 19 import java.net.URL ; 20 import java.util.Enumeration ; 21 22 25 public class JellyClassTearOff extends AbstractTearOff<JellyClassLoaderTearOff,Script,JellyException> { 26 27 public JellyClassTearOff(MetaClass owner) { 28 super(owner,JellyClassLoaderTearOff.class); 29 } 30 31 protected Script parseScript(URL res) throws JellyException { 32 return classLoader.createContext().compileScript(res); 33 } 34 35 public static void invokeScript(StaplerRequest req, StaplerResponse rsp, Script script, Object it) throws IOException , JellyTagException { 36 JellyContext context = new JellyContext(); 38 Enumeration en = req.getAttributeNames(); 39 40 while (en.hasMoreElements()) { 42 String name = (String ) en.nextElement(); 43 context.setVariable(name,req.getAttribute(name)); 44 } 45 context.setVariable("request",req); 46 context.setVariable("response",rsp); 47 context.setVariable("it",it); 48 ServletContext servletContext = req.getServletContext(); 49 context.setVariable("servletContext",servletContext); 50 context.setVariable("app",servletContext.getAttribute("app")); 51 context.setVariable("requestScope",context.getVariables()); 53 context.setVariable("org.apache.commons.jelly.tags.fmt.locale",req.getLocale()); 55 56 OutputStream output = rsp.getOutputStream(); 57 output = new FilterOutputStream (output) { 58 public void flush() { 59 } 63 }; 64 XMLOutput xmlOutput = XMLOutput.createXMLOutput(output); 65 script.run(context,xmlOutput); 66 xmlOutput.flush(); 67 xmlOutput.close(); 68 output.close(); 69 } 70 71 74 public boolean serveIndexJelly(StaplerRequest req, StaplerResponse rsp, Object node) throws ServletException , IOException { 75 try { 76 Script script = findScript("index.jelly"); 77 if(script!=null) { 78 invokeScript(req,rsp,script,node); 79 return true; 80 } 81 return false; 82 } catch (JellyException e) { 83 throw new ServletException (e); 84 } 85 } 86 87 90 public RequestDispatcher createDispatcher(Object it, String viewName) throws IOException { 91 try { 92 Script script = findScript(viewName); 93 if(script!=null) 94 return new JellyRequestDispatcher(it,script); 95 return null; 96 } catch (JellyException e) { 97 IOException io = new IOException (e.getMessage()); 98 io.initCause(e); 99 throw io; 100 } 101 } 102 } 103 | Popular Tags |