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.concurrency;
020
021import java.io.ByteArrayInputStream;
022import java.io.ByteArrayOutputStream;
023import java.nio.charset.Charset;
024import java.util.concurrent.Callable;
025
026import lucee.commons.io.IOUtil;
027import lucee.commons.lang.ExceptionUtil;
028import lucee.runtime.PageContext;
029import lucee.runtime.PageContextImpl;
030import lucee.runtime.config.ConfigImpl;
031import lucee.runtime.engine.ThreadLocalPageContext;
032import lucee.runtime.exp.PageException;
033import lucee.runtime.net.http.HttpServletResponseDummy;
034import lucee.runtime.net.http.ReqRspUtil;
035import lucee.runtime.thread.ThreadUtil;
036
037public abstract class CallerResponseStreamResult implements Callable<String> { 
038
039        private PageContext parent;
040        private PageContextImpl pc;
041        private ByteArrayOutputStream baos;
042
043        public CallerResponseStreamResult(PageContext parent) {
044                this.parent = parent;
045                this.baos = new ByteArrayOutputStream();
046                this.pc=ThreadUtil.clonePageContext(parent, baos, false, false, true);
047        }
048        
049        public final String call() throws PageException {
050                ThreadLocalPageContext.register(pc);
051                pc.getRootOut().setAllowCompression(false); // make sure content is not compressed
052                String str=null;
053                try{
054                        _call(parent,pc);
055                } 
056                finally{
057                        try {
058                        HttpServletResponseDummy rsp=(HttpServletResponseDummy) pc.getHttpServletResponse();
059                        
060                        Charset cs = ReqRspUtil.getCharacterEncoding(pc,rsp);
061                        //if(enc==null) enc="ISO-8859-1";
062                        
063                        pc.getOut().flush(); //make sure content is flushed
064                        
065                        ((ConfigImpl)pc.getConfig()).getFactory().releasePageContext(pc);
066                                str=IOUtil.toString((new ByteArrayInputStream(baos.toByteArray())), cs); // TODO add support for none string content
067                        } 
068                        catch (Throwable e) {
069                ExceptionUtil.rethrowIfNecessary(e);
070                                // TODO Auto-generated catch block
071                                e.printStackTrace();
072                        }
073                }
074                return str;
075        }
076
077        public abstract void _call(PageContext parent, PageContext pc) throws PageException;
078        //public abstract void afterCleanup(PageContext parent, ByteArrayOutputStream baos);
079}