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

Last change on this file since 12846 was 12846, checked in by bastiK, 7 years ago

see #15229 - use Config.getPref() wherever possible

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