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
020package org.apache.james.mailbox.jpa.openjpa;
021
022import java.util.Date;
023
024import javax.mail.Flags;
025import javax.mail.internet.SharedInputStream;
026
027import org.apache.james.mailbox.MailboxPathLocker;
028import org.apache.james.mailbox.acl.GroupMembershipResolver;
029import org.apache.james.mailbox.acl.MailboxACLResolver;
030import org.apache.james.mailbox.exception.MailboxException;
031import org.apache.james.mailbox.jpa.JPAMessageManager;
032import org.apache.james.mailbox.jpa.mail.model.JPAMailbox;
033import org.apache.james.mailbox.jpa.mail.model.openjpa.JPAEncryptedMessage;
034import org.apache.james.mailbox.jpa.mail.model.openjpa.JPAStreamingMessage;
035import org.apache.james.mailbox.store.MailboxEventDispatcher;
036import org.apache.james.mailbox.store.MailboxSessionMapperFactory;
037import org.apache.james.mailbox.store.mail.model.Mailbox;
038import org.apache.james.mailbox.store.mail.model.Message;
039import org.apache.james.mailbox.store.mail.model.impl.PropertyBuilder;
040import org.apache.james.mailbox.store.search.MessageSearchIndex;
041
042/**
043 * OpenJPA implementation of Mailbox
044 */
045public class OpenJPAMessageManager extends JPAMessageManager {
046
047    private final AdvancedFeature feature;
048
049    public static enum AdvancedFeature {
050        None,
051        Streaming,
052        Encryption
053    }
054    
055    public OpenJPAMessageManager(MailboxSessionMapperFactory<Long> mapperFactory, MessageSearchIndex<Long> index,
056            MailboxEventDispatcher<Long> dispatcher, MailboxPathLocker locker, Mailbox<Long> mailbox, MailboxACLResolver aclResolver, GroupMembershipResolver groupMembershipResolver) throws MailboxException {
057        this(mapperFactory, index, dispatcher, locker,  mailbox, AdvancedFeature.None, aclResolver, groupMembershipResolver);
058    }
059
060    public OpenJPAMessageManager(MailboxSessionMapperFactory<Long> mapperFactory, MessageSearchIndex<Long> index, 
061            MailboxEventDispatcher<Long> dispatcher, MailboxPathLocker locker, Mailbox<Long> mailbox, final AdvancedFeature f, MailboxACLResolver aclResolver, GroupMembershipResolver groupMembershipResolver) throws MailboxException {
062        super(mapperFactory,  index, dispatcher, locker, mailbox, aclResolver, groupMembershipResolver);
063        this.feature = f;
064    }
065
066    @Override
067    protected Message<Long> createMessage(Date internalDate, int size, int bodyStartOctet, SharedInputStream content, Flags flags, PropertyBuilder propertyBuilder) throws MailboxException {
068        int headerEnd = bodyStartOctet -2;
069        if (headerEnd < 0) {
070            headerEnd = 0;
071        }
072        switch (feature) {
073        case Streaming:
074            return new JPAStreamingMessage((JPAMailbox) getMailboxEntity(), internalDate, size, flags, content, bodyStartOctet, propertyBuilder);
075        case Encryption:
076            return new JPAEncryptedMessage((JPAMailbox) getMailboxEntity(), internalDate, size, flags, content, bodyStartOctet, propertyBuilder);
077        default:
078            return super.createMessage(internalDate, size, bodyStartOctet, content, flags,  propertyBuilder);
079        }
080       
081    }
082
083    
084
085}