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