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.type.scope;
020
021import java.io.UnsupportedEncodingException;
022
023import lucee.commons.net.URLItem;
024import lucee.runtime.PageContext;
025import lucee.runtime.PageContextImpl;
026import lucee.runtime.listener.ApplicationContext;
027import lucee.runtime.net.http.ReqRspUtil;
028import lucee.runtime.op.Caster;
029import lucee.runtime.type.Collection;
030import lucee.runtime.type.KeyImpl;
031
032/**
033 * Implements URL Scope 
034 */
035public final class URLImpl extends ScopeSupport implements URL,ScriptProtected {
036
037        private String encoding=null;
038    private int scriptProtected=ScriptProtected.UNDEFINED;
039    private static final URLItem[] empty=new URLItem[0];
040        private static final Collection.Key REQUEST_TIMEOUT = KeyImpl.intern("RequestTimeout"); 
041    private URLItem[] raw=empty;
042    
043        /**
044         * Standart Constructor
045         */
046        public URLImpl() {
047                super(true,"url",SCOPE_URL);
048        }
049
050    @Override
051    public String getEncoding() {
052        return encoding;
053    }
054
055    @Override
056    public void setEncoding(ApplicationContext ac,String encoding) throws UnsupportedEncodingException {
057        encoding=encoding.trim().toUpperCase();
058        if(encoding.equals(this.encoding)) return;
059        this.encoding = encoding;
060        if(isInitalized())fillDecoded(raw,encoding,isScriptProtected(),ac.getSameFieldAsArray(SCOPE_URL));
061    }
062
063    public void reinitialize(ApplicationContext ac) {
064        if(isInitalized()){
065                if(scriptProtected==ScriptProtected.UNDEFINED) {
066                        scriptProtected=((ac.getScriptProtect()&ApplicationContext.SCRIPT_PROTECT_URL)>0)?
067                                        ScriptProtected.YES:ScriptProtected.NO;
068                }
069            
070                fillDecodedEL(raw,encoding,isScriptProtected(),ac.getSameFieldAsArray(SCOPE_URL));
071        }
072    }
073
074        @Override
075        public void initialize(PageContext pc) {
076        if(encoding==null)encoding=((PageContextImpl)pc).getWebCharset().name();
077        if(scriptProtected==ScriptProtected.UNDEFINED) {
078                        scriptProtected=((pc.getApplicationContext().getScriptProtect()&ApplicationContext.SCRIPT_PROTECT_URL)>0)?
079                                        ScriptProtected.YES:ScriptProtected.NO;
080                }
081        
082        try {
083                        super.initialize(pc); 
084            raw=setFromQueryString(ReqRspUtil.getQueryString(pc.getHttpServletRequest()));
085            
086            fillDecoded(raw,encoding,isScriptProtected(),pc.getApplicationContext().getSameFieldAsArray(SCOPE_URL));
087            
088            if(raw.length>0 && pc.getConfig().isAllowURLRequestTimeout()){
089                Object o=get(REQUEST_TIMEOUT,null);
090                if(o!=null) {
091                        long timeout = Caster.toLongValue(o,-1);
092                        if(timeout!=-1)pc.setRequestTimeout(timeout*1000);
093                }
094                Caster.toDoubleValue(o,false,-1);
095            }
096                }
097        catch (Exception e) {}
098        }
099        @Override
100        public void release() {
101        encoding=null;
102        raw=empty;
103        scriptProtected=ScriptProtected.UNDEFINED;
104        super.release();
105    }
106        @Override
107        public void release(PageContext pc) {
108        encoding=null;
109        raw=empty;
110        scriptProtected=ScriptProtected.UNDEFINED;
111        super.release(pc);
112    }
113
114    @Override
115        public void setScriptProtecting(ApplicationContext ac,boolean scriptProtected) {
116                
117                int _scriptProtected = scriptProtected?ScriptProtected.YES:ScriptProtected.NO;
118                //print.out(isInitalized()+"x"+(_scriptProtected+"!="+this.scriptProtected));
119                if(isInitalized() && _scriptProtected!=this.scriptProtected) {
120                        fillDecodedEL(raw,encoding,scriptProtected,ac.getSameFieldAsArray(SCOPE_URL));
121                }
122                this.scriptProtected=_scriptProtected;
123        }
124        
125
126        @Override
127        public boolean isScriptProtected() {
128                return scriptProtected==ScriptProtected.YES;
129        }
130
131        /**
132         * @return the raw
133         */
134        public URLItem[] getRaw() {
135                return raw;
136        }
137}