001 package railo.runtime.op; 002 003 import java.io.File; 004 import java.io.InputStream; 005 import java.io.OutputStream; 006 import java.io.Serializable; 007 import java.util.Iterator; 008 import java.util.Map; 009 import java.util.Map.Entry; 010 011 import javax.servlet.http.Cookie; 012 import javax.servlet.http.HttpServletRequest; 013 import javax.servlet.http.HttpServletResponse; 014 import javax.servlet.http.HttpSession; 015 016 import org.w3c.dom.Document; 017 import org.xml.sax.InputSource; 018 019 import railo.commons.date.DateTimeUtil; 020 import railo.commons.io.IOUtil; 021 import railo.commons.io.res.Resource; 022 import railo.commons.io.res.util.ResourceUtil; 023 import railo.commons.lang.Pair; 024 import railo.loader.engine.CFMLEngine; 025 import railo.runtime.CFMLFactoryImpl; 026 import railo.runtime.Component; 027 import railo.runtime.PageContext; 028 import railo.runtime.config.Config; 029 import railo.runtime.config.RemoteClient; 030 import railo.runtime.db.DatasourceConnection; 031 import railo.runtime.db.SQL; 032 import railo.runtime.engine.ThreadLocalPageContext; 033 import railo.runtime.exp.DatabaseException; 034 import railo.runtime.exp.ExpressionException; 035 import railo.runtime.exp.PageException; 036 import railo.runtime.functions.system.ContractPath; 037 import railo.runtime.net.http.HttpServletRequestDummy; 038 import railo.runtime.net.http.HttpServletResponseDummy; 039 import railo.runtime.spooler.ExecutionPlan; 040 import railo.runtime.spooler.SpoolerTask; 041 import railo.runtime.spooler.remote.RemoteClientTask; 042 import railo.runtime.text.xml.XMLUtil; 043 import railo.runtime.type.Array; 044 import railo.runtime.type.ArrayImpl; 045 import railo.runtime.type.Collection; 046 import railo.runtime.type.Collection.Key; 047 import railo.runtime.type.KeyImpl; 048 import railo.runtime.type.Query; 049 import railo.runtime.type.QueryImpl; 050 import railo.runtime.type.Struct; 051 import railo.runtime.type.StructImpl; 052 import railo.runtime.type.dt.Date; 053 import railo.runtime.type.dt.DateImpl; 054 import railo.runtime.type.dt.DateTime; 055 import railo.runtime.type.dt.DateTimeImpl; 056 import railo.runtime.type.dt.Time; 057 import railo.runtime.type.dt.TimeImpl; 058 import railo.runtime.type.dt.TimeSpan; 059 import railo.runtime.type.dt.TimeSpanImpl; 060 import railo.runtime.type.scope.ClusterEntry; 061 import railo.runtime.type.scope.ClusterEntryImpl; 062 import railo.runtime.type.util.ListUtil; 063 import railo.runtime.util.Creation; 064 065 /** 066 * implemention of the ctration object 067 */ 068 public final class CreationImpl implements Creation,Serializable { 069 070 private static CreationImpl singelton; 071 072 private CreationImpl(CFMLEngine engine) { 073 // !!! do not store engine Object, the engine is not serializable 074 } 075 076 /** 077 * @return singleton instance 078 */ 079 public static Creation getInstance(CFMLEngine engine) { 080 if(singelton==null)singelton=new CreationImpl(engine); 081 return singelton; 082 } 083 084 @Override 085 public Array createArray() { 086 return new ArrayImpl(); 087 } 088 089 @Override 090 public Array createArray(String list, String delimiter,boolean removeEmptyItem, boolean trim) { 091 if(removeEmptyItem)return ListUtil.listToArrayRemoveEmpty(list, delimiter); 092 if(trim)return ListUtil.listToArrayTrim(list, delimiter); 093 return ListUtil.listToArray(list, delimiter); 094 } 095 096 @Override 097 public Array createArray(int dimension) throws PageException { 098 return new ArrayImpl(dimension); 099 } 100 101 @Override 102 public Struct createStruct() { 103 return new StructImpl(); 104 } 105 106 @Override 107 public Struct createStruct(int type) { 108 return new StructImpl(type); 109 } 110 111 @Override 112 public Query createQuery(String[] columns, int rows, String name) { 113 return new QueryImpl(columns,rows,name); 114 } 115 116 @Override 117 public Query createQuery(Collection.Key[] columns, int rows, String name) throws DatabaseException { 118 return new QueryImpl(columns,rows,name); 119 } 120 121 @Override 122 public Query createQuery(DatasourceConnection dc, SQL sql, int maxrow, String name) throws PageException { 123 return new QueryImpl(ThreadLocalPageContext.get(),dc,sql,maxrow,-1,-1,name); 124 } 125 126 public Query createQuery(DatasourceConnection dc, SQL sql, int maxrow, int fetchsize, int timeout, String name) throws PageException { 127 return new QueryImpl(ThreadLocalPageContext.get(),dc,sql,maxrow,fetchsize,timeout,name); 128 } 129 130 @Override 131 public DateTime createDateTime(long time) { 132 return new DateTimeImpl(time,false); 133 } 134 135 @Override 136 public TimeSpan createTimeSpan(int day,int hour,int minute,int second) { 137 return new TimeSpanImpl(day,hour,minute,second); 138 } 139 140 @Override 141 public Date createDate(long time) { 142 return new DateImpl(time); 143 } 144 145 @Override 146 public Time createTime(long time) { 147 return new TimeImpl(time,false); 148 } 149 150 @Override 151 public DateTime createDateTime(int year, int month, int day, int hour, int minute, int second, int millis) throws ExpressionException { 152 return DateTimeUtil.getInstance().toDateTime(ThreadLocalPageContext.getTimeZone(),year,month,day,hour,minute,second,millis); 153 } 154 155 @Override 156 public Date createDate(int year, int month, int day) throws ExpressionException { 157 return new DateImpl(DateTimeUtil.getInstance().toDateTime(null,year,month,day, 0, 0, 0,0)); 158 } 159 160 @Override 161 public Time createTime(int hour, int minute, int second, int millis) { 162 return new TimeImpl( 163 DateTimeUtil.getInstance().toTime(null,1899,12,30,hour,minute,second,millis,0),false); 164 } 165 166 @Override 167 public Document createDocument() throws PageException { 168 try { 169 return XMLUtil.newDocument(); 170 } catch (Exception e) { 171 throw Caster.toPageException(e); 172 } 173 } 174 175 @Override 176 public Document createDocument(Resource res, boolean isHTML) throws PageException { 177 InputStream is=null; 178 try { 179 return XMLUtil.parse(new InputSource(is=res.getInputStream()),null,isHTML); 180 } catch (Exception e) { 181 throw Caster.toPageException(e); 182 } 183 finally { 184 IOUtil.closeEL(is); 185 } 186 } 187 188 @Override 189 public Document createDocument(String xml, boolean isHTML) throws PageException { 190 try { 191 return XMLUtil.parse(XMLUtil.toInputSource(null, xml),null,isHTML); 192 } catch (Exception e) { 193 throw Caster.toPageException(e); 194 } 195 } 196 197 @Override 198 public Document createDocument(InputStream is, boolean isHTML) throws PageException { 199 try { 200 return XMLUtil.parse(new InputSource(is),null,isHTML); 201 } catch (Exception e) { 202 throw Caster.toPageException(e); 203 } 204 } 205 206 @Override 207 public Key createKey(String key) { 208 return KeyImpl.init(key); 209 } 210 211 public SpoolerTask createRemoteClientTask(ExecutionPlan[] plans,RemoteClient remoteClient,Struct attrColl,String callerId, String type) { 212 return new RemoteClientTask(plans,remoteClient,attrColl,callerId, type); 213 } 214 215 public ClusterEntry createClusterEntry(Key key,Serializable value, int offset) { 216 return new ClusterEntryImpl(key,value,offset); 217 } 218 219 public Resource createResource(String path, boolean existing) throws PageException { 220 if(existing)return ResourceUtil.toResourceExisting(ThreadLocalPageContext.get(), path); 221 return ResourceUtil.toResourceNotExisting(ThreadLocalPageContext.get(), path); 222 } 223 224 public HttpServletRequest createHttpServletRequest(File contextRoot,String serverName, String scriptName,String queryString, 225 Cookie[] cookies, Map<String,Object> headers, Map<String, String> parameters, Map<String,Object> attributes, HttpSession session) { 226 227 // header 228 Pair<String,Object>[] _headers=new Pair[headers.size()]; 229 { 230 int index=0; 231 Iterator<Entry<String, Object>> it = headers.entrySet().iterator(); 232 Entry<String, Object> entry; 233 while(it.hasNext()){ 234 entry = it.next(); 235 _headers[index++]=new Pair<String,Object>(entry.getKey(), entry.getValue()); 236 } 237 } 238 // parameters 239 Pair<String,Object>[] _parameters=new Pair[headers.size()]; 240 { 241 int index=0; 242 Iterator<Entry<String, String>> it = parameters.entrySet().iterator(); 243 Entry<String, String> entry; 244 while(it.hasNext()){ 245 entry = it.next(); 246 _parameters[index++]=new Pair<String,Object>(entry.getKey(), entry.getValue()); 247 } 248 } 249 250 return new HttpServletRequestDummy(ResourceUtil.toResource(contextRoot), serverName, scriptName, queryString, cookies, 251 _headers, _parameters, Caster.toStruct(attributes,null), session); 252 } 253 254 public HttpServletResponse createHttpServletResponse(OutputStream io) { 255 return new HttpServletResponseDummy(io); 256 } 257 258 @Override 259 public PageContext createPageContext(HttpServletRequest req, HttpServletResponse rsp, OutputStream out) { 260 Config config = ThreadLocalPageContext.getConfig(); 261 return (PageContext) ((CFMLFactoryImpl)config.getFactory()).getPageContext(config.getFactory().getServlet(), req, rsp, null, false, -1, false); 262 } 263 264 @Override 265 public Component createComponentFromName(PageContext pc, String fullName) throws PageException { 266 return pc.loadComponent(fullName); 267 } 268 269 @Override 270 public Component createComponentFromPath(PageContext pc, String path) throws PageException { 271 path=path.trim(); 272 String pathContracted=ContractPath.call(pc, path); 273 274 if(pathContracted.toLowerCase().endsWith(".cfc")) 275 pathContracted=pathContracted.substring(0,pathContracted.length()-4); 276 277 pathContracted=pathContracted 278 .replace(File.pathSeparatorChar, '.') 279 .replace('/', '.') 280 .replace('\\', '.'); 281 282 while(pathContracted.toLowerCase().startsWith(".")) 283 pathContracted=pathContracted.substring(1); 284 285 return createComponentFromName(pc, pathContracted); 286 } 287 288 289 }