001 package railo.commons.net.http.httpclient3; 002 003 import java.io.IOException; 004 import java.io.OutputStream; 005 006 import org.apache.commons.httpclient.methods.multipart.FilePart; 007 import org.apache.commons.httpclient.util.EncodingUtil; 008 009 import railo.commons.io.res.Resource; 010 import railo.commons.lang.StringUtil; 011 012 public class ResourcePart extends FilePart { 013 protected static final String FILE_NAME = "; filename="; 014 015 /** Attachment's file name as a byte array */ 016 private static final byte[] FILE_NAME_BYTES = EncodingUtil.getAsciiBytes(FILE_NAME); 017 018 private Resource resource; 019 020 private String headerCharset; 021 022 /*public ResourcePart(String name, ResourcePartSource partSource, String contentType, String charset) { 023 super(name, partSource, contentType, charset==null?"":charset); 024 this.resource=partSource.getResource(); 025 }*/ 026 027 public ResourcePart(String name, ResourcePartSource partSource, String contentType, String headerCharset) { 028 super(name, partSource, contentType, ""); 029 this.resource=partSource.getResource(); 030 this.headerCharset=headerCharset; 031 } 032 033 /** 034 * @return the resource 035 */ 036 public Resource getResource() { 037 return resource; 038 } 039 040 @Override 041 public String getCharSet() { 042 String cs = super.getCharSet(); 043 if(StringUtil.isEmpty(cs)) return null; 044 return cs; 045 } 046 047 048 @Override 049 protected void sendDispositionHeader(OutputStream out) throws IOException { 050 sendDispositionHeader(getName(),getSource().getFileName(),headerCharset,out); 051 } 052 053 054 public static void sendDispositionHeader(String name,String filename, String headerCharset, OutputStream out) throws IOException { 055 out.write(CONTENT_DISPOSITION_BYTES); 056 out.write(QUOTE_BYTES); 057 if(StringUtil.isAscii(name)) 058 out.write(EncodingUtil.getAsciiBytes(name)); 059 else 060 out.write(name.getBytes(headerCharset)); 061 out.write(QUOTE_BYTES); 062 063 if (filename != null) { 064 out.write(FILE_NAME_BYTES); 065 out.write(QUOTE_BYTES); 066 if(StringUtil.isAscii(filename)) 067 out.write(EncodingUtil.getAsciiBytes(filename)); 068 else 069 out.write(filename.getBytes(headerCharset)); 070 out.write(QUOTE_BYTES); 071 } 072 } 073 074 075 076 }