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