Skip to content

Commit

Permalink
[WFCORE-3187] add-user.sh tool prints unexpected characters if arrow …
Browse files Browse the repository at this point in the history
…keys are pushed during interactive mode
  • Loading branch information
MMarus committed Aug 18, 2017
1 parent a53dd75 commit 4d35dc8
Show file tree
Hide file tree
Showing 7 changed files with 240 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
</resources>

<dependencies>
<module name="org.aesh"/>
<module name="org.jboss.as.domain-management"/>
<module name="org.jboss.logmanager" services="import"/>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
</resources>

<dependencies>
<module name="org.aesh"/>
<module name="org.wildfly.openssl" />
<module name="org.wildfly.common" />
<module name="org.wildfly.security.elytron-private" />
Expand All @@ -51,6 +52,6 @@
<module name="org.picketbox" optional="true"/>
<module name="org.wildfly.transaction.client" optional="true" />
<module name="javax.api"/>
<module name="sun.jdk" export="true" />
<module name="sun.jdk" export="true" />
</dependencies>
</module>
6 changes: 5 additions & 1 deletion domain-management/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@

<additionalClasspathElements>
<additionalClasspathElement>${project.basedir}/../src/test/resources</additionalClasspathElement>
</additionalClasspathElements>
</additionalClasspathElements>
</configuration>
</plugin>
</plugins>
Expand All @@ -71,6 +71,10 @@
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.aesh</groupId>
<artifactId>aesh-readline</artifactId>
</dependency>
<dependency>
<groupId>org.apache.directory.api</groupId>
<artifactId>api-ldap-codec-standalone</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@

package org.jboss.as.domain.management.security.adduser;

import static org.jboss.as.domain.management.logging.DomainManagementLogger.ROOT_LOGGER;
import org.aesh.readline.Prompt;
import org.aesh.readline.tty.terminal.TerminalConnection;

import java.io.Console;
import java.io.IOError;
import java.io.IOException;
import java.util.IllegalFormatException;

/**
Expand Down Expand Up @@ -59,26 +61,34 @@ public void printf(String format, Object... args) throws IllegalFormatException

@Override
public String readLine(String fmt, Object... args) throws IOError {
if (hasConsole()) {
return theConsole.readLine(fmt, args);
} else {
throw ROOT_LOGGER.noConsoleAvailable();
ReadLineHandler readLineHandler = new ReadLineHandler(new Prompt(String.format(fmt, args)));
return readInputLine(readLineHandler);
}

private String readInputLine(ReadLineHandler readLineHandler) {
try {
createTerminalConnection(readLineHandler);
} catch (IOException e) {
e.printStackTrace();
}

return readLineHandler.getLine();
}

protected void createTerminalConnection(ReadLineHandler readLineHandler) throws IOException {
//Side effect - this starts the new connection to terminal and asks user for input.
//This call will block this thread while it reads a line from user.
new TerminalConnection(readLineHandler);
}

@Override
public char[] readPassword(String fmt, Object... args) throws IllegalFormatException, IOError {
if (hasConsole()) {
return theConsole.readPassword(fmt, args);
} else {
throw ROOT_LOGGER.noConsoleAvailable();
}
ReadLineHandler readLineHandler = new ReadLineHandler(new Prompt(String.format(fmt, args), Character.MIN_VALUE));
return readInputLine(readLineHandler).toCharArray();
}

@Override
public boolean hasConsole() {
return theConsole != null;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2017, Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/

package org.jboss.as.domain.management.security.adduser;

import java.util.function.Consumer;

import org.aesh.readline.Prompt;
import org.aesh.readline.Readline;
import org.aesh.readline.ReadlineBuilder;
import org.aesh.terminal.Connection;
import org.aesh.terminal.tty.Signal;

/**
* Class to read line from terminal using Aesh-readline library.
* Created by Marek Marusic <mmarusic@redhat.com> on 8/7/17.
*/
public class ReadLineHandler implements Consumer<Connection> {
private String line;
private Prompt prompt;

public ReadLineHandler(Prompt prompt) {
this.prompt = prompt;
}

public String getLine() {
return line;
}

@Override
public void accept(Connection connection) {
read(connection, ReadlineBuilder.builder().enableHistory(false).build(), prompt);
// lets open the connection to the terminal using this thread
connection.openBlocking();
}

protected void read(Connection connection, Readline readline, Prompt prompt) {
// Set the Ctrl-C handler
connection.setSignalHandler(signal -> {
if (signal == Signal.INT)
connection.close();
});

readline.readline(connection, prompt, line -> {
// we specify a simple lambda consumer to read the input thats saved into line.
if (line != null) {
this.line = line;
connection.close();
} else
read(connection, readline, prompt);
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2017, Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/

package org.jboss.as.domain.management.security.adduser;

import org.aesh.readline.terminal.Key;
import org.aesh.readline.tty.terminal.TerminalConnection;
import org.junit.Test;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.nio.charset.Charset;

import static org.junit.Assert.*;

/**
* Created by Marek Marusic <mmarusic@redhat.com> on 8/9/17.
*/
public class AeshJavaConsoleTest {

@Test
public void testLeftArrow() throws IOException, InterruptedException {
AeshJavaConsoleTestResource aeshJavaConsoleTestResource = new AeshJavaConsoleTestResource();
aeshJavaConsoleTestResource.start();
Thread.sleep(100);

aeshJavaConsoleTestResource.write("Foo");
aeshJavaConsoleTestResource.write(Key.LEFT);
aeshJavaConsoleTestResource.write("F");
aeshJavaConsoleTestResource.write(Key.ENTER);
aeshJavaConsoleTestResource.flushWrites();

Thread.sleep(100);
assertEquals("FoFo", aeshJavaConsoleTestResource.getLine());
}

@Test
public void testUpArrow() throws IOException, InterruptedException {
AeshJavaConsoleTestResource aeshJavaConsoleTestResource = new AeshJavaConsoleTestResource();
aeshJavaConsoleTestResource.start();
Thread.sleep(100);

aeshJavaConsoleTestResource.write("Foo");
aeshJavaConsoleTestResource.write(Key.UP);
aeshJavaConsoleTestResource.write("F");
aeshJavaConsoleTestResource.write(Key.ENTER);
aeshJavaConsoleTestResource.flushWrites();

Thread.sleep(100);
assertEquals("FooF", aeshJavaConsoleTestResource.getLine());
}

@Test
public void testInterruptionSignal() throws IOException, InterruptedException {
AeshJavaConsoleTestResource aeshJavaConsoleTestResource = new AeshJavaConsoleTestResource();
aeshJavaConsoleTestResource.start();
Thread.sleep(100);

aeshJavaConsoleTestResource.write("Foo".getBytes());
aeshJavaConsoleTestResource.write(Key.CTRL_C.getKeyValuesAsString().getBytes());
aeshJavaConsoleTestResource.flushWrites();

Thread.sleep(100);
assertEquals(false, aeshJavaConsoleTestResource.isAlive());
assertEquals(Thread.State.TERMINATED, aeshJavaConsoleTestResource.getState());
}

public class AeshJavaConsoleTestResource extends Thread {
private PipedOutputStream pipedOutputStream;
private PipedInputStream pipedInputStream;
private ByteArrayOutputStream out;
private String line;

public AeshJavaConsoleTestResource() throws IOException {
pipedOutputStream = new PipedOutputStream();
pipedInputStream = new PipedInputStream(pipedOutputStream);
out = new ByteArrayOutputStream();
}

public String getLine() {
return line;
}

public void flushWrites() throws IOException {
pipedOutputStream.flush();
}

public void write(String data) throws IOException {
write(data.getBytes());
}

public void write(byte[] data) throws IOException {
pipedOutputStream.write(data);
}

public void write(Key key) throws IOException {
write(key.getKeyValuesAsString().getBytes());
}

public void run() {
// Create JavaConsole with custom input, output streams for testing
JavaConsole javaConsole = new JavaConsole() {
@Override
protected void createTerminalConnection(ReadLineHandler readLineHandler) throws IOException {
new TerminalConnection(Charset.defaultCharset(), pipedInputStream, out, readLineHandler);
}
};

line = javaConsole.readLine("");
}
}

}
5 changes: 4 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,10 @@
<version.org.codehaus.woodstox.woodstox-core>5.0.3</version.org.codehaus.woodstox.woodstox-core>
<version.org.fusesource.jansi>1.16</version.org.fusesource.jansi>
<version.org.glassfish.javax.json>1.0.4</version.org.glassfish.javax.json>
<version.org.aesh-readline>1.0</version.org.aesh-readline>
<!--This has to be changed to 1.x when at least 1.1 version is released, since there are few bugs in 1.0,
which would cause failures of the tests. However for now there is no 1.1 released,
there is just snapshot available in the jboss nexus repository-->
<version.org.aesh-readline>1.1-SNAPSHOT</version.org.aesh-readline>
<version.org.jboss.byteman>3.0.6</version.org.jboss.byteman>
<version.org.jboss.classfilewriter>1.2.1.Final</version.org.jboss.classfilewriter>
<version.org.jboss.invocation>1.5.0.CR1</version.org.jboss.invocation>
Expand Down

0 comments on commit 4d35dc8

Please sign in to comment.