Package io.muserver

Interface RequestBodyListener


  • public interface RequestBodyListener

    Callbacks for reading request body data asynchronously.

    Example usage:

    
      server = httpsServer()
          .addHandler((request, response) -> {
              AsyncHandle ctx = request.handleAsync();
              ctx.setReadListener(new RequestBodyListener() {
                  public void onDataReceived(ByteBuffer bb, DoneCallback doneCallback) {
                      byte[] b = new byte[bb.remaining()];
                      bb.get(b);
                      try {
                          response.outputStream().write(b);
                          doneCallback.onComplete(null);
                      } catch (IOException e) {
                          doneCallback.onComplete(e);
                      }
                  }
    
                  public void onComplete() {
                      ctx.complete();
                  }
    
                  public void onError(Throwable t) {
                      errors.add(t);
                  }
              });
    
              return true;
          })
          .start();
     
    • Method Summary

      All Methods Instance Methods Abstract Methods 
      Modifier and Type Method Description
      void onComplete()
      Called when the request body is fully received.
      void onDataReceived​(java.nio.ByteBuffer buffer, DoneCallback doneCallback)
      Called when request data is received from the client.
      void onError​(java.lang.Throwable t)
      Called if there is an error reading the body.
    • Method Detail

      • onDataReceived

        void onDataReceived​(java.nio.ByteBuffer buffer,
                            DoneCallback doneCallback)
                     throws java.lang.Exception

        Called when request data is received from the client.

        NOTE: this method should not block as it runs on a socket acceptor thread. If you need to do any blocking operations it is recommended you process the data on another thread.

        Parameters:
        buffer - A buffer holding some of the request body data
        doneCallback - This must be called when the buffer is no longer needed
        Throws:
        java.lang.Exception - Any thrown exceptions will cause the onError(Throwable) method to be called with the thrown exception as a parameter.
      • onComplete

        void onComplete()
        Called when the request body is fully received.
      • onError

        void onError​(java.lang.Throwable t)
        Called if there is an error reading the body.
        Parameters:
        t - The error.