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