Skip to content

Commit e28f8d4

Browse files
authored
Merge pull request #930 from AdoptOpenJDK/urlKeyForCacheKey
Url key for cache key
2 parents d96e19d + ebc8895 commit e28f8d4

File tree

14 files changed

+273
-92
lines changed

14 files changed

+273
-92
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/client/parts/dialogs/security/MissingALACAttributePanel.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@
6767
import java.net.MalformedURLException;
6868
import java.net.URISyntaxException;
6969
import java.net.URL;
70-
import java.util.HashSet;
71-
import java.util.Set;
70+
import java.util.ArrayList;
71+
import java.util.List;
7272

7373
/**
7474
* http://docs.oracle.com/javase/7/docs/technotes/guides/jweb/security/manifest.html#app_library
@@ -151,7 +151,7 @@ public void hyperlinkUpdate(HyperlinkEvent e) {
151151
}
152152

153153
public static void main(String[] args) throws MalformedURLException {
154-
Set<URL> s = new HashSet<>();
154+
List<URL> s = new ArrayList<>();
155155
s.add(new URL("http:/blah.com/blah"));
156156
s.add(new URL("http:/blah.com/blah/blah"));
157157
MissingALACAttributePanel w = new MissingALACAttributePanel(null, "HelloWorld", "http://nbblah.url", UrlUtils.setOfUrlsToHtmlList(s));

core/src/main/java/net/adoptopenjdk/icedteaweb/client/parts/dialogs/security/SecurityDialogs.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
import java.net.URL;
5959
import java.security.AccessController;
6060
import java.security.PrivilegedAction;
61-
import java.util.Set;
61+
import java.util.List;
6262
import java.util.concurrent.Semaphore;
6363

6464
/**
@@ -221,7 +221,7 @@ public static NamePassword showAuthenticationPrompt(String host, int port, Strin
221221
return (NamePassword) response;
222222
}
223223

224-
public static boolean showMissingALACAttributePanel(JNLPFile file, URL codeBase, Set<URL> remoteUrls) {
224+
public static boolean showMissingALACAttributePanel(JNLPFile file, URL codeBase, List<URL> remoteUrls) {
225225

226226
SecurityDialogMessage message = new SecurityDialogMessage(file);
227227
message.dialogType = DialogType.MISSING_ALACA;
@@ -242,7 +242,7 @@ public static boolean showMissingALACAttributePanel(JNLPFile file, URL codeBase,
242242
return selectedValue.toBoolean();
243243
}
244244

245-
public static boolean showMatchingALACAttributePanel(JNLPFile file, URL documentBase, Set<URL> remoteUrls) {
245+
public static boolean showMatchingALACAttributePanel(JNLPFile file, URL documentBase, List<URL> remoteUrls) {
246246

247247
SecurityDialogMessage message = new SecurityDialogMessage(file);
248248
message.dialogType = DialogType.MATCHING_ALACA;

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

Lines changed: 30 additions & 35 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<UrlKey>> 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(new UrlKey(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(new UrlKey(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(new UrlKey(jarDesc.getLocation()));
357358
}
358359
}
359360
}
@@ -365,21 +366,17 @@ private void checkApplicationLibraryAllowableCodebaseAttribute() throws LaunchEx
365366
LOG.debug("The application is not using any url resources, skipping Application-Library-Allowable-Codebase Attribute check.");
366367
return;
367368
}
368-
final Set<URL> notOkUrls = new HashSet<>();
369+
final Set<UrlKey> 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(urlKey);
379+
LOG.warn("Warning! '{}' is NOT from codebase '{}'.", u, codebase);
383380
}
384381
}
385382
if (notOkUrls.isEmpty()) {
@@ -396,9 +393,11 @@ private void checkApplicationLibraryAllowableCodebaseAttribute() throws LaunchEx
396393
att = null;
397394
}
398395

399-
final Set<URL> notOkResources = notOkUrls.stream()
400-
.flatMap(notOk -> usedUrls.get(notOk.toString()).stream())
401-
.collect(Collectors.toSet());
396+
final List<URL> notOkResources = notOkUrls.stream()
397+
.flatMap(notOk -> usedUrls.get(notOk).stream())
398+
.collect(Collectors.toSet()).stream()
399+
.map(UrlKey::getUrl)
400+
.collect(Collectors.toList());
402401

403402
notOkResources.forEach(url -> LOG.warn("The resource '{}' is not from codebase '{}'", url, codebase));
404403

@@ -411,16 +410,12 @@ private void checkApplicationLibraryAllowableCodebaseAttribute() throws LaunchEx
411410
return;
412411
}
413412
} 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.");
413+
for (UrlKey foundUrlKey : usedUrls.keySet()) {
414+
URL foundUrl = foundUrlKey.getUrl();
415+
if (!att.matches(foundUrl)) {
416+
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.");
417+
} else {
418+
LOG.debug("The resources from {} do match the location in Application-Library-Allowable-Codebase Attribute {}. Continuing.", foundUrl, att);
424419
}
425420
}
426421
}
@@ -446,7 +441,7 @@ static URL stripDocbase(URL documentBase) {
446441
if (i <= 8 || i >= s.length()) {
447442
return documentBase;
448443
}
449-
s = s.substring(0, i+1);
444+
s = s.substring(0, i + 1);
450445
try {
451446
documentBase = new URL(s);
452447
} 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)