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 **/ 019/** 020 * Implements the CFML Function getpagecontext 021 */ 022package lucee.runtime.functions.other; 023 024import java.util.ArrayList; 025import java.util.Iterator; 026import java.util.List; 027import java.util.Map.Entry; 028 029import javax.servlet.http.Cookie; 030 031import lucee.commons.io.DevNullOutputStream; 032import lucee.commons.lang.Pair; 033import lucee.runtime.PageContext; 034import lucee.runtime.exp.ApplicationException; 035import lucee.runtime.exp.PageException; 036import lucee.runtime.ext.function.Function; 037import lucee.runtime.net.http.ReqRspUtil; 038import lucee.runtime.op.Caster; 039import lucee.runtime.thread.ThreadUtil; 040import lucee.runtime.type.Collection.Key; 041import lucee.runtime.type.Struct; 042import lucee.runtime.type.StructImpl; 043import lucee.runtime.type.util.CollectionUtil; 044 045public final class CreatePageContext implements Function { 046 047 048 049 public static Object call(PageContext pc, String serverName, String scriptName) throws PageException { 050 return call(pc,serverName,scriptName,"",new StructImpl(),new StructImpl(),new StructImpl(),new StructImpl()); 051 } 052 053 public static Object call(PageContext pc, String serverName, String scriptName,String queryString) throws PageException { 054 return call(pc,serverName,scriptName,queryString,new StructImpl(),new StructImpl(),new StructImpl(),new StructImpl()); 055 } 056 057 public static Object call(PageContext pc, String serverName, String scriptName,String queryString, Struct cookies) throws PageException { 058 return call(pc,serverName,scriptName,queryString,cookies,new StructImpl(),new StructImpl(),new StructImpl()); 059 } 060 061 public static Object call(PageContext pc, String serverName, String scriptName,String queryString, Struct cookies, Struct headers) throws PageException { 062 return call(pc,serverName,scriptName,queryString,cookies,headers,new StructImpl(),new StructImpl()); 063 } 064 065 public static Object call(PageContext pc, String serverName, String scriptName,String queryString, Struct cookies, Struct headers, Struct parameters) throws PageException { 066 return call(pc,serverName,scriptName,queryString,cookies,headers,parameters,new StructImpl()); 067 } 068 069 public static Object call(PageContext pc, String serverName, String scriptName,String queryString, Struct cookies, Struct headers, Struct parameters, Struct attributes) throws PageException { 070 return ThreadUtil.createPageContext( 071 pc.getConfig(), 072 DevNullOutputStream.DEV_NULL_OUTPUT_STREAM, 073 serverName, 074 scriptName, 075 queryString, 076 toCookies(cookies), 077 toPair(headers,true), 078 toPair(parameters,true), 079 castValuesToString(attributes)); 080 } 081 082 private static Struct castValuesToString(Struct sct) throws PageException { 083 Key[] keys = CollectionUtil.keys(sct); 084 for(int i=0;i<keys.length;i++){ 085 sct.set(keys[i], Caster.toString(sct.get(keys[i]))); 086 } 087 return sct; 088 } 089 090 private static Pair<String,Object>[] toPair(Struct sct, boolean doStringCast) throws PageException { 091 Iterator<Entry<Key, Object>> it = sct.entryIterator(); 092 Entry<Key, Object> e; 093 Object value; 094 List<Pair<String,Object>> pairs=new ArrayList<Pair<String,Object>>(); 095 while(it.hasNext()){ 096 e = it.next(); 097 value= e.getValue(); 098 if(doStringCast)value=Caster.toString(value); 099 pairs.add(new Pair<String,Object>(e.getKey().getString(),value)); 100 } 101 return pairs.toArray(new Pair[pairs.size()]); 102 } 103 104 private static Cookie[] toCookies(Struct sct) throws PageException { 105 Iterator<Entry<Key, Object>> it = sct.entryIterator(); 106 Entry<Key, Object> e; 107 List<Cookie> cookies=new ArrayList<Cookie>(); 108 Cookie c; 109 while(it.hasNext()){ 110 e = it.next(); 111 c=ReqRspUtil.toCookie(e.getKey().getString(), Caster.toString(e.getValue()),null); 112 if(c!=null)cookies.add(c); 113 else throw new ApplicationException("Cookie name ["+e.getKey().getString()+"] is invalid"); 114 } 115 return cookies.toArray(new Cookie[cookies.size()]); 116 } 117}