001    package railo.runtime.functions.file;
002    
003    import railo.commons.io.res.Resource;
004    import railo.commons.io.res.util.ResourceUtil;
005    import railo.commons.lang.StringUtil;
006    import railo.runtime.PageContext;
007    import railo.runtime.exp.FunctionException;
008    import railo.runtime.exp.PageException;
009    
010    public class FileOpen {
011    
012            public static Object call(PageContext pc, String path) throws PageException {
013                    return call(pc, path,"read",pc.getConfig().getResourceCharset(),false);
014            }
015            public static Object call(PageContext pc, String path,String mode) throws PageException {
016                    return call(pc, path,mode,pc.getConfig().getResourceCharset(),false);
017            }
018    
019            public static Object call(PageContext pc, String path,String strMode, String charset) throws PageException {
020                    return call(pc, path,strMode,charset,false);
021            }
022            
023            public static Object call(PageContext pc, String path,String strMode, String charset,boolean seekable) throws PageException {
024                    
025                    strMode=strMode.trim().toLowerCase();
026                    if(StringUtil.isEmpty(charset,true))
027                            charset=pc.getConfig().getResourceCharset();
028                    //try {
029    
030                            if("read".equals(strMode)) {
031                                    return new FileStreamWrapperRead(check(pc,ResourceUtil.toResourceExisting(pc, path)),charset,seekable);
032                            }
033                            if("readbinary".equals(strMode)) {
034                                    return new FileStreamWrapperReadBinary(check(pc,ResourceUtil.toResourceExisting(pc, path)),seekable);
035                            }
036                            if("write".equals(strMode)) {
037                                    return new FileStreamWrapperWrite(check(pc,ResourceUtil.toResourceNotExisting(pc, path)),charset,false,seekable);
038                            }
039                            if("append".equals(strMode)) {
040                                    return new FileStreamWrapperWrite(check(pc,ResourceUtil.toResourceNotExisting(pc, path)),charset,true,seekable);
041                            }
042                            if("readwrite".equals(strMode)) {
043                                    return new FileStreamWrapperReadWrite(check(pc,ResourceUtil.toResourceNotExisting(pc, path)),charset,seekable);
044                            }
045                            
046                            
047                            throw new FunctionException(pc,"FileOpen",2,"mode","invalid value ["+strMode+"], valid values for argument mode are [read,readBinary,append,write,readwrite]");
048                    
049                    
050                    
051            }
052            private static Resource check(PageContext pc, Resource res) throws PageException {
053            pc.getConfig().getSecurityManager().checkFileLocation(res);
054                    return res;
055            }
056            
057    }