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)
32 return url.toURI().toASCIIString();
34 catch (URISyntaxException e)
36 log.error("Cannot convert invalid URL \"{}\" to ASCII-string: {}", url, e.getMessage());
37 throw new IllegalArgumentException(e);
42 public URL convertToEntityAttribute(String string)
46 return URI.create(string).toURL();
48 catch (MalformedURLException e)
50 log.error("Cannot convert invalid string \"{}\" to URL: {}", string, e.getMessage());
51 throw new IllegalArgumentException(e);