Changeset 12596 in josm


Ignore:
Timestamp:
2017-08-13T00:24:03+02:00 (7 years ago)
Author:
Don-vip
Message:

fix #15141 - Make HTTP POST requests to Overpass API - allows longer/more complex queries

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

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/io/OsmServerReader.java

    r12510 r12596  
    119119
    120120    /**
    121      * Open a connection to the given url and return a reader on the input stream
     121     * Open a connection to the given url (if HTTP, trough a GET request) and return a reader on the input stream
    122122     * from that connection. In case of user cancel, return <code>null</code>.
    123123     * @param urlStr The exact url to connect to.
     
    129129     * @throws OsmTransferException if data transfer errors occur
    130130     */
     131    protected InputStream getInputStreamRaw(String urlStr, ProgressMonitor progressMonitor, String reason,
     132            boolean uncompressAccordingToContentDisposition) throws OsmTransferException {
     133        return getInputStreamRaw(urlStr, progressMonitor, reason, uncompressAccordingToContentDisposition, "GET", null);
     134    }
     135
     136    /**
     137     * Open a connection to the given url (if HTTP, with the specified method) and return a reader on the input stream
     138     * from that connection. In case of user cancel, return <code>null</code>.
     139     * @param urlStr The exact url to connect to.
     140     * @param progressMonitor progress monitoring and abort handler
     141     * @param reason The reason to show on console. Can be {@code null} if no reason is given
     142     * @param uncompressAccordingToContentDisposition Whether to inspect the HTTP header {@code Content-Disposition}
     143     *                                                for {@code filename} and uncompress a gzip/bzip2 stream.
     144     * @param httpMethod HTTP method ("GET", "POST" or "PUT")
     145     * @param requestBody HTTP request body (for "POST" and "PUT" methods only). Must be null for "GET" method.
     146     * @return An reader reading the input stream (servers answer) or <code>null</code>.
     147     * @throws OsmTransferException if data transfer errors occur
     148     * @since 12596
     149     */
    131150    @SuppressWarnings("resource")
    132151    protected InputStream getInputStreamRaw(String urlStr, ProgressMonitor progressMonitor, String reason,
    133             boolean uncompressAccordingToContentDisposition) throws OsmTransferException {
     152            boolean uncompressAccordingToContentDisposition, String httpMethod, byte[] requestBody) throws OsmTransferException {
    134153        try {
    135154            OnlineResource.JOSM_WEBSITE.checkOfflineAccess(urlStr, Main.getJOSMWebsite());
     
    151170            }
    152171
    153             final HttpClient client = HttpClient.create(url);
     172            final HttpClient client = HttpClient.create(url, httpMethod)
     173                    .setFinishOnCloseOutput(false)
     174                    .setReasonForRequest(reason)
     175                    .setRequestBody(requestBody);
    154176            activeConnection = client;
    155             client.setReasonForRequest(reason);
    156177            adaptRequest(client);
    157178            if (doAuthenticate) {
  • trunk/src/org/openstreetmap/josm/io/OverpassDownloadReader.java

    r12542 r12596  
    66import java.io.IOException;
    77import java.io.InputStream;
     8import java.nio.charset.StandardCharsets;
    89import java.util.EnumMap;
    910import java.util.Map;
     
    3536 */
    3637public class OverpassDownloadReader extends BoundingBoxDownloader {
     38
     39    private static final String DATA_PREFIX = "?data=";
    3740
    3841    static final class OverpassOsmReader extends OsmReader {
     
    144147            final String query = this.overpassQuery.replace("{{bbox}}", lat1 + "," + lon1 + "," + lat2 + "," + lon2);
    145148            final String expandedOverpassQuery = expandExtendedQueries(query);
    146             return "interpreter?data=" + Utils.encodeUrl(expandedOverpassQuery);
     149            return "interpreter" + DATA_PREFIX + Utils.encodeUrl(expandedOverpassQuery);
    147150        }
    148151    }
     
    195198                                            boolean uncompressAccordingToContentDisposition) throws OsmTransferException {
    196199        try {
    197             return super.getInputStreamRaw(urlStr, progressMonitor, reason, uncompressAccordingToContentDisposition);
     200            int index = urlStr.indexOf(DATA_PREFIX);
     201            // Make an HTTP POST request instead of a simple GET, allows more complex queries
     202            return super.getInputStreamRaw(urlStr.substring(0, index),
     203                    progressMonitor, reason, uncompressAccordingToContentDisposition,
     204                    "POST", Utils.decodeUrl(urlStr.substring(index + DATA_PREFIX.length())).getBytes(StandardCharsets.UTF_8));
    198205        } catch (OsmApiException ex) {
    199206            final String errorIndicator = "Error</strong>: ";
Note: See TracChangeset for help on using the changeset viewer.