Changeset 15543 in josm


Ignore:
Timestamp:
2019-11-30T13:44:40+01:00 (4 months ago)
Author:
Don-vip
Message:

fix #18362 - safer construction of URIs

Location:
trunk/src/org/openstreetmap/josm/tools
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/tools/OpenBrowser.java

    r14389 r15543  
    66import java.awt.Desktop;
    77import java.io.IOException;
     8import java.net.MalformedURLException;
    89import java.net.URI;
    910import java.net.URISyntaxException;
     
    6364    public static String displayUrl(String url) {
    6465        try {
    65             return displayUrl(new URI(url));
    66         } catch (URISyntaxException e) {
     66            return displayUrl(Utils.urlToURI(url));
     67        } catch (URISyntaxException | MalformedURLException e) {
    6768            Logging.debug(e);
    6869            return e.getMessage();
  • trunk/src/org/openstreetmap/josm/tools/PlatformHookUnixoid.java

    r15293 r15543  
    1212import java.io.IOException;
    1313import java.io.InputStream;
    14 import java.net.URI;
    1514import java.net.URISyntaxException;
    1615import java.nio.charset.StandardCharsets;
     
    6968            try {
    7069                if ("#DESKTOP#".equals(program)) {
    71                     Desktop.getDesktop().browse(new URI(url));
     70                    Desktop.getDesktop().browse(Utils.urlToURI(url));
    7271                } else if (program.startsWith("$")) {
    7372                    program = System.getenv().get(program.substring(1));
  • trunk/src/org/openstreetmap/josm/tools/PlatformHookWindows.java

    r15469 r15543  
    4040import java.io.Writer;
    4141import java.lang.reflect.InvocationTargetException;
    42 import java.net.URI;
    4342import java.net.URISyntaxException;
    4443import java.nio.charset.StandardCharsets;
     
    169168        try {
    170169            // Desktop API works fine under Windows
    171             Desktop.getDesktop().browse(new URI(url));
     170            Desktop.getDesktop().browse(Utils.urlToURI(url));
    172171        } catch (IOException | URISyntaxException e) {
    173172            Logging.log(Logging.LEVEL_WARN, "Desktop class failed. Platform dependent fall back for open url in browser.", e);
  • trunk/src/org/openstreetmap/josm/tools/Utils.java

    r15416 r15543  
    1818import java.io.UnsupportedEncodingException;
    1919import java.net.MalformedURLException;
     20import java.net.URI;
     21import java.net.URISyntaxException;
    2022import java.net.URL;
    2123import java.net.URLDecoder;
     
    542544    }
    543545
     546    /**
     547     * Converts the given URL to its URI.
     548     * @param url the URL to get URI from
     549     * @return the URI of given URL
     550     * @throws URISyntaxException if the URL cannot be converted to an URI
     551     * @throws MalformedURLException if no protocol is specified, or an unknown protocol is found, or {@code spec} is {@code null}.
     552     * @since 15543
     553     */
     554    public static URI urlToURI(String url) throws URISyntaxException, MalformedURLException {
     555        return urlToURI(new URL(url));
     556    }
     557
     558    /**
     559     * Converts the given URL to its URI.
     560     * @param url the URL to get URI from
     561     * @return the URI of given URL
     562     * @throws URISyntaxException if the URL cannot be converted to an URI
     563     * @since 15543
     564     */
     565    public static URI urlToURI(URL url) throws URISyntaxException {
     566        try {
     567            return url.toURI();
     568        } catch (URISyntaxException e) {
     569            Logging.trace(e);
     570            return new URI(
     571                    url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef());
     572        }
     573    }
     574
    544575    private static final double EPSILON = 1e-11;
    545576
Note: See TracChangeset for help on using the changeset viewer.