- Timestamp:
- 2013-04-17T20:14:32+02:00 (12 years ago)
- Location:
- trunk/src/org/openstreetmap/josm/io/remotecontrol
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/io/remotecontrol/AddTagsDialog.java
r5845 r5876 11 11 import java.awt.event.KeyEvent; 12 12 import java.awt.event.MouseEvent; 13 import java.io.UnsupportedEncodingException; 14 import java.net.URLDecoder; 13 15 import java.util.Collection; 14 16 import java.util.HashMap; 17 import java.util.HashSet; 18 import java.util.Map; 19 import java.util.Set; 15 20 import javax.swing.AbstractAction; 21 import javax.swing.JCheckBox; 16 22 17 23 import javax.swing.JPanel; 18 24 import javax.swing.JTable; 19 25 import javax.swing.KeyStroke; 20 import javax.swing.event.CellEditorListener;21 import javax.swing.event.ChangeEvent;22 26 import javax.swing.table.DefaultTableModel; 23 27 import javax.swing.table.TableCellEditor; … … 31 35 import org.openstreetmap.josm.data.osm.OsmPrimitive; 32 36 import org.openstreetmap.josm.gui.ExtendedDialog; 37 import org.openstreetmap.josm.gui.util.GuiHelper; 33 38 import org.openstreetmap.josm.gui.util.TableHelper; 34 39 import org.openstreetmap.josm.tools.GBC; … … 46 51 47 52 53 /** initially given tags **/ 54 String[][] tags; 55 48 56 private final JTable propertyTable; 49 57 private Collection<? extends OsmPrimitive> sel; 50 58 int[] count; 51 59 60 String sender; 61 static Set<String> trustedSenders = new HashSet<String>(); 62 52 63 /** 53 64 * Class for displaying "delete from ... objects" in the table … … 110 121 } 111 122 112 public AddTagsDialog(String[][] tags ) {123 public AddTagsDialog(String[][] tags, String senderName) { 113 124 super(Main.parent, tr("Add tags to selected objects"), new String[] { tr("Add selected tags"), tr("Add all tags"), tr("Cancel")}, 114 125 false, 115 126 true); 116 127 setToolTipTexts(new String[]{tr("Add checked tags to selected objects"), tr("Shift+Enter: Add all tags to selected objects"), ""}); 117 128 129 this.sender = senderName; 130 118 131 DataSet.addSelectionListener(this); 119 132 … … 211 224 tablePanel.add(propertyTable.getTableHeader(), GBC.eol().fill(GBC.HORIZONTAL)); 212 225 tablePanel.add(propertyTable, GBC.eol().fill(GBC.BOTH)); 226 if (!trustedSenders.contains(sender)) { 227 final JCheckBox c = new JCheckBox(); 228 c.setAction(new AbstractAction(tr("Accept all tags from {0} for this session", sender) ) { 229 @Override public void actionPerformed(ActionEvent e) { 230 if (c.isSelected()) 231 trustedSenders.add(sender); 232 else 233 trustedSenders.remove(sender); 234 } 235 } ); 236 tablePanel.add(c , GBC.eol().insets(20,10,0,0)); 237 } 213 238 setContent(tablePanel); 214 239 setDefaultButton(2); … … 251 276 } 252 277 } 278 } 279 if (buttonIndex == 2) { 280 trustedSenders.remove(sender); 253 281 } 254 282 setVisible(false); … … 260 288 findExistingTags(); 261 289 } 262 290 291 /* 292 * parse addtags parameters Example URL (part): 293 * addtags=wikipedia:de%3DResidenzschloss Dresden|name:en%3DDresden Castle 294 */ 295 public static void addTags(final Map<String, String> args, final String sender) { 296 if (args.containsKey("addtags")) { 297 GuiHelper.executeByMainWorkerInEDT(new Runnable() { 298 299 public void run() { 300 String[] tags = null; 301 try { 302 tags = URLDecoder.decode(args.get("addtags"), "UTF-8").split("\\|"); 303 } catch (UnsupportedEncodingException e) { 304 throw new RuntimeException(); 305 } 306 Set<String> tagSet = new HashSet<String>(); 307 for (String tag : tags) { 308 if (!tag.trim().isEmpty() && tag.contains("=")) { 309 tagSet.add(tag.trim()); 310 } 311 } 312 if (!tagSet.isEmpty()) { 313 String[][] keyValue = new String[tagSet.size()][2]; 314 int i = 0; 315 for (String tag : tagSet) { 316 // support a = b===c as "a"="b===c" 317 String [] pair = tag.split("\\s*=\\s*",2); 318 keyValue[i][0] = pair[0]; 319 keyValue[i][1] = pair.length<2 ? "": pair[1]; 320 i++; 321 } 322 addTagsIfNeeded(keyValue, sender); 323 } 324 } 325 326 327 }); 328 } 329 } 330 331 private static void addTagsIfNeeded(String[][] keyValue, String sender) { 332 if (trustedSenders.contains(sender)) { 333 if (Main.main.getCurrentDataSet() != null) { 334 Collection<OsmPrimitive> s = Main.main.getCurrentDataSet().getSelected(); 335 for (int j = 0; j < keyValue.length; j++) { 336 Main.main.undoRedo.add(new ChangePropertyCommand(s, keyValue[j][0], keyValue[j][1])); 337 } 338 } 339 } else { 340 new AddTagsDialog(keyValue, sender).showDialog(); 341 } 342 } 263 343 } -
trunk/src/org/openstreetmap/josm/io/remotecontrol/RequestProcessor.java
r5837 r5876 2 2 package org.openstreetmap.josm.io.remotecontrol; 3 3 4 import java.io.BufferedInputStream;5 4 import java.io.BufferedOutputStream; 5 import java.io.BufferedReader; 6 6 import java.io.IOException; 7 7 import java.io.InputStreamReader; 8 8 import java.io.OutputStream; 9 9 import java.io.OutputStreamWriter; 10 import java.io.Reader;11 10 import java.io.Writer; 12 11 import java.net.Socket; 13 12 import java.util.Arrays; 14 13 import java.util.Date; 14 import java.util.HashMap; 15 15 import java.util.Map; 16 16 import java.util.Map.Entry; 17 17 import java.util.StringTokenizer; 18 18 import java.util.TreeMap; 19 import java.util.regex.Matcher; 20 import java.util.regex.Pattern; 19 21 20 22 import org.openstreetmap.josm.io.remotecontrol.handler.AddNodeHandler; … … 136 138 request.getOutputStream()); 137 139 out = new OutputStreamWriter(raw); 138 Reader in = new InputStreamReader(new BufferedInputStream( 139 request.getInputStream()), "ASCII"); 140 141 StringBuffer requestLine = new StringBuffer(); 142 while (requestLine.length() < 1024 * 1024) { 143 int c = in.read(); 144 if (c == -1 || c == '\r' || c == '\n') 145 break; 146 requestLine.append((char) c); 147 } 148 149 System.out.println("RemoteControl received: " + requestLine); 150 String get = requestLine.toString(); 140 BufferedReader in = new BufferedReader(new InputStreamReader(request.getInputStream(), "ASCII")); 141 142 String get = in.readLine(); 143 System.out.println("RemoteControl received: " + get); 144 151 145 StringTokenizer st = new StringTokenizer(get); 152 146 if (!st.hasMoreTokens()) { … … 165 159 return; 166 160 } 167 168 String command = null; 161 169 162 int questionPos = url.indexOf('?'); 170 if(questionPos < 0) 171 { 172 command = url; 173 } 174 else 175 { 176 command = url.substring(0, questionPos); 177 } 178 163 164 String command = questionPos < 0 ? url : url.substring(0, questionPos); 165 166 Map <String,String> headers = new HashMap<String, String>(); 167 int k=0, MAX_HEADERS=20; 168 while (k<MAX_HEADERS) { 169 get=in.readLine(); 170 if (get==null) break; 171 k++; 172 String h[] = get.split(": ", 2); 173 if (h.length==2) { 174 headers.put(h[0], h[1]); 175 } else break; 176 } 177 178 // Who sent the request: trying our best to detect 179 // not from localhost => sender = IP 180 // from localhost: sender = referer header, if exists 181 String sender = null; 182 183 if (!request.getInetAddress().isLoopbackAddress()) { 184 sender = request.getInetAddress().getHostAddress(); 185 } else { 186 String ref = headers.get("Referer"); 187 Pattern r = Pattern.compile("(https?://)?([^/]*)"); 188 if (ref!=null) { 189 Matcher m = r.matcher(ref); 190 if (m.find()) { 191 sender = m.group(2); 192 } 193 } 194 if (sender == null) { 195 sender = "localhost"; 196 } 197 } 198 179 199 // find a handler for this command 180 200 Class<? extends RequestHandler> handlerClass = handlers.get(command); … … 202 222 handler.setCommand(command); 203 223 handler.setUrl(url); 224 handler.setSender(sender); 204 225 handler.handle(); 205 226 sendHeader(out, "200 OK", handler.getContentType(), false); -
trunk/src/org/openstreetmap/josm/io/remotecontrol/handler/AddNodeHandler.java
r5845 r5876 13 13 import org.openstreetmap.josm.data.osm.OsmPrimitive; 14 14 import org.openstreetmap.josm.gui.util.GuiHelper; 15 import org.openstreetmap.josm.io.remotecontrol.AddTagsDialog; 15 16 import org.openstreetmap.josm.io.remotecontrol.PermissionPrefWithDefault; 16 17 import org.openstreetmap.josm.io.remotecontrol.handler.RequestHandler.RequestHandlerBadRequestException; … … 90 91 } 91 92 // parse parameter addtags=tag1=value1|tag2=vlaue2 92 LoadAndZoomHandler.addTags(args);93 AddTagsDialog.addTags(args, sender); 93 94 } 94 95 -
trunk/src/org/openstreetmap/josm/io/remotecontrol/handler/AddWayHandler.java
r5845 r5876 20 20 import org.openstreetmap.josm.data.osm.Way; 21 21 import org.openstreetmap.josm.gui.util.GuiHelper; 22 import org.openstreetmap.josm.io.remotecontrol.AddTagsDialog; 22 23 import org.openstreetmap.josm.io.remotecontrol.PermissionPrefWithDefault; 23 24 import org.openstreetmap.josm.io.remotecontrol.handler.RequestHandler.RequestHandlerBadRequestException; … … 53 54 }); 54 55 // parse parameter addtags=tag1=value1|tag2=value2 55 LoadAndZoomHandler.addTags(args);56 AddTagsDialog.addTags(args, sender); 56 57 } 57 58 -
trunk/src/org/openstreetmap/josm/io/remotecontrol/handler/LoadAndZoomHandler.java
r5790 r5876 6 6 import java.awt.geom.Area; 7 7 import java.awt.geom.Rectangle2D; 8 import java.io.UnsupportedEncodingException;9 import java.net.URLDecoder;10 8 import java.util.HashSet; 11 import java.util.Map;12 9 import java.util.Set; 13 10 import java.util.concurrent.Future; … … 191 188 } 192 189 193 addTags(args); 194 } 195 196 /* 197 * parse addtags parameters Example URL (part): 198 * addtags=wikipedia:de%3DResidenzschloss Dresden|name:en%3DDresden Castle 199 */ 200 static void addTags(final Map<String, String> args) { 201 if (args.containsKey("addtags")) { 202 GuiHelper.executeByMainWorkerInEDT(new Runnable() { 203 204 public void run() { 205 String[] tags = null; 206 try { 207 tags = URLDecoder.decode(args.get("addtags"), "UTF-8").split("\\|"); 208 } catch (UnsupportedEncodingException e) { 209 throw new RuntimeException(); 210 } 211 Set<String> tagSet = new HashSet<String>(); 212 for (String tag : tags) { 213 if (!tag.trim().isEmpty() && tag.contains("=")) { 214 tagSet.add(tag.trim()); 215 } 216 } 217 if (!tagSet.isEmpty()) { 218 String[][] keyValue = new String[tagSet.size()][2]; 219 int i = 0; 220 for (String tag : tagSet) { 221 // support a = b===c as "a"="b===c" 222 String [] pair = tag.split("\\s*=\\s*",2); 223 keyValue[i][0] = pair[0]; 224 keyValue[i][1] = pair.length<2 ? "": pair[1]; 225 i++; 226 } 227 228 new AddTagsDialog(keyValue).showDialog(); 229 } 230 } 231 }); 232 } 190 AddTagsDialog.addTags(args, sender); 233 191 } 234 192 -
trunk/src/org/openstreetmap/josm/io/remotecontrol/handler/LoadObjectHandler.java
r5844 r5876 10 10 import org.openstreetmap.josm.data.osm.SimplePrimitiveId; 11 11 import org.openstreetmap.josm.gui.util.GuiHelper; 12 import org.openstreetmap.josm.io.remotecontrol.AddTagsDialog; 12 13 import org.openstreetmap.josm.io.remotecontrol.PermissionPrefWithDefault; 13 14 … … 48 49 public void run() { 49 50 Main.main.getCurrentDataSet().setSelected(ps); 50 LoadAndZoomHandler.addTags(args);51 AddTagsDialog.addTags(args, sender); 51 52 ps.clear(); 52 53 } -
trunk/src/org/openstreetmap/josm/io/remotecontrol/handler/RequestHandler.java
r5691 r5876 43 43 /** will be filled with the command assigned to the subclass */ 44 44 protected String myCommand; 45 46 /** 47 * who send th request? 48 * the host from refrerer header or IP of request sender 49 */ 50 protected String sender; 45 51 46 52 /** … … 242 248 } 243 249 250 public void setSender(String sender) { 251 this.sender = sender; 252 } 253 244 254 public static class RequestHandlerException extends Exception { 245 255
Note:
See TracChangeset
for help on using the changeset viewer.