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

File RecipientConverter.java

 

Coverage histogram

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

Code metrics

10
14
1
1
68
35
6
0.43
14
1
6

Classes

Class Line # Actions
RecipientConverter 41 14 0% 6 2
0.9292%
 

Contributing tests

This file is covered by 2 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;
21   
22    import java.lang.reflect.Type;
23   
24    import javax.inject.Singleton;
25    import javax.mail.Message;
26    import javax.mail.internet.MimeMessage;
27   
28    import org.xwiki.component.annotation.Component;
29    import org.xwiki.properties.converter.AbstractConverter;
30    import org.xwiki.properties.converter.ConversionException;
31   
32    /**
33    * Converts a String to a {@link javax.mail.Message.RecipientType} instance. Useful when using the Mail Sender
34    * scripting API to add a recipient.
35    *
36    * @version $Id: 14fb858411f34343e29c3f4eb5e19639a0ccbfea $
37    * @since 6.1RC1
38    */
39    @Component
40    @Singleton
 
41    public class RecipientConverter extends AbstractConverter<Message.RecipientType>
42    {
 
43  7 toggle @Override
44    protected <G extends Message.RecipientType> G convertToType(Type targetType, Object value)
45    {
46  7 if (value == null) {
47  0 return null;
48    }
49   
50  7 Message.RecipientType recipientType;
51   
52  7 String valueAsString = value.toString();
53  7 if (valueAsString.equalsIgnoreCase("to")) {
54  3 recipientType = Message.RecipientType.TO;
55  4 } else if (valueAsString.equalsIgnoreCase("cc")) {
56  1 recipientType = Message.RecipientType.CC;
57  3 } else if (valueAsString.equalsIgnoreCase("bcc")) {
58  1 recipientType = Message.RecipientType.BCC;
59  2 } else if (valueAsString.equalsIgnoreCase("newsgroups")) {
60  1 recipientType = MimeMessage.RecipientType.NEWSGROUPS;
61    } else {
62  1 throw new ConversionException(String.format("Cannot convert [%s] to [%s]", value,
63    Message.RecipientType.class.getName()));
64    }
65   
66  6 return (G) recipientType;
67    }
68    }