001 package railo.runtime.functions.other; 002 003 import java.io.ByteArrayInputStream; 004 import java.io.IOException; 005 import java.io.Serializable; 006 007 import org.apache.commons.io.output.ByteArrayOutputStream; 008 009 import railo.commons.io.IOUtil; 010 import railo.commons.io.res.Resource; 011 import railo.commons.io.res.util.ResourceUtil; 012 import railo.commons.lang.StringUtil; 013 import railo.runtime.PageContext; 014 import railo.runtime.converter.JavaConverter; 015 import railo.runtime.exp.ApplicationException; 016 import railo.runtime.exp.PageException; 017 import railo.runtime.op.Caster; 018 019 public class ObjectSave { 020 021 public synchronized static Object call(PageContext pc, Object input) throws PageException { 022 return call(pc,input,null); 023 } 024 025 public synchronized static Object call(PageContext pc, Object input,String filepath) throws PageException { 026 if(!(input instanceof Serializable)) 027 throw new ApplicationException("can only serialize object from type Serializable"); 028 029 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 030 try { 031 JavaConverter.serialize((Serializable)input,baos); 032 033 034 byte[] barr = baos.toByteArray(); 035 036 // store to file 037 if(!StringUtil.isEmpty(filepath,true)) { 038 Resource res = ResourceUtil.toResourceNotExisting(pc, filepath); 039 pc.getConfig().getSecurityManager().checkFileLocation(res); 040 IOUtil.copy(new ByteArrayInputStream(barr),res,true); 041 } 042 return barr; 043 044 } 045 catch (IOException e) { 046 throw Caster.toPageException(e); 047 } 048 } 049 }