Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -40,19 +40,19 @@ 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
*/
public static final String JSON_LD_STRING = "application/ld+json";
/**
* The set of used profiles
*/
private final Set<URL> profiles = new HashSet<>();
private final Set<URI> profiles = new HashSet<>();
/**
* The profile registry
*/
Expand All @@ -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;
}
Expand Down Expand Up @@ -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());
}

Expand All @@ -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);
Expand Down Expand Up @@ -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)) {
Expand All @@ -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);
}
Expand All @@ -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
Expand Down Expand Up @@ -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(" ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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<URL> cachedProfiles = new HashSet<URL>();
private final Set<URI> cachedProfiles = new HashSet<>();
/**
* The last times individual profiles have been updated
*/
private final Map<URL, Long> lastUpdateTimes = new Hashtable<URL, Long>();
private final Map<URI, Long> lastUpdateTimes = new Hashtable<>();
/**
* The number individual profiles have had update failures
*/
private final Map<URL, Integer> profile2failures = new Hashtable<URL, Integer>();
private final Map<URI, Integer> profile2failures = new Hashtable<>();
/**
* The last times individual profiles have had update failures
*/
private final Map<URL, Long> profile2failureTimes = new Hashtable<URL, Long>();
private final Map<URI, Long> profile2failureTimes = new Hashtable<>();
/**
* The frames needed for specific types
*/
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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
Expand All @@ -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;
}
Expand All @@ -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);
Expand Down Expand Up @@ -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("/"), "_");
}
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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 :(");
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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));
Expand All @@ -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));
}
Expand Down