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 ****************************************************************/
019 package org.apache.james.mailbox.jpa.mail.model.openjpa;
020
021 import java.io.ByteArrayInputStream;
022 import java.io.IOException;
023 import java.io.InputStream;
024 import java.util.Date;
025
026 import javax.mail.Flags;
027 import javax.mail.internet.SharedInputStream;
028 import javax.persistence.Basic;
029 import javax.persistence.Column;
030 import javax.persistence.Entity;
031 import javax.persistence.FetchType;
032 import javax.persistence.Lob;
033 import javax.persistence.Table;
034
035 import org.apache.commons.io.IOUtils;
036 import org.apache.james.mailbox.exception.MailboxException;
037 import org.apache.james.mailbox.jpa.mail.model.JPAMailbox;
038 import org.apache.james.mailbox.store.mail.model.Message;
039 import org.apache.james.mailbox.store.mail.model.impl.PropertyBuilder;
040
041 @Entity(name="Message")
042 @Table(name="JAMES_MAIL")
043 public class JPAMessage extends AbstractJPAMessage {
044
045 /** The value for the body field. Lazy loaded */
046 /** We use a max length to represent 1gb data. Thats prolly overkill, but who knows */
047 @Basic(optional = false, fetch = FetchType.LAZY)
048 @Column(name = "MAIL_BYTES", length = 1048576000, nullable = false)
049 @Lob private byte[] body;
050
051
052 /** The value for the header field. Lazy loaded */
053 /** We use a max length to represent 1gb data. Thats prolly overkill, but who knows */
054 @Basic(optional = false, fetch = FetchType.LAZY)
055 @Column(name = "HEADER_BYTES", length = 10485760, nullable = false)
056 @Lob private byte[] header;
057
058 @Deprecated
059 public JPAMessage() {}
060
061 public JPAMessage(JPAMailbox mailbox,Date internalDate, int size, Flags flags, SharedInputStream content, int bodyStartOctet, final PropertyBuilder propertyBuilder) throws MailboxException {
062 super(mailbox, internalDate, flags, size ,bodyStartOctet, propertyBuilder);
063 try {
064 int headerEnd = bodyStartOctet;
065 if (headerEnd < 0) {
066 headerEnd = 0;
067 }
068 this.header = IOUtils.toByteArray(content.newStream(0, headerEnd));
069 this.body = IOUtils.toByteArray(content.newStream(getBodyStartOctet(), -1));
070
071 } catch (IOException e) {
072 throw new MailboxException("Unable to parse message",e);
073 }
074 }
075
076 /**
077 * Create a copy of the given message
078 *
079 * @param message
080 * @throws MailboxException
081 */
082 public JPAMessage(JPAMailbox mailbox, long uid, long modSeq, Message<?> message) throws MailboxException{
083 super(mailbox, uid, modSeq, message);
084 try {
085 this.body = IOUtils.toByteArray(message.getBodyContent());
086 this.header = IOUtils.toByteArray(message.getHeaderContent());
087 } catch (IOException e) {
088 throw new MailboxException("Unable to parse message",e);
089 }
090 }
091
092
093 /**
094 * @see org.apache.james.mailbox.store.mail.model.Message#getBodyContent()
095 */
096 public InputStream getBodyContent() throws IOException {
097 return new ByteArrayInputStream(body);
098 }
099
100 /**
101 * @see org.apache.james.mailbox.store.mail.model.Message#getHeaderContent()
102 */
103 public InputStream getHeaderContent() throws IOException {
104 return new ByteArrayInputStream(header);
105 }
106
107 }