Changeset 5085 in josm


Ignore:
Timestamp:
2012-03-14T23:33:27+01:00 (8 years ago)
Author:
simon04
Message:

fix #5824 - remotecontrol: add command for opening local files + refactoring of permission preference

Location:
trunk/src/org/openstreetmap/josm
Files:
1 added
11 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/preferences/RemoteControlPreference.java

    r4976 r5085  
    99import java.awt.event.ActionEvent;
    1010import java.awt.event.ActionListener;
     11import java.util.LinkedHashMap;
     12import java.util.Map;
     13import java.util.Map.Entry;
    1114
    1215import javax.swing.BorderFactory;
     
    1619import javax.swing.JPanel;
    1720import javax.swing.JSeparator;
    18 import javax.swing.UIManager;
    1921
    2022import org.openstreetmap.josm.Main;
    2123import org.openstreetmap.josm.gui.util.GuiHelper;
     24import org.openstreetmap.josm.io.remotecontrol.PermissionPrefWithDefault;
    2225import org.openstreetmap.josm.io.remotecontrol.RemoteControl;
    23 import org.openstreetmap.josm.io.remotecontrol.handler.AddNodeHandler;
    24 import org.openstreetmap.josm.io.remotecontrol.handler.ImageryHandler;
    25 import org.openstreetmap.josm.io.remotecontrol.handler.ImportHandler;
    26 import org.openstreetmap.josm.io.remotecontrol.handler.LoadAndZoomHandler;
    2726import org.openstreetmap.josm.io.remotecontrol.handler.RequestHandler;
    28 import org.openstreetmap.josm.io.remotecontrol.handler.VersionHandler;
    2927import org.openstreetmap.josm.tools.GBC;
    3028
     
    3432 * @author Frederik Ramm
    3533 */
    36 public class RemoteControlPreference extends DefaultTabPreferenceSetting
    37 {
     34public class RemoteControlPreference extends DefaultTabPreferenceSetting {
     35
    3836    public static class Factory implements PreferenceSettingFactory {
     37
    3938        @Override
    4039        public PreferenceSetting createPreferenceSetting() {
     
    4241        }
    4342    }
    44    
     43
    4544    private RemoteControlPreference() {
    4645        super("remotecontrol", tr("Remote Control"), tr("Settings for the remote control feature."));
     46        for (PermissionPrefWithDefault p : PermissionPrefWithDefault.getPermissionPrefs()) {
     47            JCheckBox cb = new JCheckBox(p.preferenceText);
     48            cb.setSelected(p.isAllowed());
     49            prefs.put(p, cb);
     50        }
    4751    }
    48 
     52    private final Map<PermissionPrefWithDefault, JCheckBox> prefs =
     53            new LinkedHashMap<PermissionPrefWithDefault, JCheckBox>();
    4954    private JCheckBox enableRemoteControl;
    50 
    51     private JCheckBox permissionLoadData = new JCheckBox(tr("Load data from API"));
    52     private JCheckBox permissionImportData = new JCheckBox(tr("Import data from URL"));
    53     private JCheckBox permissionLoadImagery = new JCheckBox(tr("Load imagery layers"));
    54     private JCheckBox permissionCreateObjects = new JCheckBox(tr("Create new objects"));
    55     private JCheckBox permissionChangeSelection = new JCheckBox(tr("Change the selection"));
    56     private JCheckBox permissionChangeViewport = new JCheckBox(tr("Change the viewport"));
    57     private JCheckBox permissionReadProtocolversion = new JCheckBox(tr("Read protocol version"));
    5855    private JCheckBox loadInNewLayer = new JCheckBox(tr("Download objects to new layer"));
    5956    private JCheckBox alwaysAskUserConfirm = new JCheckBox(tr("Confirm all Remote Control actions manually"));
    6057
     58    @Override
    6159    public void addGui(final PreferenceTabbedPane gui) {
    6260
    6361        JPanel remote = new JPanel(new GridBagLayout());
     62
     63        final JLabel descLabel = new JLabel("<html>"
     64                + tr("Allows JOSM to be controlled from other applications, e.g. from a web browser.")
     65                + "</html>");
     66        descLabel.setFont(descLabel.getFont().deriveFont(Font.PLAIN));
     67        remote.add(descLabel, GBC.eol().insets(5, 5, 0, 10).fill(GBC.HORIZONTAL));
     68
     69        final JLabel portLabel = new JLabel("<html>" + tr("JOSM will always listen at <b>port 8111</b> on localhost. "
     70                + "<br>This port is not configurable because it is referenced by external applications talking to JOSM.") + "</html>");
     71        portLabel.setFont(portLabel.getFont().deriveFont(Font.PLAIN));
     72        remote.add(portLabel, GBC.eol().insets(5, 5, 0, 10).fill(GBC.HORIZONTAL));
    6473
    6574        remote.add(enableRemoteControl = new JCheckBox(tr("Enable remote control"), RemoteControl.PROP_REMOTECONTROL_ENABLED.get()), GBC.eol());
     
    7180        remote.add(wrapper, GBC.eol().fill(GBC.HORIZONTAL).insets(5, 5, 5, 5));
    7281
    73         final JLabel descLabel = new JLabel("<html>"+
    74                 tr("The remote control feature allows JOSM to be controlled from other applications, e.g. from a web browser.")
    75                 + "</html>");
    76         wrapper.add(descLabel, GBC.eol().insets(5,5,0,10).fill(GBC.HORIZONTAL));
    77         descLabel.setFont(descLabel.getFont().deriveFont(Font.PLAIN));
    78 
    7982        wrapper.add(new JLabel(tr("Permitted actions:")), GBC.eol());
    8083        int INDENT = 15;
    81         wrapper.add(permissionLoadData, GBC.eol().insets(INDENT,5,0,0).fill(GBC.HORIZONTAL));
    82         wrapper.add(permissionImportData, GBC.eol().insets(INDENT,5,0,0).fill(GBC.HORIZONTAL));
    83         wrapper.add(permissionLoadImagery, GBC.eol().insets(INDENT,5,0,0).fill(GBC.HORIZONTAL));
    84         wrapper.add(permissionChangeSelection, GBC.eol().insets(INDENT,5,0,0).fill(GBC.HORIZONTAL));
    85         wrapper.add(permissionChangeViewport, GBC.eol().insets(INDENT,5,0,0).fill(GBC.HORIZONTAL));
    86         wrapper.add(permissionCreateObjects, GBC.eol().insets(INDENT,5,0,0).fill(GBC.HORIZONTAL));
    87         wrapper.add(permissionReadProtocolversion, GBC.eol().insets(INDENT,5,0,0).fill(GBC.HORIZONTAL));
     84        for (JCheckBox p : prefs.values()) {
     85            wrapper.add(p, GBC.eol().insets(INDENT, 5, 0, 0).fill(GBC.HORIZONTAL));
     86        }
    8887
    8988        wrapper.add(new JSeparator(), GBC.eop().fill(GBC.HORIZONTAL).insets(15, 5, 15, 5));
    90 
    9189        wrapper.add(loadInNewLayer, GBC.eol().fill(GBC.HORIZONTAL));
    92 
    9390        wrapper.add(alwaysAskUserConfirm, GBC.eol().fill(GBC.HORIZONTAL));
    94 
    95         final JLabel portLabel = new JLabel("<html>"+tr("JOSM will always listen at port 8111 on localhost. " +
    96                 "This port is not configurable because it is referenced by external applications talking to JOSM.") + "</html>");
    97         portLabel.setFont(portLabel.getFont().deriveFont(Font.PLAIN));
    98 
    99         wrapper.add(portLabel, GBC.eol().insets(5,5,0,10).fill(GBC.HORIZONTAL));
    10091
    10192        remote.add(Box.createVerticalGlue(), GBC.eol().fill(GBC.VERTICAL));
    10293
    103         permissionLoadData.setSelected(Main.pref.getBoolean(LoadAndZoomHandler.loadDataPermissionKey, LoadAndZoomHandler.loadDataPermissionDefault));
    104         permissionImportData.setSelected(Main.pref.getBoolean(ImportHandler.permissionKey, ImportHandler.permissionDefault));
    105         permissionLoadImagery.setSelected(Main.pref.getBoolean(ImageryHandler.permissionKey, ImageryHandler.permissionDefault));
    106         permissionChangeSelection.setSelected(Main.pref.getBoolean(LoadAndZoomHandler.changeSelectionPermissionKey, LoadAndZoomHandler.changeSelectionPermissionDefault));
    107         permissionChangeViewport.setSelected(Main.pref.getBoolean(LoadAndZoomHandler.changeViewportPermissionKey, LoadAndZoomHandler.changeViewportPermissionDefault));
    108         permissionCreateObjects.setSelected(Main.pref.getBoolean(AddNodeHandler.permissionKey, AddNodeHandler.permissionDefault));
    109         permissionReadProtocolversion.setSelected(Main.pref.getBoolean(VersionHandler.permissionKey, VersionHandler.permissionDefault));
    11094        loadInNewLayer.setSelected(Main.pref.getBoolean(RequestHandler.loadInNewLayerKey, RequestHandler.loadInNewLayerDefault));
    11195        alwaysAskUserConfirm.setSelected(Main.pref.getBoolean(RequestHandler.globalConfirmationKey, RequestHandler.globalConfirmationDefault));
    11296
    11397        ActionListener remoteControlEnabled = new ActionListener() {
     98
     99            @Override
    114100            public void actionPerformed(ActionEvent e) {
    115                 boolean enabled = enableRemoteControl.isSelected();
    116101                GuiHelper.setEnabledRec(wrapper, enableRemoteControl.isSelected());
    117102                // 'setEnabled(false)' does not work for JLabel with html text, so do it manually
    118                 portLabel.setForeground(enabled ? UIManager.getColor("Label.foreground") : UIManager.getColor("Label.disabledForeground"));
    119                 descLabel.setForeground(enabled ? UIManager.getColor("Label.foreground") : UIManager.getColor("Label.disabledForeground"));
    120103                // FIXME: use QuadStateCheckBox to make checkboxes unset when disabled
    121104            }
     
    126109    }
    127110
     111    @Override
    128112    public boolean ok() {
    129113        boolean enabled = enableRemoteControl.isSelected();
    130114        boolean changed = RemoteControl.PROP_REMOTECONTROL_ENABLED.put(enabled);
    131115        if (enabled) {
    132             Main.pref.put(LoadAndZoomHandler.loadDataPermissionKey, permissionLoadData.isSelected());
    133             Main.pref.put(ImportHandler.permissionKey, permissionImportData.isSelected());
    134             Main.pref.put(ImageryHandler.permissionKey, permissionLoadImagery.isSelected());
    135             Main.pref.put(LoadAndZoomHandler.changeSelectionPermissionKey, permissionChangeSelection.isSelected());
    136             Main.pref.put(LoadAndZoomHandler.changeViewportPermissionKey, permissionChangeViewport.isSelected());
    137             Main.pref.put(AddNodeHandler.permissionKey, permissionCreateObjects.isSelected());
    138             Main.pref.put(VersionHandler.permissionKey, permissionReadProtocolversion.isSelected());
     116            for (Entry<PermissionPrefWithDefault, JCheckBox> p : prefs.entrySet()) {
     117                Main.pref.put(p.getKey().pref, p.getValue().isSelected());
     118            }
    139119            Main.pref.put(RequestHandler.loadInNewLayerKey, loadInNewLayer.isSelected());
    140120            Main.pref.put(RequestHandler.globalConfirmationKey, alwaysAskUserConfirm.isSelected());
  • trunk/src/org/openstreetmap/josm/io/remotecontrol/PermissionPrefWithDefault.java

    r4191 r5085  
    22package org.openstreetmap.josm.io.remotecontrol;
    33
     4import java.util.Arrays;
     5import java.util.List;
     6import org.openstreetmap.josm.Main;
     7import static org.openstreetmap.josm.tools.I18n.tr;
     8
    49/**
    510 * Contains a preference name to control permission for the operation
    6  * implemented by the RequestHandler, and an error message to be displayed
    7  * if not permitted.
     11 * implemented by the RequestHandler, and an error message to be displayed if
     12 * not permitted.
    813 *
    914 * @author Bodo Meissner
     
    1116public class PermissionPrefWithDefault {
    1217
    13     /** name of the preference setting to permit the remote operation */
    14     public String pref;
    15     /** message to be displayed if operation is not permitted */
    16     public String message;
     18    public static final PermissionPrefWithDefault LOAD_DATA =
     19            new PermissionPrefWithDefault("remotecontrol.permission.load-data", true, tr("Load data from API"));
     20    public static final PermissionPrefWithDefault IMPORT_DATA =
     21            new PermissionPrefWithDefault("remotecontrol.permission.import", true, tr("Import data from URL"));
     22    public static final PermissionPrefWithDefault OPEN_FILES =
     23            new PermissionPrefWithDefault("remotecontrol.permission.open-files", false, tr("Open local files"));
     24    public static final PermissionPrefWithDefault LOAD_IMAGERY =
     25            new PermissionPrefWithDefault("remotecontrol.permission.imagery", true, tr("Load imagery layers"));
     26    public static final PermissionPrefWithDefault CHANGE_SELECTION =
     27            new PermissionPrefWithDefault("remotecontrol.permission.change-selection", true, tr("Change the selection"));
     28    public static final PermissionPrefWithDefault CHANGE_VIEWPORT =
     29            new PermissionPrefWithDefault("remotecontrol.permission.change-viewport", true, tr("Change the viewport"));
     30    public static final PermissionPrefWithDefault CREATE_OBJECTS =
     31            new PermissionPrefWithDefault("remotecontrol.permission.create-objects", true, tr("Create new objects"));
     32    public static final PermissionPrefWithDefault READ_PROTOCOL_VERSION =
     33            new PermissionPrefWithDefault("remotecontrol.permission.read-protocolversion", true, tr("Read protocol version"));
     34    /**
     35     * name of the preference setting to permit the remote operation
     36     */
     37    public final String pref;
     38    /**
     39     * default preference setting
     40     */
     41    public final boolean defaultVal;
     42    /**
     43     * text for the preference dialog checkbox
     44     */
     45    public final String preferenceText;
    1746
    18     public boolean defaultVal = true;
     47    public PermissionPrefWithDefault(String pref, boolean defaultVal, String preferenceText) {
     48        this.pref = pref;
     49        this.defaultVal = defaultVal;
     50        this.preferenceText = preferenceText;
     51    }
    1952
    20     public PermissionPrefWithDefault(String pref, boolean defaultVal, String message) {
    21         this.pref = pref;
    22         this.message = message;
    23         this.defaultVal = defaultVal;
     53    public boolean isAllowed() {
     54        return Main.pref.getBoolean(pref, defaultVal);
     55    }
     56
     57    public static List<PermissionPrefWithDefault> getPermissionPrefs() {
     58        return Arrays.asList(
     59                LOAD_DATA, IMPORT_DATA, OPEN_FILES, LOAD_IMAGERY,
     60                CHANGE_SELECTION, CHANGE_VIEWPORT,
     61                CREATE_OBJECTS, READ_PROTOCOL_VERSION);
    2462    }
    2563}
  • trunk/src/org/openstreetmap/josm/io/remotecontrol/RequestProcessor.java

    r4834 r5085  
    1313import java.util.Arrays;
    1414import java.util.Date;
    15 import java.util.HashMap;
    1615import java.util.Map;
    1716import java.util.Map.Entry;
    1817import java.util.StringTokenizer;
    1918import java.util.TreeMap;
    20 import java.util.TreeSet;
    2119
    2220import org.openstreetmap.josm.io.remotecontrol.handler.AddNodeHandler;
     
    2624import org.openstreetmap.josm.io.remotecontrol.handler.LoadAndZoomHandler;
    2725import org.openstreetmap.josm.io.remotecontrol.handler.LoadObjectHandler;
     26import org.openstreetmap.josm.io.remotecontrol.handler.OpenFileHandler;
    2827import org.openstreetmap.josm.io.remotecontrol.handler.RequestHandler;
    2928import org.openstreetmap.josm.io.remotecontrol.handler.RequestHandler.RequestHandlerBadRequestException;
     
    117116    /** Add default request handlers */
    118117    static {
    119         addRequestHandlerClass(LoadAndZoomHandler.command,
    120                 LoadAndZoomHandler.class, true);
    121         addRequestHandlerClass(LoadAndZoomHandler.command2,
    122                 LoadAndZoomHandler.class, true);
     118        addRequestHandlerClass(LoadAndZoomHandler.command, LoadAndZoomHandler.class, true);
     119        addRequestHandlerClass(LoadAndZoomHandler.command2, LoadAndZoomHandler.class, true);
    123120        addRequestHandlerClass(ImageryHandler.command, ImageryHandler.class, true);
    124121        addRequestHandlerClass(AddNodeHandler.command, AddNodeHandler.class, true);
     
    127124        addRequestHandlerClass(VersionHandler.command, VersionHandler.class, true);
    128125        addRequestHandlerClass(LoadObjectHandler.command, LoadObjectHandler.class, true);
     126        addRequestHandlerClass(OpenFileHandler.command, OpenFileHandler.class, true);
    129127    }
    130128
     
    203201                    handler.setCommand(command);
    204202                    handler.setUrl(url);
    205                     handler.checkPermission();
    206203                    handler.handle();
    207204                    sendHeader(out, "200 OK", handler.getContentType(), false);
     
    216213                    sendBadRequest(out, ex.getMessage());
    217214                } catch (RequestHandlerForbiddenException ex) {
    218                     sendForbidden(out);
     215                    sendForbidden(out, ex.getMessage());
    219216                }
    220217            }
     
    281278     *             If the error can not be written
    282279     */
    283     private void sendForbidden(Writer out) throws IOException {
     280    private void sendForbidden(Writer out, String help) throws IOException {
    284281        sendHeader(out, "403 Forbidden", "text/html", true);
    285282        out.write("<HTML>\r\n");
     
    288285        out.write("<BODY>");
    289286        out.write("<H1>HTTP Error 403: Forbidden</h2>\r\n");
     287        if (help != null) {
     288            out.write(help);
     289        }
    290290        out.write("</BODY></HTML>\r\n");
    291291        out.flush();
  • trunk/src/org/openstreetmap/josm/io/remotecontrol/handler/AddNodeHandler.java

    r4946 r5085  
    1818
    1919    public static final String command = "add_node";
    20     public static final String permissionKey = "remotecontrol.permission.create-objects";
    21     public static final boolean permissionDefault = false;
    2220
    2321    @Override
     
    3937    @Override
    4038    public PermissionPrefWithDefault getPermissionPref() {
    41         return new PermissionPrefWithDefault(permissionKey, permissionDefault,
    42                 "RemoteControl: creating objects forbidden by preferences");
     39        return PermissionPrefWithDefault.CREATE_OBJECTS;
    4340    }
    4441
     
    6158        Main.main.undoRedo.add(new AddCommand(nnew));
    6259        Main.main.getCurrentDataSet().setSelected(nnew);
    63         if (Main.pref.getBoolean(LoadAndZoomHandler.changeViewportPermissionKey, LoadAndZoomHandler.changeViewportPermissionDefault)) {
     60        if (PermissionPrefWithDefault.CHANGE_VIEWPORT.isAllowed()) {
    6461            AutoScaleAction.autoScale("selection");
    6562        } else {
  • trunk/src/org/openstreetmap/josm/io/remotecontrol/handler/AddWayHandler.java

    r4946 r5085  
    1313import org.openstreetmap.josm.data.osm.Node;
    1414import org.openstreetmap.josm.data.osm.Way;
     15import org.openstreetmap.josm.io.remotecontrol.PermissionPrefWithDefault;
    1516
    1617/**
     
    4142        Main.main.undoRedo.add(new SequenceCommand(tr("Add way"), commands));
    4243        Main.main.getCurrentDataSet().setSelected(way);
    43         if (Main.pref.getBoolean(LoadAndZoomHandler.changeViewportPermissionKey, LoadAndZoomHandler.changeViewportPermissionDefault)) {
     44        if (PermissionPrefWithDefault.CHANGE_VIEWPORT.isAllowed()) {
    4445            AutoScaleAction.autoScale("selection");
    4546        } else {
     
    5253        return tr("Remote Control has been asked to create a new way.");
    5354    }
     55
     56    @Override
     57    public PermissionPrefWithDefault getPermissionPref() {
     58        return PermissionPrefWithDefault.CREATE_OBJECTS;
     59    }
    5460}
  • trunk/src/org/openstreetmap/josm/io/remotecontrol/handler/ImageryHandler.java

    r5010 r5085  
    77import java.net.URLDecoder;
    88import java.util.HashMap;
    9 import java.util.StringTokenizer;
    109
    1110import org.openstreetmap.josm.Main;
     
    1514
    1615public class ImageryHandler extends RequestHandler {
     16
    1717    public static final String command = "imagery";
    18     public static final String permissionKey = "remotecontrol.permission.imagery";
    19     public static final boolean permissionDefault = true;
    2018
    2119    @Override
    2220    public String getPermissionMessage() {
    23         return tr("Remote Control has been asked to load an imagery layer from the following URL:") +
    24         "<br>" + args.get("url");
     21        return tr("Remote Control has been asked to load an imagery layer from the following URL:")
     22                + "<br>" + args.get("url");
    2523    }
    2624
    2725    @Override
    28     public String[] getMandatoryParams()
    29     {
    30         return new String[] { "url" };
     26    public String[] getMandatoryParams() {
     27        return new String[]{"url"};
    3128    }
    3229
    3330    @Override
    34     public PermissionPrefWithDefault getPermissionPref()
    35     {
    36         return new PermissionPrefWithDefault(permissionKey, permissionDefault,
    37         "RemoteControl: import forbidden by preferences");
     31    public PermissionPrefWithDefault getPermissionPref() {
     32        return PermissionPrefWithDefault.LOAD_IMAGERY;
    3833    }
    3934
     
    4136    protected void handleRequest() throws RequestHandlerErrorException {
    4237        if (Main.map == null) //Avoid exception when creating ImageryLayer with null MapFrame
     38        {
    4339            throw new RequestHandlerErrorException();
     40        }
    4441        String url = args.get("url");
    4542        String title = args.get("title");
    4643        String type = args.get("type");
    47         if((title == null) || (title.length() == 0))
    48         {
     44        if ((title == null) || (title.length() == 0)) {
    4945            title = tr("Remote imagery");
    5046        }
  • trunk/src/org/openstreetmap/josm/io/remotecontrol/handler/ImportHandler.java

    r5010 r5085  
    1818
    1919    public static final String command = "import";
    20     public static final String permissionKey = "remotecontrol.permission.import";
    21     public static final boolean permissionDefault = true;
    2220
    2321    @Override
     
    3432
    3533    @Override
    36     public String[] getMandatoryParams()
    37     {
    38         return new String[] { "url" };
     34    public String[] getMandatoryParams() {
     35        return new String[]{"url"};
    3936    }
    4037
    4138    @Override
    4239    public String getPermissionMessage() {
    43         return tr("Remote Control has been asked to import data from the following URL:") +
    44         "<br>" + request;
     40        return tr("Remote Control has been asked to import data from the following URL:")
     41                + "<br>" + request;
    4542    }
    4643
    4744    @Override
    48     public PermissionPrefWithDefault getPermissionPref()
    49     {
    50         return new PermissionPrefWithDefault(permissionKey, permissionDefault,
    51                 "RemoteControl: import forbidden by preferences");
     45    public PermissionPrefWithDefault getPermissionPref() {
     46        return PermissionPrefWithDefault.IMPORT_DATA;
    5247    }
    5348
  • trunk/src/org/openstreetmap/josm/io/remotecontrol/handler/LoadAndZoomHandler.java

    r4945 r5085  
    2525import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
    2626import org.openstreetmap.josm.io.remotecontrol.AddTagsDialog;
     27import org.openstreetmap.josm.io.remotecontrol.PermissionPrefWithDefault;
    2728import org.openstreetmap.josm.tools.Utils;
    2829
     
    3435    public static final String command = "load_and_zoom";
    3536    public static final String command2 = "zoom";
    36 
    37     public static final String loadDataPermissionKey = "remotecontrol.permission.load-data";
    38     public static final boolean loadDataPermissionDefault = true;
    39     public static final String changeSelectionPermissionKey = "remotecontrol.permission.change-selection";
    40     public static final boolean changeSelectionPermissionDefault = true;
    41     public static final String changeViewportPermissionKey = "remotecontrol.permission.change-viewport";
    42     public static final boolean changeViewportPermissionDefault = true;
    4337
    4438    @Override
     
    7266            if(command.equals(myCommand))
    7367            {
    74                 if (!Main.pref.getBoolean(loadDataPermissionKey, loadDataPermissionDefault))
     68                if (!PermissionPrefWithDefault.LOAD_DATA.isAllowed())
    7569                {
    7670                    System.out.println("RemoteControl: download forbidden by preferences");
     
    131125        }
    132126
    133         if (args.containsKey("select") && Main.pref.getBoolean(changeSelectionPermissionKey, changeSelectionPermissionDefault)) {
     127        if (args.containsKey("select") && PermissionPrefWithDefault.CHANGE_SELECTION.isAllowed()) {
    134128            // select objects after downloading, zoom to selection.
    135129            final String selection = args.get("select");
     
    172166                    }
    173167                    ds.setSelected(newSel);
    174                     if (Main.pref.getBoolean(changeViewportPermissionKey, changeViewportPermissionDefault)) {
     168                    if (PermissionPrefWithDefault.CHANGE_VIEWPORT.isAllowed()) {
    175169                        AutoScaleAction.autoScale("selection");
    176170                    }
     
    182176                }
    183177            });
    184         } else if (Main.pref.getBoolean(changeViewportPermissionKey, changeViewportPermissionDefault)) {
     178        } else if (PermissionPrefWithDefault.CHANGE_VIEWPORT.isAllowed()) {
    185179            // after downloading, zoom to downloaded area.
    186180            zoom(minlat, maxlat, minlon, maxlon);
     
    232226        }
    233227    }
     228
     229    @Override
     230    public PermissionPrefWithDefault getPermissionPref() {
     231        return null;
     232    }
    234233}
  • trunk/src/org/openstreetmap/josm/io/remotecontrol/handler/LoadObjectHandler.java

    r4883 r5085  
    55import java.util.LinkedList;
    66import java.util.List;
    7 import org.openstreetmap.josm.Main;
    87import org.openstreetmap.josm.actions.DownloadPrimitiveAction;
    98import org.openstreetmap.josm.data.osm.PrimitiveId;
    109import org.openstreetmap.josm.data.osm.SimplePrimitiveId;
     10import org.openstreetmap.josm.io.remotecontrol.PermissionPrefWithDefault;
    1111
    1212/**
     
    2626    @Override
    2727    protected void handleRequest() throws RequestHandlerErrorException, RequestHandlerBadRequestException {
    28         if (!Main.pref.getBoolean(LoadAndZoomHandler.loadDataPermissionKey, LoadAndZoomHandler.loadDataPermissionDefault)) {
     28        if (!PermissionPrefWithDefault.LOAD_DATA.isAllowed()) {
    2929            System.out.println("RemoteControl: download forbidden by preferences");
    3030        }
     
    4242        return tr("Remote Control has been asked to load objects (specified by their id) from the API.");
    4343    }
     44
     45    @Override
     46    public PermissionPrefWithDefault getPermissionPref() {
     47        return PermissionPrefWithDefault.LOAD_DATA;
     48    }
    4449}
  • trunk/src/org/openstreetmap/josm/io/remotecontrol/handler/RequestHandler.java

    r5008 r5085  
    44import static org.openstreetmap.josm.tools.I18n.tr;
    55
     6import java.text.MessageFormat;
    67import java.util.HashMap;
    78import java.util.LinkedList;
    89import java.util.List;
    9 import java.util.StringTokenizer;
    1010
    1111import javax.swing.JOptionPane;
     
    5050    public final void handle() throws RequestHandlerForbiddenException, RequestHandlerBadRequestException, RequestHandlerErrorException
    5151    {
     52        checkMandatoryParams();
    5253        checkPermission();
    53         checkMandatoryParams();
    5454        handleRequest();
    5555    }
     
    8686     * @return the preference name and error message or null
    8787     */
    88     public PermissionPrefWithDefault getPermissionPref()
    89     {
    90         /* Example:
    91         return new PermissionPrefWithDefault("fooobar.remotecontrol",
    92         true
    93         "RemoteControl: foobar forbidden by preferences");
    94         */
    95         return null;
    96     }
    97 
    98     public String[] getMandatoryParams() {
    99         return null;
    100     }
     88    abstract public PermissionPrefWithDefault getPermissionPref();
     89
     90    abstract public String[] getMandatoryParams();
    10191
    10292    /**
     
    119109        {
    120110            if (!Main.pref.getBoolean(permissionPref.pref, permissionPref.defaultVal)) {
    121                 System.out.println(permissionPref.message);
    122                 throw new RequestHandlerForbiddenException();
     111                String err = MessageFormat.format("RemoteControl: ''{0}'' forbidden by preferences", myCommand);
     112                System.out.println(err);
     113                throw new RequestHandlerForbiddenException(err);
    123114            }
    124115        }
     
    133124                tr("Confirm Remote Control action"),
    134125                JOptionPane.YES_NO_OPTION) != JOptionPane.YES_OPTION) {
    135                     throw new RequestHandlerForbiddenException();
     126                    String err = MessageFormat.format("RemoteControl: ''{0}'' forbidden by user''s choice", myCommand);
     127                    throw new RequestHandlerForbiddenException(err);
    136128            }
    137129        }
     
    178170        List<String> missingKeys = new LinkedList<String>();
    179171        boolean error = false;
    180         for (int i = 0; i < mandatory.length; ++i) {
    181             String key = mandatory[i];
     172        for (String key : mandatory) {
    182173            String value = args.get(key);
    183174            if ((value == null) || (value.length() == 0)) {
     
    243234    public static class RequestHandlerForbiddenException extends RequestHandlerException {
    244235        private static final long serialVersionUID = 2263904699747115423L;
     236
     237        public RequestHandlerForbiddenException(String message) {
     238            super(message);
     239        }
    245240    }
    246241}
  • trunk/src/org/openstreetmap/josm/io/remotecontrol/handler/VersionHandler.java

    r3707 r5085  
    1313
    1414    public static final String command = "version";
    15     public static final String permissionKey = "remotecontrol.permission.read-protocolversion";
    16     public static final boolean permissionDefault = true;
    1715
    1816    @Override
     
    2220        contentType = "application/json";
    2321        if (args.containsKey("jsonp")) {
    24             content = args.get("jsonp")+ " && " + args.get("jsonp") + "(" + content + ")";
     22            content = args.get("jsonp") + " && " + args.get("jsonp") + "(" + content + ")";
    2523        }
    2624    }
     
    3230
    3331    @Override
    34     public PermissionPrefWithDefault getPermissionPref()
    35     {
    36         return new PermissionPrefWithDefault(permissionKey, permissionDefault,
    37                 "RemoteControl: /version forbidden by preferences");
     32    public PermissionPrefWithDefault getPermissionPref() {
     33        return PermissionPrefWithDefault.READ_PROTOCOL_VERSION;
     34    }
     35
     36    @Override
     37    public String[] getMandatoryParams() {
     38        return null;
    3839    }
    3940}
Note: See TracChangeset for help on using the changeset viewer.