[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.io.ByteArrayInputStream; |
---|
| 33 | import java.io.IOException; |
---|
| 34 | import java.io.InputStream; |
---|
| 35 | import java.net.MalformedURLException; |
---|
| 36 | import java.net.URL; |
---|
| 37 | import java.nio.ByteBuffer; |
---|
| 38 | import java.nio.channels.FileChannel; |
---|
| 39 | import java.nio.file.Files; |
---|
| 40 | import java.nio.file.Path; |
---|
| 41 | import java.nio.file.StandardOpenOption; |
---|
| 42 | |
---|
| 43 | import javax.jdo.annotations.NotPersistent; |
---|
| 44 | import javax.jdo.annotations.PersistenceCapable; |
---|
| 45 | |
---|
| 46 | import org.apache.james.mime4j.codec.Base64InputStream; |
---|
| 47 | import org.apache.james.mime4j.codec.QuotedPrintableInputStream; |
---|
| 48 | |
---|
| 49 | import org.springframework.beans.factory.annotation.Autowired; |
---|
| 50 | |
---|
| 51 | import serpro.mailarchiver.domain.metaarchive.UnstructuredField.ContentTransferEncodingField; |
---|
| 52 | import serpro.mailarchiver.util.Charsets; |
---|
| 53 | import serpro.mailarchiver.util.Environment; |
---|
| 54 | import serpro.mailarchiver.util.Logger; |
---|
| 55 | import serpro.mailarchiver.util.UserAppConfig; |
---|
| 56 | |
---|
| 57 | @PersistenceCapable |
---|
| 58 | public abstract class SingleBody |
---|
| 59 | extends Body |
---|
| 60 | { |
---|
| 61 | @NotPersistent |
---|
| 62 | private static final Logger log = Logger.getLocalLogger(); |
---|
| 63 | |
---|
| 64 | @Autowired |
---|
| 65 | @NotPersistent |
---|
| 66 | protected UserAppConfig userAppConfig; |
---|
| 67 | |
---|
| 68 | @Autowired |
---|
| 69 | @NotPersistent |
---|
| 70 | protected Environment env; |
---|
| 71 | |
---|
| 72 | //**** P E R S I S T E N T **** |
---|
| 73 | private int offset; |
---|
| 74 | private int length; |
---|
| 75 | private int size; |
---|
| 76 | //***************************** |
---|
| 77 | |
---|
| 78 | public final int getOffset() { |
---|
| 79 | return offset; |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | public final void setOffset(int offset) { |
---|
| 83 | this.offset = offset; |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | public final int getLength() { |
---|
| 87 | return length; |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | public final void setLength(int length) { |
---|
| 91 | this.length = length; |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | public final int getSize() { |
---|
| 95 | return size; |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | public final void setSize(int size) { |
---|
| 99 | this.size = size; |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | //-------------------------------------------------------------------------- |
---|
| 103 | @Override |
---|
| 104 | final void dumpTree(StringBuilder sb, String pad) { |
---|
| 105 | sb.append(toString(pad + " ")); |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | //-------------------------------------------------------------------------- |
---|
| 109 | public byte[] getRawBytes() throws IOException { |
---|
| 110 | Message message = getRootMessage(); |
---|
| 111 | if(message != null) { |
---|
| 112 | Path path = message.getAbsolutePath(); |
---|
| 113 | ByteBuffer byteBuffer = ByteBuffer.allocate(length); |
---|
| 114 | FileChannel channel = FileChannel.open(path, StandardOpenOption.READ); |
---|
| 115 | channel.read(byteBuffer, offset); |
---|
| 116 | channel.close(); |
---|
| 117 | return byteBuffer.array(); |
---|
| 118 | } |
---|
| 119 | throw new IllegalStateException(); |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | //-------------------------------------------------------------------------- |
---|
| 123 | public InputStream getDecoderInputStream() throws IOException { |
---|
| 124 | final InputStream is = new ByteArrayInputStream(getRawBytes()); |
---|
| 125 | ContentTransferEncodingField contentTransferEncodingField = getEntity().getContentTransferEncoding(); |
---|
| 126 | if(contentTransferEncodingField != null) { |
---|
| 127 | if(contentTransferEncodingField.isBase64Encoding()) { |
---|
| 128 | return new Base64InputStream(is); |
---|
| 129 | } |
---|
| 130 | else if(contentTransferEncodingField.isQuotedPrintableEncoding()) { |
---|
| 131 | return new QuotedPrintableInputStream(is); |
---|
| 132 | } |
---|
| 133 | } |
---|
| 134 | //identity: 7bit, 8bit, binary |
---|
| 135 | return is; |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | //-------------------------------------------------------------------------- |
---|
| 139 | public String getRawText() throws IOException { |
---|
| 140 | return new String(getRawBytes(), Charsets.Windows_1252); |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | //-------------------------------------------------------------------------- |
---|
| 144 | public String getFileName() { |
---|
| 145 | |
---|
| 146 | if(getEntity() == null) { |
---|
| 147 | return "part_" + getOid(); |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | String fileName = null; |
---|
| 151 | |
---|
| 152 | ContentDispositionField contentDispositionField = getEntity().getContentDispositionField(); |
---|
| 153 | if(contentDispositionField != null) { |
---|
| 154 | fileName = contentDispositionField.getFileName(); |
---|
| 155 | if(fileName != null) { |
---|
| 156 | return fileName; |
---|
| 157 | } |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | ContentTypeField contentTypeField = getEntity().getContentTypeField(); |
---|
| 161 | if(contentTypeField != null) { |
---|
| 162 | fileName = contentTypeField.getParameter("name"); |
---|
| 163 | if(fileName != null) { |
---|
| 164 | return fileName; |
---|
| 165 | } |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | UnstructuredField contentIdField = getEntity().getContentIdField(); |
---|
| 169 | if(contentIdField != null) { |
---|
| 170 | StringBuilder sb = new StringBuilder(); |
---|
| 171 | String text = contentIdField.getText(); |
---|
| 172 | int beginIdx = -1; |
---|
| 173 | int endIdx = -1; |
---|
| 174 | for(int i = 0; i < text.length(); i++) { |
---|
| 175 | char c = text.charAt(i); |
---|
| 176 | if(Character.isLetterOrDigit(c)) { |
---|
| 177 | sb.append(c); |
---|
| 178 | if(beginIdx == -1) { |
---|
| 179 | beginIdx = i; |
---|
| 180 | } |
---|
| 181 | endIdx = i; |
---|
| 182 | } |
---|
| 183 | else { |
---|
| 184 | sb.append("-"); |
---|
| 185 | } |
---|
| 186 | } |
---|
| 187 | fileName = sb.substring(beginIdx, endIdx + 1); |
---|
| 188 | } |
---|
| 189 | else { |
---|
| 190 | fileName = "part_" + getOid(); |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | if(contentTypeField != null) { |
---|
| 194 | if(contentTypeField.isImageJpegMimeType()) { |
---|
| 195 | fileName += ".jpg"; |
---|
| 196 | } |
---|
| 197 | else if(contentTypeField.isImageGifMimeType()) { |
---|
| 198 | fileName += ".gif"; |
---|
| 199 | } |
---|
| 200 | else if(contentTypeField.isImagePngMimeType()) { |
---|
| 201 | fileName += ".png"; |
---|
| 202 | } |
---|
| 203 | else if(contentTypeField.isImageBmpMimeType()) { |
---|
| 204 | fileName += ".svg"; |
---|
| 205 | } |
---|
| 206 | else if(contentTypeField.isImageTiffMimeType()) { |
---|
| 207 | fileName += ".tif"; |
---|
| 208 | } |
---|
| 209 | else if(contentTypeField.isImageIconMimeType()) { |
---|
| 210 | fileName += ".ico"; |
---|
| 211 | } |
---|
| 212 | else if(contentTypeField.isImageSvgXmlMimeType()) { |
---|
| 213 | fileName += ".svg"; |
---|
| 214 | } |
---|
| 215 | else { |
---|
| 216 | //TODO: acrescentar outros subtipos |
---|
| 217 | fileName += "." + contentTypeField.getSubType(); |
---|
| 218 | } |
---|
| 219 | } |
---|
| 220 | |
---|
| 221 | return fileName; |
---|
| 222 | } |
---|
| 223 | |
---|
| 224 | public Path getPath() { |
---|
| 225 | return userAppConfig.SERVER.getArchiveDir().resolve("temp").resolve(getOid()).resolve(getFileName()); |
---|
| 226 | } |
---|
| 227 | |
---|
| 228 | public URL getURL() { |
---|
| 229 | try { |
---|
| 230 | return new URL( |
---|
| 231 | "http", |
---|
| 232 | env.getInternetAddresses()[0].getHostAddress(), |
---|
| 233 | userAppConfig.SERVER.getPort(), |
---|
| 234 | "/temp/" + getOid() + "/" + getFileName()); |
---|
| 235 | } |
---|
| 236 | catch(MalformedURLException ex) { |
---|
| 237 | log.error(ex); |
---|
| 238 | return null; |
---|
| 239 | } |
---|
| 240 | } |
---|
| 241 | |
---|
| 242 | public URL getConfidentialURL() { |
---|
| 243 | try { |
---|
| 244 | return new URL( |
---|
| 245 | "https", |
---|
| 246 | env.getInternetAddresses()[0].getHostAddress(), |
---|
| 247 | userAppConfig.SERVER.getConfidentialPort(), |
---|
| 248 | "/temp/" + getOid() + "/" + getFileName()); |
---|
| 249 | } |
---|
| 250 | catch(MalformedURLException ex) { |
---|
| 251 | log.error(ex); |
---|
| 252 | return null; |
---|
| 253 | } |
---|
| 254 | } |
---|
| 255 | |
---|
| 256 | public void publish() throws IOException { |
---|
| 257 | |
---|
| 258 | if(Files.exists(getPath())) { |
---|
| 259 | //already published |
---|
| 260 | return; |
---|
| 261 | } |
---|
| 262 | |
---|
| 263 | log.info("Publishing: %s/%s", getOid(), getFileName()); |
---|
| 264 | |
---|
| 265 | if(Files.notExists(getPath().getParent())) { |
---|
| 266 | Files.createDirectories(getPath().getParent()); |
---|
| 267 | } |
---|
| 268 | |
---|
| 269 | InputStream is = getDecoderInputStream(); |
---|
| 270 | Files.copy(is, getPath()); |
---|
| 271 | is.close(); |
---|
| 272 | } |
---|
| 273 | |
---|
| 274 | } |
---|