1 package org.kohsuke.stapler.tags; 2 3 import javax.servlet.RequestDispatcher ; 4 import javax.servlet.ServletConfig ; 5 import javax.servlet.ServletContext ; 6 import javax.servlet.ServletException ; 7 import javax.servlet.http.HttpServletRequest ; 8 import javax.servlet.http.HttpServletResponse ; 9 import javax.servlet.http.HttpServletResponseWrapper ; 10 import javax.servlet.jsp.JspException ; 11 import javax.servlet.jsp.PageContext ; 12 import javax.servlet.jsp.tagext.SimpleTagSupport ; 13 import java.io.IOException ; 14 import java.io.PrintWriter ; 15 16 48 public class Include extends SimpleTagSupport { 49 50 private Object it; 51 52 private String page; 53 54 57 public void setPage(String page) { 58 this.page = page; 59 } 60 61 64 public void setIt(Object it) { 65 this.it = it; 66 } 67 68 private Object getPageObject( String name ) { 69 return getJspContext().getAttribute(name,PageContext.PAGE_SCOPE); 70 } 71 72 public void doTag() throws JspException , IOException { 73 Object it = getJspContext().getAttribute("it",PageContext.REQUEST_SCOPE); 74 final Object oldIt = it; 75 if(this.it!=null) 76 it = this.it; 77 78 ServletConfig cfg = (ServletConfig ) getPageObject(PageContext.CONFIG); 79 ServletContext sc = cfg.getServletContext(); 80 81 for( Class c = it.getClass(); c!=Object .class; c=c.getSuperclass() ) { 82 String name = "/WEB-INF/side-files/"+c.getName().replace('.','/')+'/'+page; 83 if(sc.getResource(name)!=null) { 84 RequestDispatcher disp = sc.getRequestDispatcher(name); 87 if(disp!=null) { 88 getJspContext().setAttribute("it",it,PageContext.REQUEST_SCOPE); 89 try { 90 HttpServletRequest request = (HttpServletRequest ) getPageObject(PageContext.REQUEST); 91 disp.include( 92 request, 93 new Wrapper ( 94 (HttpServletResponse )getPageObject(PageContext.RESPONSE), 95 new PrintWriter (getJspContext().getOut()) ) 96 ); 97 } catch (ServletException e) { 98 throw new JspException (e); 99 } finally { 100 getJspContext().setAttribute("it",oldIt,PageContext.REQUEST_SCOPE); 101 } 102 return; 103 } 104 } 105 } 106 107 throw new JspException ("Unable to find '"+page+"' for "+it.getClass()); 108 } 109 } 110 111 class Wrapper extends HttpServletResponseWrapper { 112 private final PrintWriter pw; 113 114 public Wrapper(HttpServletResponse httpServletResponse, PrintWriter w) { 115 super(httpServletResponse); 116 this.pw = w; 117 } 118 119 public PrintWriter getWriter() throws IOException { 120 return pw; 121 } 122 } | Popular Tags |