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