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.rest;
020
021import java.io.IOException;
022
023import javax.servlet.http.HttpServletResponse;
024
025import lucee.commons.io.res.Resource;
026import lucee.commons.io.res.util.ResourceUtil;
027import lucee.runtime.PageContext;
028import lucee.runtime.rest.path.Path;
029import lucee.runtime.type.Struct;
030import lucee.runtime.type.util.ListUtil;
031
032public class RestUtil {
033        
034        public static String[] splitPath(String path) {
035                return ListUtil.listToStringArray(path, '/');
036        }
037        
038        
039        /**
040         * check if caller path match the cfc path
041         * @param variables
042         * @param restPath
043         * @param callerPath
044         * @return match until which index of the given cfc path, returns -1 if there is no match
045         */
046        public static int matchPath(Struct variables,Path[] restPath, String[] callerPath) {
047                if(restPath.length>callerPath.length) return -1;
048                
049                int index=0;
050                for(;index<restPath.length;index++){
051                        if(!restPath[index].match(variables,callerPath[index])) return -1;
052                }
053                return index-1;
054        }
055
056
057        public static void setStatus(PageContext pc,int status, String msg) {
058                pc.clear();
059                if(msg!=null) {
060                        try {
061                                pc.forceWrite(msg);
062                        } 
063                        catch (IOException e) {}
064                }
065                HttpServletResponse rsp = pc.getHttpServletResponse();
066                rsp.setHeader("Connection", "close"); // IE unter IIS6, Win2K3 und Resin
067                rsp.setStatus(status);
068                
069        }
070
071
072        public static void release(Mapping[] mappings) {
073                for(int i=0;i<mappings.length;i++){
074                        mappings[i].release();
075                }
076        }
077
078        public static boolean isMatch(PageContext pc,Mapping mapping, Resource res) {
079                Resource p = mapping.getPhysical();
080                if(p!=null){
081                        return p.equals(res);
082                }
083                return ResourceUtil.toResourceNotExisting(pc, mapping.getStrPhysical()).equals(res);
084        }
085
086        /*public static void main(String[] args) {
087                Struct res=new StructImpl();
088                Source src = new Source(null,null,"test1/{a: \\d+}-{b}/");
089                String[] callerPath=splitPath("/test1/1-muster/mueller");
090                print.e(matchPath(res,src.getPath(), callerPath));
091                print.e(res);
092        }*/
093
094}