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 @Inject
76 public MailAttachment() {
77
78 }
79
80 public MailAttachment(File file, String name, String description, String disposition) {
81 this.setPath(file.getAbsolutePath());
82 this.name = name;
83 this.description = description;
84 this.disposition = disposition;
85 this.mimeMultipart = MIME_MIXED;
86 }
87
88 public MailAttachment(URL url, String _name, String _description, String _disposition) {
89 this.url = url;
90 this.name = _name;
91 this.description = _description;
92 this.disposition = _disposition;
93 this.mimeMultipart = MIME_MIXED;
94 }
95
96 public MailAttachment(URL _url, String name, String mimeMultipart) {
97 this.url = _url;
98 this.name = name;
99 this.disposition = DISPOSITION_INLINE;
100 this.description = StringUtils.EMPTY;
101 this.mimeMultipart = mimeMultipart;
102 }
103
104 public MailAttachment(URL _url, String name) {
105 this.url = _url;
106 this.name = name;
107 this.disposition = DISPOSITION_INLINE;
108 this.description = StringUtils.EMPTY;
109 this.mimeMultipart = MIME_MIXED;
110 }
111
112 public java.lang.String getDescription() {
113 return this.description;
114 }
115
116 public java.lang.String getDisposition() {
117 if(StringUtils.isEmpty(disposition)){
118 return DISPOSITION_INLINE;
119 }
120 return this.disposition;
121 }
122
123 public java.lang.String getName() {
124 return this.name;
125 }
126
127 public java.lang.String getPath() {
128 return this.url.getFile();
129 }
130
131 public java.net.URL getUrl() {
132 if (this.url.getProtocol().startsWith("file")) {
133 try {
134 return new URL( this.url.toExternalForm());
135 }
136 catch (Exception e) {
137 return null;
138 }
139 }
140
141 return this.url;
142
143 }
144
145 public java.io.File getFile() {
146 return new File(this.url.getFile());
147 }
148
149 public void setDescription(java.lang.String desc) {
150 this.description = desc;
151 }
152
153 public void setDisposition(java.lang.String aDisposition) {
154 this.disposition = aDisposition;
155 }
156
157 public void setName(java.lang.String aName) {
158 this.name = aName;
159 }
160
161 public void setPath(java.lang.String aPath) {
162 try {
163 this.url = new URL(FILE_URL_PREFIX + StringUtils.removeStart(aPath, "/"));
164 }
165 catch (Exception e) {
166 e.printStackTrace();
167 this.url = null;
168 }
169 }
170
171 public void setUrl(java.net.URL aUrl) {
172 this.url = aUrl;
173 }
174
175 public String getContentType() {
176 return MIMEMapping.getMIMEType(StringUtils.substringAfterLast(this.getPath(), "."));
177 }
178
179 public String getFileName() {
180 return new File(this.url.getFile()).getName();
181 }
182
183 public String getMimeMultipart() {
184 if(StringUtils.isEmpty(mimeMultipart)){
185 return MIME_RELATED;
186 }
187 return mimeMultipart;
188 }
189
190 public void setMimeMultipart(String mimeMultipart) {
191 this.mimeMultipart = mimeMultipart;
192 }
193 }