1 16 17 package org.apache.cocoon.mail.datasource; 18 19 import java.io.IOException ; 20 import java.io.InputStream ; 21 import java.io.OutputStream ; 22 23 import javax.activation.DataSource ; 24 25 import org.apache.cocoon.servlet.multipart.Part; 26 27 36 public class FilePartDataSource implements DataSource { 37 private Part part; 38 private String contentType = null; 39 private String name = null; 40 41 45 public FilePartDataSource(Part part) { 46 this(part,null,null); 47 } 48 49 53 public FilePartDataSource(Part part, String type, String name) { 54 this.part = part; 55 this.contentType = type; 56 this.name = name; 57 if (isNullOrEmpty(this.name)) this.name = null; 58 if (isNullOrEmpty(this.contentType)) this.contentType = null; 59 } 60 61 66 private boolean isNullOrEmpty(String str) { 67 return (str == null || "".equals(str) || "null".equals(str)); 68 } 69 70 76 public String getContentType() { 77 if (this.contentType != null) { 78 return this.contentType; 79 } 80 String mimeType = part.getMimeType(); 81 if (isNullOrEmpty(mimeType)) { 82 mimeType = "application/octet-stream"; 83 } 84 return mimeType; 85 } 86 87 93 public InputStream getInputStream() throws IOException { 94 InputStream inp; 95 try { 96 inp = part.getInputStream(); 97 } catch (Exception e) { 98 throw new IOException (e.getMessage()); 99 } 100 return inp; 101 } 102 103 108 public String getName() { 109 String name = (this.name != null ? this.name : part.getFileName()); 110 if (isNullOrEmpty(name)) name="attachment"; 111 return name; 112 } 113 114 118 public OutputStream getOutputStream() throws IOException { 119 throw new IOException ("no data sink available"); 120 } 121 } 122 | Popular Tags |