System.console() NPE over SSH

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

System.console() NPE over SSH

DBHT
Hi,

I'm getting an odd behaviour over ssh and bin/client. Searching for issue, I've found fixed issues in the past for similar situations.

When I reach part of my code over (bin/client) connected to localhost:
Console cnsl = System.console();
---> System.console() is being NULL.

AND

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String line = reader.readLine();

---> readLine() is reading a truncated input and not correctly echoing characters while typing.

Karaf is 4.2.7
OS: Linux RedHat 7.5 and/or MacOS
Java: 1.8.0_211

Is a better way to get console and read user input while I'm over remote console?

System.out.println() and ShellTable table = new ShellTable() is OK, only user input is broken.

Thanks and Regards,

Davi
Reply | Threaded
Open this post in threaded view
|

Re: System.console() NPE over SSH

jbonofre
Hi,

I guess it's not a new issue right ?

And I also guess it's working fine with the local console.

If you are in the context of a shell command, you should use the Session
instead of directly System.console().
The session is wrapping the local shell or ssh console impl.

Regards
JB

On 22/10/2019 12:35, Davi Baldin Tavares wrote:

> Hi,
>
> I'm getting an odd behaviour over ssh and bin/client. Searching for issue, I've found fixed issues in the past for similar situations.
>
> When I reach part of my code over (bin/client) connected to localhost:
> Console cnsl = System.console();
> ---> System.console() is being NULL.
>
> AND
>
> BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
> String line = reader.readLine();
>
> ---> readLine() is reading a truncated input and not correctly echoing characters while typing.
>
> Karaf is 4.2.7
> OS: Linux RedHat 7.5 and/or MacOS
> Java: 1.8.0_211
>
> Is a better way to get console and read user input while I'm over remote console?
>
> System.out.println() and ShellTable table = new ShellTable() is OK, only user input is broken.
>
> Thanks and Regards,
>
> Davi
>

--
Jean-Baptiste Onofré
[hidden email]
http://blog.nanthrax.net
Talend - http://www.talend.com
Reply | Threaded
Open this post in threaded view
|

Re: System.console() NPE over SSH

DBHT
JB, Thanks.

The working code is bellow.

Regards,

Davi

import org.apache.karaf.shell.api.action.Action;
import org.apache.karaf.shell.api.action.Command;
import org.apache.karaf.shell.api.action.Option;
import org.apache.karaf.shell.api.action.lifecycle.Reference;
import org.apache.karaf.shell.api.action.lifecycle.Service;
import org.apache.karaf.shell.api.console.Session;

import io.infolayer.siteview.keychain.vault.IKeychainServiceManagement;

@Service
@Command(scope = "keychain", name = "add", description = "Add a KeyChain
entry.")
public class KeyChainAddCommand implements Action {

        @Reference
        private IKeychainServiceManagement service;
       
        @Reference
        private Session session;
       
        @Option(name = "-t", aliases = "--type", description = "Entry type: [1 =
Username/Password pair]", required = false, multiValued = false)
    int type = 1;

        @Override
        public Object execute() throws Exception {
               
                if (!service.tryOpen()) {
    System.out.println("Keychain is closed. Open it first.");
    return null;
    }
               
                System.out.print("Adding Entry type " + type);
               
                switch (type) {
                case 1:
                       
                        System.out.println(": Username/Password.");
                       
                        if (session != null) {
                                char[] u = session.readLine("Username:", null).toCharArray(); //
readLine("Username: ").toCharArray();
                                char[] p = session.readLine("Password: ", '*').toCharArray();
                                String description = session.readLine("Description: ", null);
                                String tags = session.readLine("Tags (comma separated): ", null);
                                String label = service.storeEntry(u, p, type, description, tags);
                                System.out.println("Entry label is: " + label);
                        }else {
                                System.out.println("Unable to get console Session.");
                        }
                       
                        break;

                default:
                        System.out.println("Type must be: [1 = Username/Password pair]");
                        break;
                }
               
                return null;
        }

}




--
Sent from: http://karaf.922171.n3.nabble.com/Karaf-User-f930749.html