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

File AddressConverter.java

 

Coverage histogram

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

Code metrics

2
8
2
1
68
34
4
0.5
4
2
2

Classes

Class Line # Actions
AddressConverter 42 8 0% 4 2
0.833333383.3%
 

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 to a {@link javax.mail.Address} instance. Useful when using the Mail Sender
35    * scripting API to add a recipient.
36    *
37    * @version $Id: 060a5eaaaca77530368e71b11441171ff46937f8 $
38    * @since 6.2M1
39    */
40    @Component
41    @Singleton
 
42    public class AddressConverter extends AbstractConverter<Address>
43    {
 
44  12 toggle @Override
45    protected Address convertToType(Type targetType, Object value)
46    {
47  12 if (value == null) {
48  2 return null;
49    }
50   
51  10 Address address;
52   
53  10 try {
54  10 address = InternetAddress.parse(value.toString())[0];
55    } catch (AddressException e) {
56  1 throw new ConversionException(
57    String.format("Failed to convert [%s] to [%s]", value, Address.class.getName()), e);
58    }
59   
60  9 return address;
61    }
62   
 
63  0 toggle @Override
64    protected String convertToString(Address value)
65    {
66  0 return InternetAddress.toString(new Address[] {value});
67    }
68    }