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