Source code: com/sitemesh/taglib/page/PanelTag.java
1 package com.sitemesh.taglib.page;
2
3 import com.sitemesh.*;
4 import com.sitemesh.filter.PageResponse;
5 import com.sitemesh.taglib.AbstractTag;
6 import java.io.IOException;
7 import java.net.URL;
8 import javax.servlet.Config;
9 import javax.servlet.RequestDispatcher;
10 import javax.servlet.ServletException;
11 import javax.servlet.http.HttpServletRequest;
12 import javax.servlet.http.HttpServletResponse;
13 import javax.servlet.jsp.*;
14 import javax.servlet.jsp.tagext.*;
15
16 /**
17 * This tag inserts an external resource as a panel into the current Page.
18 *
19 * <p>The page attribute is required and points to the panel resource
20 * which should expose an entire page (e.g. another JSP file producing
21 * HTML). This attribute can be relative to the page it is being called
22 * from or an absolute path from the context-root.</p>
23 *
24 * <p>The (optional) decorator attribute is the name of the Decorator
25 * to apply to the included page. Note that the implementation of
26 * DecoratorMapper can overide this.</p>
27 *
28 * @author <a href="mailto:joe@truemesh.com">Joe Walnes</a>
29 * @version $Revision: 1.5 $
30 */
31 public class PanelTag extends AbstractTag {
32
33 private String page;
34 private String decorator;
35
36 /**
37 * Tag attribute: URI of page to include.
38 * Can be relative to page being called from, or absolute
39 * path from context-root of web-app.
40 */
41 public void setPage( String page ) {
42 this.page = page;
43 }
44
45 /**
46 * Tag attribute: Name of Decorator to apply to Page.
47 * This is passed to DecoratorMapper to retrieve appropriate
48 * Decorator. DecoratorMapper may override if needed.
49 *
50 * @see com.sitemesh.DecoratorMapper
51 */
52 public void setDecorator( String decorator ) {
53 this.decorator = decorator;
54 }
55
56 /**
57 * Standard taglib method: resets attributes.
58 */
59 public void release() {
60 setPage( null );
61 setDecorator( null );
62 super.release();
63 }
64
65 /**
66 * Standard taglib method: apply decorator to page.
67 */
68 public int doEndTag() throws JspException {
69 try {
70 Page oldPage = (Page)pageContext.getRequest().getAttribute(PAGE);
71 Page pageObj = getPage(page);
72 pageContext.getRequest().setAttribute(DECORATOR, decorator);
73 pageContext.getRequest().setAttribute(PAGE, pageObj);
74 Decorator d = getPanelDecorator(pageObj);
75 if ( d != null ) {
76 pageContext.include( d.getPage() );
77 }
78 else {
79 // decorator not found
80 // @todo: error handling
81 }
82 pageContext.getRequest().setAttribute(PAGE, oldPage);
83 return EVAL_PAGE;
84 }
85 catch (IOException e) {
86 throw new JspException(e.toString());
87 }
88 catch (ServletException e) {
89 throw new JspException(e.toString());
90 }
91 }
92
93 /**
94 * Obtain Page object for page specified in {@link #setPage(java.lang.String)} .
95 */
96 private Page getPage( String page ) throws IOException, ServletException {
97 // @todo: remove "text/html" hard-code.
98 String contentType = "text/html";
99 Config config = pageContext.getServletConfig();
100 PageParser parser = Factory.getInstance(config).getPageParser( contentType );
101
102 PageResponse pageResponse = new PageResponse( ( HttpServletResponse )pageContext.getResponse(), config );
103 RequestDispatcher rd = pageContext.getRequest().getRequestDispatcher( page );
104 rd.forward( pageContext.getRequest(), pageResponse );
105
106 Page result = pageResponse.getPage();
107 result.setRequest( (HttpServletRequest) pageContext.getRequest() );
108 return result;
109 }
110
111 /**
112 * Obtain Decorator object for page specified in {@link #setDecorator(java.lang.String)} .
113 */
114 private Decorator getPanelDecorator( Page page ) throws ServletException {
115 DecoratorMapper decoratorMapper = Factory.getInstance(pageContext.getServletConfig()).getDecoratorMapper();
116 return decoratorMapper.getDecorator( ((HttpServletRequest)pageContext.getRequest()), page );
117 }
118
119 }