001    package railo.runtime.type.scope;
002    
003    
004    import java.util.ArrayList;
005    import java.util.Enumeration;
006    import java.util.Iterator;
007    import java.util.List;
008    
009    import javax.servlet.http.HttpServletRequest;
010    
011    import railo.runtime.PageContext;
012    import railo.runtime.dump.DumpData;
013    import railo.runtime.dump.DumpProperties;
014    import railo.runtime.exp.ExpressionException;
015    import railo.runtime.exp.PageException;
016    import railo.runtime.type.Collection;
017    import railo.runtime.type.KeyImpl;
018    import railo.runtime.type.Struct;
019    import railo.runtime.type.StructImpl;
020    import railo.runtime.type.it.KeyIterator;
021    import railo.runtime.type.util.StructSupport;
022    
023    public final class RequestImpl extends StructSupport implements Request {
024    
025            
026            private HttpServletRequest _req;
027            private boolean init;
028            private static int _id=0;
029            private int id=0;
030    
031            public RequestImpl() {
032            id=++_id;
033                    //super("request",SCOPE_REQUEST,Struct.TYPE_REGULAR);
034                    
035            }
036            
037        /**
038         * @return Returns the id.
039         */
040        public int _getId() {
041            return id;
042        }
043    
044            public void initialize(PageContext pc) {
045                    _req = pc.getHttpServletRequest();//HTTPServletRequestWrap.pure(pc.getHttpServletRequest());
046                    init=true;
047                    
048            }
049    
050            public boolean isInitalized() {
051                    return init;
052            }
053    
054            public void release() {
055                    init = false;
056            }
057    
058            /**
059             * @see railo.runtime.type.Scope#getType()
060             */
061            public int getType() {
062                    return SCOPE_REQUEST;
063            }
064    
065            /**
066             * @see railo.runtime.type.Scope#getTypeAsString()
067             */
068            public String getTypeAsString() {
069                    return "request";
070            }
071    
072            /**
073             * @see java.util.Map#size()
074             */
075            public int size() {
076                    int size=0;
077                    synchronized (_req) {
078                            Enumeration<String> names = _req.getAttributeNames();
079                            while(names.hasMoreElements()){
080                                    names.nextElement();
081                                    size++;
082                            }
083                    }
084                    return size;
085            }
086    
087    
088    
089            /* (non-Javadoc)
090             * @see railo.runtime.type.Iteratorable#keyIterator()
091             */
092            public Iterator keyIterator() {
093                    return new KeyIterator(keys());
094            }
095            
096            /**
097             * @see railo.runtime.type.Collection#keys()
098             */
099            public Key[] keys() {
100                    synchronized (_req) {
101                            Enumeration<String> names = _req.getAttributeNames();
102                            List<Key> list=new ArrayList<Key>();
103                            while(names.hasMoreElements()){
104                                    list.add(KeyImpl.getInstance(names.nextElement()));
105                            }
106                            return list.toArray(new Key[list.size()]);
107                    }
108            }
109    
110            /**
111             * @see railo.runtime.type.Collection#keysAsString()
112             */
113            public String[] keysAsString() {
114                    synchronized (_req) {
115                            Enumeration<String> names = _req.getAttributeNames();
116                            List<String> list=new ArrayList<String>();
117                            while(names.hasMoreElements()){
118                                    list.add(names.nextElement());
119                            }
120                            return list.toArray(new String[list.size()]);
121                    }
122            }
123    
124            /**
125             * @see railo.runtime.type.Collection#remove(railo.runtime.type.Collection.Key)
126             */
127            public Object remove(Key key) throws PageException {
128                    Object value = removeEL(key);
129                    if(value!=null)return value;
130                    throw new ExpressionException("can't remove key ["+key+"] from struct, key doesn't exists");
131            }
132    
133            /**
134             * @see java.util.Map#clear()
135             */
136            public void clear() {
137                    synchronized (_req) {
138                            Enumeration<String> names = _req.getAttributeNames();
139                            while(names.hasMoreElements()){
140                                    _req.removeAttribute(names.nextElement());
141                            }
142                    }
143            }
144    
145            public Object get(Key key) throws PageException {
146                    Object value = get(key,null);
147                    if(value==null) throw invalidKey(key);
148                    return value;
149            }
150            
151    
152    
153            public Object removeEL(Key key) {
154                    synchronized (_req) {
155                            Object value = _req.getAttribute(key.getLowerString()); 
156                            if(value!=null) {
157                                    _req.removeAttribute(key.getLowerString());
158                                    return value;
159                            }
160                            
161                            Enumeration<String> names = _req.getAttributeNames();
162                            String k;
163                            while(names.hasMoreElements()){
164                                    k=names.nextElement();
165                                    if(k.equalsIgnoreCase(key.getString())) {
166                                            value= _req.getAttribute(k);
167                                            _req.removeAttribute(k);
168                                            return value;
169                                    }
170                            }
171                            return value;
172                    }
173            }
174    
175            /**
176             * @see railo.runtime.type.Collection#get(railo.runtime.type.Collection.Key, java.lang.Object)
177             */
178            public Object get(Key key, Object defaultValue) {
179                    synchronized (_req) {
180                            Object value = _req.getAttribute(key.getLowerString()); 
181                            if(value!=null) return value;
182                            
183                            Enumeration<String> names = _req.getAttributeNames();
184                            String k;
185                            while(names.hasMoreElements()){
186                                    k=names.nextElement();
187                                    if(k.equalsIgnoreCase(key.getString())) return _req.getAttribute(k);
188                            }
189                            return defaultValue;
190                    }
191            }
192    
193            /**
194             * @see railo.runtime.type.Collection#setEL(railo.runtime.type.Collection.Key, java.lang.Object)
195             */
196            public Object setEL(Key key, Object value) {
197                    synchronized (_req) {
198                            _req.setAttribute(key.getLowerString(), value);
199                    }
200                    return value;
201            }
202    
203            /**
204             * @see railo.runtime.type.Collection#set(railo.runtime.type.Collection.Key, java.lang.Object)
205             */
206            public Object set(Key key, Object value) throws PageException {
207                    return setEL(key, value);
208            }
209    
210            /**
211             * @see railo.runtime.type.Collection#duplicate(boolean)
212             */
213            public Collection duplicate(boolean deepCopy) {
214                    Struct trg=new StructImpl();
215                    StructImpl.copy(this, trg,deepCopy);
216                    return trg;
217            }
218    
219            /**
220             * @see railo.runtime.type.Collection#containsKey(railo.runtime.type.Collection.Key)
221             */
222            public boolean containsKey(Key key) {
223                    return get(key,null)!=null;
224            }
225            
226            public DumpData toDumpData(PageContext pageContext, int maxlevel, DumpProperties dp) {
227                    return ScopeSupport.toDumpData(pageContext, maxlevel, dp, this, getTypeAsString());
228            }
229    
230    }