View Javadoc

1   /**
2    * This file Copyright (c) 2003-2010 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
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   * Date: Apr 1, 2006 Time: 8:38:07 PM
46   * @author <a href="mailto:niko@macnica.com">Nicolas Modrzyk</a>
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 }