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

File AddressesConverter.java

 

Coverage histogram

../../../../img/srcFileCovDistChart9.png
38% of files have more coverage

Code metrics

4
10
2
1
72
37
5
0.5
5
2
2.5

Classes

Class Line # Actions
AddressesConverter 42 10 0% 5 2
0.87587.5%
 

Contributing tests

This file is covered by 4 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.Address;
26    import javax.mail.internet.AddressException;
27    import javax.mail.internet.InternetAddress;
28   
29    import org.xwiki.component.annotation.Component;
30    import org.xwiki.properties.converter.AbstractConverter;
31    import org.xwiki.properties.converter.ConversionException;
32   
33    /**
34    * Converts a String listing one or several coma-separated email addresses to an array of {@link javax.mail.Address}
35    * instance. Useful when using the Mail Sender scripting API to add a recipient.
36    *
37    * @version $Id: 38dfe0939f5c4806fda1bbfa12e0316bb0832781 $
38    * @since 6.1RC1
39    */
40    @Component
41    @Singleton
 
42    public class AddressesConverter extends AbstractConverter<Address[]>
43    {
 
44  24 toggle @Override
45    protected Address[] convertToType(Type targetType, Object value)
46    {
47  24 if (value == null) {
48  18 return null;
49    }
50   
51  6 if (value instanceof Address[]) {
52  1 return (Address[]) value;
53    }
54   
55  5 Address[] addresses;
56   
57  5 try {
58  5 addresses = InternetAddress.parse(value.toString());
59    } catch (AddressException e) {
60  1 throw new ConversionException(
61    String.format("Failed to convert [%s] to an array of [%s]", value, Address.class.getName()), e);
62    }
63   
64  4 return addresses;
65    }
66   
 
67  0 toggle @Override
68    protected String convertToString(Address[] value)
69    {
70  0 return InternetAddress.toString(value);
71    }
72    }