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.listener;
020
021import java.io.IOException;
022
023import lucee.runtime.CFMLFactory;
024import lucee.runtime.PageContext;
025import lucee.runtime.PageContextImpl;
026import lucee.runtime.PageSource;
027import lucee.runtime.exp.MissingIncludeException;
028import lucee.runtime.exp.PageException;
029import lucee.runtime.op.Caster;
030import lucee.runtime.type.UDF;
031
032public final class ClassicAppListener extends AppListenerSupport {
033
034        private int mode=MODE_CURRENT2ROOT;
035
036        @Override
037        public void onRequest(PageContext pc,PageSource requestedPage, RequestListener rl) throws PageException {
038                
039                PageSource application=//pc.isCFCRequest()?null:
040                        AppListenerUtil.getApplicationPageSource(pc,requestedPage,mode,AppListenerUtil.TYPE_CLASSIC,null);
041                
042                _onRequest(pc, requestedPage, application,rl);
043        }
044        
045        static void _onRequest(PageContext pc,PageSource requestedPage,PageSource application, RequestListener rl) throws PageException {
046                ((PageContextImpl)pc).setAppListenerType(AppListenerUtil.TYPE_CLASSIC);
047                
048                // on requestStart
049                if(application!=null)pc.doInclude(new PageSource[]{application},false);
050                
051                if(rl!=null) {
052                        requestedPage=rl.execute(pc, requestedPage);
053                        if(requestedPage==null) return;
054                }
055                
056                // request
057                try{
058                        pc.doInclude(new PageSource[]{requestedPage},false);
059                }
060                catch(MissingIncludeException mie){
061                        ApplicationContext ac = pc.getApplicationContext();
062                        boolean rethrow=true;
063                        if(ac instanceof ClassicApplicationContext) {
064                                ClassicApplicationContext cfc=(ClassicApplicationContext) ac;
065                                UDF udf = cfc.getOnMissingTemplate();
066                                if(udf!=null) {
067                                        String targetPage=requestedPage.getFullRealpath();
068                                        rethrow=(!Caster.toBooleanValue(udf.call(pc, new Object[]{targetPage}, true),true));
069                                }
070                        }
071                        if(rethrow)throw mie;
072                }
073                
074                // on Request End
075                if(application!=null){
076                        PageSource onReqEnd = application.getRealPage("OnRequestEnd.cfm");
077                if(onReqEnd.exists())pc.doInclude(new PageSource[]{onReqEnd},false);
078                }
079        }
080
081        @Override
082        public boolean onApplicationStart(PageContext pc) throws PageException {
083                // do nothing
084                return true;
085        }
086
087        @Override
088        public void onSessionStart(PageContext pc) throws PageException {
089                // do nothing
090        }
091
092        @Override
093        public void onApplicationEnd(CFMLFactory factory, String applicationName) throws PageException {
094                // do nothing   
095        }
096
097        @Override
098        public void onSessionEnd(CFMLFactory cfmlFactory, String applicationName, String cfid) throws PageException {
099                // do nothing
100        }
101
102        @Override
103        public void onDebug(PageContext pc) throws PageException {
104                _onDebug(pc);
105        }
106        
107        public static void _onDebug(PageContext pc) throws PageException {
108                try {
109                        if(pc.getConfig().debug())pc.getDebugger().writeOut(pc);
110                } 
111                catch (IOException e) {
112                        throw Caster.toPageException(e);
113                }
114        }
115
116        @Override
117        public void onError(PageContext pc,PageException pe) {
118                _onError(pc, pe);
119        }
120
121        public static void _onError(PageContext pc,PageException pe) {
122                pc.handlePageException(pe);
123        }
124
125        @Override
126        public void onTimeout(PageContext pc) {
127                _onTimeout(pc);
128        }
129        public static void _onTimeout(PageContext pc) {
130        }
131
132        @Override
133        public void setMode(int mode) {
134                this.mode=mode;
135        }
136
137        @Override
138        public int getMode() {
139                return mode;
140        }
141
142        @Override
143        public String getType() {
144                return "classic";
145        }
146        
147        @Override
148        public boolean hasOnApplicationStart(){
149                return false;
150        }
151        
152        public static boolean _hasOnApplicationStart(){
153                return false;
154        }
155        
156        @Override
157        public boolean hasOnSessionStart(PageContext pc){
158                return false;
159        }
160        
161        public static boolean _hasOnSessionStart(PageContext pc){
162                return false;
163        }
164}