1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.module.mail.templates;
35
36 import info.magnolia.cms.beans.config.MIMEMapping;
37
38 import java.io.File;
39 import java.net.URL;
40
41 import org.apache.commons.lang.StringUtils;
42
43
44
45
46
47
48
49 public class MailAttachment {
50
51 public static final String DISPOSITION_INLINE = "inline";
52
53 public static final String DISPOSITION_NORMAL = "normal";
54
55 public static final String DISPOSITION_ATTACHMENT = "attachment";
56
57 private static final String FILE_URL_PREFIX = "file://";
58
59 private String description;
60
61 private String disposition;
62
63 private String name;
64
65 private URL url;
66
67 public MailAttachment() {
68
69 }
70
71 public MailAttachment(File file, String name, String description, String disposition) {
72 this.setPath(file.getAbsolutePath());
73 this.name = name;
74 this.description = description;
75 this.disposition = disposition;
76 }
77
78 public MailAttachment(URL url, String _name, String _description, String _disposition) {
79 this.url = url;
80 this.name = _name;
81 this.description = _description;
82 this.disposition = _disposition;
83 }
84
85 public MailAttachment(URL _url, String name) {
86 this.url = _url;
87 this.name = name;
88 this.disposition = DISPOSITION_INLINE;
89 this.description = StringUtils.EMPTY;
90 }
91
92 public java.lang.String getDescription() {
93 return this.description;
94 }
95
96 public java.lang.String getDisposition() {
97 if(StringUtils.isEmpty(disposition)){
98 return DISPOSITION_INLINE;
99 }
100 return this.disposition;
101 }
102
103 public java.lang.String getName() {
104 return this.name;
105 }
106
107 public java.lang.String getPath() {
108 return this.url.getFile();
109 }
110
111 public java.net.URL getUrl() {
112 if (this.url.getProtocol().startsWith("file")) {
113 try {
114 return new URL( this.url.toExternalForm());
115 }
116 catch (Exception e) {
117 return null;
118 }
119 }
120
121 return this.url;
122
123 }
124
125 public java.io.File getFile() {
126 return new File(this.url.getFile());
127 }
128
129 public void setDescription(java.lang.String desc) {
130 this.description = desc;
131 }
132
133 public void setDisposition(java.lang.String aDisposition) {
134 this.disposition = aDisposition;
135 }
136
137 public void setName(java.lang.String aName) {
138 this.name = aName;
139 }
140
141 public void setPath(java.lang.String aPath) {
142 try {
143 this.url = new URL(FILE_URL_PREFIX + aPath);
144 }
145 catch (Exception e) {
146 e.printStackTrace();
147 this.url = null;
148 }
149 }
150
151 public void setUrl(java.net.URL aUrl) {
152 this.url = aUrl;
153 }
154
155 public String getContentType() {
156 return MIMEMapping.getMIMEType(StringUtils.substringAfterLast(this.getPath(), "."));
157 }
158
159 public String getFileName() {
160 return new File(this.url.getFile()).getName();
161 }
162
163 }