/** * MailArchiver is an application that provides services for storing and managing e-mail messages through a Web Services SOAP interface. * Copyright (C) 2012 Marcio Andre Scholl Levien and Fernando Alberto Reuter Wendt and Jose Ronaldo Nogueira Fonseca Junior * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ /******************************************************************************\ * * This product was developed by * * SERVIÇO FEDERAL DE PROCESSAMENTO DE DADOS (SERPRO), * * a government company established under Brazilian law (5.615/70), * at Department of Development of Porto Alegre. * \******************************************************************************/ package serpro.mailarchiver.domain.metaarchive; import java.util.Collections; import java.util.LinkedHashMap; import java.util.Map; import java.util.Map.Entry; import javax.jdo.JDOHelper; import javax.jdo.annotations.NotPersistent; import javax.jdo.annotations.PersistenceCapable; import serpro.mailarchiver.util.Logger; @PersistenceCapable public class ContentTypeField extends Field { /* * "Content-Type" */ @NotPersistent private static final Logger log = Logger.getLocalLogger(); //**** P E R S I S T E N T **** private String mediaType; private String subType; private LinkedHashMap parameters = new LinkedHashMap(); //***************************** public final String getMediaType() { return mediaType; } public final void setMediaType(String mediaType) { this.mediaType = mediaType; } public final String getSubType() { return subType; } public final void setSubType(String subType) { this.subType = subType; } //-------------------------------------------------------------------------- public final Map getParameters() { return Collections.unmodifiableMap(parameters); } public final String getParameter(String attribute) { return parameters.get(attribute); } public final void addParameter(String attribute, String value) { parameters.put(attribute, value); } public final void removeParameter(String attribute) { parameters.remove(attribute); } //-------------------------------------------------------------------------- @Override final void dumpTree(StringBuilder sb, String pad) { sb.append(toString(pad + " ")); } @Override final String toString(String pad) { String idx = (getEntity() == null) ? "" : ("[" + String.valueOf(getEntityIdx()) + "]"); StringBuilder sb = new StringBuilder(String.format( "ContentTypeField %1$s%n" + "%2$sjdoState: %3$s%n" + "%2$soid: %4$s%n" + "%2$shash: %5$x%n" + "%2$sname: %6$s%n" + "%2$smediaType: %7$s%n" + "%2$ssubType: %8$s%n" + "%2$svalid: %9$b" , idx , pad , JDOHelper.getObjectState(this) , getOid() , hashCode() , getName() , getMediaType() , getSubType() , isValid())); for(Entry entry : getParameters().entrySet()) { sb.append("\n").append(pad).append(entry.getKey()).append(" -> ").append(entry.getValue()); } return sb.toString(); } // //-------------------------------------------------------------------------- public final String getMimeType() { return getMediaType() + "/" + getSubType(); } //-------------------------------------------------------------------------- public final boolean isTextMediaType() { return mediaType.equalsIgnoreCase("text"); } //RFC2046, RFC3676 public final boolean isTextPlainMimeType() { return mediaType.equalsIgnoreCase("text") && subType.equalsIgnoreCase("plain"); } //RFC2854 public final boolean isTextHtmlMimeType() { return mediaType.equalsIgnoreCase("text") && subType.equalsIgnoreCase("html"); } //RFC2318 public final boolean isTextCssMimeType() { return mediaType.equalsIgnoreCase("text") && subType.equalsIgnoreCase("css"); } //RFC4180 public final boolean isTextCsvMimeType() { return mediaType.equalsIgnoreCase("text") && subType.equalsIgnoreCase("csv"); } //RFC3023 public final boolean isTextXmlMimeType() { return mediaType.equalsIgnoreCase("text") && subType.equalsIgnoreCase("xml"); } //RFC4329 public final boolean isTextJavascriptMimeType() { return mediaType.equalsIgnoreCase("text") && subType.equalsIgnoreCase("javascript"); } //-------------------------------------------------------------------------- public final boolean isImageMediaType() { return mediaType.equalsIgnoreCase("image"); } //RFC2045, RFC2046 public final boolean isImageGifMimeType() { return mediaType.equalsIgnoreCase("image") && subType.equalsIgnoreCase("gif"); } //RFC2045, RFC2046 public final boolean isImageJpegMimeType() { return mediaType.equalsIgnoreCase("image") && subType.equalsIgnoreCase("jpeg"); } //RFC2083 public final boolean isImagePngMimeType() { return mediaType.equalsIgnoreCase("image") && (subType.equalsIgnoreCase("png") || subType.equalsIgnoreCase("x-png")); } //RFC3302 public final boolean isImageTiffMimeType() { return mediaType.equalsIgnoreCase("image") && subType.equalsIgnoreCase("tiff"); } public final boolean isImageBmpMimeType() { return mediaType.equalsIgnoreCase("image") && subType.equalsIgnoreCase("bmp"); } public final boolean isImageIconMimeType() { return mediaType.equalsIgnoreCase("image") && subType.equalsIgnoreCase("vnd.microsoft.icon"); } public final boolean isImageSvgXmlMimeType() { return mediaType.equalsIgnoreCase("image") && subType.equalsIgnoreCase("svg+xml"); } //-------------------------------------------------------------------------- public final boolean isAudioMediaType() { return mediaType.equalsIgnoreCase("audio"); } //-------------------------------------------------------------------------- public final boolean isVideoMediaType() { return mediaType.equalsIgnoreCase("video"); } //-------------------------------------------------------------------------- public final boolean isApplicationMediaType() { return mediaType.equalsIgnoreCase("application"); } //-------------------------------------------------------------------------- public final boolean isMultipartMediaType() { return mediaType.equalsIgnoreCase("multipart"); } //RFC2045, RFC2046 public final boolean isMultipartDigestMimeType() { return mediaType.equalsIgnoreCase("multipart") && subType.equalsIgnoreCase("digest"); } //RFC2045, RFC2046 public final boolean isMultipartAlternativeMimeType() { return mediaType.equalsIgnoreCase("multipart") && subType.equalsIgnoreCase("alternative"); } //RFC2045, RFC2046 public final boolean isMultipartMixedMimeType() { return mediaType.equalsIgnoreCase("multipart") && subType.equalsIgnoreCase("mixed"); } //RFC2045, RFC2046 public final boolean isMultipartParallelMimeType() { return mediaType.equalsIgnoreCase("multipart") && subType.equalsIgnoreCase("parallel"); } //RFC2387 public final boolean isMultipartRelatedMimeType() { return mediaType.equalsIgnoreCase("multipart") && subType.equalsIgnoreCase("related"); } //RFC1847 public final boolean isMultipartSignedMimeType() { return mediaType.equalsIgnoreCase("multipart") && subType.equalsIgnoreCase("signed"); } //RFC1847 public final boolean isMultipartEncryptedMimeType() { return mediaType.equalsIgnoreCase("multipart") && subType.equalsIgnoreCase("encrypted"); } //RFC2388 public final boolean isMultipartFormDataMimeType() { return mediaType.equalsIgnoreCase("multipart") && subType.equalsIgnoreCase("form-data"); } //-------------------------------------------------------------------------- public final boolean isMessageMediaType() { return mediaType.equalsIgnoreCase("message"); } //RFC2045, RFC2046 public final boolean isMessageRfc822MimeType() { return mediaType.equalsIgnoreCase("message") && subType.equalsIgnoreCase("rfc822"); } //RFC2045, RFC2046 public final boolean isMessagePartialMimeType() { return mediaType.equalsIgnoreCase("message") && subType.equalsIgnoreCase("partial"); } //RFC2616 public final boolean isMessageHttpMimeType() { return mediaType.equalsIgnoreCase("message") && subType.equalsIgnoreCase("http"); } //RFC2045, RFC2046 public final boolean isMessageExternalBodyMimeType() { return mediaType.equalsIgnoreCase("message") && subType.equalsIgnoreCase("external-body"); } //RFC3462 public final boolean isMessageDeliveryStatusMimeType() { return mediaType.equalsIgnoreCase("message") && subType.equalsIgnoreCase("delivery-status"); } //-------------------------------------------------------------------------- public final boolean isDiscreteMediaType() { return isTextMediaType() || isImageMediaType() || isAudioMediaType() || isVideoMediaType() || isApplicationMediaType(); } public final boolean isCompositeMediaType() { return isMessageMediaType() || isMultipartMediaType(); } //-------------------------------------------------------------------------- public final String getBoundary() { return getParameter("boundary"); } public final String getCharset() { return getParameter("charset"); } public final boolean isUsAsciiCharset() { String charset = getCharset(); if(charset == null) { return true; } return (charset != null) && charset.equalsIgnoreCase("us-ascii"); } // }