Changeset 6803 in josm for trunk/src


Ignore:
Timestamp:
2014-02-02T23:23:27+01:00 (11 years ago)
Author:
simon04
Message:

fix #9660 - Allow to download compressed GPX tracks from osm.org/trace/ using "Download location"

This works by inspecting the filename in the Content-Disposition HTTP header.

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

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/OpenLocationAction.java

    r6643 r6803  
    184184            // TODO: handle multiple suitable tasks ?
    185185            try {
    186                 future = tasks.iterator().next().loadUrl(new_layer, url, monitor);
     186                task = tasks.iterator().next();
     187                future = task.loadUrl(new_layer, url, monitor);
    187188            } catch (IllegalArgumentException e) {
    188189                Main.error(e);
  • trunk/src/org/openstreetmap/josm/actions/downloadtasks/DownloadGpsTask.java

    r6380 r6803  
    5959    public Future<?> download(boolean newLayer, Bounds downloadArea, ProgressMonitor progressMonitor) {
    6060        downloadTask = new DownloadTask(newLayer,
    61                 new BoundingBoxDownloader(downloadArea), progressMonitor, false);
     61                new BoundingBoxDownloader(downloadArea), progressMonitor);
    6262        // We need submit instead of execute so we can wait for it to finish and get the error
    6363        // message if necessary. If no one calls getErrorMessage() it just behaves like execute.
     
    7070        if (url.matches(PATTERN_TRACE_ID) || url.matches(PATTERN_EXTERNAL_GPX_SCRIPT) || url.matches(PATTERN_EXTERNAL_GPX_FILE)) {
    7171            downloadTask = new DownloadTask(newLayer,
    72                     new OsmServerLocationReader(url), progressMonitor, url.matches(PATTERN_TRACE_ID));
     72                    new OsmServerLocationReader(url), progressMonitor);
    7373            // Extract .gpx filename from URL to set the new layer name
    7474            Matcher matcher = Pattern.compile(PATTERN_EXTERNAL_GPX_FILE).matcher(url);
     
    9999        private GpxData rawData;
    100100        private final boolean newLayer;
    101         private final boolean compressed;
    102 
    103         public DownloadTask(boolean newLayer, OsmServerReader reader, ProgressMonitor progressMonitor, boolean compressed) {
     101
     102        public DownloadTask(boolean newLayer, OsmServerReader reader, ProgressMonitor progressMonitor) {
    104103            super(tr("Downloading GPS data"));
    105104            this.reader = reader;
    106105            this.newLayer = newLayer;
    107             this.compressed = compressed;
    108106        }
    109107
     
    113111                    return;
    114112                ProgressMonitor subMonitor = progressMonitor.createSubTaskMonitor(ProgressMonitor.ALL_TICKS, false);
    115                 if (compressed) {
    116                     rawData = reader.parseRawGpsBzip2(subMonitor);
    117                 } else {
    118                     rawData = reader.parseRawGps(subMonitor);
    119                 }
     113                rawData = reader.parseRawGps(subMonitor);
    120114            } catch(Exception e) {
    121115                if (isCanceled())
  • trunk/src/org/openstreetmap/josm/io/OsmServerLocationReader.java

    r6716 r6803  
    138138        @Override
    139139        public GpxData parse() throws OsmTransferException, IllegalDataException, IOException, SAXException {
    140             in = getInputStreamRaw(url, progressMonitor.createSubTaskMonitor(1, true));
     140            in = getInputStreamRaw(url, progressMonitor.createSubTaskMonitor(1, true), null, true);
    141141            if (in == null)
    142142                return null;
  • trunk/src/org/openstreetmap/josm/io/OsmServerReader.java

    r6787 r6803  
    1111import java.net.MalformedURLException;
    1212import java.net.URL;
     13import java.util.List;
     14import java.util.Map;
    1315import java.util.zip.GZIPInputStream;
    1416import java.util.zip.Inflater;
     
    98100     */
    99101    protected InputStream getInputStreamRaw(String urlStr, ProgressMonitor progressMonitor, String reason) throws OsmTransferException {
     102        return getInputStreamRaw(urlStr, progressMonitor, reason, false);
     103    }
     104
     105    /**
     106     * Open a connection to the given url and return a reader on the input stream
     107     * from that connection. In case of user cancel, return <code>null</code>.
     108     * @param urlStr The exact url to connect to.
     109     * @param progressMonitor progress monitoring and abort handler
     110     * @param reason The reason to show on console. Can be {@code null} if no reason is given
     111     * @param uncompressAccordingToContentDisposition Whether to inspect the HTTP header {@code Content-Disposition}
     112     *                                                for {@code filename} and uncompress a gzip/bzip2 stream.
     113     * @return An reader reading the input stream (servers answer) or <code>null</code>.
     114     * @throws OsmTransferException thrown if data transfer errors occur
     115     */
     116    protected InputStream getInputStreamRaw(String urlStr, ProgressMonitor progressMonitor, String reason, boolean uncompressAccordingToContentDisposition) throws OsmTransferException {
    100117        try {
    101118            URL url = null;
     
    141158            }
    142159            try {
     160                Main.debug(activeConnection.getHeaderFields().toString());
    143161                if (activeConnection.getResponseCode() == HttpURLConnection.HTTP_UNAUTHORIZED)
    144162                    throw new OsmApiException(HttpURLConnection.HTTP_UNAUTHORIZED,null,null);
     
    169187                }
    170188
    171                 return fixEncoding(new ProgressInputStream(activeConnection, progressMonitor), encoding);
     189                InputStream in = new ProgressInputStream(activeConnection, progressMonitor);
     190                if (uncompressAccordingToContentDisposition) {
     191                    in = uncompressAccordingToContentDisposition(in, activeConnection.getHeaderFields());
     192                }
     193                return fixEncoding(in, encoding);
    172194            } catch (OsmTransferException e) {
    173195                throw e;
     
    189211    }
    190212
     213    private InputStream uncompressAccordingToContentDisposition(InputStream stream, Map<String, List<String>> headerFields) throws IOException {
     214        if (headerFields.get("Content-Disposition").toString().contains(".gz\"")) {
     215            return Compression.GZIP.getUncompressedInputStream(stream);
     216        } else if (headerFields.get("Content-Disposition").toString().contains(".bz2\"")) {
     217            return Compression.BZIP2.getUncompressedInputStream(stream);
     218        } else {
     219            return stream;
     220        }
     221    }
     222
    191223    /**
    192224     * Download OSM files from somewhere
Note: See TracChangeset for help on using the changeset viewer.