/**
* 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.io.StringReader;
import java.util.Collections;
import java.util.Date;
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 org.apache.james.mime4j.dom.datetime.DateTime;
import org.apache.james.mime4j.field.datetime.parser.DateTimeParser;
import org.apache.james.mime4j.field.datetime.parser.ParseException;
import serpro.mailarchiver.util.Logger;
@PersistenceCapable
public class ContentDispositionField
extends Field
{
/*
* "Content-Disposition"
*/
@NotPersistent
private static final Logger log = Logger.getLocalLogger();
//**** P E R S I S T E N T ****
private String dispositionType; // "inline"|"attachment"
private LinkedHashMap parameters = new LinkedHashMap();
//*****************************
public final String getDispositionType() {
return dispositionType;
}
public final void setDispositionType(String dispositionType) {
this.dispositionType = dispositionType;
}
//--------------------------------------------------------------------------
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(
"ContentDispositionField %1$s%n"
+ "%2$sjdoState: %3$s%n"
+ "%2$soid: %4$s%n"
+ "%2$shash: %5$x%n"
+ "%2$sname: %6$s%n"
+ "%2$sdispositionType: %7$s%n"
+ "%2$svalid: %8$b"
, idx
, pad
, JDOHelper.getObjectState(this)
, getOid()
, hashCode()
, getName()
, getDispositionType()
, isValid()));
for(Entry entry : getParameters().entrySet()) {
sb.append("\n").append(pad).append(entry.getKey()).append(" -> ").append(entry.getValue());
}
return sb.toString();
}
//
public final boolean isInlineDisposition() {
return dispositionType.equalsIgnoreCase("inline");
}
public final boolean isAttachmentDisposition() {
return dispositionType.equalsIgnoreCase("attachment");
}
//--------------------------------------------------------------------------
public final String getFileName() {
return getParameter("filename");
}
//--------------------------------------------------------------------------
public final long getSize() {
String strSize = getParameter("size");
if(strSize != null) {
return Long.parseLong(strSize);
}
return 0;
}
//--------------------------------------------------------------------------
public final Date getCreationDate() {
String strCreationDate = getParameter("creation-date");
if(strCreationDate != null) {
StringReader reader = new StringReader(strCreationDate);
DateTimeParser dtp = new DateTimeParser(reader);
try {
DateTime dt = dtp.parseAll();
if(dt != null) {
return dt.getDate();
}
}
catch(ParseException ex) {
log.error(ex, "creation-date");
}
}
return null;
}
public final Date getModificationDate() {
String strModificationDate = getParameter("modification-date");
if(strModificationDate != null) {
StringReader reader = new StringReader(strModificationDate);
DateTimeParser dtp = new DateTimeParser(reader);
try {
DateTime dt = dtp.parseAll();
if(dt != null) {
return dt.getDate();
}
}
catch(ParseException ex) {
log.error(ex, "modification-date");
}
}
return null;
}
public final Date getReadDate() {
String strReadDate = getParameter("read-date");
if(strReadDate != null) {
StringReader reader = new StringReader(strReadDate);
DateTimeParser dtp = new DateTimeParser(reader);
try {
DateTime dt = dtp.parseAll();
if(dt != null) {
return dt.getDate();
}
}
catch(ParseException ex) {
log.error(ex, "read-date");
}
}
return null;
}
}