/** * 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.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Collections; import java.util.List; import javax.jdo.JDOHelper; import javax.jdo.annotations.NotPersistent; import javax.jdo.annotations.PersistenceCapable; import serpro.mailarchiver.util.Charsets; import serpro.mailarchiver.util.Logger; @PersistenceCapable public class Multipart extends Body { @NotPersistent private static final Logger log = Logger.getLocalLogger(); //**** P E R S I S T E N T **** private String preamble; private String epilogue; private ArrayList bodyParts = new ArrayList(); //***************************** public final String getPreamble() { return preamble; } public final void setPreamble(String preamble) { if(preamble.length() > 1000) { preamble = preamble.substring(0, 1000); log.error("Multipart preamble is too long and has been truncated to 1000 characters"); } this.preamble = preamble; } public final void setPreambleStream(InputStream is) { setPreamble(readStream(is)); } public final String getEpilogue() { return epilogue; } public final void setEpilogue(String epilogue) { if(epilogue.length() > 1000) { epilogue = epilogue.substring(0, 1000); log.error("Multipart epilogue is too long and has been truncated to 1000 characters"); } this.epilogue = epilogue; } public final void setEpilogueStream(InputStream is) { setEpilogue(readStream(is)); } //-------------------------------------------------------------------------- public final List getBodyParts() { return Collections.unmodifiableList(bodyParts); } public final int indexOf(BodyPart bodyPart) { return bodyParts.indexOf(bodyPart); } public final void addBodyPart(BodyPart bodyPart) { if(bodyPart != null) { bodyPart.setComposite(this); } } final void internal_addBodyPart(BodyPart bodyPart) { bodyParts.add(bodyPart); } public final void removeBodyPart(BodyPart bodyPart) { if((bodyPart != null) && (bodyPart.getComposite() == this)) { bodyPart.setComposite(null); } } final void internal_removeBodyPart(BodyPart bodyPart) { bodyParts.remove(bodyPart); } //-------------------------------------------------------------------------- @Override final void dumpTree(StringBuilder sb, String pad) { int size = bodyParts.size(); sb.append(toString(pad + ((size > 0) ? "| " : " "))); for(int i = 0; i < size; i++) { sb.append("\n") .append(pad).append("|\n") .append(pad).append("+---"); if(i < (size - 1)) { bodyParts.get(i).dumpTree(sb, pad + "| "); } else { bodyParts.get(i).dumpTree(sb, pad + " "); } } } @Override final String toString(String pad) { return String.format( "Multipart%n" + "%1$sjdoState: %2$s%n" + "%1$soid: %3$s%n" + "%1$shash: %4$x%n" + "%1$spreamble: %5$s%n" + "%1$sepilogue: %6$s" , pad , JDOHelper.getObjectState(this) , getOid() , hashCode() , getPreamble() , getEpilogue()); } //-------------------------------------------------------------------------- private String readStream(InputStream is) { StringBuilder sb = new StringBuilder(); BufferedReader reader = null; try { reader = new BufferedReader(new InputStreamReader(is, Charsets.Windows_1252)); char[] cbuf = new char[1024]; int len; while((len = reader.read(cbuf)) > 0) { sb.append(cbuf, 0, len); } } catch(IOException ex) { //TODO } return sb.toString(); } }