source: josm/trunk/src/org/openstreetmap/josm/io/remotecontrol/RemoteControl.java@ 13929

Last change on this file since 13929 was 13929, checked in by Don-vip, 6 years ago

fix #11000 - add layer_locked, download_policy, upload_policy to download handlers (import, load_data, load_object and load_and_zoom). Bump remote control version to 1.8

  • Property svn:eol-style set to native
File size: 4.4 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.io.remotecontrol;
3
4import java.io.File;
5import java.net.Inet4Address;
6import java.net.Inet6Address;
7import java.net.InetAddress;
8import java.net.UnknownHostException;
9
10import org.openstreetmap.josm.data.preferences.BooleanProperty;
11import org.openstreetmap.josm.io.remotecontrol.handler.RequestHandler;
12import org.openstreetmap.josm.spi.preferences.Config;
13import org.openstreetmap.josm.tools.Logging;
14
15/**
16 * Manager class for remote control operations.
17 *
18 * IMPORTANT! increment the minor version on compatible API extensions
19 * and increment the major version and set minor to 0 on incompatible changes.
20 */
21public class RemoteControl {
22
23 /**
24 * If the remote control feature is enabled or disabled. If disabled,
25 * it should not start the server.
26 */
27 public static final BooleanProperty PROP_REMOTECONTROL_ENABLED = new BooleanProperty("remotecontrol.enabled", false);
28
29 /**
30 * If the remote control feature is enabled or disabled for HTTPS. If disabled,
31 * only HTTP access will be available.
32 * @since 7335
33 */
34 public static final BooleanProperty PROP_REMOTECONTROL_HTTPS_ENABLED = new BooleanProperty(
35 "remotecontrol.https.enabled", false);
36
37 /**
38 * RemoteControl HTTP protocol version. Change minor number for compatible
39 * interface extensions. Change major number in case of incompatible
40 * changes.
41 */
42 static final int protocolMajorVersion = 1;
43 static final int protocolMinorVersion = 8;
44
45 /**
46 * Starts the remote control server
47 */
48 public static void start() {
49 RemoteControlHttpServer.restartRemoteControlHttpServer();
50 if (supportsHttps()) {
51 RemoteControlHttpsServer.restartRemoteControlHttpsServer();
52 }
53 }
54
55 /**
56 * Stops the remote control server
57 * @since 5861
58 */
59 public static void stop() {
60 RemoteControlHttpServer.stopRemoteControlHttpServer();
61 if (supportsHttps()) {
62 RemoteControlHttpsServer.stopRemoteControlHttpsServer();
63 }
64 }
65
66 /**
67 * Determines if the current JVM support HTTPS remote control.
68 * @return {@code true} if the JVM provides {@code sun.security.x509} classes
69 * @since 12703
70 */
71 public static boolean supportsHttps() {
72 try {
73 return Class.forName("sun.security.x509.GeneralName") != null;
74 } catch (ClassNotFoundException | SecurityException e) {
75 Logging.trace(e);
76 return false;
77 }
78 }
79
80 /**
81 * Adds external request handler.
82 * Can be used by plugins that want to use remote control.
83 *
84 * @param command The command name.
85 * @param handlerClass The additional request handler.
86 */
87 public void addRequestHandler(String command, Class<? extends RequestHandler> handlerClass) {
88 RequestProcessor.addRequestHandlerClass(command, handlerClass);
89 }
90
91 /**
92 * Returns the remote control directory.
93 * @return The remote control directory
94 * @since 7335
95 */
96 public static String getRemoteControlDir() {
97 return new File(Config.getDirs().getUserDataDirectory(true), "remotecontrol").getAbsolutePath();
98 }
99
100 /**
101 * Returns the IPv6 address used for remote control.
102 * @return the IPv6 address used for remote control
103 * @throws UnknownHostException if the local host name could not be resolved into an address.
104 * @since 8337
105 */
106 public static InetAddress getInet6Address() throws UnknownHostException {
107 for (InetAddress a : InetAddress.getAllByName(Config.getPref().get("remote.control.host.ipv6", "::1"))) {
108 if (a instanceof Inet6Address) {
109 return a;
110 }
111 }
112 throw new UnknownHostException();
113 }
114
115 /**
116 * Returns the IPv4 address used for remote control.
117 * @return the IPv4 address used for remote control
118 * @throws UnknownHostException if the local host name could not be resolved into an address.
119 * @since 8337
120 */
121 public static InetAddress getInet4Address() throws UnknownHostException {
122 // Return an address to the loopback interface by default
123 for (InetAddress a : InetAddress.getAllByName(Config.getPref().get("remote.control.host.ipv4", "127.0.0.1"))) {
124 if (a instanceof Inet4Address) {
125 return a;
126 }
127 }
128 throw new UnknownHostException();
129 }
130}
Note: See TracBrowser for help on using the repository browser.