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