001/**************************************************************** 002 * Licensed to the Apache Software Foundation (ASF) under one * 003 * or more contributor license agreements. See the NOTICE file * 004 * distributed with this work for additional information * 005 * regarding copyright ownership. The ASF licenses this file * 006 * to you under the Apache License, Version 2.0 (the * 007 * "License"); you may not use this file except in compliance * 008 * with the License. You may obtain a copy of the License at * 009 * * 010 * http://www.apache.org/licenses/LICENSE-2.0 * 011 * * 012 * Unless required by applicable law or agreed to in writing, * 013 * software distributed under the License is distributed on an * 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * 015 * KIND, either express or implied. See the License for the * 016 * specific language governing permissions and limitations * 017 * under the License. * 018 ****************************************************************/ 019package org.apache.james.mailbox.jpa.mail.model.openjpa; 020 021import java.io.ByteArrayInputStream; 022import java.io.IOException; 023import java.io.InputStream; 024import java.util.Date; 025 026import javax.mail.Flags; 027import javax.mail.internet.SharedInputStream; 028import javax.persistence.Basic; 029import javax.persistence.Column; 030import javax.persistence.Entity; 031import javax.persistence.FetchType; 032import javax.persistence.Lob; 033import javax.persistence.Table; 034 035import org.apache.commons.io.IOUtils; 036import org.apache.james.mailbox.exception.MailboxException; 037import org.apache.james.mailbox.jpa.mail.model.JPAMailbox; 038import org.apache.james.mailbox.store.mail.model.Message; 039import org.apache.james.mailbox.store.mail.model.impl.PropertyBuilder; 040import org.apache.openjpa.persistence.Externalizer; 041import org.apache.openjpa.persistence.Factory; 042 043@Entity(name="Message") 044@Table(name="JAMES_MAIL") 045public class JPAEncryptedMessage extends AbstractJPAMessage { 046 047 /** The value for the body field. Lazy loaded */ 048 /** We use a max length to represent 1gb data. Thats prolly overkill, but who knows */ 049 @Basic(optional = false, fetch = FetchType.LAZY) 050 @Column(name = "MAIL_BYTES", length = 1048576000, nullable = false) 051 @Externalizer("EncryptDecryptHelper.getEncrypted") 052 @Factory("EncryptDecryptHelper.getDecrypted") 053 @Lob private byte[] body; 054 055 056 /** The value for the header field. Lazy loaded */ 057 /** We use a max length to represent 1gb data. Thats prolly overkill, but who knows */ 058 @Basic(optional = false, fetch = FetchType.LAZY) 059 @Column(name = "HEADER_BYTES", length = 10485760, nullable = false) 060 @Externalizer("EncryptDecryptHelper.getEncrypted") 061 @Factory("EncryptDecryptHelper.getDecrypted") 062 @Lob private byte[] header; 063 064 @Deprecated 065 public JPAEncryptedMessage() {} 066 067 public JPAEncryptedMessage(JPAMailbox mailbox,Date internalDate, int size, Flags flags, SharedInputStream content, int bodyStartOctet, final PropertyBuilder propertyBuilder) throws MailboxException { 068 super(mailbox, internalDate, flags, size ,bodyStartOctet, propertyBuilder); 069 try { 070 int headerEnd = bodyStartOctet; 071 if (headerEnd < 0) { 072 headerEnd = 0; 073 } 074 this.header = IOUtils.toByteArray(content.newStream(0, headerEnd)); 075 this.body = IOUtils.toByteArray(content.newStream(getBodyStartOctet(), -1)); 076 077 } catch (IOException e) { 078 throw new MailboxException("Unable to parse message",e); 079 } 080 } 081 082 /** 083 * Create a copy of the given message 084 * 085 * @param message 086 * @throws MailboxException 087 */ 088 public JPAEncryptedMessage(JPAMailbox mailbox, long uid, long modSeq, Message<?> message) throws MailboxException{ 089 super(mailbox, uid, modSeq, message); 090 try { 091 this.body = IOUtils.toByteArray(message.getBodyContent()); 092 this.header = IOUtils.toByteArray(message.getHeaderContent()); 093 } catch (IOException e) { 094 throw new MailboxException("Unable to parse message",e); 095 } 096 } 097 098 099 /** 100 * @see org.apache.james.mailbox.store.mail.model.Message#getBodyContent() 101 */ 102 public InputStream getBodyContent() throws IOException { 103 return new ByteArrayInputStream(body); 104 } 105 106 /** 107 * @see org.apache.james.mailbox.store.mail.model.Message#getHeaderContent() 108 */ 109 public InputStream getHeaderContent() throws IOException { 110 return new ByteArrayInputStream(header); 111 } 112 113}