HttpRequestAuthenticationHandler extends HttpRequestHandlerAdapter { protected static String[] getLines(WrappedByteBuffer buf) { List<String> lineList = new ArrayList<String>(); while (buf.hasRemaining()) { byte next = buf.get(); List<Byte> lineText = new ArrayList<Byte>(); while (next != 13) { lineText.add(next); if (buf.hasRemaining()) { next = buf.get(); } else { break; } } if (buf.hasRemaining()) { next = buf.get(); } byte[] lineTextBytes = new byte[lineText.size()]; int i = 0; for (Byte text : lineText) { lineTextBytes[i] = text; i++; } lineList.add(new String(lineTextBytes, UTF_8)); } String[] lines = new String[lineList.size()]; lineList.toArray(lines); return lines; }  static boolean isHTTPResponse(WrappedByteBuffer buf); @Override void processOpen(HttpRequest request); @Override void setNextHandler(HttpRequestHandler handler); @Override void setListener(HttpRequestListener listener);  }
@Test public void testGetLines() { WrappedByteBuffer buf = WrappedByteBuffer.wrap(HTTP_401_RESPONSE_TEXT.getBytes()); String[] lines = HttpRequestAuthenticationHandler.getLines(buf); assertNotNull(lines); assertEquals(7, lines.length); assertEquals("HTTP/1.1 401 Unauthorized", lines[0]); assertEquals(401, Integer.parseInt(lines[0].split(" ")[1])); String wwwAuthenticate = null; for (int i = 1; i < lines.length; i++) { if (lines[i].startsWith("WWW-Authenticate: ")) { wwwAuthenticate = lines[i].substring("WWW-Authenticate: ".length()); break; } } assertEquals("Application Basic realm=\"Kaazing WebSocket Gateway Demo\"", wwwAuthenticate); }