Changeset 12794 in josm for trunk/src


Ignore:
Timestamp:
2017-09-08T23:34:46+02:00 (7 years ago)
Author:
Don-vip
Message:

fix #15274 - Support URLs with other protocol than http or https for plugin sites (patch by floscher)

Location:
trunk/src/org/openstreetmap/josm/plugins
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/plugins/PluginDownloadTask.java

    r12620 r12794  
    122122            }
    123123            URL url = new URL(pi.downloadlink);
    124             synchronized (this) {
    125                 downloadConnection = HttpClient.create(url).setAccept(PLUGIN_MIME_TYPES);
    126                 downloadConnection.connect();
    127             }
    128             try (InputStream in = downloadConnection.getResponse().getContent()) {
    129                 Files.copy(in, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
     124            Logging.debug("Download plugin {0} from {1}...", pi.name, url);
     125            if ("https".equals(url.getProtocol()) || "http".equals(url.getProtocol())) {
     126                synchronized (this) {
     127                    downloadConnection = HttpClient.create(url).setAccept(PLUGIN_MIME_TYPES);
     128                    downloadConnection.connect();
     129                }
     130                try (InputStream in = downloadConnection.getResponse().getContent()) {
     131                    Files.copy(in, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
     132                }
     133            } else {
     134                // this is an alternative for e.g. file:// URLs where HttpClient doesn't work
     135                try (InputStream in = url.openConnection().getInputStream()) {
     136                    Files.copy(in, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
     137                }
    130138            }
    131139        } catch (MalformedURLException e) {
  • trunk/src/org/openstreetmap/josm/plugins/ReadRemotePluginInformationTask.java

    r12620 r12794  
    1111import java.io.IOException;
    1212import java.io.InputStream;
     13import java.io.InputStreamReader;
    1314import java.io.PrintWriter;
    1415import java.net.MalformedURLException;
     
    155156            monitor.indeterminateSubTask(tr("Downloading plugin list from ''{0}''", printsite));
    156157
    157             URL url = new URL(site);
    158             connection = HttpClient.create(url).useCache(false);
    159             final HttpClient.Response response = connection.connect();
    160             content = response.fetchContent();
    161             if (response.getResponseCode() != 200) {
    162                 throw new IOException(tr("Unsuccessful HTTP request"));
    163             }
    164             return content;
     158            final URL url = new URL(site);
     159            if ("https".equals(url.getProtocol()) || "http".equals(url.getProtocol())) {
     160                connection = HttpClient.create(url).useCache(false);
     161                final HttpClient.Response response = connection.connect();
     162                content = response.fetchContent();
     163                if (response.getResponseCode() != 200) {
     164                    throw new IOException(tr("Unsuccessful HTTP request"));
     165                }
     166                return content;
     167            } else {
     168                // e.g. when downloading from a file:// URL, we can't use HttpClient
     169                try (InputStreamReader in = new InputStreamReader(url.openConnection().getInputStream(), StandardCharsets.UTF_8)) {
     170                    final StringBuilder sb = new StringBuilder();
     171                    final char[] buffer = new char[8192];
     172                    int numChars;
     173                    while ((numChars = in.read(buffer)) >= 0) {
     174                        sb.append(buffer, 0, numChars);
     175                        if (canceled) {
     176                            return null;
     177                        }
     178                    }
     179                    return sb.toString();
     180                }
     181            }
     182
    165183        } catch (MalformedURLException e) {
    166184            if (canceled) return null;
Note: See TracChangeset for help on using the changeset viewer.