users@grizzly.java.net

Re: Fwd: grizzly2 -httpserver- parse HTML request

From: Oleksiy Stashok <oleksiy.stashok_at_oracle.com>
Date: Wed, 03 Oct 2012 17:43:04 +0200

Hi Sebastien,

unfortunately "String" is not defined unit, so in general you have to
deal w/ char[].
 From what I understood the entire request content is a String, so your
ReadHandler code may look like:

nioReader.notifyAvailable(new ReadHandler() {
     private final char[] tmpChar[] = new char[256];
     private final StringBuilder sb = new StringBuilder();

     public void onDataAvailable() throws Exception {
            bufferAvailableChars();
            nioReader.notifyAvailable(this);
     }

     void onAllDataRead() throws Exception {
            bufferAvailableChars();
            processRequestContent(sb.toString());
     }

     void onError(final Throwable t) {
..............
     }

     private void bufferAvailableChars() throws IOException {
           while(nioReader.isReady()) {
                 final int len = nioReader.read(tmpChar);
                 sb.append(tmpChar, 0, len);
           }
     }

});

WBR,
Alexey.

On 10/03/2012 05:22 PM, Sebastien Dionne wrote:
>
> I want to add that I want to have a streaming connection that will
> send data (client) and receive data (server). Like a websocket
> connection.
>
> In my case the data sent by the client will be string, so I will
> prefer to be notify when a String is received and not playing with
> bytes array.
>
> On Oct 3, 2012 10:18 AM, "Sebastien Dionne" <survivant00_at_gmail.com
> <mailto:survivant00_at_gmail.com>> wrote:
>
> ---------- Forwarded message ----------
> From: "Sébastien Dionne" <sebastien.dionne_at_gmail.com
> <mailto:sebastien.dionne_at_gmail.com>>
> Date: Oct 3, 2012 10:17 AM
> Subject: Fwd: grizzly2 -httpserver- parse HTML request
> To: "Survivant 00" <survivant00_at_gmail.com
> <mailto:survivant00_at_gmail.com>>
>
> ---------- Forwarded message ----------
> From: <sebastien.dionne_at_scd.desjardins.com
> <mailto:sebastien.dionne_at_scd.desjardins.com>>
> Date: Oct 3, 2012 10:16 AM
> Subject: grizzly2 -httpserver- parse HTML request
> To: <sebastien.dionne_at_gmail.com <mailto:sebastien.dionne_at_gmail.com>>
>
>
> Hello,
>
> I'm looking for a sample to parse HTML request with Grizzly 2
> HttpServer like the NIOEchoHandler but in HTML format.
>
> Is it possible to receive a builded request in the HttpServer and
> work with that directly ?
>
>
> here the sample that I looked :
>
>
> MY SERVER
>
> /**
> * @param args
> * @throws InterruptedException
> */
> public static void main(String[] args) throws
> InterruptedException {
> // create a basic server that listens on port 8080.
> HttpServer server = HttpServer.createSimpleServer();
>
> final ServerConfiguration config =
> server.getServerConfiguration();
>
> // Map the path, /echo, to the NonBlockingEchoHandler
> config.addHttpHandler(new NonBlockingEchoHandler(), "/echo");
>
> // myabe these lines are not required
>
> NetworkListener networkListener = new NetworkListener("aaa");
>
> // Construct filter chain
> FilterChainBuilder serverFilterChainBuilder =
> FilterChainBuilder.stateless();
> // Add transport filter
> serverFilterChainBuilder.add(new TransportFilter());
> // Add HttpServerFilter, which transforms Buffer <->
> HttpContent
> serverFilterChainBuilder.add(new HttpServerFilter());
>
> networkListener.setFilterChain(serverFilterChainBuilder.build());
>
> server.addListener(networkListener);
>
> try {
> server.start();
> Client client = new Client();
> client.run();
> } catch (IOException ioe) {
> ioe.printStackTrace();
> } finally {
> //server.stop();
> }
>
> Thread.sleep(300000);
> server.stop();
>
> }
>
>
> but in NonBlockingEchoHandler, I would like to receive a pre-build
> request and not parsing it myself
>
> I would like to replace
>
> private void echoAvailableData(NIOReader in, NIOWriter out, char[]
> buf)
> throws IOException {
>
> while(in.isReady()) {
> int len = in.read(buf);
> out.write(buf, 0, len);
> }
> }
>
> by something like
>
>
> private void echoAvailableData(HttpRequest request, NIOWriter out)
> throws IOException {
>
> String content = request.getText(); // something that
> return the headers + content
>
> out.write(content);
>
> }
>