001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.io.remotecontrol;
003
004import org.openstreetmap.josm.Main;
005import org.openstreetmap.josm.data.preferences.BooleanProperty;
006import org.openstreetmap.josm.io.remotecontrol.handler.RequestHandler;
007
008/**
009 * Manager class for remote control operations.
010 *
011 * IMPORTANT! increment the minor version on compatible API extensions
012 * and increment the major version and set minor to 0 on incompatible changes.
013 */
014public class RemoteControl {
015
016    /**
017     * If the remote control feature is enabled or disabled. If disabled,
018     * it should not start the server.
019     */
020    public static final BooleanProperty PROP_REMOTECONTROL_ENABLED = new BooleanProperty("remotecontrol.enabled", false);
021
022    /**
023     * If the remote control feature is enabled or disabled for HTTPS. If disabled,
024     * only HTTP access will be available.
025     * @since 7335
026     */
027    public static final BooleanProperty PROP_REMOTECONTROL_HTTPS_ENABLED = new BooleanProperty("remotecontrol.https.enabled", false);
028
029    /**
030     * RemoteControl HTTP protocol version. Change minor number for compatible
031     * interface extensions. Change major number in case of incompatible
032     * changes.
033     */
034    static final int protocolMajorVersion = 1;
035    static final int protocolMinorVersion = 5;
036
037    /**
038     * Starts the remote control server
039     */
040    public static void start() {
041        RemoteControlHttpServer.restartRemoteControlHttpServer();
042        RemoteControlHttpsServer.restartRemoteControlHttpsServer();
043    }
044
045    /**
046     * Stops the remote control server
047     * @since 5861
048     */
049    public static void stop() {
050        RemoteControlHttpServer.stopRemoteControlHttpServer();
051        RemoteControlHttpsServer.stopRemoteControlHttpsServer();
052    }
053
054    /**
055     * Adds external request handler.
056     * Can be used by plugins that want to use remote control.
057     *
058     * @param command The command name.
059     * @param handlerClass The additional request handler.
060     */
061    public void addRequestHandler(String command, Class<? extends RequestHandler> handlerClass) {
062        RequestProcessor.addRequestHandlerClass(command, handlerClass);
063    }
064
065    /**
066     * Returns the remote control directory.
067     * @return The remote control directory
068     * @since 7335
069     */
070    public static String getRemoteControlDir() {
071        return Main.pref.getPreferencesDir() + "remotecontrol/";
072    }
073}