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