Skip to content

Commit

Permalink
refactoring and clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
gitseti committed Aug 8, 2019
1 parent d705a60 commit 7c07cfa
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 38 deletions.
8 changes: 5 additions & 3 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ mqtt con { @<fileName> |
-p <port>
-i client identifier
-u {<'user name'>}
-pw {$|<'password'>| @<fileName>}
-pw {∅|$|<'password'>| @<fileName>}
-cs {0|1}
-se {0|1}
-wt {$|<'topic'>};
Expand Down Expand Up @@ -113,9 +113,11 @@ mqtt con { @<fileName> |
| -p | --port| The MQTT port. | 1883 will be used
| -i | --identifier| A unique Client identifier can be defined. | A randomly defined UTF-8 String will be generated.
| -u | --user | A User name can be defined. | Not set - user name will be empty.
| -pw | --password | A password can be defined direct or
| -pw | --password | A password can be defined directly, or

$ - via input from console - prompt, or
left blank - via input from console prompt, or

$ - via input from console prompt, or

@<fileName> - read from file
| Not set - password will be empty.
Expand Down
10 changes: 8 additions & 2 deletions assembly.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,18 @@
<outputDirectory>/</outputDirectory>
</file>
<file>
<source>target/README.html</source>
<source>hivemq-cli</source>
<destName>hivemq-cli</destName>
<outputDirectory>/</outputDirectory>
</file>
<file>
<source>hivemq-cli</source>
<destName>hivemq-cli</destName>
<outputDirectory>/brew/</outputDirectory>
</file>
<file>
<source>target/${artifactId}-${version}.jar</source>
<destName>${artifactId}-${version}.jar</destName>
<destName>${artifactId}.jar</destName>
<outputDirectory>/</outputDirectory>
</file>
</files>
Expand Down
3 changes: 3 additions & 0 deletions hivemq-cli
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
# Runs HiveMQ-Cli
java -jar ##PREFIX##/hivemq-cli.jar "$@"
31 changes: 25 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
<modelVersion>4.0.0</modelVersion>

<groupId>com.hivemq</groupId>
<artifactId>mqtt-cli</artifactId>
<version>1.0-SNAPSHOT</version>
<artifactId>hivemq-cli</artifactId>
<version>1.0</version>

<description>HiveMQ MQTT Command line Interface</description>

Expand Down Expand Up @@ -51,9 +51,9 @@
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>5.2.0</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down Expand Up @@ -184,13 +184,32 @@
</execution>
</executions>
</plugin>

<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.3</version>
<configuration>
<descriptor>assembly.xml</descriptor>
</configuration>
<executions>
<execution>
<id>create-archive</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>


<!--plugin>
<groupId>com.oracle.substratevm</groupId>
<artifactId>native-image-maven-plugin</artifactId>
<version>1.0.0-rc14</version>
<configuration>
< configuration is declared in
resources/META-INF/native-image/com.hivemq/mqtt-cli/native-image.properties
resources/META-INF/native-image/com.hivemq/hivemq-cli/native-image.properties
/>
<mainClass>com.hivemq.cli.Mqtt</mainClass>
</configuration>
Expand Down
9 changes: 4 additions & 5 deletions src/main/java/com/hivemq/cli/commands/Connect.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.hivemq.cli.converters.*;
import com.hivemq.cli.impl.ConnectionImpl;
import com.hivemq.cli.impl.MqttAction;
import com.hivemq.client.mqtt.MqttClient;
import com.hivemq.client.mqtt.MqttClientSslConfig;
import com.hivemq.client.mqtt.datatypes.MqttQos;
import com.hivemq.client.mqtt.mqtt5.datatypes.Mqtt5UserProperties;
Expand Down Expand Up @@ -125,9 +126,6 @@ public void run() {
} catch (Exception e) {
Logger.debug(e);
}
} else if (useSsl) { // use default ssl configuration
if (getPort() == DEFAULT_MQTT_PORT) setPort(DEFAULT_MQTT_SSL_PORT);
sslConfig = MqttClientSslConfig.builder().build();
}

ConnectionImpl.get(this).run();
Expand All @@ -137,7 +135,8 @@ private boolean useBuiltSslConfig() {
return !certificates.isEmpty() ||
cipherSuites != null ||
supportedTLSVersions != null ||
clientSideAuthentication != null;
clientSideAuthentication != null ||
useSsl;
}

@Override
Expand All @@ -157,7 +156,7 @@ public String getKey() {

private void buildSslConfig() throws Exception {
// use ssl Port if the user forgot to set it
if (getPort() == DEFAULT_MQTT_PORT) setPort(DEFAULT_MQTT_SSL_PORT);
if (getPort() == MqttClient.DEFAULT_SERVER_PORT) setPort(MqttClient.DEFAULT_SERVER_PORT_SSL);

// build trustManagerFactory for server side authentication and to enable tls
TrustManagerFactory trustManagerFactory = null;
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/com/hivemq/cli/commands/MqttCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
@CommandLine.Command
public abstract class MqttCommand extends AbstractCommand implements CliCommand {

static final int DEFAULT_MQTT_PORT = 1883;
static final int DEFAULT_MQTT_SSL_PORT = 8883;

//TODO Implement complete
@CommandLine.Option(names = {"-v", "--version"}, defaultValue = "5", description = "The mqtt version used by the client.")
private int version;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ public Collection<X509Certificate> convert(final @NotNull String s) throws Excep
throw new FileNotFoundException(DIRECTORY_NOT_FOUND);

if (!directory.isDirectory())
throw new Exception(NOT_A_DIRECTORY);
throw new IllegalArgumentException(NOT_A_DIRECTORY);

final File[] validFiles = directory.listFiles((dir, name) -> CertificateConverterUtils.endsWithValidExtension(name));

if (validFiles == null || validFiles.length == 0)
throw new Exception(NO_CERTIFICATES_FOUND_IN_DIRECTORY);
throw new IllegalArgumentException(NO_CERTIFICATES_FOUND_IN_DIRECTORY);

final Collection<X509Certificate> certificates = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ public X509Certificate convert(final @NotNull String s) throws Exception {
throw new FileNotFoundException(FILE_NOT_FOUND);

if (!keyFile.isFile())
throw new Exception(NOT_A_FILE);
throw new IllegalArgumentException(NOT_A_FILE);

final boolean correctExtension = CertificateConverterUtils.endsWithValidExtension(keyFile.getName());

if (!correctExtension)
throw new Exception(NO_VALID_FILE_EXTENSION);
throw new IllegalArgumentException(NO_VALID_FILE_EXTENSION);

return CertificateConverterUtils.generateX509Certificate(keyFile);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ public PrivateKey convert(final @NotNull String s) throws Exception {
}

private PrivateKey getPrivateKeyFromFile(final @NotNull File keyFile) throws Exception {
// Setup provider
Security.addProvider(new BouncyCastleProvider());

// read the keyfile
final PEMParser pemParser = new PEMParser(new FileReader(keyFile));

Expand All @@ -62,15 +60,15 @@ private PrivateKey getPrivateKeyFromFile(final @NotNull File keyFile) throws Exc
try {
privateKeyInfo = encryptedPrivateKey.decryptPrivateKeyInfo(decryptorProvider);
} catch (PKCSException pkcse) {
throw new Exception(WRONG_PASSWORD);
throw new IllegalArgumentException(WRONG_PASSWORD);
}

privateKey = converter.getPrivateKey(privateKeyInfo);
} else if (object instanceof PEMKeyPair) {
// if key pair is already decrypted
privateKey = converter.getPrivateKey(((PEMKeyPair) object).getPrivateKeyInfo());
} else {
throw new Exception(UNRECOGNIZED_KEY);
throw new IllegalArgumentException(UNRECOGNIZED_KEY);
}

// Convert extracted private key into native java Private key
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ private Mqtt5Publish createWillPublish(final @NotNull Connect connectCommand) th
// only topic is mandatory for will message creation
if (connectCommand.getWillTopic() != null) {
ByteBuffer willpayload = connectCommand.getWillMessage() != null ? connectCommand.getWillMessage() : null;
Mqtt5WillPublishBuilder builder = Mqtt5WillPublish.builder()
Mqtt5WillPublishBuilder.Complete builder = Mqtt5WillPublish.builder()
.topic(connectCommand.getWillTopic())
.payload(willpayload)
.qos(connectCommand.getWillQos())
Expand All @@ -180,14 +180,9 @@ private Mqtt5Publish createWillPublish(final @NotNull Connect connectCommand) th
.responseTopic(connectCommand.getWillResponseTopic())
.correlationData(connectCommand.getWillCorrelationData());
if (connectCommand.getWillUserProperties() != null) { // user Properties can't be completed with null
((Mqtt5WillPublishBuilder.Complete) builder).userProperties(connectCommand.getWillUserProperties());
}
try {
return ((Mqtt5WillPublishBuilder.Complete) builder).build().asWill();
} catch (Exception e) {
Logger.error("Client can't create Will Message, error: {} " + e.getMessage());
throw e;
builder.userProperties(connectCommand.getWillUserProperties());
}
return builder.build().asWill();
} else if (connectCommand.getWillMessage() != null) {
//seems somebody like to create a will message without a topic
Logger.debug("option -wt is missing if a will message is configured - command was: {} ", connectCommand.toString());
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/com/hivemq/cli/utils/PasswordUtils.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.hivemq.cli.utils;

import java.io.BufferedReader;
import java.io.Console;
import java.io.IOException;
import java.io.InputStreamReader;

Expand All @@ -14,13 +15,16 @@ public static char[] readPassword(String... args) throws IOException {
promptMessage = args[0];
}

if (STARTED_IN_IDE) {
Console console = System.console();
if (console != null) {
return console.readPassword("[%s]", promptMessage);
}
else { // Safe password prompt is not possible - maybe called program from IDE?
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.println(promptMessage);
return in.readLine().toCharArray();
} else {
return System.console().readPassword("[%s]", promptMessage);
}

}

}

0 comments on commit 7c07cfa

Please sign in to comment.