Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
MailConverters |
|
| 0.0;0 |
1 | /** |
|
2 | * Licensed to the Apache Software Foundation (ASF) under one or more |
|
3 | * contributor license agreements. See the NOTICE file distributed with |
|
4 | * this work for additional information regarding copyright ownership. |
|
5 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
|
6 | * (the "License"); you may not use this file except in compliance with |
|
7 | * the License. You may obtain a copy of the License at |
|
8 | * |
|
9 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
10 | * |
|
11 | * Unless required by applicable law or agreed to in writing, software |
|
12 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
14 | * See the License for the specific language governing permissions and |
|
15 | * limitations under the License. |
|
16 | */ |
|
17 | package org.apache.camel.component.mail; |
|
18 | ||
19 | import java.io.IOException; |
|
20 | ||
21 | import javax.mail.BodyPart; |
|
22 | import javax.mail.Message; |
|
23 | import javax.mail.MessagingException; |
|
24 | import javax.mail.Multipart; |
|
25 | import javax.mail.internet.MimeMultipart; |
|
26 | ||
27 | import org.apache.camel.Converter; |
|
28 | ||
29 | /** |
|
30 | * @version $Revision: 1.1 $ |
|
31 | */ |
|
32 | @Converter |
|
33 | 0 | public class MailConverters { |
34 | /** |
|
35 | * Converts the given JavaMail message to a String body |
|
36 | * |
|
37 | * @param message the message |
|
38 | * @return the String content |
|
39 | * @throws MessagingException |
|
40 | * @throws IOException |
|
41 | */ |
|
42 | @Converter |
|
43 | public String toString(Message message) throws MessagingException, IOException { |
|
44 | 0 | Object content = message.getContent(); |
45 | 0 | if (content instanceof MimeMultipart) { |
46 | 0 | MimeMultipart multipart = (MimeMultipart) content; |
47 | 0 | if (multipart.getCount() > 0) { |
48 | 0 | BodyPart part = multipart.getBodyPart(0); |
49 | 0 | content = part.getContent(); |
50 | } |
|
51 | } |
|
52 | 0 | if (content != null) { |
53 | 0 | return content.toString(); |
54 | } |
|
55 | 0 | return null; |
56 | } |
|
57 | ||
58 | @Converter |
|
59 | public static String toString(Multipart multipart) throws MessagingException, IOException { |
|
60 | 1 | int size = multipart.getCount(); |
61 | 2 | for (int i = 0; i < size; i++) { |
62 | 2 | BodyPart part = multipart.getBodyPart(i); |
63 | 2 | if (part.getContentType().startsWith("text")) { |
64 | 1 | return part.getContent().toString(); |
65 | } |
|
66 | } |
|
67 | 0 | return null; |
68 | } |
|
69 | } |