Skip to content

Commit 640420a

Browse files
committed
Replacing usage of URL class with URI where equals comparisons are used
1 parent 72e1729 commit 640420a

File tree

3 files changed

+66
-50
lines changed

3 files changed

+66
-50
lines changed

src/main/java/edu/kit/scc/dem/wapsrv/model/formats/JsonLdFormatter.java

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import java.io.IOException;
44
import java.net.MalformedURLException;
5+
import java.net.URI;
6+
import java.net.URISyntaxException;
57
import java.net.URL;
68
import java.util.*;
79
import java.util.regex.Pattern;
@@ -40,19 +42,19 @@ public final class JsonLdFormatter extends AbstractFormatter {
4042
/**
4143
* The default JSON-LD Profile
4244
*/
43-
public static final URL DEFAULT_PROFILE = makeUrl("http://www.w3.org/ns/anno.jsonld");
45+
public static final URI DEFAULT_PROFILE = makeUri("http://www.w3.org/ns/anno.jsonld");
4446
/**
4547
* The LDP profile for containers
4648
*/
47-
public static final URL LDP_PROFILE = makeUrl("http://www.w3.org/ns/ldp.jsonld");
49+
public static final URI LDP_PROFILE = makeUri("http://www.w3.org/ns/ldp.jsonld");
4850
/**
4951
* The string identifying JSON-LD
5052
*/
5153
public static final String JSON_LD_STRING = "application/ld+json";
5254
/**
5355
* The set of used profiles
5456
*/
55-
private final Set<URL> profiles = new HashSet<>();
57+
private final Set<URI> profiles = new HashSet<>();
5658
/**
5759
* The profile registry
5860
*/
@@ -78,8 +80,24 @@ public JsonLdFormatter() {
7880
*/
7981
private static URL makeUrl(String urlString) {
8082
try {
81-
return new java.net.URL(urlString);
82-
} catch (java.net.MalformedURLException e) {
83+
return new URL(urlString);
84+
} catch (MalformedURLException e) {
85+
LoggerFactory.getLogger(ContentNegotiator.class).error("DEFAULT_PROFILE is an invalid URL : " + urlString);
86+
return null;
87+
}
88+
}
89+
90+
/**
91+
* This is a helper method necessary to be able to use a static URL,
92+
* otherwise the exception can not be caught.
93+
*
94+
* @param urlString The String to generate a URL from
95+
* @return The generated URL Object
96+
*/
97+
private static URI makeUri(String urlString) {
98+
try {
99+
return new URI(urlString);
100+
} catch (URISyntaxException e) {
83101
LoggerFactory.getLogger(ContentNegotiator.class).error("DEFAULT_PROFILE is an invalid URL : " + urlString);
84102
return null;
85103
}
@@ -210,7 +228,7 @@ private String applyProfiles(String jsonLd, String frameString) {
210228
jsonObject = JsonLdProcessor.frame(jsonObject, frameObject, options);
211229

212230
// Collect contexts from profiles
213-
for (URL url : profiles) {
231+
for (URI url : profiles) {
214232
contexts.add(url.toString());
215233
}
216234

@@ -228,7 +246,7 @@ private String applyProfiles(String jsonLd, String frameString) {
228246
}
229247
}
230248
}
231-
for (URL url : profiles) {
249+
for (URI url : profiles) {
232250
contexts.add(url.toString());
233251
}
234252
contexts = deduplicateContexts(contexts);
@@ -335,7 +353,7 @@ public void setAcceptPart(String profilesRaw, Type type) {
335353
for (String profile : profiles) {
336354
String trimmedProfile = profile.trim();
337355
try {
338-
URL url = new URL(trimmedProfile);
356+
URI url = new URI(trimmedProfile);
339357
// At least a valid url, but is it locally cached?
340358
// We do not support uncached profiles. Inform the registry we need it cached.
341359
if (profileRegistry.cacheProfile(url)) {
@@ -344,7 +362,7 @@ public void setAcceptPart(String profilesRaw, Type type) {
344362
} else {
345363
logger.debug("Skipping not reachable profile : " + url);
346364
}
347-
} catch (MalformedURLException e) {
365+
} catch (URISyntaxException e) {
348366
// not a valid url, this is an error
349367
logger.error("Malformed URL in JSON-LD Profile : " + trimmedProfile);
350368
}
@@ -394,7 +412,7 @@ public String getContentType() {
394412
return getFormatString();
395413
}
396414
StringBuilder profileString = new StringBuilder();
397-
for (URL url : profiles) {
415+
for (URI url : profiles) {
398416
String context = url.toString();
399417
if (!profileString.isEmpty()) {
400418
profileString.append(" ");

src/main/java/edu/kit/scc/dem/wapsrv/model/formats/JsonLdProfileRegistry.java

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,7 @@
66
import java.io.FileOutputStream;
77
import java.io.IOException;
88
import java.io.InputStream;
9-
import java.net.HttpURLConnection;
10-
import java.net.InetSocketAddress;
11-
import java.net.MalformedURLException;
12-
import java.net.Proxy;
13-
import java.net.URL;
9+
import java.net.*;
1410
import java.nio.file.Files;
1511
import java.util.HashSet;
1612
import java.util.Hashtable;
@@ -33,7 +29,6 @@
3329
import edu.kit.scc.dem.wapsrv.exceptions.FormatException;
3430
import edu.kit.scc.dem.wapsrv.exceptions.InternalServerException;
3531
import edu.kit.scc.dem.wapsrv.model.FormattableObject.Type;
36-
import java.net.URI;
3732

3833
/**
3934
* Central registry for JSON-LD profiles. It manages a local in memory profile
@@ -92,19 +87,19 @@ public final class JsonLdProfileRegistry {
9287
* The set of profiles loaded and cached. This set is dynamically populated
9388
* on first usage.
9489
*/
95-
private final Set<URL> cachedProfiles = new HashSet<URL>();
90+
private final Set<URI> cachedProfiles = new HashSet<>();
9691
/**
9792
* The last times individual profiles have been updated
9893
*/
99-
private final Map<URL, Long> lastUpdateTimes = new Hashtable<URL, Long>();
94+
private final Map<URI, Long> lastUpdateTimes = new Hashtable<>();
10095
/**
10196
* The number individual profiles have had update failures
10297
*/
103-
private final Map<URL, Integer> profile2failures = new Hashtable<URL, Integer>();
98+
private final Map<URI, Integer> profile2failures = new Hashtable<>();
10499
/**
105100
* The last times individual profiles have had update failures
106101
*/
107-
private final Map<URL, Long> profile2failureTimes = new Hashtable<URL, Long>();
102+
private final Map<URI, Long> profile2failureTimes = new Hashtable<>();
108103
/**
109104
* The frames needed for specific types
110105
*/
@@ -258,10 +253,10 @@ private void updateProfiles() {
258253
for (Object key : profilesDatabase.keySet()) {
259254
final String filename = (String) key;
260255
final String urlString = profilesDatabase.getProperty(filename);
261-
URL url = null;
256+
URI url = null;
262257
try {
263-
url = new URL(urlString);
264-
} catch (MalformedURLException e) {
258+
url = new URI(urlString);
259+
} catch (URISyntaxException e) {
265260
logger.warn("Invalid profile url, skipping it : " + urlString);
266261
continue;
267262
}
@@ -354,8 +349,9 @@ private void updateProfiles() {
354349
setJsonLdOptions(jsonLdOptions);
355350
}
356351

357-
private boolean download(File destinationFile, URL srcUrl) {
352+
private boolean download(File destinationFile, URI srcUri) {
358353
try {
354+
URL srcUrl = srcUri.toURL();
359355
Proxy proxy = getProxy(srcUrl);
360356
HttpURLConnection httpConn
361357
= (HttpURLConnection) (proxy == null ? srcUrl.openConnection() : srcUrl.openConnection(proxy));
@@ -446,10 +442,10 @@ private Proxy getProxy(URL srcUrl) {
446442
* @param url The URL to test
447443
* @return True if cached, false otherwise
448444
*/
449-
public boolean isCachedProfile(URL url) {
445+
public boolean isCachedProfile(URI url) {
450446
blockUntilInitialized();
451447
// always check for http and https. The file is the same, no need to distinguish
452-
URL complementaryUrl = toComplementaryUrl(url);
448+
URI complementaryUrl = toComplementaryUrl(url);
453449
return cachedProfiles.contains(url) || cachedProfiles.contains(complementaryUrl);
454450
}
455451

@@ -634,14 +630,14 @@ private String toComplementaryUrl(String urlString) {
634630
* @param url The url to convert
635631
* @return The converted url
636632
*/
637-
private URL toComplementaryUrl(URL url) {
633+
private URI toComplementaryUrl(URI url) {
638634
if (url == null) {
639635
return null;
640636
}
641637
String compString = toComplementaryUrl(url.toString());
642638
try {
643-
return new URL(compString);
644-
} catch (MalformedURLException e) {
639+
return new URI(compString);
640+
} catch (URISyntaxException e) {
645641
// should not happen
646642
logger.warn("could not complement url, this should not happen : " + url);
647643
// return the unswitched url as fallback
@@ -658,17 +654,17 @@ private URL toComplementaryUrl(URL url) {
658654
* @param url The URL to cache
659655
* @return True if already cached or caching was successful, false otherwise
660656
*/
661-
public boolean cacheProfile(URL url) {
657+
public boolean cacheProfile(URI url) {
662658
if (url == null) {
663659
return false;
664660
}
665661
blockUntilInitialized();
666662
synchronized (cachedProfiles) {
667-
for (URL urlRegistered : cachedProfiles) {
663+
for (URI urlRegistered : cachedProfiles) {
668664
if (url.equals(urlRegistered)) {
669665
return true;
670666
}
671-
URL complementaryUrl = toComplementaryUrl(urlRegistered);
667+
URI complementaryUrl = toComplementaryUrl(urlRegistered);
672668
if (url.equals(complementaryUrl)) {
673669
return true;
674670
}
@@ -681,8 +677,8 @@ public boolean cacheProfile(URL url) {
681677
synchronized (updateLock) {
682678
final String urlString = url.toString();
683679
final File profileFile = new File(profileFolder, getFilename(url));
684-
URL urlHttp = null;
685-
URL urlHttps = null;
680+
URI urlHttp = null;
681+
URI urlHttps = null;
686682
if (url.toString().toLowerCase().startsWith("https:")) {
687683
urlHttps = url;
688684
urlHttp = toComplementaryUrl(url);
@@ -719,7 +715,7 @@ public boolean cacheProfile(URL url) {
719715
}
720716
}
721717

722-
private String getFilename(URL url) {
718+
private String getFilename(URI url) {
723719
// We use the path part and replace / with _
724720
return url.getPath().replaceAll(Pattern.quote("/"), "_");
725721
}
@@ -731,7 +727,7 @@ private String getFilename(URL url) {
731727
* @param url The url to get the update time from
732728
* @return the last update time, -1 if never updated (=not cached yet)
733729
*/
734-
protected long getLastUpdateTime(URL url) {
730+
protected long getLastUpdateTime(URI url) {
735731
blockUntilInitialized();
736732
Long lastTime = lastUpdateTimes.get(url);
737733
return lastTime == null ? -1 : lastTime;

src/test/java/edu/kit/scc/dem/wapsrv/model/formats/JsonLdProfileRegistryTest.java

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import java.io.FileOutputStream;
66
import java.io.IOException;
77
import java.net.MalformedURLException;
8+
import java.net.URI;
9+
import java.net.URISyntaxException;
810
import java.net.URL;
911
import java.util.ArrayList;
1012
import java.util.List;
@@ -150,28 +152,28 @@ private void initTestFolder() {
150152
}
151153
}
152154

153-
private URL getAnnoProfileUrl() {
155+
private URI getAnnoProfileUrl() {
154156
try {
155-
return new URL(ANNO_URL_STRING);
156-
} catch (MalformedURLException e) {
157+
return new URI(ANNO_URL_STRING);
158+
} catch (URISyntaxException e) {
157159
fail("Internal error, invalid profile url " + e.getMessage());
158160
return null;
159161
}
160162
}
161163

162-
private URL getLdpProfileUrl() {
164+
private URI getLdpProfileUrl() {
163165
try {
164-
return new URL(LDP_URL_STRING);
165-
} catch (MalformedURLException e) {
166+
return new URI(LDP_URL_STRING);
167+
} catch (URISyntaxException e) {
166168
fail("Internal error, invalid profile url " + e.getMessage());
167169
return null;
168170
}
169171
}
170172

171-
private URL getAsProfileUrl() {
173+
private URI getAsProfileUrl() {
172174
try {
173-
return new URL(AS_URL_STRING);
174-
} catch (MalformedURLException e) {
175+
return new URI(AS_URL_STRING);
176+
} catch (URISyntaxException e) {
175177
fail("Internal error, invalid profile url " + e.getMessage());
176178
return null;
177179
}
@@ -192,7 +194,7 @@ private void updateSystemProperty(String value, String key) {
192194
@Test
193195
final void testCacheUpdateWorks() {
194196
JsonLdProfileRegistry instance = getJsonLdProfileRegistry();
195-
URL profileUrl = getAnnoProfileUrl();
197+
URI profileUrl = getAnnoProfileUrl();
196198
long actualTime = instance.getLastUpdateTime(profileUrl);
197199
if (actualTime < 0) {
198200
fail("Profile not existent for cache update test :(");
@@ -220,7 +222,7 @@ final void testCacheUpdateWorks() {
220222
final void testCacheUpdateDelayWorks() {
221223
System.setProperty(JsonLdProfileRegistry.DISABLED_UPDATER_PROPERTY, "xyz");
222224
JsonLdProfileRegistry instance = getJsonLdProfileRegistry();
223-
URL annoUrl = getAnnoProfileUrl();
225+
URI annoUrl = getAnnoProfileUrl();
224226
assertTrue(instance.cacheProfile(annoUrl));
225227
final String httpProxy = System.getProperty("http.proxyHost");
226228
final String httpsProxy = System.getProperty("https.proxyHost");
@@ -258,7 +260,7 @@ final void testCacheUpdateDelayNotKeepingLocalProfiles() {
258260
System.setProperty(JsonLdProfileRegistry.DISABLED_UPDATER_PROPERTY, "xyz");
259261
JsonLdProfileRegistry instance = getJsonLdProfileRegistry(false);
260262
profileRegistryHidden.init(config, false);
261-
URL annoUrl = getAnnoProfileUrl();
263+
URI annoUrl = getAnnoProfileUrl();
262264
assertTrue(instance.cacheProfile(annoUrl));
263265
final String httpProxy = System.getProperty("http.proxyHost");
264266
final String httpsProxy = System.getProperty("https.proxyHost");
@@ -324,7 +326,7 @@ final void testGetJsonLdOptions() {
324326
@Test
325327
final void testCacheProfile() {
326328
JsonLdProfileRegistry profileRegistry = getJsonLdProfileRegistry();
327-
URL url = this.getAsProfileUrl();
329+
URI url = this.getAsProfileUrl();
328330
assertFalse(profileRegistry.isCachedProfile(url));
329331
assertTrue(profileRegistry.cacheProfile(url));
330332
assertTrue(profileRegistry.isCachedProfile(url));
@@ -336,7 +338,7 @@ final void testCacheProfile() {
336338
@Test
337339
final void testGetLastUpdateTime() {
338340
JsonLdProfileRegistry profileRegistry = getJsonLdProfileRegistry();
339-
URL url = this.getAnnoProfileUrl();
341+
URI url = this.getAnnoProfileUrl();
340342
assertTrue(profileRegistry.cacheProfile(url));
341343
assertNotEquals(-1, profileRegistry.getLastUpdateTime(url));
342344
}

0 commit comments

Comments
 (0)