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.io.res.type.s3;
020
021import java.io.IOException;
022import java.io.OutputStream;
023import java.net.SocketException;
024
025import lucee.commons.io.TemporaryStream;
026import lucee.commons.lang.ExceptionUtil;
027import lucee.commons.lang.StringUtil;
028import lucee.commons.net.http.httpclient3.HTTPEngine3Impl;
029
030public final class S3ResourceOutputStream extends OutputStream {
031        
032        private final S3 s3;
033        
034        private final String contentType="application";
035        private final String bucketName;
036        private final String objectName;
037        private final int acl;
038
039        private TemporaryStream ts;
040        
041        public S3ResourceOutputStream(S3 s3,String bucketName,String objectName,int acl) {
042                this.s3=s3;
043                this.bucketName=bucketName;
044                this.objectName=objectName;
045                this.acl=acl;
046                
047                ts = new TemporaryStream();
048        }
049        
050        @Override
051        public void close() throws IOException {
052                ts.close();
053                
054                //InputStream is = ts.getInputStream();
055                try {
056                        s3.put(bucketName, objectName, acl, HTTPEngine3Impl.getTemporaryStreamEntity(ts,contentType));
057                } 
058
059                catch (SocketException se) {
060                        String msg = StringUtil.emptyIfNull(se.getMessage());
061                        if(StringUtil.indexOfIgnoreCase(msg, "Socket closed")==-1)
062                                throw se;
063                }
064                catch (Exception e) {
065                        throw ExceptionUtil.toIOException(e);
066                }
067        }
068
069        @Override
070        public void flush() throws IOException {
071                ts.flush();
072        }
073
074        @Override
075        public void write(byte[] b, int off, int len) throws IOException {
076                ts.write(b, off, len);
077        }
078
079        @Override
080        public void write(byte[] b) throws IOException {
081                ts.write(b);
082        }
083
084        @Override
085        public void write(int b) throws IOException {
086                ts.write(b);
087        }
088}