001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.commons.net.http;
020
021import java.io.IOException;
022import java.io.InputStream;
023
024import lucee.commons.io.IOUtil;
025import lucee.commons.io.res.ContentType;
026import lucee.commons.io.res.ContentTypeImpl;
027import lucee.commons.lang.StringUtil;
028import lucee.commons.net.HTTPUtil;
029import lucee.runtime.PageContext;
030import lucee.runtime.PageContextImpl;
031import lucee.runtime.engine.ThreadLocalPageContext;
032import lucee.runtime.op.Caster;
033
034public abstract class HTTPResponseSupport implements HTTPResponse {
035        
036
037
038        @Override
039        public final long getContentLength() throws IOException {
040                Header ct = getLastHeaderIgnoreCase("Content-Length");
041                if(ct!=null) return Caster.toLongValue(ct.getValue(),-1);
042                
043                InputStream is=null;
044                long length=0;
045                try{
046                        is=getContentAsStream();
047
048            if ( is == null )
049                return 0;
050
051                        byte[] buffer = new byte[1024];
052                    int len;
053                    while((len = is.read(buffer)) !=-1){
054                      length+=len;
055                    }
056                        return length;
057                }
058                finally {
059                        IOUtil.closeEL(is);
060                }
061        }
062
063        @Override
064        public final ContentType getContentType() {
065                Header header = getLastHeaderIgnoreCase("Content-Type");
066                if(header==null) return null;
067                
068                String[] mimeCharset = HTTPUtil.splitMimeTypeAndCharset(header.getValue(), null);
069                if(mimeCharset==null) return null;
070                
071                String[] typeSub = HTTPUtil.splitTypeAndSubType(mimeCharset[0]);
072                return new ContentTypeImpl(typeSub[0],typeSub[1],mimeCharset[1]);
073        }
074
075        @Override
076        public final String getCharset() {
077                ContentType ct = getContentType();
078                String charset=null;
079                if(ct!=null)charset=ct.getCharset();
080                if(!StringUtil.isEmpty(charset)) return charset;
081                
082                PageContext pc = ThreadLocalPageContext.get();
083                if(pc!=null) return ((PageContextImpl)pc).getWebCharset().name();
084                return "ISO-8859-1";
085        }
086
087}