001 package railo.runtime.functions.file; 002 003 import java.io.IOException; 004 import java.util.Date; 005 import java.util.Iterator; 006 007 import railo.commons.io.res.Resource; 008 import railo.commons.lang.StringUtil; 009 import railo.runtime.PageContext; 010 import railo.runtime.dump.DumpData; 011 import railo.runtime.dump.DumpProperties; 012 import railo.runtime.exp.ExpressionException; 013 import railo.runtime.exp.PageException; 014 import railo.runtime.exp.PageRuntimeException; 015 import railo.runtime.type.Collection; 016 import railo.runtime.type.Struct; 017 import railo.runtime.type.StructImpl; 018 import railo.runtime.type.dt.DateTime; 019 import railo.runtime.type.dt.DateTimeImpl; 020 import railo.runtime.type.util.KeyConstants; 021 import railo.runtime.type.util.StructSupport; 022 023 public abstract class FileStreamWrapper extends StructSupport implements Struct { 024 025 public static final String STATE_OPEN = "open"; 026 public static final String STATE_CLOSE = "close"; 027 028 protected Resource res; 029 private String status=STATE_OPEN; 030 private Struct info; 031 private long lastModifed; 032 private long length; 033 034 035 public FileStreamWrapper(Resource res) { 036 this.res=res; 037 this.lastModifed=System.currentTimeMillis(); 038 this.length=res.length(); 039 } 040 041 public final String getFilename() { 042 return res.getName(); 043 } 044 045 public final String getLabel(){ 046 return StringUtil.ucFirst(res.getResourceProvider().getScheme())+": "+getFilename(); 047 } 048 049 050 public final String getFilepath() { 051 return res.getAbsolutePath(); 052 } 053 054 055 public final String getStatus() { 056 return status; 057 } 058 059 public final void setStatus(String status) { 060 this.status=status; 061 } 062 063 public final Date getLastmodified() { 064 return new DateTimeImpl(lastModifed,false); 065 } 066 067 public Object getMetadata() { 068 return info(); 069 } 070 071 public Struct info() { 072 if(info==null) { 073 info=new StructImpl(); 074 info.setEL(KeyConstants._mode, getMode()); 075 info.setEL(KeyConstants._name, res.getName()); 076 info.setEL(KeyConstants._path, res.getParent()); 077 info.setEL(KeyConstants._status, getStatus()); 078 info.setEL(KeyConstants._size, getSize()+" bytes"); 079 info.setEL(KeyConstants._lastmodified, getLastmodified()); 080 } 081 082 return info; 083 } 084 085 public boolean isEndOfFile() { 086 return false; 087 } 088 089 public long getSize() { 090 return length; 091 } 092 093 094 public void write(Object obj) throws IOException { 095 throw notSupported("write"); 096 } 097 098 public String readLine() throws IOException { 099 throw notSupported("readLine"); 100 } 101 102 public Object read(int len) throws IOException{ 103 throw notSupported("read"); 104 } 105 106 public abstract String getMode(); 107 public abstract void close() throws IOException; 108 109 private IOException notSupported(String method) { 110 return new IOException(method+" can't be called when the file is opened in ["+getMode()+"] mode"); 111 } 112 113 public Resource getResource() { 114 return res; 115 } 116 117 @Override 118 public String toString() { 119 return res.getAbsolutePath(); 120 } 121 122 @Override 123 public void clear() { 124 throw new RuntimeException("can't clear struct, struct is readonly"); 125 126 } 127 128 @Override 129 public boolean containsKey(Key key) { 130 return info().containsKey(key); 131 } 132 133 @Override 134 public Collection duplicate(boolean deepCopy) { 135 throw new RuntimeException("can't duplicate File Object, Object depends on File Stream"); 136 } 137 138 139 @Override 140 public Object get(Key key) throws PageException { 141 return info().get(key); 142 } 143 144 @Override 145 public Object get(Key key, Object defaultValue) { 146 return info().get(key, defaultValue); 147 } 148 149 @Override 150 public Key[] keys() { 151 return info().keys(); 152 } 153 154 public Object remove(Key key) throws PageException { 155 throw new PageRuntimeException("can't remove key ["+key.getString()+"] from struct, struct is readonly"); 156 } 157 158 @Override 159 public Object removeEL(Key key) { 160 throw new PageRuntimeException("can't remove key ["+key.getString()+"] from struct, struct is readonly"); 161 } 162 163 @Override 164 public Object set(Key key, Object value) throws PageException { 165 throw new ExpressionException("can't set key ["+key.getString()+"] to struct, struct is readonly"); 166 } 167 168 @Override 169 public Object setEL(Key key, Object value) { 170 throw new PageRuntimeException("can't set key ["+key.getString()+"] to struct, struct is readonly"); 171 } 172 173 @Override 174 public int size() { 175 return info().size(); 176 } 177 178 @Override 179 public DumpData toDumpData(PageContext pageContext, int maxlevel, DumpProperties dp) { 180 return info().toDumpData(pageContext, maxlevel,dp); 181 } 182 183 @Override 184 public Iterator<Collection.Key> keyIterator() { 185 return info().keyIterator(); 186 } 187 188 @Override 189 public Iterator<String> keysAsStringIterator() { 190 return info().keysAsStringIterator(); 191 } 192 193 @Override 194 public Iterator<Entry<Key, Object>> entryIterator() { 195 return info().entryIterator(); 196 } 197 198 @Override 199 public Iterator<Object> valueIterator() { 200 return info().valueIterator(); 201 } 202 203 @Override 204 public boolean castToBooleanValue() throws PageException { 205 return info().castToBooleanValue(); 206 } 207 208 @Override 209 public Boolean castToBoolean(Boolean defaultValue) { 210 return info().castToBoolean(defaultValue); 211 } 212 213 @Override 214 public DateTime castToDateTime() throws PageException { 215 return info().castToDateTime(); 216 } 217 218 @Override 219 public DateTime castToDateTime(DateTime defaultValue) { 220 return info().castToDateTime(defaultValue); 221 } 222 223 @Override 224 public double castToDoubleValue() throws PageException { 225 return info().castToDoubleValue(); 226 } 227 228 @Override 229 public double castToDoubleValue(double defaultValue) { 230 return info().castToDoubleValue(defaultValue); 231 } 232 233 @Override 234 public String castToString() throws PageException { 235 return info().castToString(); 236 } 237 @Override 238 public String castToString(String defaultValue) { 239 return info().castToString(defaultValue); 240 } 241 242 @Override 243 public int compareTo(String str) throws PageException { 244 return info().compareTo(str); 245 } 246 247 @Override 248 public int compareTo(boolean b) throws PageException { 249 return info().compareTo(b); 250 } 251 252 @Override 253 public int compareTo(double d) throws PageException { 254 return info().compareTo(d); 255 } 256 257 @Override 258 public int compareTo(DateTime dt) throws PageException { 259 return info.compareTo(dt); 260 } 261 262 @Override 263 public boolean containsValue(Object value) { 264 return info().containsValue(value); 265 } 266 267 @Override 268 public java.util.Collection values() { 269 return info().values(); 270 } 271 272 public abstract void skip(int len) throws PageException; 273 274 public abstract void seek(long pos) throws PageException; 275 }