diff --git a/src/main/java/edu/kit/scc/dem/wapsrv/model/formats/JsonLdFormatter.java b/src/main/java/edu/kit/scc/dem/wapsrv/model/formats/JsonLdFormatter.java index 106ab16..e3b5164 100644 --- a/src/main/java/edu/kit/scc/dem/wapsrv/model/formats/JsonLdFormatter.java +++ b/src/main/java/edu/kit/scc/dem/wapsrv/model/formats/JsonLdFormatter.java @@ -1,8 +1,8 @@ package edu.kit.scc.dem.wapsrv.model.formats; import java.io.IOException; -import java.net.MalformedURLException; -import java.net.URL; +import java.net.URI; +import java.net.URISyntaxException; import java.util.*; import java.util.regex.Pattern; import org.apache.commons.collections4.map.ListOrderedMap; @@ -40,11 +40,11 @@ public final class JsonLdFormatter extends AbstractFormatter { /** * The default JSON-LD Profile */ - public static final URL DEFAULT_PROFILE = makeUrl("http://www.w3.org/ns/anno.jsonld"); + public static final URI DEFAULT_PROFILE = makeUri("http://www.w3.org/ns/anno.jsonld"); /** * The LDP profile for containers */ - public static final URL LDP_PROFILE = makeUrl("http://www.w3.org/ns/ldp.jsonld"); + public static final URI LDP_PROFILE = makeUri("http://www.w3.org/ns/ldp.jsonld"); /** * The string identifying JSON-LD */ @@ -52,7 +52,7 @@ public final class JsonLdFormatter extends AbstractFormatter { /** * The set of used profiles */ - private final Set profiles = new HashSet<>(); + private final Set profiles = new HashSet<>(); /** * The profile registry */ @@ -76,10 +76,10 @@ public JsonLdFormatter() { * @param urlString The String to generate a URL from * @return The generated URL Object */ - private static URL makeUrl(String urlString) { + private static URI makeUri(String urlString) { try { - return new java.net.URL(urlString); - } catch (java.net.MalformedURLException e) { + return new URI(urlString); + } catch (URISyntaxException e) { LoggerFactory.getLogger(ContentNegotiator.class).error("DEFAULT_PROFILE is an invalid URL : " + urlString); return null; } @@ -210,7 +210,7 @@ private String applyProfiles(String jsonLd, String frameString) { jsonObject = JsonLdProcessor.frame(jsonObject, frameObject, options); // Collect contexts from profiles - for (URL url : profiles) { + for (URI url : profiles) { contexts.add(url.toString()); } @@ -228,7 +228,7 @@ private String applyProfiles(String jsonLd, String frameString) { } } } - for (URL url : profiles) { + for (URI url : profiles) { contexts.add(url.toString()); } contexts = deduplicateContexts(contexts); @@ -335,7 +335,7 @@ public void setAcceptPart(String profilesRaw, Type type) { for (String profile : profiles) { String trimmedProfile = profile.trim(); try { - URL url = new URL(trimmedProfile); + URI url = new URI(trimmedProfile); // At least a valid url, but is it locally cached? // We do not support uncached profiles. Inform the registry we need it cached. if (profileRegistry.cacheProfile(url)) { @@ -344,7 +344,7 @@ public void setAcceptPart(String profilesRaw, Type type) { } else { logger.debug("Skipping not reachable profile : " + url); } - } catch (MalformedURLException e) { + } catch (URISyntaxException e) { // not a valid url, this is an error logger.error("Malformed URL in JSON-LD Profile : " + trimmedProfile); } @@ -357,15 +357,12 @@ public void setAcceptPart(String profilesRaw, Type type) { // The default profiles are always added if no accept header is given at all if (profilesRaw == null || WapServerConfig.getInstance().shouldAlwaysAddDefaultProfilesToJsonLdRequests()) { switch (type) { + //TODO: check if this behaviour is working as intended case CONTAINER: - if (!profiles.contains(LDP_PROFILE)) { - profiles.add(LDP_PROFILE); - } + profiles.add(LDP_PROFILE); case ANNOTATION: case PAGE: - if (!profiles.contains(DEFAULT_PROFILE)) { - profiles.add(DEFAULT_PROFILE); - } + profiles.add(DEFAULT_PROFILE); break; default: // Unknown type ? do nothing because profile negotiation can be ignored by the server @@ -394,7 +391,7 @@ public String getContentType() { return getFormatString(); } StringBuilder profileString = new StringBuilder(); - for (URL url : profiles) { + for (URI url : profiles) { String context = url.toString(); if (!profileString.isEmpty()) { profileString.append(" "); diff --git a/src/main/java/edu/kit/scc/dem/wapsrv/model/formats/JsonLdProfileRegistry.java b/src/main/java/edu/kit/scc/dem/wapsrv/model/formats/JsonLdProfileRegistry.java index 02a1ac1..d5b7fe0 100644 --- a/src/main/java/edu/kit/scc/dem/wapsrv/model/formats/JsonLdProfileRegistry.java +++ b/src/main/java/edu/kit/scc/dem/wapsrv/model/formats/JsonLdProfileRegistry.java @@ -6,11 +6,7 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; -import java.net.HttpURLConnection; -import java.net.InetSocketAddress; -import java.net.MalformedURLException; -import java.net.Proxy; -import java.net.URL; +import java.net.*; import java.nio.file.Files; import java.util.HashSet; import java.util.Hashtable; @@ -33,7 +29,6 @@ import edu.kit.scc.dem.wapsrv.exceptions.FormatException; import edu.kit.scc.dem.wapsrv.exceptions.InternalServerException; import edu.kit.scc.dem.wapsrv.model.FormattableObject.Type; -import java.net.URI; /** * Central registry for JSON-LD profiles. It manages a local in memory profile @@ -92,19 +87,19 @@ public final class JsonLdProfileRegistry { * The set of profiles loaded and cached. This set is dynamically populated * on first usage. */ - private final Set cachedProfiles = new HashSet(); + private final Set cachedProfiles = new HashSet<>(); /** * The last times individual profiles have been updated */ - private final Map lastUpdateTimes = new Hashtable(); + private final Map lastUpdateTimes = new Hashtable<>(); /** * The number individual profiles have had update failures */ - private final Map profile2failures = new Hashtable(); + private final Map profile2failures = new Hashtable<>(); /** * The last times individual profiles have had update failures */ - private final Map profile2failureTimes = new Hashtable(); + private final Map profile2failureTimes = new Hashtable<>(); /** * The frames needed for specific types */ @@ -258,10 +253,10 @@ private void updateProfiles() { for (Object key : profilesDatabase.keySet()) { final String filename = (String) key; final String urlString = profilesDatabase.getProperty(filename); - URL url = null; + URI url = null; try { - url = new URL(urlString); - } catch (MalformedURLException e) { + url = new URI(urlString); + } catch (URISyntaxException e) { logger.warn("Invalid profile url, skipping it : " + urlString); continue; } @@ -354,8 +349,9 @@ private void updateProfiles() { setJsonLdOptions(jsonLdOptions); } - private boolean download(File destinationFile, URL srcUrl) { + private boolean download(File destinationFile, URI srcUri) { try { + URL srcUrl = srcUri.toURL(); Proxy proxy = getProxy(srcUrl); HttpURLConnection httpConn = (HttpURLConnection) (proxy == null ? srcUrl.openConnection() : srcUrl.openConnection(proxy)); @@ -446,10 +442,10 @@ private Proxy getProxy(URL srcUrl) { * @param url The URL to test * @return True if cached, false otherwise */ - public boolean isCachedProfile(URL url) { + public boolean isCachedProfile(URI url) { blockUntilInitialized(); // always check for http and https. The file is the same, no need to distinguish - URL complementaryUrl = toComplementaryUrl(url); + URI complementaryUrl = toComplementaryUrl(url); return cachedProfiles.contains(url) || cachedProfiles.contains(complementaryUrl); } @@ -573,10 +569,10 @@ private void updateJsonLdOptions(JsonLdOptions jsonLdOptions) { for (Object filenameObject : profilesDatabase.keySet()) { final String filename = (String) filenameObject; final String urlString = profilesDatabase.getProperty(filename); - URL url = null; + URI url = null; try { - url = new URL(urlString); - } catch (MalformedURLException e) { + url = new URI(urlString); + } catch (URISyntaxException e) { logger.warn("Invalid profile url, skipping it : " + urlString); continue; } @@ -634,14 +630,14 @@ private String toComplementaryUrl(String urlString) { * @param url The url to convert * @return The converted url */ - private URL toComplementaryUrl(URL url) { + private URI toComplementaryUrl(URI url) { if (url == null) { return null; } String compString = toComplementaryUrl(url.toString()); try { - return new URL(compString); - } catch (MalformedURLException e) { + return new URI(compString); + } catch (URISyntaxException e) { // should not happen logger.warn("could not complement url, this should not happen : " + url); // return the unswitched url as fallback @@ -658,17 +654,17 @@ private URL toComplementaryUrl(URL url) { * @param url The URL to cache * @return True if already cached or caching was successful, false otherwise */ - public boolean cacheProfile(URL url) { + public boolean cacheProfile(URI url) { if (url == null) { return false; } blockUntilInitialized(); synchronized (cachedProfiles) { - for (URL urlRegistered : cachedProfiles) { + for (URI urlRegistered : cachedProfiles) { if (url.equals(urlRegistered)) { return true; } - URL complementaryUrl = toComplementaryUrl(urlRegistered); + URI complementaryUrl = toComplementaryUrl(urlRegistered); if (url.equals(complementaryUrl)) { return true; } @@ -681,8 +677,8 @@ public boolean cacheProfile(URL url) { synchronized (updateLock) { final String urlString = url.toString(); final File profileFile = new File(profileFolder, getFilename(url)); - URL urlHttp = null; - URL urlHttps = null; + URI urlHttp = null; + URI urlHttps = null; if (url.toString().toLowerCase().startsWith("https:")) { urlHttps = url; urlHttp = toComplementaryUrl(url); @@ -719,7 +715,7 @@ public boolean cacheProfile(URL url) { } } - private String getFilename(URL url) { + private String getFilename(URI url) { // We use the path part and replace / with _ return url.getPath().replaceAll(Pattern.quote("/"), "_"); } @@ -731,7 +727,7 @@ private String getFilename(URL url) { * @param url The url to get the update time from * @return the last update time, -1 if never updated (=not cached yet) */ - protected long getLastUpdateTime(URL url) { + protected long getLastUpdateTime(URI url) { blockUntilInitialized(); Long lastTime = lastUpdateTimes.get(url); return lastTime == null ? -1 : lastTime; diff --git a/src/test/java/edu/kit/scc/dem/wapsrv/model/formats/JsonLdProfileRegistryTest.java b/src/test/java/edu/kit/scc/dem/wapsrv/model/formats/JsonLdProfileRegistryTest.java index f5774c3..b6ce03d 100644 --- a/src/test/java/edu/kit/scc/dem/wapsrv/model/formats/JsonLdProfileRegistryTest.java +++ b/src/test/java/edu/kit/scc/dem/wapsrv/model/formats/JsonLdProfileRegistryTest.java @@ -4,8 +4,8 @@ import java.io.File; import java.io.FileOutputStream; import java.io.IOException; -import java.net.MalformedURLException; -import java.net.URL; +import java.net.URI; +import java.net.URISyntaxException; import java.util.ArrayList; import java.util.List; import java.util.Properties; @@ -150,28 +150,28 @@ private void initTestFolder() { } } - private URL getAnnoProfileUrl() { + private URI getAnnoProfileUrl() { try { - return new URL(ANNO_URL_STRING); - } catch (MalformedURLException e) { + return new URI(ANNO_URL_STRING); + } catch (URISyntaxException e) { fail("Internal error, invalid profile url " + e.getMessage()); return null; } } - private URL getLdpProfileUrl() { + private URI getLdpProfileUrl() { try { - return new URL(LDP_URL_STRING); - } catch (MalformedURLException e) { + return new URI(LDP_URL_STRING); + } catch (URISyntaxException e) { fail("Internal error, invalid profile url " + e.getMessage()); return null; } } - private URL getAsProfileUrl() { + private URI getAsProfileUrl() { try { - return new URL(AS_URL_STRING); - } catch (MalformedURLException e) { + return new URI(AS_URL_STRING); + } catch (URISyntaxException e) { fail("Internal error, invalid profile url " + e.getMessage()); return null; } @@ -192,7 +192,7 @@ private void updateSystemProperty(String value, String key) { @Test final void testCacheUpdateWorks() { JsonLdProfileRegistry instance = getJsonLdProfileRegistry(); - URL profileUrl = getAnnoProfileUrl(); + URI profileUrl = getAnnoProfileUrl(); long actualTime = instance.getLastUpdateTime(profileUrl); if (actualTime < 0) { fail("Profile not existent for cache update test :("); @@ -220,7 +220,7 @@ final void testCacheUpdateWorks() { final void testCacheUpdateDelayWorks() { System.setProperty(JsonLdProfileRegistry.DISABLED_UPDATER_PROPERTY, "xyz"); JsonLdProfileRegistry instance = getJsonLdProfileRegistry(); - URL annoUrl = getAnnoProfileUrl(); + URI annoUrl = getAnnoProfileUrl(); assertTrue(instance.cacheProfile(annoUrl)); final String httpProxy = System.getProperty("http.proxyHost"); final String httpsProxy = System.getProperty("https.proxyHost"); @@ -258,7 +258,7 @@ final void testCacheUpdateDelayNotKeepingLocalProfiles() { System.setProperty(JsonLdProfileRegistry.DISABLED_UPDATER_PROPERTY, "xyz"); JsonLdProfileRegistry instance = getJsonLdProfileRegistry(false); profileRegistryHidden.init(config, false); - URL annoUrl = getAnnoProfileUrl(); + URI annoUrl = getAnnoProfileUrl(); assertTrue(instance.cacheProfile(annoUrl)); final String httpProxy = System.getProperty("http.proxyHost"); final String httpsProxy = System.getProperty("https.proxyHost"); @@ -324,7 +324,7 @@ final void testGetJsonLdOptions() { @Test final void testCacheProfile() { JsonLdProfileRegistry profileRegistry = getJsonLdProfileRegistry(); - URL url = this.getAsProfileUrl(); + URI url = this.getAsProfileUrl(); assertFalse(profileRegistry.isCachedProfile(url)); assertTrue(profileRegistry.cacheProfile(url)); assertTrue(profileRegistry.isCachedProfile(url)); @@ -336,7 +336,7 @@ final void testCacheProfile() { @Test final void testGetLastUpdateTime() { JsonLdProfileRegistry profileRegistry = getJsonLdProfileRegistry(); - URL url = this.getAnnoProfileUrl(); + URI url = this.getAnnoProfileUrl(); assertTrue(profileRegistry.cacheProfile(url)); assertNotEquals(-1, profileRegistry.getLastUpdateTime(url)); }