Changeset 8191 in josm for trunk


Ignore:
Timestamp:
2015-04-14T22:23:31+02:00 (10 years ago)
Author:
simon04
Message:

see #10646 - Remote Control: simplify load_data implementation

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/io/remotecontrol/handler/LoadDataHandler.java

    r7937 r8191  
    55
    66import java.io.ByteArrayInputStream;
    7 import java.io.InputStream;
    87import java.io.UnsupportedEncodingException;
    9 import java.net.URLDecoder;
    10 import java.util.HashMap;
    11 import java.util.Map;
     8import java.net.URLEncoder;
     9import java.nio.charset.StandardCharsets;
    1210
    1311import org.openstreetmap.josm.Main;
    1412import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmTask;
    1513import org.openstreetmap.josm.data.osm.DataSet;
     14import org.openstreetmap.josm.io.IllegalDataException;
    1615import org.openstreetmap.josm.io.OsmReader;
    1716import org.openstreetmap.josm.io.remotecontrol.PermissionPrefWithDefault;
     17import org.openstreetmap.josm.tools.Utils;
    1818
    1919/**
     
    3636
    3737    /**
    38      * Holds the mime type. Currently only OSM_MIME_TYPE is supported
    39      * But it could be extended to text/csv, application/gpx+xml, ... or even binary encoded data
     38     * Holds the parsed data set
    4039     */
    41     private String mimeType;
     40    private DataSet dataSet;
    4241
    4342    @Override
    4443    protected void handleRequest() throws RequestHandlerErrorException {
    45         try {
    46             // Transform data string to inputstream
    47             InputStream source = new ByteArrayInputStream(data.getBytes("UTF-8"));
    48             DataSet dataSet = new DataSet();
    49             if (mimeType != null && mimeType.contains(OSM_MIME_TYPE))
    50                 dataSet = OsmReader.parseDataSet(source, null);
    51             Main.worker.submit(new LoadDataTask(isLoadInNewLayer(), dataSet, args.get("layer_name")));
    52         } catch (Exception e) {
    53             Main.warn("Problem with data: " + data);
    54             throw new RequestHandlerErrorException(e);
    55         }
     44        Main.worker.submit(new LoadDataTask(isLoadInNewLayer(), dataSet, args.get("layer_name")));
    5645    }
    5746
     
    7362    @Override
    7463    public String[] getUsageExamples() {
    75         return new String[] {
    76                 "/load_data?layer_name=extra_layer&new_layer=true&data=%3Cosm%3E%3Cnode%3E...%3C%2Fnode%3E%3C%2Fosm%3E" };
     64        try {
     65            final String data = URLEncoder.encode("<osm version='0.6'><node id='-1' lat='1' lon='2' /></osm>", "UTF-8");
     66            return new String[]{
     67                    "/load_data?layer_name=extra_layer&new_layer=true&data=" + data};
     68        } catch (UnsupportedEncodingException ex) {
     69            throw new IllegalStateException(ex);
     70        }
    7771    }
    7872
     
    9084
    9185    @Override
    92     protected void parseArgs() {
    93         if (request.indexOf('?') == -1)
    94             return; // nothing to do
    95 
    96         Map<String, String> args = new HashMap<>();
    97 
    98         // The data itself shouldn't contain any &, = or ? chars.
    99         // Those are reserved for the URL parsing
    100         // and should be URL encoded as %26, %3D or %3F
    101         String query = request.substring(request.indexOf('?') + 1);
    102         String[] params = query.split("&");
    103         for (String param : params) {
    104             String[] kv = param.split("=");
    105             if (kv.length == 2)
    106                 args.put(kv[0], kv[1]);
    107         }
    108         this.args = args;
    109     }
    110 
    111     @Override
    11286    protected void validateRequest() throws RequestHandlerBadRequestException {
    113         if (args.get("data") == null)
    114             throw new RequestHandlerBadRequestException("RemoteControl: No data defined in URL");
     87        this.data = args.get("data");
     88        /**
     89         * Holds the mime type. Currently only OSM_MIME_TYPE is supported
     90         * But it could be extended to text/csv, application/gpx+xml, ... or even binary encoded data
     91         */
     92        final String mimeType = Utils.firstNonNull(args.get("mime_type"), OSM_MIME_TYPE);
    11593        try {
    116             data = URLDecoder.decode(args.get("data"), "UTF-8");
    117         } catch (UnsupportedEncodingException e) {
    118             throw new RequestHandlerBadRequestException("RemoteControl: UnsupportedEncodingException: " + e.getMessage(), e);
    119         }
    120         mimeType = args.get("mime_type");
    121         if (mimeType == null) {
    122             mimeType = OSM_MIME_TYPE;
     94            if (OSM_MIME_TYPE.equals(mimeType)) {
     95                final ByteArrayInputStream in = new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8));
     96                dataSet = OsmReader.parseDataSet(in, null);
     97            } else {
     98                dataSet = new DataSet();
     99            }
     100        } catch (IllegalDataException e) {
     101            throw new RequestHandlerBadRequestException("Failed to parse " + data + ": " + e.getMessage(), e);
    123102        }
    124103    }
Note: See TracChangeset for help on using the changeset viewer.