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.functions.rest;
020
021
022import lucee.commons.io.res.Resource;
023import lucee.commons.lang.StringUtil;
024import lucee.runtime.PageContext;
025import lucee.runtime.config.ConfigWebAdmin;
026import lucee.runtime.config.ConfigWebImpl;
027import lucee.runtime.exp.PageException;
028import lucee.runtime.op.Caster;
029import lucee.runtime.rest.Mapping;
030import lucee.runtime.rest.RestUtil;
031
032public class RestInitApplication {
033
034        public static String call(PageContext pc , String dirPath) throws PageException {
035                return _call(pc, dirPath, null,null,null);
036        }
037        
038        public static String call(PageContext pc , String dirPath, String serviceMapping) throws PageException {
039                return _call(pc, dirPath, serviceMapping, null,null);
040        }
041
042        public static String call(PageContext pc , String dirPath, String serviceMapping, boolean defaultMapping) throws PageException {
043                return _call(pc, dirPath, serviceMapping, defaultMapping, null);
044        }
045
046        public static String call(PageContext pc , String dirPath, String serviceMapping, boolean defaultMapping, String webAdminPassword) throws PageException {
047                return _call(pc, dirPath, serviceMapping, defaultMapping, webAdminPassword);
048        }
049        
050        public static String _call(PageContext pc , String dirPath, String serviceMapping, Boolean defaultMapping, String webAdminPassword) throws PageException {
051                if(StringUtil.isEmpty(serviceMapping,true)){
052                        serviceMapping=pc.getApplicationContext().getName();
053                }
054                Resource dir=RestDeleteApplication.toResource(pc,dirPath);
055                
056                ConfigWebImpl config=(ConfigWebImpl) pc.getConfig();
057                Mapping[] mappings = config.getRestMappings();
058                Mapping mapping;
059                
060                // id is mapping name
061                
062                String virtual=serviceMapping.trim();
063                if(!virtual.startsWith("/")) virtual="/"+virtual;
064                if(!virtual.endsWith("/")) virtual+="/";
065                boolean hasResetted=false;
066                for(int i=0;i<mappings.length;i++){
067                        mapping=mappings[i];
068                        if(mapping.getVirtualWithSlash().equals(virtual)){
069                                // directory has changed
070                                if(!RestUtil.isMatch(pc, mapping, dir) || (defaultMapping!=null && mapping.isDefault()!=defaultMapping.booleanValue())) {
071                                        update(pc,dir,virtual,RestDeleteApplication.getPassword(pc,webAdminPassword),defaultMapping==null?mapping.isDefault():defaultMapping.booleanValue());
072                                }
073                                mapping.reset(pc);
074                                hasResetted=true;
075                        }
076                }
077                if(!hasResetted) {
078                        update(pc,dir,virtual,RestDeleteApplication.getPassword(pc,webAdminPassword),defaultMapping==null?false:defaultMapping.booleanValue());
079                }
080        
081                return null;
082        }
083
084        private static void update(PageContext pc,Resource dir, String virtual, String webAdminPassword, boolean defaultMapping) throws PageException {
085                try {
086                        ConfigWebAdmin admin = ConfigWebAdmin.newInstance((ConfigWebImpl)pc.getConfig(),webAdminPassword);
087                        admin.updateRestMapping(virtual, dir.getAbsolutePath(), defaultMapping);
088                        admin.store();
089                } 
090                catch (Exception e) {
091                        throw Caster.toPageException(e);
092                }
093        }
094        
095}