Skip to content

Commit 71cb061

Browse files
committed
Use UrlKey as cachekey instead of URL.toString()
1 parent 3b47ce5 commit 71cb061

File tree

10 files changed

+102
-81
lines changed

10 files changed

+102
-81
lines changed

core/src/main/java/net/adoptopenjdk/icedteaweb/client/parts/browser/HtmlBrowserPanel.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ private void put(URL url, List<URL> where) {
178178
if (url != null) {
179179
if (where.isEmpty()) {
180180
where.add(0, url);
181-
} else if (!where.get(0).toString().equals(url.toString())) {
181+
} else if (!UrlUtils.equalUrls(where.get(0), url)) {
182182
where.add(0, url);
183183
}
184184
}

core/src/main/java/net/adoptopenjdk/icedteaweb/manifest/ManifestAttributesChecker.java

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import net.sourceforge.jnlp.runtime.classloader.JNLPClassLoader.SigningState;
5353
import net.sourceforge.jnlp.runtime.classloader.SecurityDelegate;
5454
import net.sourceforge.jnlp.util.ClasspathMatcher.ClasspathMatchers;
55+
import net.sourceforge.jnlp.util.UrlKey;
5556
import net.sourceforge.jnlp.util.UrlUtils;
5657

5758
import java.net.MalformedURLException;
@@ -77,7 +78,7 @@ public class ManifestAttributesChecker {
7778
private final SecurityDelegate securityDelegate;
7879

7980
public ManifestAttributesChecker(final SecurityDesc security, final JNLPFile file,
80-
final SigningState signing, final SecurityDelegate securityDelegate) {
81+
final SigningState signing, final SecurityDelegate securityDelegate) {
8182
this.security = security;
8283
this.file = file;
8384
this.signing = signing;
@@ -122,7 +123,7 @@ public void checkAll() throws LaunchException {
122123
}
123124

124125
if (attributesCheck.contains(MANIFEST_ATTRIBUTES_CHECK.ALAC) ||
125-
attributesCheck.contains(MANIFEST_ATTRIBUTES_CHECK.ALL)) {
126+
attributesCheck.contains(MANIFEST_ATTRIBUTES_CHECK.ALL)) {
126127
checkApplicationLibraryAllowableCodebaseAttribute();
127128
} else {
128129
LOG.warn("check on {} skipped because property of deployment.manifest.attributes.check was not set to ALL or includes {} in the combination of options", "Application Library Allowable Codebase", "ALAC");
@@ -142,7 +143,7 @@ public static List<MANIFEST_ATTRIBUTES_CHECK> getAttributesCheck() {
142143
final List<String> configs = JNLPRuntime.getConfiguration().getPropertyAsList(ConfigurationConstants.KEY_ENABLE_MANIFEST_ATTRIBUTES_CHECK);
143144
List<MANIFEST_ATTRIBUTES_CHECK> manifestAttributesCheckList = new ArrayList<>();
144145
for (String attribute : configs) {
145-
for (MANIFEST_ATTRIBUTES_CHECK manifestAttribute : MANIFEST_ATTRIBUTES_CHECK.values()) {
146+
for (MANIFEST_ATTRIBUTES_CHECK manifestAttribute : MANIFEST_ATTRIBUTES_CHECK.values()) {
146147
if (manifestAttribute.toString().equals(attribute)) {
147148
manifestAttributesCheckList.add(manifestAttribute);
148149
}
@@ -331,20 +332,20 @@ private void checkApplicationLibraryAllowableCodebaseAttribute() throws LaunchEx
331332
final URL codebase = file.getCodeBase();
332333

333334
//cases
334-
final Map<String, Set<URL>> usedUrls = new HashMap<>();
335+
final Map<UrlKey, Set<URL>> usedUrls = new HashMap<>();
335336
final URL sourceLocation = file.getSourceLocation();
336337
final ResourcesDesc[] resourcesDescs = file.getResourcesDescs();
337338
if ((sourceLocation != null) && !FILE_PROTOCOL.equals(sourceLocation.getProtocol())) {
338339
final URL urlWithoutFileName = UrlUtils.removeFileName(sourceLocation);
339-
usedUrls.computeIfAbsent(urlWithoutFileName.toString(), url -> new HashSet<>()).add(sourceLocation);
340+
usedUrls.computeIfAbsent(new UrlKey(urlWithoutFileName), url -> new HashSet<>()).add(sourceLocation);
340341
}
341342
for (ResourcesDesc resourcesDesc : resourcesDescs) {
342343
ExtensionDesc[] ex = resourcesDesc.getExtensions();
343344
if (ex != null) {
344345
for (ExtensionDesc extensionDesc : ex) {
345346
if (extensionDesc != null) {
346347
final URL urlWithoutFileName = UrlUtils.removeFileName(extensionDesc.getLocation());
347-
usedUrls.computeIfAbsent(urlWithoutFileName.toString(), url -> new HashSet<>()).add(extensionDesc.getLocation());
348+
usedUrls.computeIfAbsent(new UrlKey(urlWithoutFileName), url -> new HashSet<>()).add(extensionDesc.getLocation());
348349
}
349350
}
350351
}
@@ -353,7 +354,7 @@ private void checkApplicationLibraryAllowableCodebaseAttribute() throws LaunchEx
353354
for (JARDesc jarDesc : jars) {
354355
if (jarDesc != null) {
355356
final URL urlWithoutFileName = UrlUtils.removeFileName(jarDesc.getLocation());
356-
usedUrls.computeIfAbsent(urlWithoutFileName.toString(), url -> new HashSet<>()).add(jarDesc.getLocation());
357+
usedUrls.computeIfAbsent(new UrlKey(urlWithoutFileName), url -> new HashSet<>()).add(jarDesc.getLocation());
357358
}
358359
}
359360
}
@@ -367,19 +368,15 @@ private void checkApplicationLibraryAllowableCodebaseAttribute() throws LaunchEx
367368
}
368369
final Set<URL> notOkUrls = new HashSet<>();
369370
final boolean skipResourcesFromFileSystem = Boolean.parseBoolean(JNLPRuntime.getConfiguration().getProperty(ConfigurationConstants.KEY_ASSUME_FILE_STEM_IN_CODEBASE));
370-
for (String urlString : usedUrls.keySet()) {
371-
try {
372-
final URL u = new URL(urlString);
373-
if (UrlUtils.urlRelativeTo(u, codebase)) {
374-
LOG.debug("OK - '{}' is from codebase '{}'.", u, codebase);
375-
} else if (skipResourcesFromFileSystem && FILE_PROTOCOL.equals(u.getProtocol())) {
376-
LOG.debug("OK - '{}' is from file system", u);
377-
} else {
378-
notOkUrls.add(u);
379-
LOG.warn("Warning! '{}' is NOT from codebase '{}'.", u, codebase);
380-
}
381-
} catch (MalformedURLException mue) {
382-
LOG.debug("Malformed URL checkApplicationLibraryAllowableCodebaseAttribute '{}'.", urlString);
371+
for (UrlKey urlKey : usedUrls.keySet()) {
372+
final URL u = urlKey.getUrl();
373+
if (UrlUtils.urlRelativeTo(u, codebase)) {
374+
LOG.debug("OK - '{}' is from codebase '{}'.", u, codebase);
375+
} else if (skipResourcesFromFileSystem && FILE_PROTOCOL.equals(u.getProtocol())) {
376+
LOG.debug("OK - '{}' is from file system", u);
377+
} else {
378+
notOkUrls.add(u);
379+
LOG.warn("Warning! '{}' is NOT from codebase '{}'.", u, codebase);
383380
}
384381
}
385382
if (notOkUrls.isEmpty()) {
@@ -397,7 +394,7 @@ private void checkApplicationLibraryAllowableCodebaseAttribute() throws LaunchEx
397394
}
398395

399396
final Set<URL> notOkResources = notOkUrls.stream()
400-
.flatMap(notOk -> usedUrls.get(notOk.toString()).stream())
397+
.flatMap(notOk -> usedUrls.get(new UrlKey(notOk)).stream())
401398
.collect(Collectors.toSet());
402399

403400
notOkResources.forEach(url -> LOG.warn("The resource '{}' is not from codebase '{}'", url, codebase));
@@ -411,16 +408,12 @@ private void checkApplicationLibraryAllowableCodebaseAttribute() throws LaunchEx
411408
return;
412409
}
413410
} else {
414-
for (String foundUrlString : usedUrls.keySet()) {
415-
try {
416-
URL foundUrl = new URL(foundUrlString);
417-
if (!att.matches(foundUrl)) {
418-
throw new LaunchException("The resources " + usedUrls.get(foundUrlString) + " do not match the location in Application-Library-Allowable-Codebase Attribute " + att + ". Blocking the application from running.");
419-
} else {
420-
LOG.debug("The resources from {} do match the location in Application-Library-Allowable-Codebase Attribute {}. Continuing.", foundUrl, att);
421-
}
422-
} catch (MalformedURLException mue) {
423-
throw new LaunchException("Malformed URL " + foundUrlString + ". Resources do not match the location in Application-Library-Allowable-Codebase Attribute " + att + ". Blocking the application from running.");
411+
for (UrlKey foundUrlKey : usedUrls.keySet()) {
412+
URL foundUrl = foundUrlKey.getUrl();
413+
if (!att.matches(foundUrl)) {
414+
throw new LaunchException("The resources " + usedUrls.get(foundUrlKey) + " do not match the location in Application-Library-Allowable-Codebase Attribute " + att + ". Blocking the application from running.");
415+
} else {
416+
LOG.debug("The resources from {} do match the location in Application-Library-Allowable-Codebase Attribute {}. Continuing.", foundUrl, att);
424417
}
425418
}
426419
}
@@ -446,7 +439,7 @@ static URL stripDocbase(URL documentBase) {
446439
if (i <= 8 || i >= s.length()) {
447440
return documentBase;
448441
}
449-
s = s.substring(0, i+1);
442+
s = s.substring(0, i + 1);
450443
try {
451444
documentBase = new URL(s);
452445
} catch (MalformedURLException ex) {

core/src/main/java/net/adoptopenjdk/icedteaweb/resources/ResourceTracker.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import net.sourceforge.jnlp.cache.CacheUtil;
2525
import net.sourceforge.jnlp.config.ConfigurationConstants;
2626
import net.sourceforge.jnlp.runtime.JNLPRuntime;
27+
import net.sourceforge.jnlp.util.UrlKey;
2728
import net.sourceforge.jnlp.util.UrlUtils;
2829

2930
import javax.jnlp.DownloadServiceListener;
@@ -97,7 +98,7 @@ public class ResourceTracker {
9798
/**
9899
* the resources known about by this resource tracker
99100
*/
100-
private final Map<String, Resource> resources = new HashMap<>();
101+
private final Map<UrlKey, Resource> resources = new HashMap<>();
101102

102103
/**
103104
* whether to download parts before requested
@@ -166,10 +167,11 @@ public void addResource(URL location, final VersionString version, final UpdateP
166167
*/
167168
private boolean addToResources(Resource resource) {
168169
synchronized (resources) {
169-
final Resource existingResource = resources.get(resource.getLocation().toString());
170+
final UrlKey urlKey = new UrlKey(resource.getLocation());
171+
final Resource existingResource = resources.get(urlKey);
170172

171173
if (existingResource == null) {
172-
resources.put(resource.getLocation().toString(), resource);
174+
resources.put(urlKey, resource);
173175
return true;
174176
}
175177

@@ -203,7 +205,7 @@ private void startDownloadingIfPrefetch(Resource resource) {
203205
public void removeResource(URL location) {
204206
synchronized (resources) {
205207
Resource resource = getResource(location);
206-
resources.remove(resource.getLocation().toString());
208+
resources.remove(new UrlKey(resource.getLocation()));
207209
}
208210
}
209211

@@ -362,7 +364,7 @@ private Resource[] getResources(URL[] urls) {
362364
private Resource getResource(URL location) {
363365
final URL normalizedLocation = normalizeUrlQuietly(location);
364366
synchronized (resources) {
365-
final Resource result = resources.get(normalizedLocation.toString());
367+
final Resource result = resources.get(new UrlKey(normalizedLocation));
366368
if (result == null) {
367369
throw new IllegalResourceDescriptorException("Location " + location + " does not specify a resource being tracked.");
368370
}

core/src/main/java/net/adoptopenjdk/icedteaweb/resources/cache/CacheKey.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package net.adoptopenjdk.icedteaweb.resources.cache;
22

33
import net.adoptopenjdk.icedteaweb.jnlp.version.VersionId;
4+
import net.sourceforge.jnlp.util.UrlKey;
45

56
import java.net.URL;
67
import java.util.Objects;
@@ -10,12 +11,12 @@
1011
class CacheKey {
1112

1213
private final URL location;
13-
private final String locationString;
14+
private final UrlKey urlKey;
1415
private final VersionId version;
1516

1617
public CacheKey(final URL location, final VersionId version) {
1718
this.location = requireNonNull(location, "location");
18-
this.locationString = location.toString();
19+
this.urlKey = new UrlKey(location);
1920
this.version = version;
2021
}
2122

@@ -30,7 +31,7 @@ public VersionId getVersion() {
3031
@Override
3132
public String toString() {
3233
return "CacheKey{" +
33-
"location=" + locationString +
34+
"location=" + location +
3435
", version=" + version +
3536
'}';
3637
}
@@ -40,15 +41,15 @@ public boolean equals(final Object o) {
4041
if (this == o) return true;
4142
if (o == null || getClass() != o.getClass()) return false;
4243
CacheKey cacheKey = (CacheKey) o;
43-
return Objects.equals(locationString, cacheKey.locationString) && Objects.equals(version, cacheKey.version);
44+
return Objects.equals(urlKey, cacheKey.urlKey) && Objects.equals(version, cacheKey.version);
4445
}
4546

4647
@Override
4748
public int hashCode() {
48-
return Objects.hash(locationString, version);
49+
return Objects.hash(urlKey, version);
4950
}
5051

5152
public boolean matches(URL resource) {
52-
return resource != null && locationString.equals(resource.toString());
53+
return resource != null && urlKey.equals(new UrlKey(resource));
5354
}
5455
}

core/src/main/java/net/sourceforge/jnlp/Launcher.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import net.sourceforge.jnlp.runtime.classloader.JNLPClassLoader;
3636
import net.sourceforge.jnlp.services.InstanceExistsException;
3737
import net.sourceforge.jnlp.services.ServiceUtil;
38+
import net.sourceforge.jnlp.util.UrlUtils;
3839

3940
import javax.imageio.ImageIO;
4041
import javax.swing.text.html.parser.ParserDelegator;
@@ -309,7 +310,7 @@ private JNLPFile fromUrl(URL location) throws LaunchException {
309310
}
310311
if (!isLocal && haveHref) {
311312
//this is case when remote file have href to different file
312-
if (file.getSourceLocation() == null || !location.toString().equals(file.getSourceLocation().toString())) {
313+
if (!UrlUtils.equalUrls(location, file.getSourceLocation())) {
313314
//mark local true, so the following condition will be true and
314315
//new jnlp file will be downloaded
315316
isLocal = true;

core/src/main/java/net/sourceforge/jnlp/runtime/CachedJarFileCallback.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import net.adoptopenjdk.icedteaweb.logging.Logger;
4141
import net.adoptopenjdk.icedteaweb.logging.LoggerFactory;
4242
import net.sourceforge.jnlp.util.JarFile;
43+
import net.sourceforge.jnlp.util.UrlKey;
4344
import net.sourceforge.jnlp.util.UrlUtils;
4445
import sun.net.www.protocol.jar.URLJarFile;
4546
import sun.net.www.protocol.jar.URLJarFileCallBack;
@@ -73,24 +74,24 @@ public synchronized static CachedJarFileCallback getInstance() {
7374
}
7475

7576
/* our managed cache */
76-
private final Map<String, URL> mapping;
77+
private final Map<UrlKey, URL> mapping;
7778

7879
private CachedJarFileCallback() {
79-
mapping = new ConcurrentHashMap<String, URL>();
80+
mapping = new ConcurrentHashMap<>();
8081
}
8182

8283
public void addMapping(URL remoteUrl, URL localUrl) {
8384
LOG.debug("CachedJarFileCallback.addMapping : {} -> {} ", remoteUrl, localUrl);
84-
mapping.put(remoteUrl.toString(), localUrl);
85+
mapping.put(new UrlKey(remoteUrl), localUrl);
8586
}
8687

8788
@Override
8889
public java.util.jar.JarFile retrieve(URL url) throws IOException {
89-
URL localUrl = mapping.get(url.toString());
90+
URL localUrl = mapping.get(new UrlKey(url));
9091
if (localUrl == null) {
9192
if (url.getRef() != null) {
9293
url = new URL(url.toString().substring(0, url.toString().lastIndexOf(url.getRef()) - 1));
93-
localUrl = mapping.get(url.toString());
94+
localUrl = mapping.get(new UrlKey(url));
9495
}
9596
}
9697

0 commit comments

Comments
 (0)