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.httpclient3;
020
021import java.io.IOException;
022import java.io.OutputStream;
023
024import lucee.commons.io.res.Resource;
025import lucee.commons.lang.StringUtil;
026
027import org.apache.commons.httpclient.methods.multipart.FilePart;
028import org.apache.commons.httpclient.util.EncodingUtil;
029
030public class ResourcePart extends FilePart {
031        protected static final String FILE_NAME = "; filename=";
032
033    /** Attachment's file name as a byte array */
034    private static final byte[] FILE_NAME_BYTES = EncodingUtil.getAsciiBytes(FILE_NAME);
035    
036        private Resource resource;
037
038        private String headerCharset;
039
040        /*public ResourcePart(String name, ResourcePartSource partSource, String contentType, String charset) {
041                super(name, partSource, contentType, charset==null?"":charset);
042                this.resource=partSource.getResource();
043        }*/
044        
045        public ResourcePart(String name, ResourcePartSource partSource, String contentType, String headerCharset) {
046                super(name, partSource, contentType, "");
047                this.resource=partSource.getResource();
048                this.headerCharset=headerCharset;
049        }
050
051        /**
052         * @return the resource
053         */
054        public Resource getResource() {
055                return resource;
056        }
057
058        @Override
059        public String getCharSet() {
060                String cs = super.getCharSet();
061                if(StringUtil.isEmpty(cs)) return null;
062                return cs;
063        }
064        
065
066    @Override
067        protected void sendDispositionHeader(OutputStream out)  throws IOException {
068                sendDispositionHeader(getName(),getSource().getFileName(),headerCharset,out);
069        }
070        
071        
072    public static void sendDispositionHeader(String name,String filename, String headerCharset, OutputStream out)  throws IOException {
073        out.write(CONTENT_DISPOSITION_BYTES);
074        out.write(QUOTE_BYTES);
075        if(StringUtil.isAscii(name))
076                out.write(EncodingUtil.getAsciiBytes(name));
077        else
078                out.write(name.getBytes(headerCharset));
079        out.write(QUOTE_BYTES);
080
081        if (filename != null) {
082                out.write(FILE_NAME_BYTES);
083            out.write(QUOTE_BYTES);
084            if(StringUtil.isAscii(filename))
085                out.write(EncodingUtil.getAsciiBytes(filename));
086            else
087                out.write(filename.getBytes(headerCharset));
088            out.write(QUOTE_BYTES);
089        }
090    }
091
092        
093
094}