KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > kohsuke > stapler > jelly > JellyClassTearOff


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 JavaDoc;
14 import javax.servlet.ServletContext JavaDoc;
15 import javax.servlet.ServletException JavaDoc;
16 import java.io.FilterOutputStream JavaDoc;
17 import java.io.IOException JavaDoc;
18 import java.io.OutputStream JavaDoc;
19 import java.net.URL JavaDoc;
20 import java.util.Enumeration JavaDoc;
21
22 /**
23  * @author Kohsuke Kawaguchi
24  */

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 JavaDoc res) throws JellyException {
32         return classLoader.createContext().compileScript(res);
33     }
34
35     public static void invokeScript(StaplerRequest req, StaplerResponse rsp, Script script, Object JavaDoc it) throws IOException JavaDoc, JellyTagException {
36         // invoke Jelly script to render result
37
JellyContext context = new JellyContext();
38         Enumeration JavaDoc en = req.getAttributeNames();
39
40         // expose request attributes, just like JSP
41
while (en.hasMoreElements()) {
42             String JavaDoc name = (String JavaDoc) 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 JavaDoc servletContext = req.getServletContext();
49         context.setVariable("servletContext",servletContext);
50         context.setVariable("app",servletContext.getAttribute("app"));
51         // property bag to store request scope variables
52
context.setVariable("requestScope",context.getVariables());
53         // this variable is needed to make "jelly:fmt" taglib work correctly
54
context.setVariable("org.apache.commons.jelly.tags.fmt.locale",req.getLocale());
55
56         OutputStream JavaDoc output = rsp.getOutputStream();
57         output = new FilterOutputStream JavaDoc(output) {
58             public void flush() {
59                 // flushing ServletOutputStream causes Tomcat to
60
// send out headers, making it impossible to set contentType from the script.
61
// so don't let Jelly flush.
62
}
63         };
64         XMLOutput xmlOutput = XMLOutput.createXMLOutput(output);
65         script.run(context,xmlOutput);
66         xmlOutput.flush();
67         xmlOutput.close();
68         output.close();
69     }
70
71     /**
72      * Serves <tt>indx.jelly</tt> if it's available, and returns true.
73      */

74     public boolean serveIndexJelly(StaplerRequest req, StaplerResponse rsp, Object JavaDoc node) throws ServletException JavaDoc, IOException JavaDoc {
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 JavaDoc(e);
84         }
85     }
86
87     /**
88      * Creates a {@link RequestDispatcher} that forwards to the jelly view, if available.
89      */

90     public RequestDispatcher JavaDoc createDispatcher(Object JavaDoc it, String JavaDoc viewName) throws IOException JavaDoc {
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 JavaDoc io = new IOException JavaDoc(e.getMessage());
98             io.initCause(e);
99             throw io;
100         }
101     }
102 }
103
Popular Tags