1 | /** |
---|
2 | * MailArchiver is an application that provides services for storing and managing e-mail messages through a Web Services SOAP interface. |
---|
3 | * Copyright (C) 2012 Marcio Andre Scholl Levien and Fernando Alberto Reuter Wendt and Jose Ronaldo Nogueira Fonseca Junior |
---|
4 | * |
---|
5 | * This program is free software: you can redistribute it and/or modify |
---|
6 | * it under the terms of the GNU Affero General Public License as |
---|
7 | * published by the Free Software Foundation, either version 3 of the |
---|
8 | * License, or (at your option) any later version. |
---|
9 | * |
---|
10 | * This program is distributed in the hope that it will be useful, |
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
13 | * GNU Affero General Public License for more details. |
---|
14 | * |
---|
15 | * You should have received a copy of the GNU Affero General Public License |
---|
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
17 | */ |
---|
18 | |
---|
19 | /******************************************************************************\ |
---|
20 | * |
---|
21 | * This product was developed by |
---|
22 | * |
---|
23 | * SERVIÃO FEDERAL DE PROCESSAMENTO DE DADOS (SERPRO), |
---|
24 | * |
---|
25 | * a government company established under Brazilian law (5.615/70), |
---|
26 | * at Department of Development of Porto Alegre. |
---|
27 | * |
---|
28 | \******************************************************************************/ |
---|
29 | |
---|
30 | package serpro.mailarchiver.domain.metaarchive; |
---|
31 | |
---|
32 | import java.io.StringReader; |
---|
33 | import java.util.Collections; |
---|
34 | import java.util.Date; |
---|
35 | import java.util.LinkedHashMap; |
---|
36 | import java.util.Map; |
---|
37 | import java.util.Map.Entry; |
---|
38 | |
---|
39 | import javax.jdo.JDOHelper; |
---|
40 | import javax.jdo.annotations.NotPersistent; |
---|
41 | import javax.jdo.annotations.PersistenceCapable; |
---|
42 | |
---|
43 | import org.apache.james.mime4j.dom.datetime.DateTime; |
---|
44 | import org.apache.james.mime4j.field.datetime.parser.DateTimeParser; |
---|
45 | import org.apache.james.mime4j.field.datetime.parser.ParseException; |
---|
46 | |
---|
47 | import serpro.mailarchiver.util.Logger; |
---|
48 | |
---|
49 | @PersistenceCapable |
---|
50 | public class ContentDispositionField |
---|
51 | extends Field |
---|
52 | { |
---|
53 | /* |
---|
54 | * "Content-Disposition" |
---|
55 | */ |
---|
56 | |
---|
57 | @NotPersistent |
---|
58 | private static final Logger log = Logger.getLocalLogger(); |
---|
59 | |
---|
60 | //**** P E R S I S T E N T **** |
---|
61 | private String dispositionType; // "inline"|"attachment" |
---|
62 | private LinkedHashMap<String,String> parameters = new LinkedHashMap<String,String>(); |
---|
63 | //***************************** |
---|
64 | |
---|
65 | public final String getDispositionType() { |
---|
66 | return dispositionType; |
---|
67 | } |
---|
68 | |
---|
69 | public final void setDispositionType(String dispositionType) { |
---|
70 | this.dispositionType = dispositionType; |
---|
71 | } |
---|
72 | |
---|
73 | //-------------------------------------------------------------------------- |
---|
74 | public final Map<String,String> getParameters() { |
---|
75 | return Collections.unmodifiableMap(parameters); |
---|
76 | } |
---|
77 | |
---|
78 | public final String getParameter(String attribute) { |
---|
79 | return parameters.get(attribute); |
---|
80 | } |
---|
81 | |
---|
82 | public final void addParameter(String attribute, String value) { |
---|
83 | parameters.put(attribute, value); |
---|
84 | } |
---|
85 | |
---|
86 | public final void removeParameter(String attribute) { |
---|
87 | parameters.remove(attribute); |
---|
88 | } |
---|
89 | |
---|
90 | //-------------------------------------------------------------------------- |
---|
91 | @Override |
---|
92 | final void dumpTree(StringBuilder sb, String pad) { |
---|
93 | sb.append(toString(pad + " ")); |
---|
94 | } |
---|
95 | |
---|
96 | @Override |
---|
97 | final String toString(String pad) { |
---|
98 | String idx = (getEntity() == null) ? "" : ("[" + String.valueOf(getEntityIdx()) + "]"); |
---|
99 | StringBuilder sb = new StringBuilder(String.format( |
---|
100 | "ContentDispositionField %1$s%n" |
---|
101 | + "%2$sjdoState: %3$s%n" |
---|
102 | + "%2$soid: %4$s%n" |
---|
103 | + "%2$shash: %5$x%n" |
---|
104 | + "%2$sname: %6$s%n" |
---|
105 | + "%2$sdispositionType: %7$s%n" |
---|
106 | + "%2$svalid: %8$b" |
---|
107 | , idx |
---|
108 | , pad |
---|
109 | , JDOHelper.getObjectState(this) |
---|
110 | , getOid() |
---|
111 | , hashCode() |
---|
112 | , getName() |
---|
113 | , getDispositionType() |
---|
114 | , isValid())); |
---|
115 | |
---|
116 | for(Entry<String,String> entry : getParameters().entrySet()) { |
---|
117 | sb.append("\n").append(pad).append(entry.getKey()).append(" -> ").append(entry.getValue()); |
---|
118 | } |
---|
119 | return sb.toString(); |
---|
120 | } |
---|
121 | |
---|
122 | |
---|
123 | //<editor-fold defaultstate="collapsed" desc=" convenience "> |
---|
124 | |
---|
125 | public final boolean isInlineDisposition() { |
---|
126 | return dispositionType.equalsIgnoreCase("inline"); |
---|
127 | } |
---|
128 | |
---|
129 | public final boolean isAttachmentDisposition() { |
---|
130 | return dispositionType.equalsIgnoreCase("attachment"); |
---|
131 | } |
---|
132 | |
---|
133 | //-------------------------------------------------------------------------- |
---|
134 | public final String getFileName() { |
---|
135 | return getParameter("filename"); |
---|
136 | } |
---|
137 | |
---|
138 | //-------------------------------------------------------------------------- |
---|
139 | public final long getSize() { |
---|
140 | String strSize = getParameter("size"); |
---|
141 | if(strSize != null) { |
---|
142 | return Long.parseLong(strSize); |
---|
143 | } |
---|
144 | return 0; |
---|
145 | } |
---|
146 | |
---|
147 | //-------------------------------------------------------------------------- |
---|
148 | public final Date getCreationDate() { |
---|
149 | String strCreationDate = getParameter("creation-date"); |
---|
150 | if(strCreationDate != null) { |
---|
151 | StringReader reader = new StringReader(strCreationDate); |
---|
152 | DateTimeParser dtp = new DateTimeParser(reader); |
---|
153 | try { |
---|
154 | DateTime dt = dtp.parseAll(); |
---|
155 | if(dt != null) { |
---|
156 | return dt.getDate(); |
---|
157 | } |
---|
158 | } |
---|
159 | catch(ParseException ex) { |
---|
160 | log.error(ex, "creation-date"); |
---|
161 | } |
---|
162 | } |
---|
163 | return null; |
---|
164 | } |
---|
165 | |
---|
166 | public final Date getModificationDate() { |
---|
167 | String strModificationDate = getParameter("modification-date"); |
---|
168 | if(strModificationDate != null) { |
---|
169 | StringReader reader = new StringReader(strModificationDate); |
---|
170 | DateTimeParser dtp = new DateTimeParser(reader); |
---|
171 | try { |
---|
172 | DateTime dt = dtp.parseAll(); |
---|
173 | if(dt != null) { |
---|
174 | return dt.getDate(); |
---|
175 | } |
---|
176 | } |
---|
177 | catch(ParseException ex) { |
---|
178 | log.error(ex, "modification-date"); |
---|
179 | } |
---|
180 | } |
---|
181 | return null; |
---|
182 | } |
---|
183 | |
---|
184 | public final Date getReadDate() { |
---|
185 | String strReadDate = getParameter("read-date"); |
---|
186 | if(strReadDate != null) { |
---|
187 | StringReader reader = new StringReader(strReadDate); |
---|
188 | DateTimeParser dtp = new DateTimeParser(reader); |
---|
189 | try { |
---|
190 | DateTime dt = dtp.parseAll(); |
---|
191 | if(dt != null) { |
---|
192 | return dt.getDate(); |
---|
193 | } |
---|
194 | } |
---|
195 | catch(ParseException ex) { |
---|
196 | log.error(ex, "read-date"); |
---|
197 | } |
---|
198 | } |
---|
199 | return null; |
---|
200 | } |
---|
201 | |
---|
202 | } |
---|