001    /**
002     * Implements the Cold Fusion Function getpagecontext
003     */
004    package railo.runtime.functions.other;
005    
006    import javax.servlet.http.Cookie;
007    
008    import railo.commons.io.DevNullOutputStream;
009    import railo.commons.lang.Pair;
010    import railo.runtime.PageContext;
011    import railo.runtime.exp.PageException;
012    import railo.runtime.ext.function.Function;
013    import railo.runtime.op.Caster;
014    import railo.runtime.thread.ThreadUtil;
015    import railo.runtime.type.Collection.Key;
016    import railo.runtime.type.Struct;
017    import railo.runtime.type.StructImpl;
018    
019    public final class CreatePageContext implements Function {
020    
021    
022            
023            public static Object call(PageContext pc, String serverName, String scriptName) throws PageException {
024                    return call(pc,serverName,scriptName,"",new StructImpl(),new StructImpl(),new StructImpl(),new StructImpl());
025            }
026            
027            public static Object call(PageContext pc, String serverName, String scriptName,String queryString) throws PageException {
028                    return call(pc,serverName,scriptName,queryString,new StructImpl(),new StructImpl(),new StructImpl(),new StructImpl());
029            }
030            
031            public static Object call(PageContext pc, String serverName, String scriptName,String queryString, Struct cookies) throws PageException {
032                    return call(pc,serverName,scriptName,queryString,cookies,new StructImpl(),new StructImpl(),new StructImpl());
033            }
034            
035            public static Object call(PageContext pc, String serverName, String scriptName,String queryString, Struct cookies, Struct headers) throws PageException {
036                    return call(pc,serverName,scriptName,queryString,cookies,headers,new StructImpl(),new StructImpl());
037            }
038            
039            public static Object call(PageContext pc, String serverName, String scriptName,String queryString, Struct cookies, Struct headers, Struct parameters) throws PageException {
040                    return call(pc,serverName,scriptName,queryString,cookies,headers,parameters,new StructImpl());
041            }
042            
043            public static Object call(PageContext pc, String serverName, String scriptName,String queryString, Struct cookies, Struct headers, Struct parameters, Struct attributes) throws PageException {
044                    return ThreadUtil.createPageContext(
045                                    pc.getConfig(), 
046                                    DevNullOutputStream.DEV_NULL_OUTPUT_STREAM, 
047                                    serverName, 
048                                    scriptName, 
049                                    queryString, 
050                                    toCookies(cookies), 
051                                    toPair(headers,true), 
052                                    toPair(parameters,true), 
053                                    castValuesToString(attributes));
054            }
055    
056            private static Struct castValuesToString(Struct sct) throws PageException {
057                    Key[] keys = sct.keys();
058                    for(int i=0;i<keys.length;i++){
059                            sct.set(keys[i], Caster.toString(sct.get(keys[i])));
060                    }
061                    return sct;
062            }
063    
064            private static Pair[] toPair(Struct sct, boolean doStringCast) throws PageException {
065                    Key[] keys = sct.keys();
066                    Object value;
067                    Pair[] pairs=new Pair[keys.length];
068                    for(int i=0;i<keys.length;i++){
069                            value= sct.get(keys[i]);
070                            if(doStringCast)value=Caster.toString(value);
071                            pairs[i]=new Pair(keys[i].getString(),value);
072                    }
073                    return pairs;
074            }
075    
076            private static Cookie[] toCookies(Struct sct) throws PageException {
077                    Key[] keys = sct.keys();
078                    Cookie[] cookies=new Cookie[keys.length];
079                    for(int i=0;i<keys.length;i++){
080                            cookies[i]=new Cookie(keys[i].getString(), Caster.toString(sct.get(keys[i])));
081                    }
082                    return cookies;
083            }
084    }