- Timestamp:
- 2015-04-14T22:23:31+02:00 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/io/remotecontrol/handler/LoadDataHandler.java
r7937 r8191 5 5 6 6 import java.io.ByteArrayInputStream; 7 import java.io.InputStream;8 7 import java.io.UnsupportedEncodingException; 9 import java.net.URLDecoder; 10 import java.util.HashMap; 11 import java.util.Map; 8 import java.net.URLEncoder; 9 import java.nio.charset.StandardCharsets; 12 10 13 11 import org.openstreetmap.josm.Main; 14 12 import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmTask; 15 13 import org.openstreetmap.josm.data.osm.DataSet; 14 import org.openstreetmap.josm.io.IllegalDataException; 16 15 import org.openstreetmap.josm.io.OsmReader; 17 16 import org.openstreetmap.josm.io.remotecontrol.PermissionPrefWithDefault; 17 import org.openstreetmap.josm.tools.Utils; 18 18 19 19 /** … … 36 36 37 37 /** 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 40 39 */ 41 private String mimeType;40 private DataSet dataSet; 42 41 43 42 @Override 44 43 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"))); 56 45 } 57 46 … … 73 62 @Override 74 63 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 } 77 71 } 78 72 … … 90 84 91 85 @Override 92 protected void parseArgs() {93 if (request.indexOf('?') == -1)94 return; // nothing to do95 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 parsing100 // and should be URL encoded as %26, %3D or %3F101 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 @Override112 86 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); 115 93 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); 123 102 } 124 103 }
Note:
See TracChangeset
for help on using the changeset viewer.