Mark Rowlands
Mark Rowlands

Reputation: 5453

Apache TelnetClient, InputStream unable to convert to String using Apache IOUtils

I'm having some trouble with the InputStream from a telnet session. I have made the connection, I can successfully send commands to via telnet and, using System.out.println, I can see that I am receiving the correct response.

I am needed to return the responses from the session, as a string, as I need to perform regex on it to verify a value for a test.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
import java.net.SocketException;

import org.apache.commons.io.IOUtils;
import org.apache.commons.net.telnet.TelnetClient;

public class TelnetService {

private final String server;
private final int port;
private TelnetClient telnet = new TelnetClient();
private InputStream dataIn;
private PrintStream dataOut;
private String prompt = "~$";

public TelnetService(String server, int port) {
    this.server = server.replace("http://", "");
    this.port = port;
}

    private void startTelnetSession() throws SocketException, IOException {
    telnet.connect(server, port);
    dataIn = telnet.getInputStream();
    dataOut = new PrintStream(telnet.getOutputStream());
}

public String getTelnetSessionAsString() {
    try {
        startTelnetSession();
        loginUser();
        readUntil(prompt);
        write("this_command");
        readUntil(prompt);
        StringWriter writer = new StringWriter();
        IOUtils.copy(dataIn, writer, "UTF-8");
        dataOut.flush();
        dataIn.close();
        dataOut.close();
        String received = writer.toString();
        telnet.disconnect();
        return received;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

private void loginUser() {
    readUntil("login:");
    write("foo");
    readUntil("Password:");
    write("bar");
}



private void write(String value) {
    try {
        dataOut.println(value);
        dataOut.flush();
        System.out.println(value);
    } catch (Exception e) {
        e.printStackTrace();
    }

}

private String readUntil(String pattern) {
    try {
        char lastChar = pattern.charAt(pattern.length() - 1);
        StringBuffer buffer = new StringBuffer();
        char ch = (char) dataIn.read();
        while (true) {
            System.out.print(ch);
            buffer.append(ch);
            if (ch == lastChar) {
                if (buffer.toString().endsWith(pattern)) {
                    return buffer.toString();
                }
            }
            ch = (char) dataIn.read();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
}

As it is, whenever the test runs that calls this class, it appears to 'hang' during the

IOUtils.copy(dataIn, writer, "UTF-8");

I'm sure there is something really simple that I'm missing. Is anyone out there able to help? It would be much appreciated!

Upvotes: 1

Views: 1274

Answers (1)

Mark Rowlands
Mark Rowlands

Reputation: 5453

As it happens I was able to solve this issue - not exactly how I wished to but it was suitable enough for a simple test function.

I wrote the telnet inputStream into a file rather than System.out.print'ed it. This allowed me to then read back from the file as a String.

I'd still liked to have solved the issue - I guess it was a threading issue and I'm still a never beginner so haven't tackled anything like that.

Upvotes: 1

Related Questions