1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.mail.internal.factory.text

File TextMimeBodyPartFactory.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

2
9
2
1
83
36
3
0.33
4.5
2
1.5

Classes

Class Line # Actions
TextMimeBodyPartFactory 43 9 0% 3 0
1.0100%
 

Contributing tests

This file is covered by 7 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.mail.internal.factory.text;
21   
22    import java.nio.charset.StandardCharsets;
23    import java.util.Map;
24   
25    import javax.inject.Inject;
26    import javax.inject.Singleton;
27    import javax.mail.MessagingException;
28    import javax.mail.internet.MimeBodyPart;
29   
30    import org.apache.commons.lang3.StringUtils;
31    import org.slf4j.Logger;
32    import org.xwiki.component.annotation.Component;
33    import org.xwiki.mail.internal.factory.AbstractMimeBodyPartFactory;
34   
35    /**
36    * Creates text message body Part to be added to a Multi Part message.
37    *
38    * @version $Id: 1f8d8a93d5b240f1e127e60a7bdfbf6b036d4d0d $
39    * @since 6.1M2
40    */
41    @Component
42    @Singleton
 
43    public class TextMimeBodyPartFactory extends AbstractMimeBodyPartFactory<String>
44    {
45    private static final String TEXT_PLAIN_CONTENT_TYPE = "text/plain; charset=" + StandardCharsets.UTF_8.name();
46   
47    /**
48    * Provides access to the logger.
49    */
50    @Inject
51    private Logger logger;
52   
 
53  17 toggle @Override
54    public MimeBodyPart create(String content, Map<String, Object> parameters) throws MessagingException
55    {
56    // Create the body part of the email
57  17 MimeBodyPart bodyPart = new MimeBodyPart();
58   
59  17 bodyPart.setContent(content, TEXT_PLAIN_CONTENT_TYPE);
60   
61  17 bodyPart.setHeader("Content-Type", getMimetype(parameters));
62   
63    // Handle headers passed as parameter
64  17 addHeaders(bodyPart, parameters);
65   
66  17 return bodyPart;
67    }
68   
69    /**
70    * Extracts the mime type from the passed parameters Map.
71    *
72    * @param parameters the parameters from which to extract the mime type if defined
73    * @return either the extracted mimetype or {@code text/plain} if not the property is not passed in the parameters
74    */
 
75  17 toggle protected String getMimetype(Map<String, Object> parameters)
76    {
77  17 String mimeType = (String) parameters.get("mimetype");
78  17 if (StringUtils.isEmpty(mimeType)) {
79  7 mimeType = TEXT_PLAIN_CONTENT_TYPE;
80    }
81  17 return mimeType;
82    }
83    }