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.listener;
020
021import java.util.ArrayList;
022import java.util.Iterator;
023import java.util.List;
024import java.util.Map.Entry;
025
026import lucee.commons.io.res.Resource;
027import lucee.commons.lang.ClassException;
028import lucee.commons.lang.ExceptionUtil;
029import lucee.commons.lang.StringUtil;
030import lucee.commons.lang.types.RefBoolean;
031import lucee.runtime.Mapping;
032import lucee.runtime.MappingImpl;
033import lucee.runtime.PageContext;
034import lucee.runtime.PageContextImpl;
035import lucee.runtime.PageSource;
036import lucee.runtime.config.Config;
037import lucee.runtime.config.ConfigImpl;
038import lucee.runtime.config.ConfigWeb;
039import lucee.runtime.config.ConfigWebImpl;
040import lucee.runtime.config.Constants;
041import lucee.runtime.db.ApplicationDataSource;
042import lucee.runtime.db.DBUtil;
043import lucee.runtime.db.DBUtil.DataSourceDefintion;
044import lucee.runtime.db.DataSource;
045import lucee.runtime.db.DataSourceImpl;
046import lucee.runtime.exp.ApplicationException;
047import lucee.runtime.exp.PageException;
048import lucee.runtime.net.s3.Properties;
049import lucee.runtime.net.s3.PropertiesImpl;
050import lucee.runtime.op.Caster;
051import lucee.runtime.op.Decision;
052import lucee.runtime.orm.ORMConfigurationImpl;
053import lucee.runtime.type.Array;
054import lucee.runtime.type.ArrayImpl;
055import lucee.runtime.type.Collection;
056import lucee.runtime.type.Collection.Key;
057import lucee.runtime.type.KeyImpl;
058import lucee.runtime.type.Struct;
059import lucee.runtime.type.StructImpl;
060import lucee.runtime.type.scope.Scope;
061import lucee.runtime.type.scope.Undefined;
062import lucee.runtime.type.util.CollectionUtil;
063import lucee.runtime.type.util.KeyConstants;
064import lucee.runtime.type.util.ListUtil;
065
066public final class AppListenerUtil {
067
068        public static final int TYPE_ALL = 0;
069        public static final int TYPE_NEW = 1;
070        public static final int TYPE_CLASSIC = 2;
071        
072        public static final Collection.Key ACCESS_KEY_ID = KeyImpl.intern("accessKeyId");
073        public static final Collection.Key AWS_SECRET_KEY = KeyImpl.intern("awsSecretKey");
074        public static final Collection.Key DEFAULT_LOCATION = KeyImpl.intern("defaultLocation");
075        public static final Collection.Key CONNECTION_STRING = KeyImpl.intern("connectionString");
076        
077        public static final Collection.Key BLOB = KeyImpl.intern("blob");
078        public static final Collection.Key CLOB = KeyImpl.intern("clob");
079        public static final Collection.Key CONNECTION_LIMIT = KeyImpl.intern("connectionLimit");
080        public static final Collection.Key CONNECTION_TIMEOUT = KeyImpl.intern("connectionTimeout");
081        public static final Collection.Key META_CACHE_TIMEOUT = KeyImpl.intern("metaCacheTimeout");
082        public static final Collection.Key TIMEZONE = KeyImpl.intern("timezone");
083        public static final Collection.Key ALLOW = KeyImpl.intern("allow");
084        public static final Collection.Key STORAGE = KeyImpl.intern("storage");
085        public static final Collection.Key READ_ONLY = KeyImpl.intern("readOnly");
086        public static final Collection.Key DATABASE = KeyConstants._database;
087        
088        public static PageSource getApplicationPageSource(PageContext pc,PageSource requestedPage, int mode,int type, RefBoolean isCFC) {
089                if(mode==ApplicationListener.MODE_CURRENT2ROOT)
090                        return getApplicationPageSourceCurrToRoot(pc, requestedPage,type, isCFC);
091                if(mode==ApplicationListener.MODE_CURRENT_OR_ROOT)
092                        return getApplicationPageSourceCurrOrRoot(pc, requestedPage,type, isCFC);
093                if(mode==ApplicationListener.MODE_CURRENT)
094                        return getApplicationPageSourceCurrent(requestedPage,type, isCFC);
095                return getApplicationPageSourceRoot(pc,type, isCFC);
096        }
097
098        public static PageSource getApplicationPageSourceCurrent(PageSource requestedPage, int type, RefBoolean isCFC) {
099                if(type!=TYPE_CLASSIC) {
100                        PageSource res=requestedPage.getRealPage(Constants.APP_CFC);
101                    if(res.exists()) {
102                        if(isCFC!=null)isCFC.setValue(true);
103                        return res;
104                    }
105                }
106                if(type!=TYPE_NEW) {
107                        PageSource res=requestedPage.getRealPage(Constants.APP_CFM);
108                        if(res.exists()) return res;
109                }
110                
111                return null;
112        }
113        
114        public static PageSource getApplicationPageSourceRoot(PageContext pc, int type, RefBoolean isCFC) {
115                if(type!=TYPE_CLASSIC) {
116                        PageSource ps = ((PageContextImpl)pc).getPageSourceExisting("/"+Constants.APP_CFC);
117                        if(ps!=null) {
118                                if(isCFC!=null)isCFC.setValue(true);
119                        return ps;
120                        }
121                }
122                
123                if(type!=TYPE_NEW) {
124                        PageSource ps = ((PageContextImpl)pc).getPageSourceExisting("/"+Constants.APP_CFM);
125                        if(ps!=null) return ps;
126                }
127                return null;
128        }
129        
130
131        public static PageSource getApplicationPageSourceCurrToRoot(PageContext pc,PageSource requestedPage, int type, RefBoolean isCFC) {
132            
133                PageSource res = getApplicationPageSourceCurrent(requestedPage, type, isCFC);
134                if(res!=null) return res;
135                
136            Array arr=lucee.runtime.type.util.ListUtil.listToArrayRemoveEmpty(requestedPage.getFullRealpath(),"/");
137                //Config config = pc.getConfig();
138                String path;
139                for(int i=arr.size()-1;i>0;i--) {
140                    StringBuilder sb=new StringBuilder("/");
141                        for(int y=1;y<i;y++) {
142                            sb.append((String)arr.get(y,""));
143                            sb.append('/');
144                        }
145                        path=sb.toString();
146                        if(type!=TYPE_CLASSIC) {
147                                res = ((PageContextImpl)pc).getPageSourceExisting(path.concat(Constants.APP_CFC));
148                                if(res!=null) {
149                                        if(isCFC!=null)isCFC.setValue(true);
150                                        return res;
151                                }
152                        }
153                        if(type!=TYPE_NEW) {
154                                res = ((PageContextImpl)pc).getPageSourceExisting(path.concat(Constants.APP_CFM));
155                                if(res!=null) return res;
156                        }
157                        
158                }
159                return null;
160        }
161        
162        public static PageSource getApplicationPageSourceCurrOrRoot(PageContext pc,PageSource requestedPage, int type,RefBoolean isCFC) {
163            // current 
164                PageSource res = getApplicationPageSourceCurrent(requestedPage, type, isCFC);
165                if(res!=null) return res;
166                
167            // root
168            return getApplicationPageSourceRoot(pc,type, isCFC);
169        }
170        
171        public static String toStringMode(int mode) {
172                if(mode==ApplicationListener.MODE_CURRENT)      return "curr";
173                if(mode==ApplicationListener.MODE_ROOT)         return "root";
174                if(mode==ApplicationListener.MODE_CURRENT2ROOT)         return "curr2root";
175                if(mode==ApplicationListener.MODE_CURRENT_OR_ROOT)              return "currorroot";
176                return "curr2root";
177        }
178
179        public static String toStringType(ApplicationListener listener) {
180                if(listener instanceof NoneAppListener)                 return "none";
181                else if(listener instanceof MixedAppListener)   return "mixed";
182                else if(listener instanceof ClassicAppListener) return "classic";
183                else if(listener instanceof ModernAppListener)  return "modern";
184                return "";
185        }
186        
187        public static DataSource[] toDataSources(Object o,DataSource[] defaultValue) {
188                try {
189                        return toDataSources(o);
190                } catch (Throwable t) {
191                        ExceptionUtil.rethrowIfNecessary(t);
192                        return defaultValue;
193                }
194        }
195
196        public static DataSource[] toDataSources(Object o) throws PageException, ClassException {
197                Struct sct = Caster.toStruct(o);
198                Iterator<Entry<Key, Object>> it = sct.entryIterator();
199                Entry<Key, Object> e;
200                java.util.List<DataSource> dataSources=new ArrayList<DataSource>();
201                while(it.hasNext()) {
202                        e = it.next();
203                        dataSources.add(toDataSource(e.getKey().getString().trim(), Caster.toStruct(e.getValue())));
204                }
205                return dataSources.toArray(new DataSource[dataSources.size()]);
206        }
207
208        public static DataSource toDataSource(String name,Struct data) throws PageException, ClassException {
209                        String user = Caster.toString(data.get(KeyConstants._username,null),null);
210                        String pass = Caster.toString(data.get(KeyConstants._password,""),"");
211                        if(StringUtil.isEmpty(user)) {
212                                user=null;
213                                pass=null;
214                        }
215                        else {
216                                user=user.trim();
217                                pass=pass.trim();
218                        }
219                        
220                        // first check for {class:... , connectionString:...}
221                        Object oConnStr=data.get(CONNECTION_STRING,null);
222                        if(oConnStr!=null)
223                                return ApplicationDataSource.getInstance(
224                                        name, 
225                                        Caster.toString(data.get(KeyConstants._class)), 
226                                        Caster.toString(oConnStr), 
227                                        user, pass,
228                                        Caster.toBooleanValue(data.get(BLOB,null),false),
229                                        Caster.toBooleanValue(data.get(CLOB,null),false), 
230                                        Caster.toIntValue(data.get(CONNECTION_LIMIT,null),-1), 
231                                        Caster.toIntValue(data.get(CONNECTION_TIMEOUT,null),1), 
232                                        Caster.toLongValue(data.get(META_CACHE_TIMEOUT,null),60000L), 
233                                        Caster.toTimeZone(data.get(TIMEZONE,null),null), 
234                                        Caster.toIntValue(data.get(ALLOW,null),DataSource.ALLOW_ALL),
235                                        Caster.toBooleanValue(data.get(STORAGE,null),false),
236                                        Caster.toBooleanValue(data.get(READ_ONLY,null),false));
237                        
238                        // then for {type:... , host:... , ...}
239                        String type=Caster.toString(data.get(KeyConstants._type));
240                        DataSourceDefintion dbt = DBUtil.getDataSourceDefintionForType(type, null);
241                        if(dbt==null) throw new ApplicationException("no datasource type ["+type+"] found");
242                        DataSourceImpl ds = new DataSourceImpl(
243                                        name, 
244                                        dbt.className, 
245                                        Caster.toString(data.get(KeyConstants._host)), 
246                                        dbt.connectionString,
247                                        Caster.toString(data.get(DATABASE)), 
248                                        Caster.toIntValue(data.get(KeyConstants._port,null),-1), 
249                                        user,pass, 
250                                        Caster.toIntValue(data.get(CONNECTION_LIMIT,null),-1), 
251                                        Caster.toIntValue(data.get(CONNECTION_TIMEOUT,null),1), 
252                                        Caster.toLongValue(data.get(META_CACHE_TIMEOUT,null),60000L), 
253                                        Caster.toBooleanValue(data.get(BLOB,null),false), 
254                                        Caster.toBooleanValue(data.get(CLOB,null),false), 
255                                        DataSource.ALLOW_ALL, 
256                                        Caster.toStruct(data.get(KeyConstants._custom,null),null,false), 
257                                        Caster.toBooleanValue(data.get(READ_ONLY,null),false), 
258                                        true, 
259                                        Caster.toBooleanValue(data.get(STORAGE,null),false), 
260                                        Caster.toTimeZone(data.get(TIMEZONE,null),null),
261                                        "",
262                                        Caster.toBooleanValue(data.get("literalTimestampWithTSOffset",null),false)
263                        );
264
265                        return ds;
266                
267        }
268
269        public static Mapping[] toMappings(ConfigWeb cw,Object o,Mapping[] defaultValue, Resource source) { 
270                try {
271                        return toMappings(cw, o,source);
272                } catch (Throwable t) {
273                        ExceptionUtil.rethrowIfNecessary(t);
274                        return defaultValue;
275                }
276        }
277
278        public static Mapping[] toMappings(ConfigWeb cw,Object o, Resource source) throws PageException {
279                Struct sct = Caster.toStruct(o);
280                Iterator<Entry<Key, Object>> it = sct.entryIterator();
281                Entry<Key, Object> e;
282                java.util.List<Mapping> mappings=new ArrayList<Mapping>();
283                ConfigWebImpl config=(ConfigWebImpl) cw;
284                String virtual;
285                while(it.hasNext()) {
286                        e = it.next();
287                        virtual=translateMappingVirtual(e.getKey().getString());
288                        MappingData md=toMappingData(e.getValue(),source);
289                        mappings.add(config.getApplicationMapping("application",virtual,md.physical,md.archive,md.physicalFirst,false));
290                }
291                return mappings.toArray(new Mapping[mappings.size()]);
292        }
293        
294
295        private static MappingData toMappingData(Object value, Resource source) throws PageException {
296                MappingData md=new MappingData();
297                
298                if(Decision.isStruct(value)) {
299                        Struct map=Caster.toStruct(value);
300                        
301                        
302                        // physical
303                        String physical=Caster.toString(map.get("physical",null),null);
304                        if(!StringUtil.isEmpty(physical,true)) 
305                                md.physical=translateMappingPhysical(physical.trim(),source);
306
307                        // archive
308                        String archive = Caster.toString(map.get("archive",null),null);
309                        if(!StringUtil.isEmpty(archive,true)) 
310                                md.archive=translateMappingPhysical(archive.trim(),source);
311                        
312                        if(archive==null && physical==null) 
313                                throw new ApplicationException("you must define archive or/and physical!");
314                        
315                        // primary
316                        md.physicalFirst=true;
317                        // primary is only of interest when both values exists
318                        if(archive!=null && physical!=null) {
319                                String primary = Caster.toString(map.get("primary",null),null);
320                                if(primary!=null && primary.trim().equalsIgnoreCase("archive")) md.physicalFirst=false;
321                        }
322                        // only a archive
323                        else if(archive!=null) md.physicalFirst=false;
324                }
325                // simple value == only a physical path
326                else {
327                        md.physical=translateMappingPhysical(Caster.toString(value).trim(),source);
328                        md.physicalFirst=true;
329                }
330                
331                return md;
332        }
333
334        private static String translateMappingPhysical(String path, Resource source) {
335                if(source==null) return path;
336                source=source.getParentResource().getRealResource(path);
337                if(source.exists()) return source.getAbsolutePath();
338                return path;
339        }
340
341        private static String translateMappingVirtual(String virtual) {
342                virtual=virtual.replace('\\', '/');
343                if(!StringUtil.startsWith(virtual,'/'))virtual="/".concat(virtual);
344                return virtual;
345        }
346        
347        public static Mapping[] toCustomTagMappings(ConfigWeb cw, Object o, Resource source) throws PageException {
348                return toMappings(cw,"custom", o,false,source);
349        }
350
351        public static Mapping[] toCustomTagMappings(ConfigWeb cw, Object o, Resource source, Mapping[] defaultValue) {
352                try {
353                        return toMappings(cw,"custom", o,false,source);
354                } catch (Throwable t) {
355                        ExceptionUtil.rethrowIfNecessary(t);
356                        return defaultValue;
357                }
358        }
359
360        public static Mapping[] toComponentMappings(ConfigWeb cw, Object o, Resource source) throws PageException {
361                return toMappings(cw,"component", o,true,source);
362        }
363
364        public static Mapping[] toComponentMappings(ConfigWeb cw, Object o, Resource source,Mapping[] defaultValue) {
365                
366                try {
367                        return toMappings(cw,"component", o,true,source);
368                } catch (Throwable t) {
369                        ExceptionUtil.rethrowIfNecessary(t);
370                        return defaultValue;
371                }
372        }
373
374        private static Mapping[] toMappings(ConfigWeb cw,String type, Object o, boolean useStructNames, Resource source) throws PageException {
375                ConfigWebImpl config=(ConfigWebImpl) cw;
376                Array array;
377                if(o instanceof String){
378                        array=ListUtil.listToArrayRemoveEmpty(Caster.toString(o),',');
379                }
380                else if(o instanceof Struct){
381                        Struct sct=(Struct) o;
382                        if(useStructNames) {
383                                Iterator<Entry<Key, Object>> it = sct.entryIterator();
384                                List<Mapping> list=new ArrayList<Mapping>();
385                                Entry<Key, Object> e;
386                                String virtual;
387                                while(it.hasNext()) {
388                                        e = it.next();
389                                        virtual=e.getKey().getString();
390                                        if(virtual.length()==0) virtual="/";
391                                        if(!virtual.startsWith("/")) virtual="/"+virtual;
392                                if(!virtual.equals("/") && virtual.endsWith("/"))virtual=virtual.substring(0,virtual.length()-1);
393                                MappingData md=toMappingData(e.getValue(),source);
394                                        list.add(config.getApplicationMapping(type,virtual,md.physical,md.archive,md.physicalFirst,true));
395                                }
396                                return list.toArray(new Mapping[list.size()]);
397                        }
398                        
399                        array=new ArrayImpl();
400                        Iterator<Object> it = sct.valueIterator();
401                        while(it.hasNext()) {
402                                array.append(it.next());
403                        }
404                }
405                else {
406                        array=Caster.toArray(o);
407                }
408                MappingImpl[] mappings=new MappingImpl[array.size()];
409                for(int i=0;i<mappings.length;i++) {
410                        
411                        MappingData md=toMappingData(array.getE(i+1),source);
412                        mappings[i]=(MappingImpl) config.getApplicationMapping(type,"/"+i,md.physical,md.archive,md.physicalFirst,true);
413                }
414                return mappings;
415        }
416
417
418        public static String toLocalMode(int mode, String defaultValue) {
419                if(Undefined.MODE_LOCAL_OR_ARGUMENTS_ALWAYS==mode) return "modern";
420                if(Undefined.MODE_LOCAL_OR_ARGUMENTS_ONLY_WHEN_EXISTS==mode)return "classic";
421                return defaultValue;
422        }
423        
424        public static int toLocalMode(Object oMode, int defaultValue) {
425                if(oMode==null) return defaultValue;
426                
427                if(Decision.isBoolean(oMode)) {
428                        if(Caster.toBooleanValue(oMode, false))
429                                return Undefined.MODE_LOCAL_OR_ARGUMENTS_ALWAYS;
430                        return Undefined.MODE_LOCAL_OR_ARGUMENTS_ONLY_WHEN_EXISTS;
431                }
432                String strMode=Caster.toString(oMode,null);
433                if("always".equalsIgnoreCase(strMode) || "modern".equalsIgnoreCase(strMode)) 
434                        return Undefined.MODE_LOCAL_OR_ARGUMENTS_ALWAYS;
435                if("update".equalsIgnoreCase(strMode) || "classic".equalsIgnoreCase(strMode)) 
436                        return Undefined.MODE_LOCAL_OR_ARGUMENTS_ONLY_WHEN_EXISTS;
437                return defaultValue;
438        }
439        
440        public static int toLocalMode(String strMode) throws ApplicationException {
441                int lm = toLocalMode(strMode, -1);
442                if(lm!=-1) return lm;
443                throw new ApplicationException("invalid localMode definition ["+strMode+"] for tag "+Constants.CFAPP_NAME+"/"+Constants.APP_CFC+", valid values are [classic,modern,true,false]");
444        }
445
446        public static short toSessionType(String str, short defaultValue) {
447                if(!StringUtil.isEmpty(str,true)){
448                        str=str.trim().toLowerCase();
449                        if("cfml".equals(str)) return Config.SESSION_TYPE_CFML;
450                        if("j2ee".equals(str)) return Config.SESSION_TYPE_J2EE;
451                        if("cfm".equals(str)) return Config.SESSION_TYPE_CFML;
452                        if("jee".equals(str)) return Config.SESSION_TYPE_J2EE;
453                        if("j".equals(str)) return Config.SESSION_TYPE_J2EE;
454                        if("c".equals(str)) return Config.SESSION_TYPE_J2EE;
455                }
456                return defaultValue;
457        }
458
459        public static short toSessionType(String str) throws ApplicationException {
460                if(!StringUtil.isEmpty(str,true)){
461                        str=str.trim().toLowerCase();
462                        if("cfml".equals(str)) return Config.SESSION_TYPE_CFML;
463                        if("j2ee".equals(str)) return Config.SESSION_TYPE_J2EE;
464                        if("cfm".equals(str)) return Config.SESSION_TYPE_CFML;
465                        if("jee".equals(str)) return Config.SESSION_TYPE_J2EE;
466                        if("j".equals(str)) return Config.SESSION_TYPE_J2EE;
467                        if("c".equals(str)) return Config.SESSION_TYPE_J2EE;
468                }
469                throw new ApplicationException("invalid sessionType definition ["+str+"] for tag "+Constants.CFAPP_NAME+"/"+Constants.APP_CFC+", valid values are [cfml,j2ee]");
470        }
471        
472        public static Properties toS3(Struct sct) {
473                String host=Caster.toString(sct.get(KeyConstants._host,null),null);
474                if(StringUtil.isEmpty(host))host=Caster.toString(sct.get(KeyConstants._server,null),null);
475                
476                return toS3(
477                                Caster.toString(sct.get(ACCESS_KEY_ID,null),null),
478                                Caster.toString(sct.get(AWS_SECRET_KEY,null),null),
479                                Caster.toString(sct.get(DEFAULT_LOCATION,null),null),
480                                host
481                        );
482        }
483
484        public static Properties toS3(String accessKeyId, String awsSecretKey, String defaultLocation, String host) {
485                PropertiesImpl s3 = new PropertiesImpl();
486                if(!StringUtil.isEmpty(accessKeyId))s3.setAccessKeyId(accessKeyId);
487                if(!StringUtil.isEmpty(awsSecretKey))s3.setSecretAccessKey(awsSecretKey);
488                if(!StringUtil.isEmpty(defaultLocation))s3.setDefaultLocation(defaultLocation);
489                if(!StringUtil.isEmpty(host))s3.setHost(host);
490                return s3;
491        }
492
493        public static void setORMConfiguration(PageContext pc, ApplicationContext ac,Struct sct) throws PageException {
494                if(sct==null)sct=new StructImpl();
495                Resource res=pc.getCurrentTemplatePageSource().getResourceTranslated(pc).getParentResource();
496                ConfigImpl config=(ConfigImpl) pc.getConfig();
497                ac.setORMConfiguration(ORMConfigurationImpl.load(config,ac,sct,res,config.getORMConfig()));
498                
499                // datasource
500                Object o = sct.get(KeyConstants._datasource,null);
501                
502                if(o!=null) {
503                        o=toDefaultDatasource(o);
504                        if(o!=null) ((ApplicationContextPro)ac).setORMDataSource(o);
505                }
506        }
507        
508        
509        /**
510         * translate int definition of script protect to string definition
511         * @param scriptProtect
512         * @return
513         */
514        public static String translateScriptProtect(int scriptProtect) {
515                if(scriptProtect==ApplicationContext.SCRIPT_PROTECT_NONE) return "none";
516                if(scriptProtect==ApplicationContext.SCRIPT_PROTECT_ALL) return "all";
517                
518                Array arr=new ArrayImpl();
519                if((scriptProtect&ApplicationContext.SCRIPT_PROTECT_CGI)>0) arr.appendEL("cgi");
520                if((scriptProtect&ApplicationContext.SCRIPT_PROTECT_COOKIE)>0) arr.appendEL("cookie");
521                if((scriptProtect&ApplicationContext.SCRIPT_PROTECT_FORM)>0) arr.appendEL("form");
522                if((scriptProtect&ApplicationContext.SCRIPT_PROTECT_URL)>0) arr.appendEL("url");
523                
524                
525                
526                try {
527                        return ListUtil.arrayToList(arr, ",");
528                } catch (PageException e) {
529                        return "none";
530                } 
531        }
532        
533
534        /**
535         * translate string definition of script protect to int definition
536         * @param strScriptProtect
537         * @return
538         */
539        public static int translateScriptProtect(String strScriptProtect) {
540                strScriptProtect=strScriptProtect.toLowerCase().trim();
541                
542                if("none".equals(strScriptProtect)) return ApplicationContext.SCRIPT_PROTECT_NONE;
543                if("no".equals(strScriptProtect)) return ApplicationContext.SCRIPT_PROTECT_NONE;
544                if("false".equals(strScriptProtect)) return ApplicationContext.SCRIPT_PROTECT_NONE;
545                
546                if("all".equals(strScriptProtect)) return ApplicationContext.SCRIPT_PROTECT_ALL;
547                if("true".equals(strScriptProtect)) return ApplicationContext.SCRIPT_PROTECT_ALL;
548                if("yes".equals(strScriptProtect)) return ApplicationContext.SCRIPT_PROTECT_ALL;
549                
550                String[] arr = ListUtil.listToStringArray(strScriptProtect, ',');
551                String item;
552                int scriptProtect=0;
553                for(int i=0;i<arr.length;i++) {
554                        item=arr[i].trim();
555                        if("cgi".equals(item) && (scriptProtect&ApplicationContext.SCRIPT_PROTECT_CGI)==0)
556                                scriptProtect+=ApplicationContext.SCRIPT_PROTECT_CGI;
557                        else if("cookie".equals(item) && (scriptProtect&ApplicationContext.SCRIPT_PROTECT_COOKIE)==0)
558                                scriptProtect+=ApplicationContext.SCRIPT_PROTECT_COOKIE;
559                        else if("form".equals(item) && (scriptProtect&ApplicationContext.SCRIPT_PROTECT_FORM)==0)
560                                scriptProtect+=ApplicationContext.SCRIPT_PROTECT_FORM;
561                        else if("url".equals(item) && (scriptProtect&ApplicationContext.SCRIPT_PROTECT_URL)==0)
562                                scriptProtect+=ApplicationContext.SCRIPT_PROTECT_URL;
563                }
564                return scriptProtect;
565        }
566        
567
568        public static String translateLoginStorage(int loginStorage) {
569                if(loginStorage==Scope.SCOPE_SESSION) return "session";
570                return "cookie";
571        }
572        
573
574        public static int translateLoginStorage(String strLoginStorage, int defaultValue) {
575                strLoginStorage=strLoginStorage.toLowerCase().trim();
576            if(strLoginStorage.equals("session"))return Scope.SCOPE_SESSION;
577            if(strLoginStorage.equals("cookie"))return Scope.SCOPE_COOKIE;
578            return defaultValue;
579        }
580        
581
582        public static int translateLoginStorage(String strLoginStorage) throws ApplicationException {
583                int ls=translateLoginStorage(strLoginStorage, -1);
584                if(ls!=-1) return ls;
585            throw new ApplicationException("invalid loginStorage definition ["+strLoginStorage+"], valid values are [session,cookie]");
586        }
587        
588        public static Object toDefaultDatasource(Object o) throws PageException {
589                if(Decision.isStruct(o)) {
590                        Struct sct=(Struct) o;
591                        
592                        // fix for Jira ticket LUCEE-1931
593                        if(sct.size()==1) {
594                                Key[] keys = CollectionUtil.keys(sct);
595                                if(keys.length==1 && keys[0].equalsIgnoreCase(KeyConstants._name)) {
596                                        return Caster.toString(sct.get(KeyConstants._name));
597                                }
598                        }
599                        
600                        try {
601                                return AppListenerUtil.toDataSource("__default__",sct);
602                        } 
603                        catch (PageException pe) { 
604                                // again try fix for Jira ticket LUCEE-1931
605                                String name= Caster.toString(sct.get(KeyConstants._name,null),null);
606                                if(!StringUtil.isEmpty(name)) return name;
607                                throw pe;
608                        }
609                        catch (ClassException e) {
610                                throw Caster.toPageException(e);
611                        }
612                }
613                return Caster.toString(o);
614        }
615
616        public static String toWSType(short wstype, String defaultValue) {
617                if(ApplicationContextPro.WS_TYPE_AXIS1== wstype) return "Axis1";
618                if(ApplicationContextPro.WS_TYPE_JAX_WS== wstype) return "JAX-WS";
619                if(ApplicationContextPro.WS_TYPE_CXF== wstype) return "CXF";
620                return defaultValue;
621        }
622        
623        public static short toWSType(String wstype, short defaultValue) {
624                if(wstype==null) return defaultValue;
625                wstype=wstype.trim();
626                
627                if("axis".equalsIgnoreCase(wstype) || "axis1".equalsIgnoreCase(wstype))
628                        return ApplicationContextPro.WS_TYPE_AXIS1;
629                /*if("jax".equalsIgnoreCase(wstype) || "jaxws".equalsIgnoreCase(wstype) || "jax-ws".equalsIgnoreCase(wstype))
630                        return ApplicationContextPro.WS_TYPE_JAX_WS;
631                if("cxf".equalsIgnoreCase(wstype))
632                        return ApplicationContextPro.WS_TYPE_CXF;*/
633                return defaultValue;
634        }
635        
636        public static short toWSType(String wstype) throws ApplicationException {
637                String str="";
638                KeyImpl cs=new KeyImpl(str){
639                        
640                        public String getString() {
641                                return null;
642                        }
643                        
644                };
645                
646                
647                
648                short wst = toWSType(wstype,(short)-1);
649                if(wst!=-1) return wst;
650                throw new ApplicationException("invalid webservice type ["+wstype+"], valid values are [axis1]");
651                //throw new ApplicationException("invalid webservice type ["+wstype+"], valid values are [axis1,jax-ws,cxf]");
652        }
653        static private class MappingData {
654                private String physical;
655                private String archive;
656                private boolean physicalFirst;
657        }
658}
659
660