001    package com.allaire.cfx;
002    
003    import java.util.Enumeration;
004    import java.util.Hashtable;
005    
006    import railo.loader.engine.CFMLEngineFactory;
007    import railo.runtime.type.Struct;
008    
009    /**
010     * Implementation of the Debug Request
011     */
012    public class DebugRequest implements Request {
013    
014        private Struct attributes;
015        private Query query;
016        private Struct settings;
017    
018        /**
019         * constructor of the class
020         * @param attributes
021         */
022        public DebugRequest(Hashtable attributes) {
023            this(attributes,null,null);
024        }
025    
026    
027        /**
028         * constructor of the class
029         * @param attributes
030         * @param query
031         */
032        public DebugRequest(Hashtable attributes, Query query) {
033            this(attributes,query,null);
034        }
035        
036        /**
037         * constructor of the class
038         * @param attributes
039         * @param query
040         * @param settings
041         */
042        public DebugRequest(Hashtable attributes, Query query, Hashtable settings) {
043            this.attributes=toStruct(attributes);
044            this.query=query;
045            this.settings=toStruct(settings);
046            
047        }
048        
049        /**
050         * @see com.allaire.cfx.Request#attributeExists(java.lang.String)
051         */
052        public boolean attributeExists(String key) {
053            return attributes.containsKey(key);
054        }
055    
056        /**
057         * @see com.allaire.cfx.Request#debug()
058         */
059        public boolean debug() {
060            Object o=attributes.get("debug",Boolean.FALSE);
061            return CFMLEngineFactory.getInstance().getCastUtil().toBooleanValue(o,false);
062        }
063    
064        /**
065         * @see com.allaire.cfx.Request#getAttribute(java.lang.String, java.lang.String)
066         */
067        public String getAttribute(String key, String defaultValue) {
068            return CFMLEngineFactory.getInstance().getCastUtil().toString(attributes.get(key,defaultValue),defaultValue);
069        }
070    
071        /**
072         * @see com.allaire.cfx.Request#getAttribute(java.lang.String)
073         */
074        public String getAttribute(String key) {
075            return getAttribute(key, "");
076        }
077    
078        /**
079         * @see com.allaire.cfx.Request#getAttributeList()
080         */
081        public String[] getAttributeList() {
082            return attributes.keysAsString();
083        }
084    
085        /**
086         * @see com.allaire.cfx.Request#getIntAttribute(java.lang.String, int)
087         */
088        public int getIntAttribute(String key, int defaultValue) {
089            Object o=attributes.get(key,null);
090            if(o==null) return defaultValue;
091            return (int)CFMLEngineFactory.getInstance().getCastUtil().toDoubleValue(o,defaultValue);
092        }
093    
094        /**
095         * @see com.allaire.cfx.Request#getIntAttribute(java.lang.String)
096         */
097        public int getIntAttribute(String key) throws NumberFormatException {
098            return getIntAttribute(key, -1);
099        }
100    
101        /**
102         * @see com.allaire.cfx.Request#getQuery()
103         */
104        public Query getQuery() {
105            return query;
106        }
107    
108        /**
109         * @see com.allaire.cfx.Request#getSetting(java.lang.String)
110         */
111        public String getSetting(String key) {
112            return settings==null?"":CFMLEngineFactory.getInstance().getCastUtil().toString(settings.get(key,""),"");
113        }
114        
115        /**
116         * @param hashTable a Hashtable to a Struct
117         * @return casted struct
118         */
119        private static Struct toStruct(Hashtable hashTable) {
120            if(hashTable==null) return null;
121            
122            Enumeration e = hashTable.keys();
123            Struct sct=CFMLEngineFactory.getInstance().getCreationUtil().createStruct();
124            while(e.hasMoreElements()) {
125                Object key=e.nextElement();
126                sct.setEL(key.toString(),hashTable.get(key));
127            }
128            return sct;
129        }
130    }