From 28c42081d4d12447ec8ef482572bb4064b41d877 Mon Sep 17 00:00:00 2001 From: Niki Roo Date: Sat, 4 May 2019 17:25:31 +0200 Subject: [PATCH] server: limit API breakage --- .../server/ConnectActionClientObject.java | 98 +++++++++++++++++++ .../server/ConnectActionClientString.java | 98 +++++++++++++++++++ .../utils/serial/server/ServerObject.java | 59 ++++++++++- .../utils/serial/server/ServerString.java | 61 +++++++++++- .../utils/test_code/SerialServerTest.java | 36 +++---- 5 files changed, 328 insertions(+), 24 deletions(-) diff --git a/src/be/nikiroo/utils/serial/server/ConnectActionClientObject.java b/src/be/nikiroo/utils/serial/server/ConnectActionClientObject.java index 791f850..9385645 100644 --- a/src/be/nikiroo/utils/serial/server/ConnectActionClientObject.java +++ b/src/be/nikiroo/utils/serial/server/ConnectActionClientObject.java @@ -4,6 +4,8 @@ import java.io.IOException; import java.net.Socket; import java.net.UnknownHostException; +import be.nikiroo.utils.Version; + /** * Class used for the client basic handling. *

@@ -26,6 +28,21 @@ public class ConnectActionClientObject extends ConnectActionClient { super(s, key); } + /** + * Create a new {@link ConnectActionClientObject} . + * + * @param s + * the socket to bind to + * @param key + * an optional key to encrypt all the communications (if NULL, + * everything will be sent in clear text) + * @param clientVersion + * the version of the client + */ + public ConnectActionClientObject(Socket s, String key, Version clientVersion) { + super(s, key, clientVersion); + } + /** * Create a new {@link ConnectActionClientObject}. * @@ -50,6 +67,32 @@ public class ConnectActionClientObject extends ConnectActionClient { super(host, port, key); } + /** + * Create a new {@link ConnectActionClientObject}. + * + * @param host + * the host to bind to + * @param port + * the port to bind to + * @param key + * an optional key to encrypt all the communications (if NULL, + * everything will be sent in clear text) + * @param clientVersion + * the version of the client + * + * @throws IOException + * in case of I/O error + * @throws UnknownHostException + * if the IP address of the host could not be determined + * @throws IllegalArgumentException + * if the port parameter is outside the specified range of valid + * port values, which is between 0 and 65535, inclusive + */ + public ConnectActionClientObject(String host, int port, String key, + Version clientVersion) throws IOException { + super(host, port, key, clientVersion); + } + /** * Serialise and send the given object to the server (and return the * deserialised answer). @@ -74,4 +117,59 @@ public class ConnectActionClientObject extends ConnectActionClient { NoSuchMethodException, ClassNotFoundException { return action.sendObject(data); } + + // Deprecated // + + /** + * @deprecated SSL support has been replaced by key-based encryption. + *

+ * Please use the version with key encryption (this deprecated + * version uses an empty key when ssl is TRUE and no + * key (NULL) when ssl is FALSE). + */ + @Deprecated + public ConnectActionClientObject(String host, int port, boolean ssl) + throws IOException { + this(host, port, ssl ? "" : null); + } + + /** + * @deprecated SSL support has been replaced by key-based encryption. + *

+ * Please use the version with key encryption (this deprecated + * version uses an empty key when ssl is TRUE and no + * key (NULL) when ssl is FALSE). + */ + @Deprecated + public ConnectActionClientObject(String host, int port, boolean ssl, + Version version) throws IOException { + this(host, port, ssl ? "" : null, version); + } + + /** + * @deprecated SSL support has been replaced by key-based encryption. + *

+ * Please use the version with key encryption (this deprecated + * version uses an empty key when ssl is TRUE and no + * key (NULL) when ssl is FALSE). + */ + @SuppressWarnings("unused") + @Deprecated + public ConnectActionClientObject(Socket s, boolean ssl) throws IOException { + this(s, ssl ? "" : null); + } + + /** + * @deprecated SSL support has been replaced by key-based encryption. + *

+ * Please use the version with key encryption (this deprecated + * version uses an empty key when ssl is TRUE and no + * key (NULL) when ssl is FALSE). + */ + @SuppressWarnings("unused") + @Deprecated + public ConnectActionClientObject(Socket s, boolean ssl, Version version) + throws IOException { + this(s, ssl ? "" : null, version); + } } \ No newline at end of file diff --git a/src/be/nikiroo/utils/serial/server/ConnectActionClientString.java b/src/be/nikiroo/utils/serial/server/ConnectActionClientString.java index 17da668..3005cee 100644 --- a/src/be/nikiroo/utils/serial/server/ConnectActionClientString.java +++ b/src/be/nikiroo/utils/serial/server/ConnectActionClientString.java @@ -4,6 +4,8 @@ import java.io.IOException; import java.net.Socket; import java.net.UnknownHostException; +import be.nikiroo.utils.Version; + /** * Class used for the client basic handling. *

@@ -26,6 +28,21 @@ public class ConnectActionClientString extends ConnectActionClient { super(s, key); } + /** + * Create a new {@link ConnectActionClientString}. + * + * @param s + * the socket to bind to + * @param key + * an optional key to encrypt all the communications (if NULL, + * everything will be sent in clear text) + * @param clientVersion + * the version of this client + */ + public ConnectActionClientString(Socket s, String key, Version clientVersion) { + super(s, key, clientVersion); + } + /** * Create a new {@link ConnectActionClientString}. * @@ -50,6 +67,32 @@ public class ConnectActionClientString extends ConnectActionClient { super(host, port, key); } + /** + * Create a new {@link ConnectActionClientString}. + * + * @param host + * the host to bind to + * @param port + * the port to bind to + * @param key + * an optional key to encrypt all the communications (if NULL, + * everything will be sent in clear text) + * @param clientVersion + * the version of this client + * + * @throws IOException + * in case of I/O error + * @throws UnknownHostException + * if the IP address of the host could not be determined + * @throws IllegalArgumentException + * if the port parameter is outside the specified range of valid + * port values, which is between 0 and 65535, inclusive + */ + public ConnectActionClientString(String host, int port, String key, + Version clientVersion) throws IOException { + super(host, port, key, clientVersion); + } + /** * Send the given object to the server (and return the answer). * @@ -64,4 +107,59 @@ public class ConnectActionClientString extends ConnectActionClient { public String send(String data) throws IOException { return action.sendString(data); } + + // Deprecated // + + /** + * @deprecated SSL support has been replaced by key-based encryption. + *

+ * Please use the version with key encryption (this deprecated + * version uses an empty key when ssl is TRUE and no + * key (NULL) when ssl is FALSE). + */ + @Deprecated + public ConnectActionClientString(String host, int port, boolean ssl) + throws IOException { + this(host, port, ssl ? "" : null); + } + + /** + * @deprecated SSL support has been replaced by key-based encryption. + *

+ * Please use the version with key encryption (this deprecated + * version uses an empty key when ssl is TRUE and no + * key (NULL) when ssl is FALSE). + */ + @Deprecated + public ConnectActionClientString(String host, int port, boolean ssl, + Version version) throws IOException { + this(host, port, ssl ? "" : null, version); + } + + /** + * @deprecated SSL support has been replaced by key-based encryption. + *

+ * Please use the version with key encryption (this deprecated + * version uses an empty key when ssl is TRUE and no + * key (NULL) when ssl is FALSE). + */ + @SuppressWarnings("unused") + @Deprecated + public ConnectActionClientString(Socket s, boolean ssl) throws IOException { + this(s, ssl ? "" : null); + } + + /** + * @deprecated SSL support has been replaced by key-based encryption. + *

+ * Please use the version with key encryption (this deprecated + * version uses an empty key when ssl is TRUE and no + * key (NULL) when ssl is FALSE). + */ + @SuppressWarnings("unused") + @Deprecated + public ConnectActionClientString(Socket s, boolean ssl, Version version) + throws IOException { + this(s, ssl ? "" : null, version); + } } \ No newline at end of file diff --git a/src/be/nikiroo/utils/serial/server/ServerObject.java b/src/be/nikiroo/utils/serial/server/ServerObject.java index 0941f70..a6a5dd1 100644 --- a/src/be/nikiroo/utils/serial/server/ServerObject.java +++ b/src/be/nikiroo/utils/serial/server/ServerObject.java @@ -74,7 +74,7 @@ abstract public class ServerObject extends Server { for (Object data = rec(); true; data = rec()) { Object rep = null; try { - rep = onRequest(this, data, id); + rep = onRequest(this, clientVersion, data, id); if (isClosing()) { return; } @@ -122,6 +122,59 @@ abstract public class ServerObject extends Server { * @throws Exception * in case of an exception, the error will only be logged */ - abstract protected Object onRequest(ConnectActionServerObject action, - Object data, long id) throws Exception; + protected Object onRequest(ConnectActionServerObject action, + Version clientVersion, Object data, + @SuppressWarnings("unused") long id) throws Exception { + // TODO: change to abstract when deprecated method is removed + // Default implementation for compat + return onRequest(action, clientVersion, data); + } + + // Deprecated // + + /** + * @deprecated SSL support has been replaced by key-based encryption. + *

+ * Please use the version with key encryption (this deprecated + * version uses an empty key when ssl is TRUE and no + * key (NULL) when ssl is FALSE). + */ + @Deprecated + public ServerObject(int port, boolean ssl) throws IOException { + this(port, ssl ? "" : null); + } + + /** + * @deprecated SSL support has been replaced by key-based encryption. + *

+ * Please use the version with key encryption (this deprecated + * version uses an empty key when ssl is TRUE and no + * key (NULL) when ssl is FALSE). + */ + @Deprecated + public ServerObject(String name, int port, boolean ssl) throws IOException { + this(name, port, ssl ? "" : null); + } + + /** + * Will be called if the correct version is not overrided. + * + * @deprecated use the version with the id. + * + * @param action + * the client action + * @param data + * the data sent by the client + * + * @return the answer to return to the client + * + * @throws Exception + * in case of an exception, the error will only be logged + */ + @Deprecated + @SuppressWarnings("unused") + protected Object onRequest(ConnectActionServerObject action, + Version version, Object data) throws Exception { + return null; + } } diff --git a/src/be/nikiroo/utils/serial/server/ServerString.java b/src/be/nikiroo/utils/serial/server/ServerString.java index b321ced..3c982fd 100644 --- a/src/be/nikiroo/utils/serial/server/ServerString.java +++ b/src/be/nikiroo/utils/serial/server/ServerString.java @@ -73,7 +73,7 @@ abstract public class ServerString extends Server { for (String data = rec(); data != null; data = rec()) { String rep = null; try { - rep = onRequest(this, data, id); + rep = onRequest(this, clientVersion, data, id); if (isClosing()) { return; } @@ -112,6 +112,8 @@ abstract public class ServerString extends Server { * * @param action * the client action + * @param clientVersion + * the client version * @param data * the data sent by the client * @param id @@ -123,6 +125,59 @@ abstract public class ServerString extends Server { * @throws Exception * in case of an exception, the error will only be logged */ - abstract protected String onRequest(ConnectActionServerString action, - String data, long id) throws Exception; + protected String onRequest(ConnectActionServerString action, + Version clientVersion, String data, + @SuppressWarnings("unused") long id) throws Exception { + // TODO: change to abstract when deprecated method is removed + // Default implementation for compat + return onRequest(action, clientVersion, data); + } + + // Deprecated // + + /** + * @deprecated SSL support has been replaced by key-based encryption. + *

+ * Please use the version with key encryption (this deprecated + * version uses an empty key when ssl is TRUE and no + * key (NULL) when ssl is FALSE). + */ + @Deprecated + public ServerString(int port, boolean ssl) throws IOException { + this(port, ssl ? "" : null); + } + + /** + * @deprecated SSL support has been replaced by key-based encryption. + *

+ * Please use the version with key encryption (this deprecated + * version uses an empty key when ssl is TRUE and no + * key (NULL) when ssl is FALSE). + */ + @Deprecated + public ServerString(String name, int port, boolean ssl) throws IOException { + this(name, port, ssl ? "" : null); + } + + /** + * Will be called if the correct version is not overrided. + * + * @deprecated use the version with the id. + * + * @param action + * the client action + * @param data + * the data sent by the client + * + * @return the answer to return to the client + * + * @throws Exception + * in case of an exception, the error will only be logged + */ + @Deprecated + @SuppressWarnings("unused") + protected String onRequest(ConnectActionServerString action, + Version version, String data) throws Exception { + return null; + } } diff --git a/src/be/nikiroo/utils/test_code/SerialServerTest.java b/src/be/nikiroo/utils/test_code/SerialServerTest.java index 265e96b..c10a158 100644 --- a/src/be/nikiroo/utils/test_code/SerialServerTest.java +++ b/src/be/nikiroo/utils/test_code/SerialServerTest.java @@ -52,8 +52,8 @@ class SerialServerTest extends TestLauncher { ServerString server = new ServerString(this.getName(), 0, key) { @Override protected String onRequest( - ConnectActionServerString action, String data, - long id) throws Exception { + ConnectActionServerString action, Version version, + String data, long id) throws Exception { return null; } @@ -113,8 +113,8 @@ class SerialServerTest extends TestLauncher { ServerString server = new ServerString(this.getName(), 0, key) { @Override protected String onRequest( - ConnectActionServerString action, String data, - long id) throws Exception { + ConnectActionServerString action, Version version, + String data, long id) throws Exception { sent[0] = data; return "pong"; } @@ -175,8 +175,8 @@ class SerialServerTest extends TestLauncher { ServerString server = new ServerString(this.getName(), 0, key) { @Override protected String onRequest( - ConnectActionServerString action, String data, - long id) throws Exception { + ConnectActionServerString action, Version version, + String data, long id) throws Exception { sent[0] = data; action.send("pong"); sent[1] = action.rec(); @@ -242,8 +242,8 @@ class SerialServerTest extends TestLauncher { ServerString server = new ServerString(this.getName(), 0, key) { @Override protected String onRequest( - ConnectActionServerString action, String data, - long id) throws Exception { + ConnectActionServerString action, Version version, + String data, long id) throws Exception { sent[Integer.parseInt(data)] = data; return "" + (Integer.parseInt(data) * 2); } @@ -315,8 +315,8 @@ class SerialServerTest extends TestLauncher { ServerObject server = new ServerObject(this.getName(), 0, key) { @Override protected Object onRequest( - ConnectActionServerObject action, Object data, - long id) throws Exception { + ConnectActionServerObject action, Version version, + Object data, long id) throws Exception { return null; } @@ -375,8 +375,8 @@ class SerialServerTest extends TestLauncher { ServerObject server = new ServerObject(this.getName(), 0, key) { @Override protected Object onRequest( - ConnectActionServerObject action, Object data, - long id) throws Exception { + ConnectActionServerObject action, Version version, + Object data, long id) throws Exception { sent[0] = data; return "pong"; } @@ -437,8 +437,8 @@ class SerialServerTest extends TestLauncher { ServerObject server = new ServerObject(this.getName(), 0, key) { @Override protected Object onRequest( - ConnectActionServerObject action, Object data, - long id) throws Exception { + ConnectActionServerObject action, Version version, + Object data, long id) throws Exception { sent[0] = data; action.send("pong"); sent[1] = action.rec(); @@ -504,8 +504,8 @@ class SerialServerTest extends TestLauncher { ServerObject server = new ServerObject(this.getName(), 0, key) { @Override protected Object onRequest( - ConnectActionServerObject action, Object data, - long id) throws Exception { + ConnectActionServerObject action, Version version, + Object data, long id) throws Exception { sent[0] = data; return new Object[] { "ACK" }; } @@ -576,8 +576,8 @@ class SerialServerTest extends TestLauncher { ServerObject server = new ServerObject(this.getName(), 0, key) { @Override protected Object onRequest( - ConnectActionServerObject action, Object data, - long id) throws Exception { + ConnectActionServerObject action, Version version, + Object data, long id) throws Exception { sent[(Integer) data] = data; return ((Integer) data) * 2; } -- 2.27.0