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.util;
020
021import java.io.UnsupportedEncodingException;
022import java.util.HashMap;
023import java.util.Map;
024
025import lucee.commons.net.URLDecoder;
026import lucee.commons.net.URLItem;
027import lucee.runtime.net.http.ReqRspUtil;
028
029public class ScopeUtil {
030
031        public static Map<String,String[]> getParameterMap(URLItem[][] itemsArr, String[] encodings) {
032                String n,v;
033                String[] arr;
034                Map<String,String[]> parameters=new HashMap<String, String[]>();
035                URLItem[] items;
036                String encoding;
037                for(int x=0;x<itemsArr.length;x++){
038                        items=itemsArr[x];
039                        encoding=encodings[x];
040                        for(int i=0;i<items.length;i++){
041                                n=items[i].getName();
042                                v=items[i].getValue();
043                                if(items[i].isUrlEncoded()) {
044                                        try{
045                                        n=URLDecoder.decode(n,encoding,true);
046                        v=URLDecoder.decode(v,encoding,true);
047                                        }
048                                        catch(UnsupportedEncodingException e){}
049                                }
050                                arr=parameters.get(n);
051                                if(arr==null)parameters.put(n, new String[]{v});
052                                else {
053                                        String[] tmp = new String[arr.length+1];
054                                        System.arraycopy(arr, 0, tmp, 0, arr.length);
055                                        tmp[arr.length]=v;
056                                        parameters.put(n, tmp);
057                                }
058                        }
059                }
060                return parameters;
061        }
062        
063
064        public static String[] getParameterValues(URLItem[][] itemsArr, String[] encodings,String name) {
065                String n,v;
066                String encName;
067                
068                String[] arr=null;
069                URLItem[] items;
070                String encoding;
071                for(int x=0;x<itemsArr.length;x++){
072                        items=itemsArr[x];
073                        encoding=encodings[x];
074                        if(ReqRspUtil.needEncoding(name, false)) encName=ReqRspUtil.encode(name, encoding);
075                        else encName=null;
076                        for(int i=0;i<items.length;i++){
077                                n=items[i].getName();
078                                if(!name.equals(n) && (encName==null || !encName.equals(n))) {
079                                        continue;
080                                }
081                                v=items[i].getValue();
082                                if(items[i].isUrlEncoded()) {
083                                        try{
084                                                n=URLDecoder.decode(n,encoding,true);
085                                                v=URLDecoder.decode(v,encoding,true);
086                                        }
087                                        catch(UnsupportedEncodingException e){}
088                                }
089                                if(arr==null)arr=new String[]{v};
090                                else {
091                                        String[] tmp = new String[arr.length+1];
092                                        System.arraycopy(arr, 0, tmp, 0, arr.length);
093                                        tmp[arr.length]=v;
094                                        arr=tmp;
095                                }
096                        }
097                }
098                return arr;
099        }
100
101}