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 javax.inject.Inject;
42
43 import org.apache.commons.lang.StringUtils;
44
45
46
47
48
49
50
51 public class MailAttachment {
52
53 public static final String DISPOSITION_INLINE = "inline";
54
55 public static final String DISPOSITION_NORMAL = "normal";
56
57 public static final String DISPOSITION_ATTACHMENT = "attachment";
58
59 public static final String MIME_RELATED = "related";
60
61 public static final String MIME_MIXED = "mixed";
62
63 private static final String FILE_URL_PREFIX = "file:///";
64
65 private String description;
66
67 private String disposition;
68
69 private String name;
70
71 private URL url;
72
73 private String mimeMultipart;
74
75 private boolean enabled;
76
77 @Inject
78 public MailAttachment() {
79
80 }
81
82 public MailAttachment(File file, String name, String description, String disposition) {
83 this.setPath(file.getAbsolutePath());
84 this.name = name;
85 this.description = description;
86 this.disposition = disposition;
87 this.mimeMultipart = MIME_MIXED;
88 }
89
90 public MailAttachment(URL url, String _name, String _description, String _disposition) {
91 this.url = url;
92 this.name = _name;
93 this.description = _description;
94 this.disposition = _disposition;
95 this.mimeMultipart = MIME_MIXED;
96 }
97
98 public MailAttachment(URL _url, String name, String mimeMultipart) {
99 this.url = _url;
100 this.name = name;
101 this.disposition = DISPOSITION_INLINE;
102 this.description = StringUtils.EMPTY;
103 this.mimeMultipart = mimeMultipart;
104 }
105
106 public MailAttachment(URL _url, String name) {
107 this.url = _url;
108 this.name = name;
109 this.disposition = DISPOSITION_INLINE;
110 this.description = StringUtils.EMPTY;
111 this.mimeMultipart = MIME_MIXED;
112 }
113
114 public java.lang.String getDescription() {
115 return this.description;
116 }
117
118 public java.lang.String getDisposition() {
119 if(StringUtils.isEmpty(disposition)){
120 return DISPOSITION_INLINE;
121 }
122 return this.disposition;
123 }
124
125 public java.lang.String getName() {
126 return this.name;
127 }
128
129 public java.lang.String getPath() {
130 return this.url.getFile();
131 }
132
133 public java.net.URL getUrl() {
134 if (this.url.getProtocol().startsWith("file")) {
135 try {
136 return new URL( this.url.toExternalForm());
137 }
138 catch (Exception e) {
139 return null;
140 }
141 }
142
143 return this.url;
144
145 }
146
147 public java.io.File getFile() {
148 return new File(this.url.getFile());
149 }
150
151 public void setDescription(java.lang.String desc) {
152 this.description = desc;
153 }
154
155 public void setDisposition(java.lang.String aDisposition) {
156 this.disposition = aDisposition;
157 }
158
159 public void setName(java.lang.String aName) {
160 this.name = aName;
161 }
162
163 public void setPath(java.lang.String aPath) {
164 try {
165 this.url = new URL(FILE_URL_PREFIX + StringUtils.removeStart(aPath, "/"));
166 }
167 catch (Exception e) {
168 e.printStackTrace();
169 this.url = null;
170 }
171 }
172
173 public void setUrl(java.net.URL aUrl) {
174 this.url = aUrl;
175 }
176
177 public String getContentType() {
178 return MIMEMapping.getMIMEType(StringUtils.substringAfterLast(this.getPath(), "."));
179 }
180
181 public String getFileName() {
182 return new File(this.url.getFile()).getName();
183 }
184
185 public String getMimeMultipart() {
186 if(StringUtils.isEmpty(mimeMultipart)){
187 return MIME_RELATED;
188 }
189 return mimeMultipart;
190 }
191
192 public void setMimeMultipart(String mimeMultipart) {
193 this.mimeMultipart = mimeMultipart;
194 }
195
196 public boolean isEnabled() {
197 return enabled;
198 }
199
200 public void setEnabled(boolean enabled) {
201 this.enabled = enabled;
202 }
203 }