001    package railo.runtime;
002    
003    import java.util.HashMap;
004    import java.util.Map;
005    
006    import railo.runtime.dump.DumpUtil;
007    import railo.runtime.dump.DumpWriter;
008    import railo.runtime.exp.ApplicationException;
009    import railo.runtime.exp.PageException;
010    import railo.runtime.net.http.ReqRspUtil;
011    import railo.runtime.op.Caster;
012    import railo.runtime.type.KeyImpl;
013    
014    /**
015     * A Page that can produce Components
016     */
017    public abstract class InterfacePage extends PagePlus  {
018            
019            private static final railo.runtime.type.Collection.Key METHOD = KeyImpl.intern("method");
020            private static final railo.runtime.type.Collection.Key COMPONENT = KeyImpl.intern("component");
021            
022            
023            @Override
024            public void call(PageContext pc) throws PageException {
025            try {
026                pc.setSilent();
027                InterfaceImpl interf = null;
028                try {
029                    interf = newInstance(getPageSource().getComponentName(),false,new HashMap());
030                }
031                finally {
032                    pc.unsetSilent();
033                }
034                
035                            String qs=ReqRspUtil.getQueryString(pc.getHttpServletRequest());
036                if(pc.getBasePageSource()==this.getPageSource())
037                    pc.getDebugger().setOutput(false);
038                boolean isPost=pc. getHttpServletRequest().getMethod().equalsIgnoreCase("POST");
039                
040                // POST
041                if(isPost) {
042                    // Soap
043                    if(ComponentPage.isSoap(pc)) 
044                            throw new ApplicationException("can not instantiate interface ["+this.getPageSource().getComponentName()+"] as a component");
045                }
046                // GET
047                else if(qs!=null && qs.trim().equalsIgnoreCase("wsdl")) 
048                            throw new ApplicationException("can not instantiate interface ["+this.getPageSource().getComponentName()+"] as a component");   
049                
050                // WDDX
051                if(pc.urlFormScope().containsKey(METHOD)) 
052                    throw new ApplicationException("can not instantiate interface ["+this.getPageSource().getComponentName()+"] as a component");
053                
054                // invoking via include
055                if(pc.getTemplatePath().size()>1) {
056                    throw new ApplicationException("can not invoke interface ["+this.getPageSource().getComponentName()+"] as a page");
057                }
058                
059                            // DUMP
060                            //TODO component.setAccess(pc,Component.ACCESS_PUBLIC);
061                            String cdf = pc.getConfig().getComponentDumpTemplate();
062                            if(cdf!=null && cdf.trim().length()>0) {
063                                pc.variablesScope().set(COMPONENT,interf);
064                                pc.doInclude(cdf);
065                            }
066                            else pc.write(pc.getConfig().getDefaultDumpWriter(DumpWriter.DEFAULT_RICH).toString(pc,interf.toDumpData(pc, 9999,DumpUtil.toDumpProperties()),true));
067                            
068                    }
069                    catch(Throwable t) {
070                            throw Caster.toPageException(t);//Exception Handler.castAnd Stack(t, this, pc);
071                    }
072            }
073            
074        public abstract void initInterface(InterfaceImpl i) 
075            throws PageException;
076    
077            public abstract InterfaceImpl newInstance(String callPath,boolean isRealPath,Map interfaceUDFs)
078                    throws railo.runtime.exp.PageException;
079    
080    }