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;
020
021import java.util.HashMap;
022import java.util.Map;
023
024import lucee.runtime.dump.DumpUtil;
025import lucee.runtime.dump.DumpWriter;
026import lucee.runtime.exp.ApplicationException;
027import lucee.runtime.exp.PageException;
028import lucee.runtime.net.http.ReqRspUtil;
029import lucee.runtime.op.Caster;
030import lucee.runtime.type.util.KeyConstants;
031
032/**
033 * A Page that can produce Components
034 */
035public abstract class InterfacePage extends PagePlus  {
036        
037        @Override
038        public void call(PageContext pc) throws PageException {
039        try {
040            pc.setSilent();
041            InterfaceImpl interf = null;
042            try {
043                interf = newInstance(getPageSource().getComponentName(),false,new HashMap());
044            }
045            finally {
046                pc.unsetSilent();
047            }
048            
049                        String qs=ReqRspUtil.getQueryString(pc.getHttpServletRequest());
050            if(pc.getBasePageSource()==this.getPageSource() && pc.getConfig().debug())
051                pc.getDebugger().setOutput(false);
052            boolean isPost=pc. getHttpServletRequest().getMethod().equalsIgnoreCase("POST");
053            
054            // POST
055            if(isPost) {
056                // Soap
057                if(ComponentPage.isSoap(pc)) 
058                        throw new ApplicationException("can not instantiate interface ["+this.getPageSource().getComponentName()+"] as a component");
059            }
060            // GET
061            else if(qs!=null && qs.trim().equalsIgnoreCase("wsdl")) 
062                        throw new ApplicationException("can not instantiate interface ["+this.getPageSource().getComponentName()+"] as a component");   
063            
064            // WDDX
065            if(pc.urlFormScope().containsKey(KeyConstants._method)) 
066                throw new ApplicationException("can not instantiate interface ["+this.getPageSource().getComponentName()+"] as a component");
067            
068            // invoking via include
069            if(pc.getTemplatePath().size()>1) {
070                throw new ApplicationException("can not invoke interface ["+this.getPageSource().getComponentName()+"] as a page");
071            }
072            
073                        // DUMP
074                        //TODO component.setAccess(pc,Component.ACCESS_PUBLIC);
075                        String cdf = pc.getConfig().getComponentDumpTemplate();
076                        if(cdf!=null && cdf.trim().length()>0) {
077                            pc.variablesScope().set(KeyConstants._component,interf);
078                            pc.doInclude(cdf);
079                        }
080                        else pc.write(pc.getConfig().getDefaultDumpWriter(DumpWriter.DEFAULT_RICH).toString(pc,interf.toDumpData(pc, 9999,DumpUtil.toDumpProperties()),true));
081                        
082                }
083                catch(Throwable t) {
084                        throw Caster.toPageException(t);//Exception Handler.castAnd Stack(t, this, pc);
085                }
086        }
087        
088    public abstract void initInterface(InterfaceImpl i) 
089        throws PageException;
090
091        public abstract InterfaceImpl newInstance(String callPath,boolean isRelPath,Map interfaceUDFs)
092                throws lucee.runtime.exp.PageException;
093
094}