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
021import lucee.commons.io.res.Resource;
022import lucee.commons.io.res.util.ResourceUtil;
023import lucee.commons.lang.StringUtil;
024import lucee.runtime.PageContext;
025import lucee.runtime.config.ConfigWebAdmin;
026import lucee.runtime.config.ConfigWebImpl;
027import lucee.runtime.exp.FunctionException;
028import lucee.runtime.exp.PageException;
029import lucee.runtime.exp.SecurityException;
030import lucee.runtime.listener.ApplicationContext;
031import lucee.runtime.listener.ModernApplicationContext;
032import lucee.runtime.op.Caster;
033import lucee.runtime.rest.Mapping;
034import lucee.runtime.rest.RestUtil;
035import lucee.runtime.type.KeyImpl;
036
037public class RestDeleteApplication {
038        public static String call(PageContext pc , String dirPath) throws PageException {
039                return call(pc, dirPath,null);
040        }
041
042        public static String call(PageContext pc , String dirPath,String webAdminPassword) throws PageException {
043                webAdminPassword=getPassword(pc, webAdminPassword);
044        
045                Resource dir=RestDeleteApplication.toResource(pc,dirPath);
046                ConfigWebImpl config=(ConfigWebImpl) pc.getConfig();
047
048                try {
049                        ConfigWebAdmin admin = ConfigWebAdmin.newInstance((ConfigWebImpl)pc.getConfig(),webAdminPassword);
050                        Mapping[] mappings = config.getRestMappings();
051                        Mapping mapping;
052                        for(int i=0;i<mappings.length;i++){
053                                mapping=mappings[i];
054                                if(RestUtil.isMatch(pc,mapping,dir)){
055                                        admin.removeRestMapping(mapping.getVirtual());
056                                        admin.store();
057                                }
058                        }
059                } 
060        catch (Exception e) {
061                        throw Caster.toPageException(e);
062                }
063        
064        
065                return null;
066        }
067        
068        static String getPassword(PageContext pc,String password) throws SecurityException {
069                if(!StringUtil.isEmpty(password, true)) {
070                        password=password.trim();
071                }
072                else {
073                        ApplicationContext ac = pc.getApplicationContext();
074                        if(ac instanceof ModernApplicationContext) {
075                                ModernApplicationContext mac=(ModernApplicationContext) ac;
076                                password = Caster.toString(mac.getCustom(KeyImpl.init("webAdminPassword")),null);
077                        }
078                }
079                if(StringUtil.isEmpty(password, true))
080                        throw new SecurityException("To manipulate a REST mapping you need to define the password for the current Web Administartor, " +
081                                        "you can do this as argument with this function or inside the application.cfc with the variable [this.webAdminPassword].");
082
083                return password;
084        }
085        
086        static Resource toResource(PageContext pc, String dirPath) throws PageException {
087                Resource dir = ResourceUtil.toResourceNotExisting(pc.getConfig(), dirPath);
088                pc.getConfig().getSecurityManager().checkFileLocation(dir);
089                if(!dir.isDirectory())
090                        throw new FunctionException(pc, "RestInitApplication", 1, "dirPath", "argument value ["+dirPath+"] must contain a existing directory");
091                
092                return dir;
093        }
094}