1
/****************************************************************
2
* Licensed to the Apache Software Foundation (ASF) under one *
3
* or more contributor license agreements. See the NOTICE file *
4
* distributed with this work for additional information *
5
* regarding copyright ownership. The ASF licenses this file *
6
* to you under the Apache License, Version 2.0 (the *
7
* "License"); you may not use this file except in compliance *
8
* with the License. You may obtain a copy of the License at *
10
* http://www.apache.org/licenses/LICENSE-2.0 *
12
* Unless required by applicable law or agreed to in writing, *
13
* software distributed under the License is distributed on an *
14
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15
* KIND, either express or implied. See the License for the *
16
* specific language governing permissions and limitations *
17
* under the License. *
18
****************************************************************/
19
package org.apache.james.mime4j.message;
21
import org.apache.james.mime4j.codec.DecodeMonitor;
22
import org.apache.james.mime4j.dom.MessageBuilder;
23
import org.apache.james.mime4j.dom.MessageServiceFactory;
24
import org.apache.james.mime4j.dom.MessageWriter;
25
import org.apache.james.mime4j.stream.BodyDescriptorBuilder;
26
import org.apache.james.mime4j.stream.MimeConfig;
29
* The default MessageBuilderFactory bundled with Mime4j.
31
* Supports the "StorageProvider", "MimeEntityConfig" and "MutableBodyDescriptorFactory"
34
public class MessageServiceFactoryImpl extends MessageServiceFactory {
36
private BodyFactory bodyFactory = null;
37
private MimeConfig mimeEntityConfig = null;
38
private BodyDescriptorBuilder bodyDescriptorBuilder = null;
39
private DecodeMonitor decodeMonitor = null;
40
private Boolean flatMode = null;
41
private Boolean contentDecoding = null;
44
public MessageBuilder newMessageBuilder() {
45
DefaultMessageBuilder m = new DefaultMessageBuilder();
46
if (bodyFactory != null) m.setBodyFactory(bodyFactory);
47
if (mimeEntityConfig != null) m.setMimeEntityConfig(mimeEntityConfig);
48
if (bodyDescriptorBuilder != null) m.setBodyDescriptorBuilder(bodyDescriptorBuilder);
49
if (flatMode != null) m.setFlatMode(flatMode.booleanValue());
50
if (contentDecoding != null) m.setContentDecoding(contentDecoding.booleanValue());
51
if (decodeMonitor != null) m.setDecodeMonitor(decodeMonitor);
56
public MessageWriter newMessageWriter() {
57
return new DefaultMessageWriter();
61
public void setAttribute(String name, Object value)
62
throws IllegalArgumentException {
63
if ("BodyFactory".equals(name)) {
64
if (value instanceof BodyFactory) {
65
this.bodyFactory = (BodyFactory) value;
67
} else throw new IllegalArgumentException("Unsupported attribute value type for "+name+", expected a BodyFactory");
68
} else if ("MimeEntityConfig".equals(name)) {
69
if (value instanceof MimeConfig) {
70
this.mimeEntityConfig = (MimeConfig) value;
72
} else throw new IllegalArgumentException("Unsupported attribute value type for "+name+", expected a MimeConfig");
73
} else if ("MutableBodyDescriptorFactory".equals(name)) {
74
if (value instanceof BodyDescriptorBuilder) {
75
this.bodyDescriptorBuilder = (BodyDescriptorBuilder) value;
77
} else throw new IllegalArgumentException("Unsupported attribute value type for "+name+", expected a MutableBodyDescriptorFactory");
78
} else if ("DecodeMonitor".equals(name)) {
79
if (value instanceof DecodeMonitor) {
80
this.decodeMonitor = (DecodeMonitor) value;
82
} else throw new IllegalArgumentException("Unsupported attribute value type for "+name+", expected a DecodeMonitor");
83
} else if ("FlatMode".equals(name)) {
84
if (value instanceof Boolean) {
85
this.flatMode = (Boolean) value;
87
} else throw new IllegalArgumentException("Unsupported attribute value type for "+name+", expected a Boolean");
88
} else if ("ContentDecoding".equals(name)) {
89
if (value instanceof Boolean) {
90
this.contentDecoding = (Boolean) value;
92
} else throw new IllegalArgumentException("Unsupported attribute value type for "+name+", expected a Boolean");
95
throw new IllegalArgumentException("Unsupported attribute: "+name);