/
MimePartParser.java
122 lines (108 loc) · 4.97 KB
/
MimePartParser.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/****************************************************************
* Licensed to the Apache Software Foundation (ASF) under one *
* or more contributor license agreements. See the NOTICE file *
* distributed with this work for additional information *
* regarding copyright ownership. The ASF licenses this file *
* to you under the Apache License, Version 2.0 (the *
* "License"); you may not use this file except in compliance *
* with the License. You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, *
* software distributed under the License is distributed on an *
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
* KIND, either express or implied. See the License for the *
* specific language governing permissions and limitations *
* under the License. *
****************************************************************/
package org.apache.james.mailbox.elasticsearch.json;
import com.google.common.base.Preconditions;
import org.apache.james.mailbox.store.extractor.TextExtractor;
import org.apache.james.mailbox.store.mail.model.MailboxId;
import org.apache.james.mailbox.store.mail.model.MailboxMessage;
import org.apache.james.mime4j.MimeException;
import org.apache.james.mime4j.message.DefaultBodyDescriptorBuilder;
import org.apache.james.mime4j.message.MaximalBodyDescriptor;
import org.apache.james.mime4j.stream.EntityState;
import org.apache.james.mime4j.stream.MimeConfig;
import org.apache.james.mime4j.stream.MimeTokenStream;
import java.io.IOException;
import java.util.Deque;
import java.util.LinkedList;
public class MimePartParser {
private final MailboxMessage<? extends MailboxId> message;
private final TextExtractor textExtractor;
private final MimeTokenStream stream;
private final Deque<MimePartContainerBuilder> builderStack;
private MimePart result;
private MimePartContainerBuilder currentlyBuildMimePart;
public MimePartParser(MailboxMessage<? extends MailboxId> message, TextExtractor textExtractor) {
this.message = message;
this.textExtractor = textExtractor;
this.builderStack = new LinkedList<>();
this.currentlyBuildMimePart = new RootMimePartContainerBuilder();
this.stream = new MimeTokenStream(
MimeConfig.custom().setMaxLineLen(-1).setMaxHeaderLen(-1).build(),
new DefaultBodyDescriptorBuilder());
}
public MimePart parse() throws IOException, MimeException {
stream.parse(message.getFullContent());
for (EntityState state = stream.getState(); state != EntityState.T_END_OF_STREAM; state = stream.next()) {
processMimePart(stream, state);
}
return result;
}
private void processMimePart(MimeTokenStream stream, EntityState state) throws IOException {
switch (state) {
case T_START_MULTIPART:
case T_START_MESSAGE:
stackCurrent();
break;
case T_START_HEADER:
currentlyBuildMimePart = MimePart.builder();
break;
case T_FIELD:
currentlyBuildMimePart.addToHeaders(stream.getField());
break;
case T_BODY:
manageBodyExtraction(stream);
closeMimePart();
break;
case T_END_MULTIPART:
case T_END_MESSAGE:
unstackToCurrent();
closeMimePart();
break;
default:
break;
}
}
private void stackCurrent() {
builderStack.push(currentlyBuildMimePart);
currentlyBuildMimePart = null;
}
private void unstackToCurrent() {
currentlyBuildMimePart = builderStack.pop();
}
private void closeMimePart() {
MimePart bodyMimePart = currentlyBuildMimePart.using(textExtractor).build();
if (!builderStack.isEmpty()) {
builderStack.peek().addChild(bodyMimePart);
} else {
Preconditions.checkState(result == null);
result = bodyMimePart;
}
}
private void manageBodyExtraction(MimeTokenStream stream) throws IOException {
extractMimePartBodyDescription(stream);
currentlyBuildMimePart.addBodyContent(stream.getDecodedInputStream());
}
private void extractMimePartBodyDescription(MimeTokenStream stream) {
final MaximalBodyDescriptor descriptor = (MaximalBodyDescriptor) stream.getBodyDescriptor();
currentlyBuildMimePart.addMediaType(descriptor.getMediaType())
.addSubType(descriptor.getSubType())
.addContentDisposition(descriptor.getContentDispositionType())
.addFileName(descriptor.getContentDispositionFilename());
}
}