1 16 package org.apache.cocoon.mail.datasource; 17 18 import java.io.IOException ; 19 import java.io.InputStream ; 20 import java.io.OutputStream ; 21 22 import javax.activation.DataSource ; 23 24 import org.apache.excalibur.source.Source; 25 26 37 public class SourceDataSource implements DataSource { 38 private Source src; 39 private String contentType; 40 private String name; 41 42 46 public SourceDataSource(Source src) { 47 this(src, null, null); 48 } 49 50 54 public SourceDataSource(Source src, String type, String name) { 55 this.src = src; 56 this.contentType = type; 57 this.name = name; 58 if (isNullOrEmpty(this.name)) this.name = null; 59 if (isNullOrEmpty(this.contentType)) this.contentType = null; 60 } 61 62 67 private boolean isNullOrEmpty(String str) { 68 return (str == null || "".equals(str) || "null".equals(str)); 69 } 70 71 79 public String getContentType() { 80 if (this.contentType != null) { 81 return this.contentType; 82 } 83 String mimeType = src.getMimeType(); 84 if (isNullOrEmpty(mimeType)) { 85 mimeType = "application/octet-stream"; 86 } 87 return mimeType; 88 } 89 90 97 public InputStream getInputStream() throws IOException { 98 return src.getInputStream(); 99 } 100 101 109 public String getName() { 110 if (this.name != null){ 111 return this.name; 112 } 113 String name = src.getURI(); 114 name = name.substring(name.lastIndexOf('/') + 1); 115 return ("".equals(name)? "attachment" : name); 116 } 117 118 123 public OutputStream getOutputStream() throws IOException { 124 throw new IOException ("no data sink available"); 125 } 126 } 127 | Popular Tags |