001 package railo.commons.net; 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 /** 041 * @see org.apache.commons.httpclient.methods.multipart.PartBase#getCharSet() 042 */ 043 public String getCharSet() { 044 String cs = super.getCharSet(); 045 if(StringUtil.isEmpty(cs)) return null; 046 return cs; 047 } 048 049 050 /** 051 * Write the disposition header to the output stream 052 * @param out The output stream 053 * @throws IOException If an IO problem occurs 054 * @see Part#sendDispositionHeader(OutputStream) 055 */ 056 protected void sendDispositionHeader(OutputStream out) throws IOException { 057 sendDispositionHeader(getName(),getSource().getFileName(),headerCharset,out); 058 } 059 060 061 public static void sendDispositionHeader(String name,String filename, String headerCharset, OutputStream out) throws IOException { 062 out.write(CONTENT_DISPOSITION_BYTES); 063 out.write(QUOTE_BYTES); 064 if(StringUtil.isAscci(name)) 065 out.write(EncodingUtil.getAsciiBytes(name)); 066 else 067 out.write(name.getBytes(headerCharset)); 068 out.write(QUOTE_BYTES); 069 070 if (filename != null) { 071 out.write(FILE_NAME_BYTES); 072 out.write(QUOTE_BYTES); 073 if(StringUtil.isAscci(filename)) 074 out.write(EncodingUtil.getAsciiBytes(filename)); 075 else 076 out.write(filename.getBytes(headerCharset)); 077 out.write(QUOTE_BYTES); 078 } 079 } 080 081 082 083 }