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