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