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