1 package de.juplo.jpa.converters;
3 import java.net.MalformedURLException;
5 import java.net.URISyntaxException;
7 import javax.persistence.AttributeConverter;
8 import javax.persistence.Converter;
9 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
14 * Converts an {@link URL} to an ASCII-representation using the class
17 * If the {@link URL} cannot be converted to an {@link URI}, an
18 * {@link IllegalArgumentException} is thrown.
21 @Converter(autoApply = true)
22 public class URLConverter implements AttributeConverter<URL, String>
24 private final static Logger log = LoggerFactory.getLogger(URLConverter.class);
28 public String convertToDatabaseColumn(URL url)
34 return url.toURI().toASCIIString();
36 catch (URISyntaxException e)
38 log.error("Cannot convert invalid URL \"{}\" to ASCII-string: {}", url, e.getMessage());
39 throw new IllegalArgumentException(e);
44 public URL convertToEntityAttribute(String string)
50 return URI.create(string).toURL();
52 catch (MalformedURLException e)
54 log.error("Cannot convert invalid string \"{}\" to URL: {}", string, e.getMessage());
55 throw new IllegalArgumentException(e);