This is an automated email from the ASF dual-hosted git repository. tabish pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/qpid-protonj2.git
The following commit(s) were added to refs/heads/main by this push: new e65540ba PROTON-2683 Provide some APIs in SASL layer that accept strings e65540ba is described below commit e65540ba3bb392c5b80d809fb7627e4f9be6e216 Author: Timothy Bish <tabish...@gmail.com> AuthorDate: Wed Feb 15 14:26:38 2023 -0500 PROTON-2683 Provide some APIs in SASL layer that accept strings Convert string to symbol for users to avoid a lot of boilerplate conversion code. --- .../protonj2/engine/sasl/SaslClientContext.java | 19 ++++++++++++++++++ .../protonj2/engine/sasl/SaslServerContext.java | 16 +++++++++++++++ .../org/apache/qpid/protonj2/types/Symbol.java | 15 ++++++++++++++ .../org/apache/qpid/protonj2/types/SymbolTest.java | 23 ++++++++++++++++++++++ 4 files changed, 73 insertions(+) diff --git a/protonj2/src/main/java/org/apache/qpid/protonj2/engine/sasl/SaslClientContext.java b/protonj2/src/main/java/org/apache/qpid/protonj2/engine/sasl/SaslClientContext.java index e889089c..c23c031b 100644 --- a/protonj2/src/main/java/org/apache/qpid/protonj2/engine/sasl/SaslClientContext.java +++ b/protonj2/src/main/java/org/apache/qpid/protonj2/engine/sasl/SaslClientContext.java @@ -80,6 +80,25 @@ public interface SaslClientContext extends SaslContext { */ SaslClientContext sendChosenMechanism(Symbol mechanism, String host, ProtonBuffer initialResponse) throws EngineStateException; + /** + * Sends a response to the SASL server indicating the chosen mechanism for this + * client and the host name that this client is identifying itself as. + * + * @param mechanism + * The chosen mechanism selected from the list the server provided. + * @param host + * The host name that the client is identified as or null if none selected. + * @param initialResponse + * The initial response data sent as defined by the chosen mechanism or null if none required. + * + * @return this client context. + * + * @throws EngineStateException if the {@link Engine} has been shut down or a failure occurs processing this mechanism. + */ + default SaslClientContext sendChosenMechanism(String mechanism, String host, ProtonBuffer initialResponse) throws EngineStateException { + return sendChosenMechanism(Symbol.getSymbol(mechanism), host, initialResponse); + } + /** * Sends a response to a server side challenge that comprises the challenge / response * exchange for the chosen SASL mechanism. diff --git a/protonj2/src/main/java/org/apache/qpid/protonj2/engine/sasl/SaslServerContext.java b/protonj2/src/main/java/org/apache/qpid/protonj2/engine/sasl/SaslServerContext.java index 59d0dc8a..d8792fa4 100644 --- a/protonj2/src/main/java/org/apache/qpid/protonj2/engine/sasl/SaslServerContext.java +++ b/protonj2/src/main/java/org/apache/qpid/protonj2/engine/sasl/SaslServerContext.java @@ -64,6 +64,22 @@ public interface SaslServerContext extends SaslContext { */ SaslServerContext sendMechanisms(Symbol[] mechanisms) throws EngineStateException; + /** + * Sends the set of supported mechanisms to the SASL client from which it must + * choose and return one mechanism which will then be the basis for the SASL + * authentication negotiation. + * + * @param mechanisms + * The mechanisms that this server supports. + * + * @return this server context. + * + * @throws EngineStateException if the engine has already shutdown or failed while processing the mechanisms. + */ + default SaslServerContext sendMechanisms(String[] mechanisms) throws EngineStateException { + return sendMechanisms(Symbol.getSymbols(mechanisms)); + } + /** * Sends the SASL challenge defined by the SASL mechanism that is in use during * this SASL negotiation. The challenge is an opaque binary that is provided to diff --git a/protonj2/src/main/java/org/apache/qpid/protonj2/types/Symbol.java b/protonj2/src/main/java/org/apache/qpid/protonj2/types/Symbol.java index d7a4149b..f57b79fe 100644 --- a/protonj2/src/main/java/org/apache/qpid/protonj2/types/Symbol.java +++ b/protonj2/src/main/java/org/apache/qpid/protonj2/types/Symbol.java @@ -23,6 +23,7 @@ import java.util.concurrent.ConcurrentHashMap; import org.apache.qpid.protonj2.buffer.ProtonBuffer; import org.apache.qpid.protonj2.buffer.ProtonBufferAllocator; +import org.apache.qpid.protonj2.engine.util.StringUtils; /** * Class that represents an AMQP Symbol value. The creation of a Symbol object @@ -210,4 +211,18 @@ public final class Symbol implements Comparable<Symbol> { return symbol; } + + /** + * Look up a set of {@link Symbol} instances that matches the given {@link String} + * array names of the {@link Symbol} values and return them as a new {@link Symbol} + * array. + * + * @param stringValues + * The {@link String} array version of the {@link Symbol} values. + * + * @return a {@link Symbol} array that matches the given {@link String} array values. + */ + public static Symbol[] getSymbols(String[] stringValues) { + return StringUtils.toSymbolArray(stringValues); + } } diff --git a/protonj2/src/test/java/org/apache/qpid/protonj2/types/SymbolTest.java b/protonj2/src/test/java/org/apache/qpid/protonj2/types/SymbolTest.java index cca1a1ae..911017ff 100644 --- a/protonj2/src/test/java/org/apache/qpid/protonj2/types/SymbolTest.java +++ b/protonj2/src/test/java/org/apache/qpid/protonj2/types/SymbolTest.java @@ -16,6 +16,7 @@ */ package org.apache.qpid.protonj2.types; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -219,4 +220,26 @@ public class SymbolTest { assertNotSame(symbol1, symbol2); assertNotSame(symbol1.toString(), symbol2.toString()); } + + @Test + public void testGetSymbols() { + String[] symbolStrings = new String[] { "one", "two", "three" }; + + Symbol[] symbols1 = Symbol.getSymbols(symbolStrings); + Symbol[] symbols2 = Symbol.getSymbols(symbolStrings); + + assertEquals(symbolStrings.length, symbols1.length); + assertEquals(symbolStrings.length, symbols2.length); + assertArrayEquals(symbols1, symbols2); + + for (int i = 0; i < symbolStrings.length; ++i) { + assertSame(symbols1[i], symbols2[i]); + } + } + + @Test + public void testGetSymbolsWithNullOrEmptyArg() { + assertNull(Symbol.getSymbols(null)); + assertNotNull(Symbol.getSymbols(new String[0])); + } } --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@qpid.apache.org For additional commands, e-mail: commits-h...@qpid.apache.org