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 public class MailAttachment {
49
50 public static final String DISPOSITION_INLINE = "inline";
51
52 public static final String DISPOSITION_NORMAL = "normal";
53
54 public static final String DISPOSITION_ATTACHMENT = "attachment";
55
56 private static final String FILE_URL_PREFIX = "file://";
57
58 private String description;
59
60 private String disposition;
61
62 private String name;
63
64 private URL url;
65
66 public MailAttachment() {
67
68 }
69
70 public MailAttachment(File file, String name, String description, String disposition) {
71 this.setPath(file.getAbsolutePath());
72 this.name = name;
73 this.description = description;
74 this.disposition = disposition;
75 }
76
77 public MailAttachment(URL url, String _name, String _description, String _disposition) {
78 this.url = url;
79 this.name = _name;
80 this.description = _description;
81 this.disposition = _disposition;
82 }
83
84 public MailAttachment(URL _url, String name) {
85 this.url = _url;
86 this.name = name;
87 this.disposition = DISPOSITION_INLINE;
88 this.description = StringUtils.EMPTY;
89 }
90
91 public java.lang.String getDescription() {
92 return this.description;
93 }
94
95 public java.lang.String getDisposition() {
96 if(StringUtils.isEmpty(disposition)){
97 return DISPOSITION_INLINE;
98 }
99 return this.disposition;
100 }
101
102 public java.lang.String getName() {
103 return this.name;
104 }
105
106 public java.lang.String getPath() {
107 return this.url.getFile();
108 }
109
110 public java.net.URL getUrl() {
111 if (this.url.getProtocol().startsWith("file")) {
112 try {
113 return new URL( this.url.toExternalForm());
114 }
115 catch (Exception e) {
116 return null;
117 }
118 }
119
120 return this.url;
121
122 }
123
124 public java.io.File getFile() {
125 return new File(this.url.getFile());
126 }
127
128 public void setDescription(java.lang.String desc) {
129 this.description = desc;
130 }
131
132 public void setDisposition(java.lang.String aDisposition) {
133 this.disposition = aDisposition;
134 }
135
136 public void setName(java.lang.String aName) {
137 this.name = aName;
138 }
139
140 public void setPath(java.lang.String aPath) {
141 try {
142 this.url = new URL(FILE_URL_PREFIX + aPath);
143 }
144 catch (Exception e) {
145 e.printStackTrace();
146 this.url = null;
147 }
148 }
149
150 public void setUrl(java.net.URL aUrl) {
151 this.url = aUrl;
152 }
153
154 public String getContentType() {
155 return MIMEMapping.getMIMEType(StringUtils.substringAfterLast(this.getPath(), "."));
156 }
157
158 public String getFileName() {
159 return new File(this.url.getFile()).getName();
160 }
161
162 }