KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > kohsuke > stapler > tags > Include


1 package org.kohsuke.stapler.tags;
2
3 import javax.servlet.RequestDispatcher JavaDoc;
4 import javax.servlet.ServletConfig JavaDoc;
5 import javax.servlet.ServletContext JavaDoc;
6 import javax.servlet.ServletException JavaDoc;
7 import javax.servlet.http.HttpServletRequest JavaDoc;
8 import javax.servlet.http.HttpServletResponse JavaDoc;
9 import javax.servlet.http.HttpServletResponseWrapper JavaDoc;
10 import javax.servlet.jsp.JspException JavaDoc;
11 import javax.servlet.jsp.PageContext JavaDoc;
12 import javax.servlet.jsp.tagext.SimpleTagSupport JavaDoc;
13 import java.io.IOException JavaDoc;
14 import java.io.PrintWriter JavaDoc;
15
16 /**
17  * Includes a side JSP file for the "it" object.
18  *
19  * <p>
20  * This tag looks for a side JSP file of the given name
21  * from the inheritance hierarchy of the it object,
22  * and includes the contents of it, just like &lt;jsp:include>.
23  *
24  * <p>
25  * For example, if the "it" object is an instance of the <tt>Foo</tt> class,
26  * which looks like the following:
27  *
28  * <pre>
29  * class Foo extends Bar { ... }
30  * class Bar extends Zot { ... }
31  * </pre>
32  *
33  * <p>
34  * And if you write:
35  * <pre><xmp>
36  * <st:include page="abc.jsp"/>
37  * </xmp></pre>
38  * then, it looks for the following files in this order,
39  * and includes the first one found.
40  * <ol>
41  * <li>a side-file of the Foo class named abc.jsp (/WEB-INF/side-files/Foo/abc.jsp)
42  * <li>a side-file of the Bar class named abc.jsp (/WEB-INF/side-files/Bar/abc.jsp)
43  * <li>a side-file of the Zot class named abc.jsp (/WEB-INF/side-files/Zot/abc.jsp)
44  * </ol>
45  *
46  * @author Kohsuke Kawaguchi
47  */

48 public class Include extends SimpleTagSupport JavaDoc {
49
50     private Object JavaDoc it;
51
52     private String JavaDoc page;
53
54     /**
55      * Specifies the name of the JSP to be included.
56      */

57     public void setPage(String JavaDoc page) {
58         this.page = page;
59     }
60
61     /**
62      * Specifies the object for which JSP will be included.
63      */

64     public void setIt(Object JavaDoc it) {
65         this.it = it;
66     }
67
68     private Object JavaDoc getPageObject( String JavaDoc name ) {
69         return getJspContext().getAttribute(name,PageContext.PAGE_SCOPE);
70     }
71
72     public void doTag() throws JspException JavaDoc, IOException JavaDoc {
73         Object JavaDoc it = getJspContext().getAttribute("it",PageContext.REQUEST_SCOPE);
74         final Object JavaDoc oldIt = it;
75         if(this.it!=null)
76             it = this.it;
77
78         ServletConfig JavaDoc cfg = (ServletConfig JavaDoc) getPageObject(PageContext.CONFIG);
79         ServletContext JavaDoc sc = cfg.getServletContext();
80
81         for( Class JavaDoc c = it.getClass(); c!=Object JavaDoc.class; c=c.getSuperclass() ) {
82             String JavaDoc name = "/WEB-INF/side-files/"+c.getName().replace('.','/')+'/'+page;
83             if(sc.getResource(name)!=null) {
84                 // Tomcat returns a RequestDispatcher even if the JSP file doesn't exist.
85
// so check if the resource exists first.
86
RequestDispatcher JavaDoc disp = sc.getRequestDispatcher(name);
87                 if(disp!=null) {
88                     getJspContext().setAttribute("it",it,PageContext.REQUEST_SCOPE);
89                     try {
90                         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) getPageObject(PageContext.REQUEST);
91                         disp.include(
92                             request,
93                             new Wrapper JavaDoc(
94                                 (HttpServletResponse JavaDoc)getPageObject(PageContext.RESPONSE),
95                                 new PrintWriter JavaDoc(getJspContext().getOut()) )
96                         );
97                     } catch (ServletException JavaDoc e) {
98                         throw new JspException JavaDoc(e);
99                     } finally {
100                         getJspContext().setAttribute("it",oldIt,PageContext.REQUEST_SCOPE);
101                     }
102                     return;
103                 }
104             }
105         }
106
107         throw new JspException JavaDoc("Unable to find '"+page+"' for "+it.getClass());
108     }
109 }
110
111 class Wrapper extends HttpServletResponseWrapper JavaDoc {
112     private final PrintWriter JavaDoc pw;
113
114     public Wrapper(HttpServletResponse JavaDoc httpServletResponse, PrintWriter JavaDoc w) {
115         super(httpServletResponse);
116         this.pw = w;
117     }
118
119     public PrintWriter JavaDoc getWriter() throws IOException JavaDoc {
120         return pw;
121     }
122 }
Popular Tags