15

The code I'm using now:

    Pooled<ByteBuffer> pooledByteBuffer = exchange.getConnection().getBufferPool().allocate();
    ByteBuffer byteBuffer = pooledByteBuffer.getResource();

    int limit = byteBuffer.limit();

    byteBuffer.clear();

    exchange.getRequestChannel().read(byteBuffer);
    int pos = byteBuffer.position();
    byteBuffer.rewind();
    byte[] bytes = new byte[pos];
    byteBuffer.get(bytes);

    String requestBody = new String(bytes, Charset.forName("UTF-8") );

    byteBuffer.clear();
    pooledByteBuffer.free();

It seems to work OK but I'm not sure about the need to clear() ByteBuffer before returning it to the pool. I'm not even sure about using exchange.getConnection().getBufferPool().allocate();. There is not much about it in the documentation.

atok
  • 5,880
  • 3
  • 33
  • 62

4 Answers4

8

A simpler way to read the request body is to dispatch to a worker thread, which makes HttpExchange#getInputStream() available.

There are two ways of doing this: using a BlockingHandler or the dispatch pattern shown in the documentation. Here's an example of using the BlockingHandler:

new BlockingHandler(myHandler)

The BlockingHandler basically does the dispatch for you.

Community
  • 1
  • 1
Mwanji Ezana
  • 924
  • 8
  • 15
5

To do it in non-blocking way please see io.undertow.io.Receiver interface. The example of handler could be:

pathHandler.put("/test", new HttpHandler() {
    @Override
    public void handleRequest(HttpServerExchange exchange) throws Exception {
        exchange.getRequestReceiver().receiveFullBytes(new FullBytesCallback() {
            @Override
            public void handle(HttpServerExchange exchange, byte[] message) {
                System.out.println(new String(message));
            }                    
        });

        exchange.getResponseSender().send("Hello World");
    }
});

Or for Java 8:

pathHandler.put("/test", (ex) -> {
    ex.getRequestReceiver().receiveFullBytes((e, m) -> {
        System.out.println(new String(m));
    });
    ex.getResponseSender().send("Hello World");
});
Mike Shauneu
  • 3,201
  • 19
  • 21
2

@atok, I use your method for a while, but sometimes I get an empty body when the stream is closed before the read call. This works like charm:

BufferedReader reader = null;
StringBuilder builder = new StringBuilder( );

try {
    exchange.startBlocking( );
    reader = new BufferedReader( new InputStreamReader( exchange.getInputStream( ) ) );

    String line;
    while( ( line = reader.readLine( ) ) != null ) {
        builder.append( line );
    }
} catch( IOException e ) {
    e.printStackTrace( );
} finally {
    if( reader != null ) {
        try {
            reader.close( );
        } catch( IOException e ) {
            e.printStackTrace( );
        }
    }
}

String body = builder.toString( );
eheck
  • 325
  • 2
  • 7
1
import io.undertow.Handlers;
import io.undertow.Undertow;
import io.undertow.server.HttpHandler;
import io.undertow.server.HttpServerExchange;
import io.undertow.server.handlers.BlockingHandler;


public static void main(String[] args) {

    Undertow server = Undertow.builder()
            .addHttpListener(8087, "xx.xx.xx.xx")
            .setHandler(Handlers.pathTemplate().add("/webhook", new BlockingHandler(new ItemHandler())))
            .build();
    server.start();
}

static class ItemHandler implements HttpHandler {


    @Override
    public void handleRequest(HttpServerExchange exchange) throws Exception {
       exchange.getInputStream());
    }
}
Rob Austin
  • 620
  • 3
  • 5