Skip to content

Commit

Permalink
Convert URL to URI for decoding.
Browse files Browse the repository at this point in the history
URI will decode entities when retrieving the path, something URL won't
do.  This makes a difference when the location was something like
"file:/C:/Program%20Files/Jenkins/"
  • Loading branch information
olivierdagenais committed Oct 25, 2013
1 parent bd21c0d commit f4e33a2
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/main/java/hudson/plugins/tfs/model/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.io.IOException;
import java.io.Reader;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.security.CodeSource;
import java.security.ProtectionDomain;
Expand Down Expand Up @@ -67,7 +68,15 @@ static synchronized void ensureNativeLibrariesConfigured() {
final CodeSource codeSource = protectionDomain.getCodeSource();
// TODO: codeSource could be null; what should we do, then?
final URL location = codeSource.getLocation();
final String stringPathToJar = location.getFile();
URI locationUri = null;
try {
locationUri = location.toURI();
} catch (URISyntaxException e) {
// this shouldn't happen
// TODO: consider logging this situation if it ever happens
return;
}
final String stringPathToJar = locationUri.getPath();
final File pathToJar = new File(stringPathToJar);

This comment has been minimized.

Copy link
@jglick

jglick Nov 13, 2013

Member

This is wrong; never ever try to interconvert a URI.path with File.path directly. Instead use File.<init>(URI) which takes care of a number of issues automatically: path separator, handling of unusual characters such as #, etc.

The reverse operation is File.toUri. Never use File.toURL which is badly broken.

(The only thing File/URI methods do not handle quite correctly is Windows UNC paths such as \\server\share\path, for which you need to depend on Java 7: java.nio.file methods properly interconvert URI and Path even when UNC paths are involved, e.g. as file://server/share/path, whereas File/URI conversion works with file:////server/share/path which does not survive a round trip through URI.normalize.)

final File pathToLibFolder = pathToJar.getParentFile();
final File pathToNativeFolder = new File(pathToLibFolder, "native");
Expand Down

0 comments on commit f4e33a2

Please sign in to comment.