Ignore:
Timestamp:
2014-02-02T23:23:27+01:00 (10 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.