Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #38952

readLine() and newline problem

X-Received by 2002:a0c:d44a:: with SMTP id r10mr7482794qvh.131.1557469015658; Thu, 09 May 2019 23:16:55 -0700 (PDT)
X-Received by 2002:a81:63c3:: with SMTP id x186mr4560649ywb.248.1557469015384; Thu, 09 May 2019 23:16:55 -0700 (PDT)
Path csiph.com!weretis.net!feeder6.news.weretis.net!feeder.usenetexpress.com!feeder-in1.iad1.usenetexpress.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!t9no279853qtn.0!news-out.google.com!b26ni67qtp.1!nntp.google.com!t9no279851qtn.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail
Newsgroups comp.lang.java.programmer
Date Thu, 9 May 2019 23:16:55 -0700 (PDT)
Complaints-To groups-abuse@google.com
Injection-Info glegroupsg2000goo.googlegroups.com; posting-host=192.176.1.83; posting-account=1c_fOgoAAADuOXlL0A4-T9PUmVHtMSYd
NNTP-Posting-Host 192.176.1.83
User-Agent G2/1.0
MIME-Version 1.0
Message-ID <8beb1189-8953-4072-9b4b-0d54cf64eaa5@googlegroups.com> (permalink)
Subject readLine() and newline problem
From mike <mikaelpetterson@hotmail.com>
Injection-Date Fri, 10 May 2019 06:16:55 +0000
Content-Type text/plain; charset="UTF-8"
Content-Transfer-Encoding quoted-printable
Lines 142
Xref csiph.com comp.lang.java.programmer:38952

Show key headers only | View raw


Hi,

We have the following code for reading a NETCONF "hello" message from a NETCONF Device.

public class MessageHandler  {

        public StringBuilder readLines(InputStream is) {
           final StringBuilder buffer = new StringBuilder();
           final String delimiter = "]]>]]>";
           String tmp;
           
           try (BufferedReader reader = new BufferedReader(new InputStreamReader(is))) {
               do {

                   tmp = reader.readLine();

                   if (tmp != null) {
                       buffer.append(tmp);
                   }
               } while (tmp != null && !tmp.endsWith(delimiter));

           } catch (IOException e) {
               LOGGER.info("Failed to read <rpc-reply> message ",
                       e);
           }
           
           return buffer;
           
       }
        
       


}

We get the following string from the device ( just an example):

<?xml version="1.0" encoding="UTF-8"?>
    <hello xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
      <capabilities>
        <capability>
          urn:ietf:params:netconf:base:1.1
        </capability>
        <capability>
          urn:ietf:params:ns:netconf:capability:startup:1.0
        </capability>
      </capabilities>
      <session-id>4</session-id>
    </hello>
    ]]>]]>

The problem here is that some devices add a newline after "]]>]]>" which is the message separator. And of course some devices don't since it is not required in the RFC6242.

So when there is no newline from device then the above method will hang. Then I thought it would be easy to create a unit test that recreates the problem. One with a hello message with message separator and newline and one with only message separator. But problem is that I get no exception in the test without the newline. Any ideas what I am missing or how I can re-create the issue?

br,

//mike

public class MessageHandlerTest {

    private MessageHandler messageHandler;

    @BeforeClass
    public void setup() {
        messageHandler = new MessageHandler();
    }

    @Test
    public void testHelloServerWithOutNewLine() throws Exception {
        InputStream is = getHelloWithoutNewline();
        StringBuilder actual = messageHandler.readLines(is);
        Assert.assertTrue(actual.length() > 0);

    }

    @Test
    public void testHelloServerWithNewline() throws Exception {
        InputStream is = getHelloWithNewline();
        StringBuilder actual = messageHandler.readLines(is);
        Assert.assertTrue(actual.length() > 0);

    }

    
    private InputStream getHelloWithoutNewline() {
        final String HELLO_REPLY = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + 
                "   <hello xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">" + 
                "      <capabilities>" + 
                "        <capability>" + 
                "          urn:ietf:params:netconf:base:1.1" + 
                "        </capability>" + 
                "        <capability>" + 
                "          urn:ietf:params:ns:netconf:capability:startup:1.0" + 
                "        </capability>" + 
                "      </capabilities>" + 
                "      <session-id>4</session-id>" + 
                "    </hello>" + 
                "    ]]>]]>";
        
        return new ByteArrayInputStream(HELLO_REPLY.getBytes());

    }

    private InputStream getHelloWithNewline() {
        final String HELLO_REPLY = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + 
                "   <hello xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" + 
                "      <capabilities>\n" + 
                "        <capability>\n" + 
                "          urn:ietf:params:netconf:base:1.1\n" + 
                "        </capability>\n" + 
                "        <capability>\n" + 
                "          urn:ietf:params:ns:netconf:capability:startup:1.0\n" + 
                "        </capability>\n" + 
                "      </capabilities>\n" + 
                "      <session-id>4</session-id>\n" + 
                "    </hello>\n" + 
                "    ]]>]]>\n";
        
        return new ByteArrayInputStream(HELLO_REPLY.getBytes());

    }

}



Back to comp.lang.java.programmer | Previous | NextNext in thread | Find similar | Unroll thread


Thread

readLine() and newline problem mike <mikaelpetterson@hotmail.com> - 2019-05-09 23:16 -0700
  Re: readLine() and newline problem Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2019-05-10 09:02 +0200
    Re: readLine() and newline problem mike <mikaelpetterson@hotmail.com> - 2019-05-10 01:29 -0700
      Re: readLine() and newline problem Martin Gregorie <martin@mydomain.invalid> - 2019-05-10 10:22 +0000
      Re: readLine() and newline problem Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2019-05-10 18:50 +0200

csiph-web