Example usage for org.apache.http.io SessionInputBuffer readLine

List of usage examples for org.apache.http.io SessionInputBuffer readLine

Introduction

In this page you can find the example usage for org.apache.http.io SessionInputBuffer readLine.

Prototype

String readLine() throws IOException;

Source Link

Usage

From source file:org.saintandreas.serket.ssdp.Message.java

public static Message parseMessage(DatagramPacket packet) throws IOException, HttpException {
    Type type;// w ww  .  j  a  va2s. c o m
    Map<String, Header> headers = new HashMap<String, Header>();
    String usn = null;

    LineParser parser = new BasicLineParser();
    SessionInputBuffer inBuffer = new ByteArrayInputBuffer(packet.getData());
    String command = inBuffer.readLine();
    Header[] hs = AbstractMessageParser.parseHeaders(inBuffer, 64, 1024, parser);
    for (Header h : hs) {
        headers.put(h.getName(), h);
    }

    if (Message.NOTIFY_START.startsWith(command)) {
        Header h = headers.get("NTS");
        usn = headers.get("USN").getValue();
        if ("ssdp:alive".equals(h.getValue())) {
            // LogFactory.getLog(SSDPTests.class).debug("Notify message alive " + usn);
            type = Type.NOTIFY_ALIVE;
        } else if ("ssdp:update".equals(h.getValue())) {
            // LogFactory.getLog(SSDPTests.class).debug("Notify message update " + usn);
            type = Type.NOTIFY_UPDATE;
        } else if ("ssdp:byebye".equals(h.getValue())) {
            // LogFactory.getLog(SSDPTests.class).debug("Notify message byebye " + usn);
            type = Type.NOTIFY_BYEBYE;
        } else
            throw new RuntimeException("unknown type");
    } else if (Message.SEARCH_START.startsWith(command)) {
        usn = headers.get("ST").getValue();
        // LogFactory.getLog(SSDPTests.class).debug("Search message " + usn);
        type = Type.SEARCH;
    } else if (Message.HTTP_OK_START.startsWith(command)) {
        // LogFactory.getLog(SSDPTests.class).debug("Response message");
        type = Type.RESPONSE;
    } else
        throw new RuntimeException("unknown type");
    return new Message(type, headers, usn, packet,
            new String(packet.getData(), 0, packet.getLength(), Charsets.US_ASCII));
}