001 package railo.commons.net.http; 002 003 import java.io.IOException; 004 import java.io.InputStream; 005 006 import railo.print; 007 import railo.commons.io.IOUtil; 008 import railo.commons.io.res.ContentType; 009 import railo.commons.io.res.ContentTypeImpl; 010 import railo.commons.lang.StringUtil; 011 import railo.commons.net.HTTPUtil; 012 import railo.runtime.PageContext; 013 import railo.runtime.engine.ThreadLocalPageContext; 014 import railo.runtime.op.Caster; 015 016 public abstract class HTTPResponseSupport implements HTTPResponse { 017 018 019 020 @Override 021 public final long getContentLength() throws IOException { 022 Header ct = getLastHeaderIgnoreCase("Content-Length"); 023 if(ct!=null) return Caster.toLongValue(ct.getValue(),-1); 024 025 InputStream is=null; 026 long length=0; 027 try{ 028 is=getContentAsStream(); 029 030 if ( is == null ) 031 return 0; 032 033 byte[] buffer = new byte[1024]; 034 int len; 035 while((len = is.read(buffer)) !=-1){ 036 length+=len; 037 } 038 return length; 039 } 040 finally { 041 IOUtil.closeEL(is); 042 } 043 } 044 045 @Override 046 public final ContentType getContentType() { 047 Header header = getLastHeaderIgnoreCase("Content-Type"); 048 if(header==null) return null; 049 050 String[] mimeCharset = HTTPUtil.splitMimeTypeAndCharset(header.getValue(), null); 051 if(mimeCharset==null) return null; 052 053 String[] typeSub = HTTPUtil.splitTypeAndSubType(mimeCharset[0]); 054 return new ContentTypeImpl(typeSub[0],typeSub[1],mimeCharset[1]); 055 } 056 057 @Override 058 public final String getCharset() { 059 ContentType ct = getContentType(); 060 String charset=null; 061 if(ct!=null)charset=ct.getCharset(); 062 if(!StringUtil.isEmpty(charset)) return charset; 063 064 PageContext pc = ThreadLocalPageContext.get(); 065 if(pc!=null) return pc.getConfig().getWebCharset(); 066 return "ISO-8859-1"; 067 } 068 069 }