001// Generated by delombok at Sat Feb 25 17:58:34 UTC 2023 002package io.cui.tools.io; 003 004import static io.cui.tools.base.Preconditions.checkArgument; 005import java.io.IOException; 006import java.io.InputStream; 007import java.net.MalformedURLException; 008import java.net.URL; 009import java.net.URLConnection; 010import java.util.Optional; 011import java.util.concurrent.TimeUnit; 012import io.cui.tools.logging.CuiLogger; 013 014/** 015 * This {@link FileLoader} takes a {@link URL} as its parameter which is useful when e.g. iterating 016 * over an 017 * enumeration of URLs from {@link ClassLoader#getResources(String)}. It converts the given URL to a 018 * {@code String} 019 * and prefixes it with {@link FileTypePrefix#URL}. 020 * 021 * @author Sven Haag 022 */ 023public class UrlLoader implements FileLoader { 024 private static final long serialVersionUID = -8758614099334823819L; 025 private static final CuiLogger log = new CuiLogger(UrlLoader.class); 026 private final URL url; 027 private transient URLConnection connection; 028 029 /** 030 * @param url representing a valid URL 031 * @throws IllegalArgumentException indicating that the given String represents a valid URL 032 */ 033 public UrlLoader(final String url) { 034 var sanitizedUrl = url; 035 if (FileTypePrefix.URL.is(url)) { 036 sanitizedUrl = FileTypePrefix.URL.removePrefix(url); 037 } 038 try { 039 this.url = new URL(sanitizedUrl); 040 } catch (final MalformedURLException e) { 041 throw new IllegalArgumentException("Provided URL is invalid: " + url, e); 042 } 043 } 044 045 /** 046 * @param url hopefully a JAR URL 047 */ 048 public UrlLoader(final URL url) { 049 checkArgument(null != url, "url must not be null"); 050 this.url = url; 051 } 052 053 /** 054 * @return true, if a connection to {@link #getURL()} can be established 055 */ 056 @Override 057 public boolean isReadable() { 058 try { 059 var con = getConnection(); 060 if (con.isPresent()) { 061 con.get().connect(); 062 return true; 063 } 064 } catch (final IOException e) { 065 log.error(e, "Could not read from URL: {}", getURL()); 066 } 067 return false; 068 } 069 070 @Override 071 public StructuredFilename getFileName() { 072 return new StructuredFilename(getURL().getPath()); 073 } 074 075 @Override 076 public InputStream inputStream() throws IOException { 077 var con = getConnection(); 078 if (con.isPresent()) { 079 return con.get().getInputStream(); 080 } 081 return null; 082 } 083 084 @Override 085 public URL getURL() { 086 return url; 087 } 088 089 @Override 090 public boolean isFilesystemLoader() { 091 return false; 092 } 093 094 private Optional<URLConnection> getConnection() { 095 if (null == connection) { 096 try { 097 connection = url.openConnection(); 098 connection.setConnectTimeout((int) TimeUnit.SECONDS.toMillis(5)); 099 connection.setReadTimeout((int) TimeUnit.SECONDS.toMillis(5)); 100 } catch (final IOException e) { 101 log.error(e, "Portal-538: Could not read from URL: {}", getURL()); 102 } 103 } 104 return Optional.ofNullable(connection); 105 } 106 107 @java.lang.Override 108 @java.lang.SuppressWarnings("all") 109 @lombok.Generated 110 public boolean equals(final java.lang.Object o) { 111 if (o == this) return true; 112 if (!(o instanceof UrlLoader)) return false; 113 final UrlLoader other = (UrlLoader) o; 114 if (!other.canEqual((java.lang.Object) this)) return false; 115 final java.lang.Object this$url = this.getURL(); 116 final java.lang.Object other$url = other.getURL(); 117 if (this$url == null ? other$url != null : !this$url.equals(other$url)) return false; 118 return true; 119 } 120 121 @java.lang.SuppressWarnings("all") 122 @lombok.Generated 123 protected boolean canEqual(final java.lang.Object other) { 124 return other instanceof UrlLoader; 125 } 126 127 @java.lang.Override 128 @java.lang.SuppressWarnings("all") 129 @lombok.Generated 130 public int hashCode() { 131 final int PRIME = 59; 132 int result = 1; 133 final java.lang.Object $url = this.getURL(); 134 result = result * PRIME + ($url == null ? 43 : $url.hashCode()); 135 return result; 136 } 137 138 @java.lang.Override 139 @java.lang.SuppressWarnings("all") 140 @lombok.Generated 141 public java.lang.String toString() { 142 return "UrlLoader(url=" + this.getURL() + ")"; 143 } 144}