Skip to content

Commit 9c5f3f3

Browse files
committed
[cli-protonj2] Move connection options to shared superclass
1 parent ea0aa79 commit 9c5f3f3

File tree

3 files changed

+62
-67
lines changed

3 files changed

+62
-67
lines changed

cli-protonj2/src/main/java/com/redhat/mqe/CliProtonJ2Receiver.java

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,6 @@ public class CliProtonJ2Receiver extends CliProtonJ2SenderReceiver implements Ca
7575
@CommandLine.Option(names = {"-b", "--broker"}, description = "MD5, SHA-1, SHA-256, ...")
7676
private String broker = "MD5";
7777

78-
@CommandLine.Option(names = {"--conn-username"}, description = "MD5, SHA-1, SHA-256, ...")
79-
private String connUsername = "MD5";
80-
81-
@CommandLine.Option(names = {"--conn-password"}, description = "MD5, SHA-1, SHA-256, ...")
82-
private String connPassword = "MD5";
83-
8478
@CommandLine.Option(names = {"--conn-clientid"})
8579
private String connClientId;
8680

@@ -108,10 +102,6 @@ public class CliProtonJ2Receiver extends CliProtonJ2SenderReceiver implements Ca
108102
@CommandLine.Option(names = {"--timeout"}, description = "MD5, SHA-1, SHA-256, ...")
109103
private int timeout;
110104

111-
@CommandLine.Option(names = {"--conn-auth-mechanisms"}, description = "MD5, SHA-1, SHA-256, ...")
112-
// todo, want to accept comma-separated lists; there is https://picocli.info/#_split_regex
113-
private List<AuthMechanism> connAuthMechanisms = new ArrayList<>();
114-
115105
@CommandLine.Option(names = {"--process-reply-to"})
116106
private boolean processReplyTo = false;
117107

@@ -136,15 +126,9 @@ public class CliProtonJ2Receiver extends CliProtonJ2SenderReceiver implements Ca
136126
@CommandLine.Option(names = {"--msg-content-to-file"})
137127
private String msgContentToFile;
138128

139-
@CommandLine.Option(names = {"--conn-reconnect"})
140-
private String reconnectString = "false";
141-
142129
@CommandLine.Option(names = {"--conn-prefetch"})
143130
private Integer connPrefetch;
144131

145-
@CommandLine.Option(names = {"--conn-heartbeat"})
146-
private Long connHeartbeat;
147-
148132
public CliProtonJ2Receiver() {
149133
this.messageFormatter = new ProtonJ2MessageFormatter();
150134
}
@@ -195,25 +179,10 @@ public Integer call() throws Exception {
195179
client = Client.create();
196180
}
197181

198-
final ConnectionOptions options = new ConnectionOptions();
199-
if (stringToBool(reconnectString)) {
200-
options.reconnectEnabled(true);
201-
}
202-
if (connHeartbeat != null) {
203-
options.idleTimeout(2 * connHeartbeat, TimeUnit.SECONDS);
204-
}
205-
options.user(connUsername);
206-
options.password(connPassword);
207-
for (AuthMechanism mech : connAuthMechanisms) {
208-
options.saslOptions().addAllowedMechanism(mech.name());
209-
}
210182

211-
// TODO: what do I actually need/want here?
212-
// TODO, same problem, lib has Symbols in ClientConstants class
213-
// cli proton cpp does not do this, btw
214-
// options.desiredCapabilities(
215-
// "sole-connection-for-container", "DELAYED_DELIVERY", "SHARED-SUBS", "ANONYMOUS-RELAY"
216-
// );
183+
final ConnectionOptions options = getConnectionOptions();
184+
185+
217186

218187
/*
219188
TODO API usability, hard to ask for queue when dealing with broker that likes to autocreate topics

cli-protonj2/src/main/java/com/redhat/mqe/CliProtonJ2Sender.java

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import java.util.List;
3737
import java.util.Map;
3838
import java.util.concurrent.Callable;
39-
import java.util.concurrent.TimeUnit;
4039

4140
import static com.redhat.mqe.lib.ClientOptionManager.QUEUE_PREFIX;
4241
import static com.redhat.mqe.lib.ClientOptionManager.TOPIC_PREFIX;
@@ -63,12 +62,6 @@ public class CliProtonJ2Sender extends CliProtonJ2SenderReceiver implements Call
6362
@CommandLine.Option(names = {"-b", "--broker"}, description = "")
6463
private String broker = "MD5";
6564

66-
@CommandLine.Option(names = {"--conn-username"}, description = "")
67-
private String connUsername = "MD5";
68-
69-
@CommandLine.Option(names = {"--conn-password"}, description = "")
70-
private String connPassword = "MD5";
71-
7265
@CommandLine.Option(names = {"-a", "--address"}, description = "")
7366
private String address = "MD5";
7467

@@ -81,10 +74,6 @@ public class CliProtonJ2Sender extends CliProtonJ2SenderReceiver implements Call
8174
@CommandLine.Option(names = {"--duration"})
8275
private Float duration = 0.0f;
8376

84-
@CommandLine.Option(names = {"--conn-auth-mechanisms"}, description = "MD5, SHA-1, SHA-256, ...")
85-
// todo, want to accept comma-separated lists; there is https://picocli.info/#_split_regex
86-
private List<AuthMechanism> connAuthMechanisms = new ArrayList<>();
87-
8877
@CommandLine.Option(names = {"--msg-property"}) // picocli Map options works for this, sounds like
8978
private List<String> msgProperties = new ArrayList<>();
9079

@@ -169,12 +158,6 @@ public class CliProtonJ2Sender extends CliProtonJ2SenderReceiver implements Call
169158
@CommandLine.Option(names = {"--duration-mode"})
170159
private DurationModeSender durationMode = DurationModeSender.afterSend;
171160

172-
@CommandLine.Option(names = {"--conn-reconnect"})
173-
private String reconnectString = "false";
174-
175-
@CommandLine.Option(names = {"--conn-heartbeat"})
176-
private Long connHeartbeat;
177-
178161
public CliProtonJ2Sender() {
179162
this.messageFormatter = new ProtonJ2MessageFormatter();
180163
}
@@ -210,21 +193,7 @@ public Integer call() throws Exception { // your business logic goes here...
210193

211194
final Client client = Client.create();
212195

213-
final ConnectionOptions options = new ConnectionOptions();
214-
// TODO typo in javadoc: This option enables or disables reconnection to a remote remote peer after IO errors. To control
215-
// TODO API: unclear if reconnect is on or off by default (public static final boolean DEFAULT_RECONNECT_ENABLED = false;)
216-
if (stringToBool(reconnectString)) {
217-
options.reconnectEnabled(true);
218-
}
219-
if (connHeartbeat != null) {
220-
// TODO finish that 2x investigation for heartbeats and document it somewhere (jira?)
221-
options.idleTimeout(2 * connHeartbeat, TimeUnit.SECONDS);
222-
}
223-
options.user(connUsername);
224-
options.password(connPassword);
225-
for (AuthMechanism mech : connAuthMechanisms) {
226-
options.saslOptions().addAllowedMechanism(mech.name());
227-
}
196+
final ConnectionOptions options = getConnectionOptions();
228197

229198
/*
230199
TODO API usablility, hard to ask for queue when dealing with broker that likes to autocreate topics

cli-protonj2/src/main/java/com/redhat/mqe/Main.java

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
package com.redhat.mqe;
22

3+
import org.apache.qpid.protonj2.client.ConnectionOptions;
4+
import org.jetbrains.annotations.NotNull;
35
import picocli.CommandLine;
46
import picocli.CommandLine.Command;
57

8+
import java.util.ArrayList;
9+
import java.util.List;
610
import java.util.concurrent.Callable;
11+
import java.util.concurrent.TimeUnit;
712

813
@Command(
914
name = "cli-protonj2",
@@ -33,12 +38,64 @@ public static void main(String... args) {
3338
}
3439

3540
class CliProtonJ2SenderReceiver {
36-
41+
@CommandLine.Option(names = {"--conn-username"}, description = "")
42+
private String connUsername = "MD5";
43+
@CommandLine.Option(names = {"--conn-password"}, description = "")
44+
private String connPassword = "MD5";
45+
@CommandLine.Option(names = {"--conn-auth-mechanisms"}, description = "MD5, SHA-1, SHA-256, ...")
46+
// todo, want to accept comma-separated lists; there is https://picocli.info/#_split_regex
47+
private List<AuthMechanism> connAuthMechanisms = new ArrayList<>();
48+
@CommandLine.Option(names = {"--conn-reconnect"})
49+
private String reconnectString = "false";
50+
@CommandLine.Option(names = {"--conn-heartbeat"})
51+
private Long connHeartbeat;
52+
@CommandLine.Option(names = {"--conn-ssl-verify-host"}, arity = "0..1")
53+
private Boolean connSslVerifyHost;
54+
@CommandLine.Option(names = {"--conn-ssl-trust-all"}, arity = "0..1")
55+
private Boolean connSslTrustAll;
3756

3857
protected boolean stringToBool(String string) {
3958
boolean bool = string.equalsIgnoreCase("true") || string.equalsIgnoreCase("yes");
4059
return bool;
4160
}
61+
62+
@NotNull
63+
protected ConnectionOptions getConnectionOptions() {
64+
final ConnectionOptions options = new ConnectionOptions();
65+
// TODO typo in javadoc: This option enables or disables reconnection to a remote remote peer after IO errors. To control
66+
// TODO API: unclear if reconnect is on or off by default (public static final boolean DEFAULT_RECONNECT_ENABLED = false;)
67+
if (stringToBool(reconnectString)) {
68+
options.reconnectEnabled(true);
69+
}
70+
if (connHeartbeat != null) {
71+
// TODO finish that 2x investigation for heartbeats and document it somewhere (jira?)
72+
options.idleTimeout(2 * connHeartbeat, TimeUnit.SECONDS);
73+
}
74+
options.user(connUsername);
75+
options.password(connPassword);
76+
for (AuthMechanism mech : connAuthMechanisms) {
77+
options.saslOptions().addAllowedMechanism(mech.name());
78+
}
79+
if (connSslVerifyHost != null || connSslTrustAll != null) {
80+
options.sslEnabled(true);
81+
}
82+
83+
// TODO: why is there both `options.sslEnabled and options.sslOptions().sslEnabled()`?
84+
if (connSslVerifyHost != null) {
85+
options.sslOptions().verifyHost(connSslVerifyHost);
86+
}
87+
if (connSslTrustAll != null) {
88+
options.sslOptions().trustAll(connSslTrustAll);
89+
}
90+
91+
// TODO: what do I actually need/want here?
92+
// TODO, same problem, lib has Symbols in ClientConstants class
93+
// cli proton cpp does not do this, btw
94+
// options.desiredCapabilities(
95+
// "sole-connection-for-container", "DELAYED_DELIVERY", "SHARED-SUBS", "ANONYMOUS-RELAY"
96+
// );
97+
return options;
98+
}
4299
}
43100

44101
enum AuthMechanism {

0 commit comments

Comments
 (0)