001    package railo.runtime.net.http;
002    
003    import java.io.IOException;
004    
005    import javax.servlet.RequestDispatcher;
006    import javax.servlet.ServletException;
007    import javax.servlet.ServletRequest;
008    import javax.servlet.ServletResponse;
009    
010    import railo.commons.net.HTTPUtil;
011    import railo.runtime.PageContext;
012    import railo.runtime.engine.ThreadLocalPageContext;
013    
014    public class RequestDispatcherWrap implements RequestDispatcher {
015    
016            private String realPath;
017            private HTTPServletRequestWrap req;
018    
019            public RequestDispatcherWrap(HTTPServletRequestWrap req, String realPath) {
020                    this.realPath=realPath;
021                    this.req=req;
022            }
023    
024            public void forward(ServletRequest req, ServletResponse rsp)throws ServletException, IOException {
025                    PageContext pc = ThreadLocalPageContext.get();
026                    req=HTTPUtil.removeWrap(req);
027                    if(pc==null){
028                            this.req.getOriginalRequestDispatcher(realPath).forward(req, rsp);
029                            return;
030                    }
031                    
032                    
033                    realPath=HTTPUtil.optimizeRealPath(pc,realPath);
034                    
035                    try{
036                            RequestDispatcher disp = this.req.getOriginalRequestDispatcher(realPath);
037                            disp.forward(req,rsp);
038                    }
039                    finally{
040                    ThreadLocalPageContext.register(pc);
041                    }
042            }
043    
044            /*public void include(ServletRequest req, ServletResponse rsp)throws ServletException, IOException {
045                    PageContext pc = ThreadLocalPageContext.get();
046                    if(pc==null){
047                            this.req.getOriginalRequestDispatcher(realPath).include(req, rsp);
048                            return;
049                    }
050                    try{
051                            realPath=HTTPUtil.optimizeRealPath(pc,realPath);
052                            RequestDispatcher disp = this.req.getOriginalRequestDispatcher(realPath);
053                    disp.include(req,rsp);
054                    }
055                    finally{
056                    ThreadLocalPageContext.register(pc);
057                    }
058            }*/
059            
060            
061    
062            public void include(ServletRequest req, ServletResponse rsp)throws ServletException, IOException {
063                    PageContext pc = ThreadLocalPageContext.get();
064                    if(pc==null){
065                            this.req.getOriginalRequestDispatcher(realPath).include(req, rsp);
066                            return;
067                    }
068                    //rsp.getWriter().flush();
069                    //print.out("abc:"+rsp);
070                    HTTPUtil.include(pc,req, rsp,realPath);
071                    
072                    /*
073                    realPath=HTTPUtil.optimizeRealPath(pc,realPath);
074                    ByteArrayOutputStream baos=new ByteArrayOutputStream();
075                            
076                    try{
077                            HttpServletResponse drsp=new HttpServletResponseWrap(pc.getHttpServletResponse(),baos);
078                            RequestDispatcher disp = pc.getServletContext().getRequestDispatcher(realPath);
079                            if(disp==null)
080                            throw new PageServletException(new ApplicationException("Page "+realPath+" not found"));
081                    disp.include(req,drsp);
082                    if(!drsp.isCommitted())drsp.flushBuffer();
083                    pc.write(IOUtil.toString(baos.toByteArray(), drsp.getCharacterEncoding()));
084                    }
085                    finally{
086                    ThreadLocalPageContext.register(pc);
087                    }*/
088            }
089    }