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.httpclient4.entity;
020
021import java.io.IOException;
022import java.io.InputStream;
023import java.io.OutputStream;
024
025import lucee.commons.io.IOUtil;
026import lucee.commons.io.res.Resource;
027
028import org.apache.http.entity.AbstractHttpEntity;
029
030/**
031 * A RequestEntity that represents a Resource.
032 */
033public class ResourceHttpEntity extends AbstractHttpEntity implements Entity4 {
034
035    final Resource res;
036        private String strContentType;
037    
038    public ResourceHttpEntity(final Resource res, final String contentType) {
039        super();
040        this.res = res;
041        setContentType(contentType);
042        strContentType = contentType;
043    }
044   
045    @Override
046    public long getContentLength() {
047        return this.res.length();
048    }
049
050    @Override
051    public boolean isRepeatable() {
052        return true;
053    }
054    
055    @Override
056    public InputStream getContent() throws IOException {
057        return res.getInputStream();
058    }
059
060    @Override
061    public void writeTo(final OutputStream out) throws IOException {
062       IOUtil.copy(res.getInputStream(), out,true,false);
063    }
064
065        @Override
066        public boolean isStreaming() {
067                return false;
068        }
069
070        @Override
071        public long contentLength() {
072                return getContentLength();
073        }
074
075        @Override
076        public String contentType() {
077                return strContentType;
078        }
079}