001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.runtime.net.http;
020
021import java.io.IOException;
022
023import javax.servlet.RequestDispatcher;
024import javax.servlet.ServletException;
025import javax.servlet.ServletRequest;
026import javax.servlet.ServletResponse;
027
028import lucee.commons.net.HTTPUtil;
029import lucee.runtime.PageContext;
030import lucee.runtime.engine.ThreadLocalPageContext;
031
032public class RequestDispatcherWrap implements RequestDispatcher {
033
034        private String relPath;
035        private HTTPServletRequestWrap req;
036
037        public RequestDispatcherWrap(HTTPServletRequestWrap req, String relPath) {
038                this.relPath=relPath;
039                this.req=req;
040        }
041
042        public void forward(ServletRequest req, ServletResponse rsp)throws ServletException, IOException {
043                PageContext pc = ThreadLocalPageContext.get();
044                req=HTTPUtil.removeWrap(req);
045                if(pc==null){
046                        this.req.getOriginalRequestDispatcher(relPath).forward(req, rsp);
047                        return;
048                }
049                
050                
051                relPath=HTTPUtil.optimizeRelPath(pc,relPath);
052                
053                try{
054                        RequestDispatcher disp = this.req.getOriginalRequestDispatcher(relPath);
055                        disp.forward(req,rsp);
056                }
057                finally{
058                ThreadLocalPageContext.register(pc);
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(relPath).include(req, rsp);
066                        return;
067                }
068                try{
069                        relPath=HTTPUtil.optimizeRelPath(pc,relPath);
070                        RequestDispatcher disp = this.req.getOriginalRequestDispatcher(relPath);
071                disp.include(req,rsp);
072                }
073                finally{
074                ThreadLocalPageContext.register(pc);
075                }
076        }*/
077        
078        
079
080        public void include(ServletRequest req, ServletResponse rsp)throws ServletException, IOException {
081                PageContext pc = ThreadLocalPageContext.get();
082                if(pc==null){
083                        this.req.getOriginalRequestDispatcher(relPath).include(req, rsp);
084                        return;
085                }
086                //rsp.getWriter().flush();
087                //print.out("abc:"+rsp);
088                HTTPUtil.include(pc,req, rsp,relPath);
089                
090                /*
091                relPath=HTTPUtil.optimizeRelPath(pc,relPath);
092                ByteArrayOutputStream baos=new ByteArrayOutputStream();
093                        
094                try{
095                        HttpServletResponse drsp=new HttpServletResponseWrap(pc.getHttpServletResponse(),baos);
096                        RequestDispatcher disp = pc.getServletContext().getRequestDispatcher(relPath);
097                        if(disp==null)
098                        throw new PageServletException(new ApplicationException("Page "+relPath+" not found"));
099                disp.include(req,drsp);
100                if(!drsp.isCommitted())drsp.flushBuffer();
101                pc.write(IOUtil.toString(baos.toByteArray(), drsp.getCharacterEncoding()));
102                }
103                finally{
104                ThreadLocalPageContext.register(pc);
105                }*/
106        }
107}