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.BufferedReader; |
---|
33 | import java.io.IOException; |
---|
34 | import java.io.InputStream; |
---|
35 | import java.io.InputStreamReader; |
---|
36 | import java.util.ArrayList; |
---|
37 | import java.util.Collections; |
---|
38 | import java.util.List; |
---|
39 | |
---|
40 | import javax.jdo.JDOHelper; |
---|
41 | import javax.jdo.annotations.NotPersistent; |
---|
42 | import javax.jdo.annotations.PersistenceCapable; |
---|
43 | |
---|
44 | import serpro.mailarchiver.util.Charsets; |
---|
45 | import serpro.mailarchiver.util.Logger; |
---|
46 | |
---|
47 | @PersistenceCapable |
---|
48 | public class Multipart |
---|
49 | extends Body |
---|
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 preamble; |
---|
56 | private String epilogue; |
---|
57 | private ArrayList<BodyPart> bodyParts = new ArrayList<BodyPart>(); |
---|
58 | //***************************** |
---|
59 | |
---|
60 | public final String getPreamble() { |
---|
61 | return preamble; |
---|
62 | } |
---|
63 | |
---|
64 | public final void setPreamble(String preamble) { |
---|
65 | if(preamble.length() > 1000) { |
---|
66 | preamble = preamble.substring(0, 1000); |
---|
67 | log.error("Multipart preamble is too long and has been truncated to 1000 characters"); |
---|
68 | } |
---|
69 | this.preamble = preamble; |
---|
70 | } |
---|
71 | |
---|
72 | public final void setPreambleStream(InputStream is) { |
---|
73 | setPreamble(readStream(is)); |
---|
74 | } |
---|
75 | |
---|
76 | public final String getEpilogue() { |
---|
77 | return epilogue; |
---|
78 | } |
---|
79 | |
---|
80 | public final void setEpilogue(String epilogue) { |
---|
81 | if(epilogue.length() > 1000) { |
---|
82 | epilogue = epilogue.substring(0, 1000); |
---|
83 | log.error("Multipart epilogue is too long and has been truncated to 1000 characters"); |
---|
84 | } |
---|
85 | this.epilogue = epilogue; |
---|
86 | } |
---|
87 | |
---|
88 | public final void setEpilogueStream(InputStream is) { |
---|
89 | setEpilogue(readStream(is)); |
---|
90 | } |
---|
91 | |
---|
92 | //-------------------------------------------------------------------------- |
---|
93 | public final List<BodyPart> getBodyParts() { |
---|
94 | return Collections.unmodifiableList(bodyParts); |
---|
95 | } |
---|
96 | |
---|
97 | public final int indexOf(BodyPart bodyPart) { |
---|
98 | return bodyParts.indexOf(bodyPart); |
---|
99 | } |
---|
100 | |
---|
101 | public final void addBodyPart(BodyPart bodyPart) { |
---|
102 | if(bodyPart != null) { |
---|
103 | bodyPart.setComposite(this); |
---|
104 | } |
---|
105 | } |
---|
106 | |
---|
107 | final void internal_addBodyPart(BodyPart bodyPart) { |
---|
108 | bodyParts.add(bodyPart); |
---|
109 | } |
---|
110 | |
---|
111 | public final void removeBodyPart(BodyPart bodyPart) { |
---|
112 | if((bodyPart != null) && (bodyPart.getComposite() == this)) { |
---|
113 | bodyPart.setComposite(null); |
---|
114 | } |
---|
115 | } |
---|
116 | |
---|
117 | final void internal_removeBodyPart(BodyPart bodyPart) { |
---|
118 | bodyParts.remove(bodyPart); |
---|
119 | } |
---|
120 | |
---|
121 | //-------------------------------------------------------------------------- |
---|
122 | @Override |
---|
123 | final void dumpTree(StringBuilder sb, String pad) { |
---|
124 | int size = bodyParts.size(); |
---|
125 | sb.append(toString(pad + ((size > 0) ? "| " : " "))); |
---|
126 | for(int i = 0; i < size; i++) { |
---|
127 | sb.append("\n") |
---|
128 | .append(pad).append("|\n") |
---|
129 | .append(pad).append("+---"); |
---|
130 | if(i < (size - 1)) { |
---|
131 | bodyParts.get(i).dumpTree(sb, pad + "| "); |
---|
132 | } |
---|
133 | else { |
---|
134 | bodyParts.get(i).dumpTree(sb, pad + " "); |
---|
135 | } |
---|
136 | } |
---|
137 | } |
---|
138 | |
---|
139 | @Override |
---|
140 | final String toString(String pad) { |
---|
141 | return String.format( |
---|
142 | "Multipart%n" |
---|
143 | + "%1$sjdoState: %2$s%n" |
---|
144 | + "%1$soid: %3$s%n" |
---|
145 | + "%1$shash: %4$x%n" |
---|
146 | + "%1$spreamble: %5$s%n" |
---|
147 | + "%1$sepilogue: %6$s" |
---|
148 | , pad |
---|
149 | , JDOHelper.getObjectState(this) |
---|
150 | , getOid() |
---|
151 | , hashCode() |
---|
152 | , getPreamble() |
---|
153 | , getEpilogue()); |
---|
154 | } |
---|
155 | |
---|
156 | //-------------------------------------------------------------------------- |
---|
157 | private String readStream(InputStream is) { |
---|
158 | StringBuilder sb = new StringBuilder(); |
---|
159 | BufferedReader reader = null; |
---|
160 | try { |
---|
161 | reader = new BufferedReader(new InputStreamReader(is, Charsets.Windows_1252)); |
---|
162 | char[] cbuf = new char[1024]; |
---|
163 | int len; |
---|
164 | while((len = reader.read(cbuf)) > 0) { |
---|
165 | sb.append(cbuf, 0, len); |
---|
166 | } |
---|
167 | } |
---|
168 | catch(IOException ex) { |
---|
169 | //TODO |
---|
170 | } |
---|
171 | return sb.toString(); |
---|
172 | } |
---|
173 | } |
---|