[6785] | 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.util.Collections; |
---|
| 33 | import java.util.LinkedHashMap; |
---|
| 34 | import java.util.Map; |
---|
| 35 | import java.util.Map.Entry; |
---|
| 36 | |
---|
| 37 | import javax.jdo.JDOHelper; |
---|
| 38 | import javax.jdo.annotations.NotPersistent; |
---|
| 39 | import javax.jdo.annotations.PersistenceCapable; |
---|
| 40 | |
---|
| 41 | import serpro.mailarchiver.util.Logger; |
---|
| 42 | |
---|
| 43 | @PersistenceCapable |
---|
| 44 | public class ContentTypeField |
---|
| 45 | extends Field |
---|
| 46 | { |
---|
| 47 | /* |
---|
| 48 | * "Content-Type" |
---|
| 49 | */ |
---|
| 50 | |
---|
| 51 | @NotPersistent |
---|
| 52 | private static final Logger log = Logger.getLocalLogger(); |
---|
| 53 | |
---|
| 54 | //**** P E R S I S T E N T **** |
---|
| 55 | private String mediaType; |
---|
| 56 | private String subType; |
---|
| 57 | private LinkedHashMap<String,String> parameters = new LinkedHashMap<String,String>(); |
---|
| 58 | //***************************** |
---|
| 59 | |
---|
| 60 | public final String getMediaType() { |
---|
| 61 | return mediaType; |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | public final void setMediaType(String mediaType) { |
---|
| 65 | this.mediaType = mediaType; |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | public final String getSubType() { |
---|
| 69 | return subType; |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | public final void setSubType(String subType) { |
---|
| 73 | this.subType = subType; |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | //-------------------------------------------------------------------------- |
---|
| 77 | public final Map<String,String> getParameters() { |
---|
| 78 | return Collections.unmodifiableMap(parameters); |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | public final String getParameter(String attribute) { |
---|
| 82 | return parameters.get(attribute); |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | public final void addParameter(String attribute, String value) { |
---|
| 86 | parameters.put(attribute, value); |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | public final void removeParameter(String attribute) { |
---|
| 90 | parameters.remove(attribute); |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | //-------------------------------------------------------------------------- |
---|
| 94 | @Override |
---|
| 95 | final void dumpTree(StringBuilder sb, String pad) { |
---|
| 96 | sb.append(toString(pad + " ")); |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | @Override |
---|
| 100 | final String toString(String pad) { |
---|
| 101 | String idx = (getEntity() == null) ? "" : ("[" + String.valueOf(getEntityIdx()) + "]"); |
---|
| 102 | StringBuilder sb = new StringBuilder(String.format( |
---|
| 103 | "ContentTypeField %1$s%n" |
---|
| 104 | + "%2$sjdoState: %3$s%n" |
---|
| 105 | + "%2$soid: %4$s%n" |
---|
| 106 | + "%2$shash: %5$x%n" |
---|
| 107 | + "%2$sname: %6$s%n" |
---|
| 108 | + "%2$smediaType: %7$s%n" |
---|
| 109 | + "%2$ssubType: %8$s%n" |
---|
| 110 | + "%2$svalid: %9$b" |
---|
| 111 | , idx |
---|
| 112 | , pad |
---|
| 113 | , JDOHelper.getObjectState(this) |
---|
| 114 | , getOid() |
---|
| 115 | , hashCode() |
---|
| 116 | , getName() |
---|
| 117 | , getMediaType() |
---|
| 118 | , getSubType() |
---|
| 119 | , isValid())); |
---|
| 120 | |
---|
| 121 | for(Entry<String,String> entry : getParameters().entrySet()) { |
---|
| 122 | sb.append("\n").append(pad).append(entry.getKey()).append(" -> ").append(entry.getValue()); |
---|
| 123 | } |
---|
| 124 | return sb.toString(); |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | |
---|
| 128 | //<editor-fold defaultstate="collapsed" desc=" convenience "> |
---|
| 129 | |
---|
| 130 | //-------------------------------------------------------------------------- |
---|
| 131 | public final String getMimeType() { |
---|
| 132 | return getMediaType() + "/" + getSubType(); |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | //-------------------------------------------------------------------------- |
---|
| 136 | public final boolean isTextMediaType() { |
---|
| 137 | return mediaType.equalsIgnoreCase("text"); |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | //RFC2046, RFC3676 |
---|
| 141 | public final boolean isTextPlainMimeType() { |
---|
| 142 | return mediaType.equalsIgnoreCase("text") |
---|
| 143 | && subType.equalsIgnoreCase("plain"); |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | //RFC2854 |
---|
| 147 | public final boolean isTextHtmlMimeType() { |
---|
| 148 | return mediaType.equalsIgnoreCase("text") |
---|
| 149 | && subType.equalsIgnoreCase("html"); |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | //RFC2318 |
---|
| 153 | public final boolean isTextCssMimeType() { |
---|
| 154 | return mediaType.equalsIgnoreCase("text") |
---|
| 155 | && subType.equalsIgnoreCase("css"); |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | //RFC4180 |
---|
| 159 | public final boolean isTextCsvMimeType() { |
---|
| 160 | return mediaType.equalsIgnoreCase("text") |
---|
| 161 | && subType.equalsIgnoreCase("csv"); |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | //RFC3023 |
---|
| 165 | public final boolean isTextXmlMimeType() { |
---|
| 166 | return mediaType.equalsIgnoreCase("text") |
---|
| 167 | && subType.equalsIgnoreCase("xml"); |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | //RFC4329 |
---|
| 171 | public final boolean isTextJavascriptMimeType() { |
---|
| 172 | return mediaType.equalsIgnoreCase("text") |
---|
| 173 | && subType.equalsIgnoreCase("javascript"); |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | //-------------------------------------------------------------------------- |
---|
| 177 | public final boolean isImageMediaType() { |
---|
| 178 | return mediaType.equalsIgnoreCase("image"); |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | //RFC2045, RFC2046 |
---|
| 182 | public final boolean isImageGifMimeType() { |
---|
| 183 | return mediaType.equalsIgnoreCase("image") |
---|
| 184 | && subType.equalsIgnoreCase("gif"); |
---|
| 185 | } |
---|
| 186 | |
---|
| 187 | //RFC2045, RFC2046 |
---|
| 188 | public final boolean isImageJpegMimeType() { |
---|
| 189 | return mediaType.equalsIgnoreCase("image") |
---|
| 190 | && subType.equalsIgnoreCase("jpeg"); |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | //RFC2083 |
---|
| 194 | public final boolean isImagePngMimeType() { |
---|
| 195 | return mediaType.equalsIgnoreCase("image") |
---|
| 196 | && (subType.equalsIgnoreCase("png") || subType.equalsIgnoreCase("x-png")); |
---|
| 197 | } |
---|
| 198 | |
---|
| 199 | //RFC3302 |
---|
| 200 | public final boolean isImageTiffMimeType() { |
---|
| 201 | return mediaType.equalsIgnoreCase("image") |
---|
| 202 | && subType.equalsIgnoreCase("tiff"); |
---|
| 203 | } |
---|
| 204 | |
---|
| 205 | public final boolean isImageBmpMimeType() { |
---|
| 206 | return mediaType.equalsIgnoreCase("image") |
---|
| 207 | && subType.equalsIgnoreCase("bmp"); |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | public final boolean isImageIconMimeType() { |
---|
| 211 | return mediaType.equalsIgnoreCase("image") |
---|
| 212 | && subType.equalsIgnoreCase("vnd.microsoft.icon"); |
---|
| 213 | } |
---|
| 214 | |
---|
| 215 | public final boolean isImageSvgXmlMimeType() { |
---|
| 216 | return mediaType.equalsIgnoreCase("image") |
---|
| 217 | && subType.equalsIgnoreCase("svg+xml"); |
---|
| 218 | } |
---|
| 219 | |
---|
| 220 | //-------------------------------------------------------------------------- |
---|
| 221 | public final boolean isAudioMediaType() { |
---|
| 222 | return mediaType.equalsIgnoreCase("audio"); |
---|
| 223 | } |
---|
| 224 | |
---|
| 225 | //-------------------------------------------------------------------------- |
---|
| 226 | public final boolean isVideoMediaType() { |
---|
| 227 | return mediaType.equalsIgnoreCase("video"); |
---|
| 228 | } |
---|
| 229 | |
---|
| 230 | //-------------------------------------------------------------------------- |
---|
| 231 | public final boolean isApplicationMediaType() { |
---|
| 232 | return mediaType.equalsIgnoreCase("application"); |
---|
| 233 | } |
---|
| 234 | |
---|
| 235 | //-------------------------------------------------------------------------- |
---|
| 236 | public final boolean isMultipartMediaType() { |
---|
| 237 | return mediaType.equalsIgnoreCase("multipart"); |
---|
| 238 | } |
---|
| 239 | |
---|
| 240 | //RFC2045, RFC2046 |
---|
| 241 | public final boolean isMultipartDigestMimeType() { |
---|
| 242 | return mediaType.equalsIgnoreCase("multipart") |
---|
| 243 | && subType.equalsIgnoreCase("digest"); |
---|
| 244 | } |
---|
| 245 | |
---|
| 246 | //RFC2045, RFC2046 |
---|
| 247 | public final boolean isMultipartAlternativeMimeType() { |
---|
| 248 | return mediaType.equalsIgnoreCase("multipart") |
---|
| 249 | && subType.equalsIgnoreCase("alternative"); |
---|
| 250 | } |
---|
| 251 | |
---|
| 252 | //RFC2045, RFC2046 |
---|
| 253 | public final boolean isMultipartMixedMimeType() { |
---|
| 254 | return mediaType.equalsIgnoreCase("multipart") |
---|
| 255 | && subType.equalsIgnoreCase("mixed"); |
---|
| 256 | } |
---|
| 257 | |
---|
| 258 | //RFC2045, RFC2046 |
---|
| 259 | public final boolean isMultipartParallelMimeType() { |
---|
| 260 | return mediaType.equalsIgnoreCase("multipart") |
---|
| 261 | && subType.equalsIgnoreCase("parallel"); |
---|
| 262 | } |
---|
| 263 | |
---|
| 264 | //RFC2387 |
---|
| 265 | public final boolean isMultipartRelatedMimeType() { |
---|
| 266 | return mediaType.equalsIgnoreCase("multipart") |
---|
| 267 | && subType.equalsIgnoreCase("related"); |
---|
| 268 | } |
---|
| 269 | |
---|
| 270 | //RFC1847 |
---|
| 271 | public final boolean isMultipartSignedMimeType() { |
---|
| 272 | return mediaType.equalsIgnoreCase("multipart") |
---|
| 273 | && subType.equalsIgnoreCase("signed"); |
---|
| 274 | } |
---|
| 275 | |
---|
| 276 | //RFC1847 |
---|
| 277 | public final boolean isMultipartEncryptedMimeType() { |
---|
| 278 | return mediaType.equalsIgnoreCase("multipart") |
---|
| 279 | && subType.equalsIgnoreCase("encrypted"); |
---|
| 280 | } |
---|
| 281 | |
---|
| 282 | //RFC2388 |
---|
| 283 | public final boolean isMultipartFormDataMimeType() { |
---|
| 284 | return mediaType.equalsIgnoreCase("multipart") |
---|
| 285 | && subType.equalsIgnoreCase("form-data"); |
---|
| 286 | } |
---|
| 287 | |
---|
| 288 | //-------------------------------------------------------------------------- |
---|
| 289 | public final boolean isMessageMediaType() { |
---|
| 290 | return mediaType.equalsIgnoreCase("message"); |
---|
| 291 | } |
---|
| 292 | |
---|
| 293 | //RFC2045, RFC2046 |
---|
| 294 | public final boolean isMessageRfc822MimeType() { |
---|
| 295 | return mediaType.equalsIgnoreCase("message") |
---|
| 296 | && subType.equalsIgnoreCase("rfc822"); |
---|
| 297 | } |
---|
| 298 | |
---|
| 299 | //RFC2045, RFC2046 |
---|
| 300 | public final boolean isMessagePartialMimeType() { |
---|
| 301 | return mediaType.equalsIgnoreCase("message") |
---|
| 302 | && subType.equalsIgnoreCase("partial"); |
---|
| 303 | } |
---|
| 304 | |
---|
| 305 | //RFC2616 |
---|
| 306 | public final boolean isMessageHttpMimeType() { |
---|
| 307 | return mediaType.equalsIgnoreCase("message") |
---|
| 308 | && subType.equalsIgnoreCase("http"); |
---|
| 309 | } |
---|
| 310 | |
---|
| 311 | //RFC2045, RFC2046 |
---|
| 312 | public final boolean isMessageExternalBodyMimeType() { |
---|
| 313 | return mediaType.equalsIgnoreCase("message") |
---|
| 314 | && subType.equalsIgnoreCase("external-body"); |
---|
| 315 | } |
---|
| 316 | |
---|
| 317 | //RFC3462 |
---|
| 318 | public final boolean isMessageDeliveryStatusMimeType() { |
---|
| 319 | return mediaType.equalsIgnoreCase("message") |
---|
| 320 | && subType.equalsIgnoreCase("delivery-status"); |
---|
| 321 | } |
---|
| 322 | |
---|
| 323 | //-------------------------------------------------------------------------- |
---|
| 324 | public final boolean isDiscreteMediaType() { |
---|
| 325 | return isTextMediaType() |
---|
| 326 | || isImageMediaType() |
---|
| 327 | || isAudioMediaType() |
---|
| 328 | || isVideoMediaType() |
---|
| 329 | || isApplicationMediaType(); |
---|
| 330 | } |
---|
| 331 | |
---|
| 332 | public final boolean isCompositeMediaType() { |
---|
| 333 | return isMessageMediaType() |
---|
| 334 | || isMultipartMediaType(); |
---|
| 335 | } |
---|
| 336 | |
---|
| 337 | //-------------------------------------------------------------------------- |
---|
| 338 | public final String getBoundary() { |
---|
| 339 | return getParameter("boundary"); |
---|
| 340 | } |
---|
| 341 | |
---|
| 342 | public final String getCharset() { |
---|
| 343 | return getParameter("charset"); |
---|
| 344 | } |
---|
| 345 | |
---|
| 346 | public final boolean isUsAsciiCharset() { |
---|
| 347 | String charset = getCharset(); |
---|
| 348 | if(charset == null) { |
---|
| 349 | return true; |
---|
| 350 | } |
---|
| 351 | return (charset != null) && charset.equalsIgnoreCase("us-ascii"); |
---|
| 352 | } |
---|
| 353 | |
---|
| 354 | //</editor-fold> |
---|
| 355 | |
---|
| 356 | } |
---|