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.cfx; 020 021import lucee.runtime.PageContext; 022import lucee.runtime.exp.ApplicationException; 023import lucee.runtime.exp.PageException; 024import lucee.runtime.op.Caster; 025import lucee.runtime.op.Decision; 026import lucee.runtime.type.Collection; 027import lucee.runtime.type.Struct; 028import lucee.runtime.type.util.CollectionUtil; 029import lucee.runtime.type.util.KeyConstants; 030 031import com.allaire.cfx.Query; 032import com.allaire.cfx.Request; 033 034 035 036/** 037 * Implementation of the CFX Request Interface 038 */ 039public final class RequestImpl implements Request { 040 041 private static final Collection.Key QUERY = KeyConstants._query; 042 private static final Collection.Key DEBUG = KeyConstants._debug; 043 private Struct attributes; 044 private Struct settings; 045 private Query query; 046 047 /** 048 * constructor of the class 049 * @param pc 050 * @param attributes 051 * @throws PageException 052 */ 053 public RequestImpl(PageContext pc,Struct attributes) throws PageException { 054 this.attributes=attributes; 055 Object o=attributes.get(QUERY,null); 056 String varName=Caster.toString(o,null); 057 058 if(o!=null) { 059 if(varName!=null) { 060 this.query=new QueryWrap(Caster.toQuery(pc.getVariable(varName))); 061 attributes.removeEL(QUERY); 062 } 063 else if(Decision.isQuery(o)) { 064 this.query=new QueryWrap(Caster.toQuery(o)); 065 attributes.removeEL(QUERY); 066 } 067 else { 068 throw new ApplicationException("Attribute query doesn't contain a Query or a Name of a Query"); 069 } 070 } 071 } 072 073 /** 074 * constructor of the class 075 * @param attributes 076 * @param query 077 * @param settings 078 */ 079 public RequestImpl(Struct attributes,Query query, Struct settings) { 080 this.attributes=attributes; 081 this.query=query; 082 this.settings=settings; 083 } 084 085 @Override 086 public boolean attributeExists(String key) { 087 return attributes.get(key,null)!=null; 088 } 089 090 @Override 091 public boolean debug() { 092 Object o=attributes.get(DEBUG,Boolean.FALSE); 093 if(o==null) return false; 094 return Caster.toBooleanValue(o,false); 095 } 096 097 @Override 098 public String getAttribute(String key) { 099 return getAttribute(key, ""); 100 } 101 102 @Override 103 public String getAttribute(String key, String defaultValue) { 104 return Caster.toString(attributes.get(key,defaultValue),defaultValue); 105 } 106 107 @Override 108 public String[] getAttributeList() { 109 return CollectionUtil.keysAsString(attributes); 110 } 111 112 @Override 113 public int getIntAttribute(String key) throws NumberFormatException { 114 return getIntAttribute(key, -1); 115 } 116 117 @Override 118 public int getIntAttribute(String key, int defaultValue) { 119 Object o=attributes.get(key,null); 120 if(o==null) return defaultValue; 121 try { 122 return Caster.toIntValue(o); 123 } catch (PageException e) { 124 return defaultValue; 125 } 126 } 127 128 @Override 129 public Query getQuery() { 130 return query; 131 } 132 133 @Override 134 public String getSetting(String key) { 135 return settings==null?"":Caster.toString(settings.get(key,""),""); 136 } 137 138}