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.mime4j.message;
020
021import org.apache.james.mime4j.codec.DecodeMonitor;
022import org.apache.james.mime4j.dom.MessageBuilder;
023import org.apache.james.mime4j.dom.MessageServiceFactory;
024import org.apache.james.mime4j.dom.MessageWriter;
025import org.apache.james.mime4j.stream.BodyDescriptorBuilder;
026import org.apache.james.mime4j.stream.MimeConfig;
027
028/**
029 * The default MessageBuilderFactory bundled with Mime4j.
030 *
031 * Supports the "StorageProvider", "MimeEntityConfig" and "MutableBodyDescriptorFactory"
032 * attributes.
033 */
034public class MessageServiceFactoryImpl extends MessageServiceFactory {
035
036    private BodyFactory bodyFactory = null;
037    private MimeConfig mimeEntityConfig = null;
038    private BodyDescriptorBuilder bodyDescriptorBuilder = null;
039    private DecodeMonitor decodeMonitor = null;
040    private Boolean flatMode = null;
041    private Boolean contentDecoding = null;
042
043    @Override
044    public MessageBuilder newMessageBuilder() {
045        DefaultMessageBuilder m = new DefaultMessageBuilder();
046        if (bodyFactory != null) m.setBodyFactory(bodyFactory);
047        if (mimeEntityConfig != null) m.setMimeEntityConfig(mimeEntityConfig);
048        if (bodyDescriptorBuilder != null) m.setBodyDescriptorBuilder(bodyDescriptorBuilder);
049        if (flatMode != null) m.setFlatMode(flatMode.booleanValue());
050        if (contentDecoding != null) m.setContentDecoding(contentDecoding.booleanValue());
051        if (decodeMonitor != null) m.setDecodeMonitor(decodeMonitor);
052        return m;
053    }
054
055    @Override
056    public MessageWriter newMessageWriter() {
057        return new DefaultMessageWriter();
058    }
059
060    @Override
061    public void setAttribute(String name, Object value)
062            throws IllegalArgumentException {
063        if ("BodyFactory".equals(name)) {
064            if (value instanceof BodyFactory) {
065                this.bodyFactory  = (BodyFactory) value;
066                return;
067            } else throw new IllegalArgumentException("Unsupported attribute value type for "+name+", expected a BodyFactory");
068        } else if ("MimeEntityConfig".equals(name)) {
069            if (value instanceof MimeConfig) {
070                this.mimeEntityConfig = (MimeConfig) value;
071                return;
072            } else throw new IllegalArgumentException("Unsupported attribute value type for "+name+", expected a MimeConfig");
073        } else if ("MutableBodyDescriptorFactory".equals(name)) {
074            if (value instanceof BodyDescriptorBuilder) {
075                this.bodyDescriptorBuilder  = (BodyDescriptorBuilder) value;
076                return;
077            } else throw new IllegalArgumentException("Unsupported attribute value type for "+name+", expected a MutableBodyDescriptorFactory");
078        } else if ("DecodeMonitor".equals(name)) {
079            if (value instanceof DecodeMonitor) {
080                this.decodeMonitor = (DecodeMonitor) value;
081                return;
082            } else throw new IllegalArgumentException("Unsupported attribute value type for "+name+", expected a DecodeMonitor");
083        } else if ("FlatMode".equals(name)) {
084            if (value instanceof Boolean) {
085                this.flatMode  = (Boolean) value;
086                return;
087            } else throw new IllegalArgumentException("Unsupported attribute value type for "+name+", expected a Boolean");
088        } else if ("ContentDecoding".equals(name)) {
089            if (value instanceof Boolean) {
090                this.contentDecoding = (Boolean) value;
091                return;
092            } else throw new IllegalArgumentException("Unsupported attribute value type for "+name+", expected a Boolean");
093        }
094
095        throw new IllegalArgumentException("Unsupported attribute: "+name);
096
097    }
098
099}