Ignore:
Timestamp:
2010-08-22T20:18:44+02:00 (14 years ago)
Author:
bomm
Message:

Add a getVersion method to RemoteControlPlugin to detect future incompatibilities in dependent plug-ins.
Use PermissionPrefWithDefault instead of PermissionPref to allow specification of individual default values.
Use constants for permission preference keys and default values.

Location:
applications/editors/josm/plugins/remotecontrol/src/org/openstreetmap/josm/plugins/remotecontrol
Files:
1 added
9 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/remotecontrol/src/org/openstreetmap/josm/plugins/remotecontrol/PermissionPref.java

    r22675 r22733  
    55 * implemented by the RequestHandler, and an error message to be displayed
    66 * if not permitted.
     7 *
     8 * Use @see PermissionPrefWithDefault instead of this class.
     9 *
     10 * @author Bodo Meissner
    711 */
     12 @Deprecated
    813public class PermissionPref {
     14        /** name of the preference setting to permit the remote operation */
    915        String pref;
     16        /** message to be displayed if operation is not permitted */
    1017        String message;
     18       
    1119        public PermissionPref(String pref, String message)
    1220        {
  • applications/editors/josm/plugins/remotecontrol/src/org/openstreetmap/josm/plugins/remotecontrol/RemoteControlPlugin.java

    r22675 r22733  
    88
    99/**
    10 
     10 * Base plugin for remote control operations.
     11 * This plugin contains operations that use JOSM core only.
     12 *
     13 * Other plugins can register additional operations by calling
     14 * @see addRequestHandler().
     15 * To allow API changes this plugin contains a @see getVersion() method.
     16 *
     17 * IMPORTANT! increment the minor version on compatible API extensions
     18 * and increment the major version and set minor to 0 on incompatible changes.
    1119 */
    1220public class RemoteControlPlugin extends Plugin
    1321{
     22        /** API version
     23         * IMPORTANT! update the version number on API changes.
     24         */
     25        static final int apiMajorVersion = 1;
     26        static final int apiMinorVersion = 0;
     27       
     28        /**
     29         * RemoteControl HTTP protocol version. Change minor number for compatible
     30         * interface extensions. Change major number in case of incompatible
     31         * changes.
     32         */
     33        static final int protocolMajorVersion = 1;
     34        static final int protocolMinorVersion = 2;
     35       
    1436    /** The HTTP server this plugin launches */
    1537    static HttpServer server;
    1638
     39    /**
     40     * Returns an array of int values with major and minor API version
     41     * and major and minor HTTP protocol version.
     42     * 
     43     * The function returns an int[4] instead of an object with fields
     44     * to avoid ClassNotFound errors with old versions of remotecontrol.
     45     * 
     46     * @return array of integer version numbers:
     47     *    apiMajorVersion, apiMinorVersion, protocolMajorVersion, protocolMajorVersion
     48     */
     49    public int[] getVersion()
     50    {
     51        int versions[] = {apiMajorVersion, apiMinorVersion, protocolMajorVersion, protocolMajorVersion};
     52        return versions;
     53    }
     54   
    1755    /**
    1856     * Creates the plugin, and starts the HTTP server
  • applications/editors/josm/plugins/remotecontrol/src/org/openstreetmap/josm/plugins/remotecontrol/RemoteControlPreferences.java

    r22702 r22733  
    1515import org.openstreetmap.josm.gui.preferences.PreferenceSetting;
    1616import org.openstreetmap.josm.gui.preferences.PreferenceTabbedPane;
     17import org.openstreetmap.josm.plugins.remotecontrol.handler.AddNodeHandler;
     18import org.openstreetmap.josm.plugins.remotecontrol.handler.ImportHandler;
     19import org.openstreetmap.josm.plugins.remotecontrol.handler.LoadAndZoomHandler;
     20import org.openstreetmap.josm.plugins.remotecontrol.handler.VersionHandler;
    1721import org.openstreetmap.josm.tools.GBC;
    1822
     
    2529{
    2630    private JCheckBox permissionLoadData = new JCheckBox(tr("load data from API"));
     31    private JCheckBox permissionImportData = new JCheckBox(tr("import data from URL"));
    2732    private JCheckBox permissionCreateObjects = new JCheckBox(tr("create new objects"));
    2833    private JCheckBox permissionChangeSelection = new JCheckBox(tr("change the selection"));
     
    4247        perms.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.gray), tr("Permitted actions")));
    4348        perms.add(permissionLoadData, GBC.eol().insets(0,5,0,0).fill(GBC.HORIZONTAL));
     49        perms.add(permissionImportData, GBC.eol().insets(0,5,0,0).fill(GBC.HORIZONTAL));
    4450        perms.add(permissionChangeSelection, GBC.eol().insets(0,5,0,0).fill(GBC.HORIZONTAL));
    4551        perms.add(permissionChangeViewport, GBC.eol().insets(0,5,0,0).fill(GBC.HORIZONTAL));
     
    5157        remote.add(Box.createVerticalGlue(), GBC.eol().fill(GBC.VERTICAL));
    5258
    53         permissionLoadData.setSelected(Main.pref.getBoolean("remotecontrol.permission.load-data", true));
    54         permissionChangeSelection.setSelected(Main.pref.getBoolean("remotecontrol.permission.change-selection", true));
    55         permissionChangeViewport.setSelected(Main.pref.getBoolean("remotecontrol.permission.change-viewport", true));
    56         permissionCreateObjects.setSelected(Main.pref.getBoolean("remotecontrol.permission.create-objects", false));
    57         permissionReadProtocolversion.setSelected(Main.pref.getBoolean("remotecontrol.permission.read-protocolversion", true));
    58         alwaysAskUserConfirm.setSelected(Main.pref.getBoolean("remotecontrol.always-confirm", false));
     59        permissionLoadData.setSelected(Main.pref.getBoolean(LoadAndZoomHandler.loadDataPermissionKey, LoadAndZoomHandler.loadDataPermissionDefault));
     60        permissionImportData.setSelected(Main.pref.getBoolean(ImportHandler.permissionKey, ImportHandler.permissionDefault));
     61        permissionChangeSelection.setSelected(Main.pref.getBoolean(LoadAndZoomHandler.changeSelectionPermissionKey, LoadAndZoomHandler.changeSelectionPermissionDefault));
     62        permissionChangeViewport.setSelected(Main.pref.getBoolean(LoadAndZoomHandler.changeViewportPermissionKey, LoadAndZoomHandler.changeViewportPermissionDefault));
     63        permissionCreateObjects.setSelected(Main.pref.getBoolean(AddNodeHandler.permissionKey, AddNodeHandler.permissionDefault));
     64        permissionReadProtocolversion.setSelected(Main.pref.getBoolean(VersionHandler.permissionKey, VersionHandler.permissionDefault));
     65        alwaysAskUserConfirm.setSelected(Main.pref.getBoolean(RequestHandler.globalConfirmationKey, RequestHandler.globalConfirmationDefault));
    5966
    6067    }
    6168
    6269    public boolean ok() {
    63         Main.pref.put("remotecontrol.permission.load-data", permissionLoadData.isSelected());
    64         Main.pref.put("remotecontrol.permission.change-selection", permissionChangeSelection.isSelected());
    65         Main.pref.put("remotecontrol.permission.change-viewport", permissionChangeViewport.isSelected());
    66         Main.pref.put("remotecontrol.permission.create-objects", permissionCreateObjects.isSelected());
    67         Main.pref.put("remotecontrol.permission.read-protocolversion", permissionReadProtocolversion.isSelected());
    68         Main.pref.put("remotecontrol.always-confirm", alwaysAskUserConfirm.isSelected());
     70        Main.pref.put(LoadAndZoomHandler.loadDataPermissionKey, permissionLoadData.isSelected());
     71        Main.pref.put(ImportHandler.permissionKey, permissionImportData.isSelected());
     72        Main.pref.put(LoadAndZoomHandler.changeSelectionPermissionKey, permissionChangeSelection.isSelected());
     73        Main.pref.put(LoadAndZoomHandler.changeViewportPermissionKey, permissionChangeViewport.isSelected());
     74        Main.pref.put(AddNodeHandler.permissionKey, permissionCreateObjects.isSelected());
     75        Main.pref.put(VersionHandler.permissionKey, permissionReadProtocolversion.isSelected());
     76        Main.pref.put(RequestHandler.globalConfirmationKey, alwaysAskUserConfirm.isSelected());
    6977        // FIXME confirm return value - really no restart needed?
    7078        return false /* no restart needed */;
  • applications/editors/josm/plugins/remotecontrol/src/org/openstreetmap/josm/plugins/remotecontrol/RequestHandler.java

    r22675 r22733  
    1818public abstract class RequestHandler
    1919{
     20        public static final String globalConfirmationKey = "remotecontrol.always-confirm";
     21        public static final boolean globalConfirmationDefault = false;
     22       
    2023        /** The GET request arguments */
    2124        protected HashMap<String,String> args;
     
    3033
    3134    /** will be filled with the command assigned to the subclass */
    32     private String myCommand;
     35    protected String myCommand;
    3336   
    3437    /**
     
    7780     * @return the preference name and error message or null
    7881     */
    79     public PermissionPref getPermissionPref()
     82        @SuppressWarnings("deprecation")
     83        public PermissionPref getPermissionPref()
    8084    {
    8185        /* Example:
    82         return new PermissionPref("fooobar.remotecontrol",
     86        return new PermissionPrefWithDefault("fooobar.remotecontrol",
     87        true
    8388        "RemoteControl: foobar forbidden by preferences");
    8489        */
     
    97102     * @throws RequestHandlerForbiddenException
    98103     */
    99     final public void checkPermission() throws RequestHandlerForbiddenException
     104    @SuppressWarnings("deprecation")
     105        final public void checkPermission() throws RequestHandlerForbiddenException
    100106    {
    101107        /*
    102108         * If the subclass defines a specific preference and if this is set
    103109         * to false, abort with an error message.
     110         *
     111         * Note: we use the deprecated class here for compatibility with
     112         * older versions of WMSPlugin.
    104113         */
    105114        PermissionPref permissionPref = getPermissionPref();
    106115        if((permissionPref != null) && (permissionPref.pref != null))
    107116        {
    108             if (!Main.pref.getBoolean(permissionPref.pref, true)) {
    109                 System.out.println(permissionPref.message);
     117            PermissionPrefWithDefault permissionPrefWithDefault;
     118            if(permissionPref instanceof PermissionPrefWithDefault)
     119            {
     120                permissionPrefWithDefault = (PermissionPrefWithDefault) permissionPref;
     121            }
     122            else
     123            {
     124                permissionPrefWithDefault = new PermissionPrefWithDefault(permissionPref);
     125            }
     126            if (!Main.pref.getBoolean(permissionPrefWithDefault.pref,
     127                        permissionPrefWithDefault.defaultVal)) {
     128                System.out.println(permissionPrefWithDefault.message);
    110129                throw new RequestHandlerForbiddenException();
    111130            }
     
    115134         * If yes, display specific confirmation message.
    116135         */
    117         if (Main.pref.getBoolean("remotecontrol.always-confirm", false)) {
     136        if (Main.pref.getBoolean(globalConfirmationKey, globalConfirmationDefault)) {
    118137            if (JOptionPane.showConfirmDialog(Main.parent,
    119138                "<html>" + getPermissionMessage() +
  • applications/editors/josm/plugins/remotecontrol/src/org/openstreetmap/josm/plugins/remotecontrol/RequestProcessor.java

    r22675 r22733  
    2828         * changes.
    2929         */
    30         public static final String PROTOCOLVERSION = "{\"protocolversion\": {\"major\": 1, \"minor\": 0}, \"application\": \"JOSM RemoteControl\"}";
     30        public static final String PROTOCOLVERSION = "{\"protocolversion\": {\"major\": " +
     31                RemoteControlPlugin.protocolMajorVersion + ", \"minor\": " +
     32                RemoteControlPlugin.protocolMinorVersion +
     33                "}, \"application\": \"JOSM RemoteControl\"}";
    3134
    3235        /** The socket this processor listens on */
     
    8285        private static void addRequestHandlerClass(String command,
    8386                                Class<? extends RequestHandler> handler, boolean silent) {
    84                 if (handlers.get(command) != null) {
    85                         System.out.println("RemoteControl: duplicate command " + command
     87                if(command.charAt(0) == '/')
     88                {
     89                        command = command.substring(1);
     90                }
     91                if (handlers.get("/" + command) != null) {
     92                        System.out.println("RemoteControl: ignoring duplicate command " + command
    8693                                        + " with handler " + handler.getName());
    8794                } else {
    88                         if(!silent) System.out.println("RemoteControl: adding command command " +
    89                                         command + " (handled by " + handler.getName() + ")");
     95                        if(!silent) System.out.println("RemoteControl: adding command \"" +
     96                                        command + "\" (handled by " + handler.getSimpleName() + ")");
    9097                        handlers.put(command, handler);
    9198                }
     
    95102        static {
    96103                addRequestHandlerClass(LoadAndZoomHandler.command,
     104                                LoadAndZoomHandler.class, true);
     105                addRequestHandlerClass(LoadAndZoomHandler.command2,
    97106                                LoadAndZoomHandler.class, true);
    98107                addRequestHandlerClass(AddNodeHandler.command, AddNodeHandler.class, true);
  • applications/editors/josm/plugins/remotecontrol/src/org/openstreetmap/josm/plugins/remotecontrol/handler/AddNodeHandler.java

    r22675 r22733  
    99import org.openstreetmap.josm.data.coor.LatLon;
    1010import org.openstreetmap.josm.data.osm.Node;
    11 import org.openstreetmap.josm.plugins.remotecontrol.PermissionPref;
     11import org.openstreetmap.josm.plugins.remotecontrol.PermissionPrefWithDefault;
    1212import org.openstreetmap.josm.plugins.remotecontrol.RequestHandler;
    13 import org.openstreetmap.josm.plugins.remotecontrol.RequestHandlerBadRequestException;
    1413
    1514/**
     
    1817public class AddNodeHandler extends RequestHandler {
    1918
    20         public static final String command = "/add_node";
     19        public static final String command = "add_node";
     20        public static final String permissionKey = "remotecontrol.permission.create-objects";
     21        public static final boolean permissionDefault = false;
    2122
    2223        @Override
     
    3738
    3839        @Override
    39         public PermissionPref getPermissionPref()
     40        public PermissionPrefWithDefault getPermissionPref()
    4041        {
    41                 return new PermissionPref("remotecontrol.permission.create-objects",
     42                return new PermissionPrefWithDefault(permissionKey, permissionDefault,
    4243                                "RemoteControl: creating objects forbidden by preferences");
    4344        }
  • applications/editors/josm/plugins/remotecontrol/src/org/openstreetmap/josm/plugins/remotecontrol/handler/ImportHandler.java

    r22675 r22733  
    77import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmTask;
    88import org.openstreetmap.josm.actions.downloadtasks.DownloadTask;
    9 import org.openstreetmap.josm.plugins.remotecontrol.PermissionPref;
     9import org.openstreetmap.josm.plugins.remotecontrol.PermissionPrefWithDefault;
    1010import org.openstreetmap.josm.plugins.remotecontrol.RequestHandler;
    11 import org.openstreetmap.josm.plugins.remotecontrol.RequestHandlerBadRequestException;
    1211import org.openstreetmap.josm.plugins.remotecontrol.RequestHandlerErrorException;
    1312
     
    1716public class ImportHandler extends RequestHandler {
    1817
    19         public static final String command = "/import";
     18        public static final String command = "import";
     19        public static final String permissionKey = "remotecontrol.permission.import";
     20        public static final boolean permissionDefault = true;
    2021
    2122        @Override
     
    4445
    4546        @Override
    46         public PermissionPref getPermissionPref()
     47        public PermissionPrefWithDefault getPermissionPref()
    4748        {
    48                 return new PermissionPref("remotecontrol.permission.import",
     49                return new PermissionPrefWithDefault(permissionKey, permissionDefault,
    4950                                "RemoteControl: import forbidden by preferences");
    5051        }
  • applications/editors/josm/plugins/remotecontrol/src/org/openstreetmap/josm/plugins/remotecontrol/handler/LoadAndZoomHandler.java

    r22675 r22733  
    2222import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
    2323import org.openstreetmap.josm.plugins.remotecontrol.RequestHandler;
    24 import org.openstreetmap.josm.plugins.remotecontrol.RequestHandlerBadRequestException;
    2524import org.openstreetmap.josm.plugins.remotecontrol.RequestHandlerErrorException;
    2625
     
    3029public class LoadAndZoomHandler extends RequestHandler
    3130{
    32         public static final String command = "/load_and_zoom";
    33 
     31        public static final String command = "load_and_zoom";
     32        public static final String command2 = "zoom";
     33       
     34        public static final String loadDataPermissionKey = "remotecontrol.permission.load-data";
     35        public static final boolean loadDataPermissionDefault = true;
     36        public static final String changeSelectionPermissionKey = "remotecontrol.permission.change-selection";
     37        public static final boolean changeSelectionPermissionDefault = true;
     38        public static final String changeViewportPermissionKey = "remotecontrol.permission.change-viewport";
     39        public static final boolean changeViewportPermissionDefault = true;
     40       
    3441    @Override
    3542    public String getPermissionMessage()
     
    5966                        maxlon = Double.parseDouble(args.get("right"));
    6067
    61                         if (!Main.pref.getBoolean("remotecontrol.permission.load-data", true))
     68                        if(command.equals(myCommand))
    6269                        {
    63                                 System.out.println("RemoteControl: download forbidden by preferences");
    64                         }
    65                         else
    66                         {
    67 
    68                                 // find out whether some data has already been downloaded
    69                                 Area present = null;
    70                                 Area toDownload = null;
    71                                 DataSet ds = Main.main.getCurrentDataSet();
    72                                 if (ds != null)
    73                                         present = ds.getDataSourceArea();
    74                                 if (present != null && !present.isEmpty()) {
    75                                         toDownload = new Area(new Rectangle2D.Double(minlon,minlat,maxlon-minlon,maxlat-minlat));
    76                                         toDownload.subtract(present);
    77                                         if (!toDownload.isEmpty())
    78                                         {
    79                                                 // the result might not be a rectangle (L shaped etc)
    80                                                 Rectangle2D downloadBounds = toDownload.getBounds2D();
    81                                                 minlat = downloadBounds.getMinY();
    82                                                 minlon = downloadBounds.getMinX();
    83                                                 maxlat = downloadBounds.getMaxY();
    84                                                 maxlon = downloadBounds.getMaxX();
    85                                         }
    86                                 }
    87                                 if((toDownload != null) && toDownload.isEmpty())
     70                                if (!Main.pref.getBoolean(loadDataPermissionKey, loadDataPermissionDefault))
    8871                                {
    89                                         System.out.println("RemoteControl: no download necessary");
     72                                        System.out.println("RemoteControl: download forbidden by preferences");
    9073                                }
    9174                                else
    9275                                {
    93                     Future<?> future = osmTask.download(false /*no new layer*/, new Bounds(minlat,minlon,maxlat,maxlon), null /* let the task manage the progress monitor */);
    94                     Main.worker.submit(new PostDownloadHandler(osmTask, future));
     76       
     77                                        // find out whether some data has already been downloaded
     78                                        Area present = null;
     79                                        Area toDownload = null;
     80                                        DataSet ds = Main.main.getCurrentDataSet();
     81                                        if (ds != null)
     82                                                present = ds.getDataSourceArea();
     83                                        if (present != null && !present.isEmpty()) {
     84                                                toDownload = new Area(new Rectangle2D.Double(minlon,minlat,maxlon-minlon,maxlat-minlat));
     85                                                toDownload.subtract(present);
     86                                                if (!toDownload.isEmpty())
     87                                                {
     88                                                        // the result might not be a rectangle (L shaped etc)
     89                                                        Rectangle2D downloadBounds = toDownload.getBounds2D();
     90                                                        minlat = downloadBounds.getMinY();
     91                                                        minlon = downloadBounds.getMinX();
     92                                                        maxlat = downloadBounds.getMaxY();
     93                                                        maxlon = downloadBounds.getMaxX();
     94                                                }
     95                                        }
     96                                        if((toDownload != null) && toDownload.isEmpty())
     97                                        {
     98                                                System.out.println("RemoteControl: no download necessary");
     99                                        }
     100                                        else
     101                                        {
     102                            Future<?> future = osmTask.download(false /*no new layer*/, new Bounds(minlat,minlon,maxlat,maxlon), null /* let the task manage the progress monitor */);
     103                            Main.worker.submit(new PostDownloadHandler(osmTask, future));
     104                                        }
    95105                                }
    96106                        }
     
    100110                        throw new RequestHandlerErrorException();
    101111                }
    102                 if (args.containsKey("select") && Main.pref.getBoolean("remotecontrol.permission.change-selection", true)) {
     112                if (args.containsKey("select") && Main.pref.getBoolean(changeSelectionPermissionKey, changeSelectionPermissionDefault)) {
    103113                        // select objects after downloading, zoom to selection.
    104114                        final String selection = args.get("select");
     
    129139                                        for (Relation r : ds.getRelations()) if (relations.contains(r.getId())) newSel.add(r);
    130140                                        ds.setSelected(newSel);
    131                                         if (Main.pref.getBoolean("remotecontrol.permission.change-viewport", true))
     141                                        if (Main.pref.getBoolean(changeViewportPermissionKey, changeViewportPermissionDefault))
    132142                                                new AutoScaleAction("selection").actionPerformed(null);
    133143                                }
    134144                        });
    135                 } else if (Main.pref.getBoolean("remotecontrol.permission.change-viewport", true)) {
     145                } else if (Main.pref.getBoolean(changeViewportPermissionKey, changeViewportPermissionDefault)) {
    136146                        // after downloading, zoom to downloaded area.
    137                         final Bounds bounds = new Bounds(new LatLon(minlat, minlon),
    138                                         new LatLon(maxlat, maxlon));
     147                        zoom(minlat, maxlat, minlon, maxlon);
     148                }
     149        }
    139150
    140                         // make sure this isn't called unless there *is* a MapView
    141                         //
    142                         if (Main.map != null && Main.map.mapView != null) {
    143                                 Main.worker.execute(new Runnable() {
    144                                         public void run() {
    145                                                 BoundingXYVisitor bbox = new BoundingXYVisitor();
    146                                                 bbox.visit(bounds);
    147                                                 Main.map.mapView.recalculateCenterScale(bbox);
    148                                         }
    149                                 });
    150                         }
     151        protected void zoom(double minlat, double maxlat, double minlon, double maxlon) {
     152                final Bounds bounds = new Bounds(new LatLon(minlat, minlon),
     153                                new LatLon(maxlat, maxlon));
     154
     155                // make sure this isn't called unless there *is* a MapView
     156                //
     157                if (Main.map != null && Main.map.mapView != null) {
     158                        Main.worker.execute(new Runnable() {
     159                                public void run() {
     160                                        BoundingXYVisitor bbox = new BoundingXYVisitor();
     161                                        bbox.visit(bounds);
     162                                        Main.map.mapView.recalculateCenterScale(bbox);
     163                                }
     164                        });
    151165                }
    152166        }
  • applications/editors/josm/plugins/remotecontrol/src/org/openstreetmap/josm/plugins/remotecontrol/handler/VersionHandler.java

    r22675 r22733  
    33import static org.openstreetmap.josm.tools.I18n.tr;
    44
    5 import org.openstreetmap.josm.plugins.remotecontrol.PermissionPref;
     5import org.openstreetmap.josm.plugins.remotecontrol.PermissionPrefWithDefault;
    66import org.openstreetmap.josm.plugins.remotecontrol.RequestHandler;
    77import org.openstreetmap.josm.plugins.remotecontrol.RequestHandlerBadRequestException;
     
    1414public class VersionHandler extends RequestHandler {
    1515
    16         public static final String command = "/version";
     16        public static final String command = "version";
     17        public static final String permissionKey = "remotecontrol.permission.read-protocolversion";
     18        public static final boolean permissionDefault = true;
    1719
    1820        @Override
     
    3133        }
    3234
    33         public PermissionPref getPermissionPref()
     35        @Override
     36        public PermissionPrefWithDefault getPermissionPref()
    3437        {
    35                 return new PermissionPref("remotecontrol.permission.read-protocolversion",
     38                return new PermissionPrefWithDefault(permissionKey, permissionDefault,
    3639                                "RemoteControl: /version forbidden by preferences");
    3740        }
Note: See TracChangeset for help on using the changeset viewer.