Package org.xlightweb.client

Examples of org.xlightweb.client.HttpClientConnection$MessageHandler


       
        Server server = new Server(dh);
        server.start();
       
       
        HttpClientConnection con = new HttpClientConnection("localhost", server.getLocalPort());

        OnHeaderReceivedResponseHandler respHdl = new OnHeaderReceivedResponseHandler();
        con.send(new GetRequest("http://localhost:" + server.getLocalPort() + "/"), respHdl);
       
        QAUtil.sleep(1000);
        Assert.assertEquals(200, respHdl.responseRef.get().getStatus());
        Assert.assertEquals(0, respHdl.responseRef.get().getNonBlockingBody().available());
       
        QAUtil.sleep(3000);
        Assert.assertEquals(5, respHdl.responseRef.get().getNonBlockingBody().available());
       
        con.close();
        server.close();
    }   
View Full Code Here


       
        Server server = new Server(dh);
        server.start();
       
       
        HttpClientConnection con = new HttpClientConnection("localhost", server.getLocalPort());

        OnMessageReceivedResponseHandler respHdl = new OnMessageReceivedResponseHandler();
        con.send(new GetRequest("http://localhost:" + server.getLocalPort() + "/"), respHdl);
       
        QAUtil.sleep(1000);
        Assert.assertNull(respHdl.responseRef.get());
       
        QAUtil.sleep(3000);
        Assert.assertEquals(200, respHdl.responseRef.get().getStatus());
        Assert.assertEquals(5, respHdl.responseRef.get().getNonBlockingBody().available());
       
        con.close();
        server.close();
    }  
View Full Code Here

        System.out.println("testStreaming");

        WebContainer container = new WebContainer(new MyServlet());
        container.start();
            
        HttpClientConnection con = new HttpClientConnection("localhost", container.getLocalPort());
        con.setBodyDataReceiveTimeoutMillis(5000);
       
       
        for (int i = 0; i < 1000; i++) {
            FutureResponseHandler respHdl = new FutureResponseHandler();
            BodyDataSink dataSink = con.send(new HttpRequestHeader("POST", "http://localhost:" + container.getLocalPort() + "/test"), respHdl);
           
            dataSink.write("test");
           
            IHttpResponse response = respHdl.getResponse();
            Assert.assertEquals(200, response.getStatus());
           
            BodyDataSource dataSource = response.getBody();
            Assert.assertEquals("test", dataSource.readStringByLength(4));
           
            dataSink.write("12345");
            Assert.assertEquals("12345", dataSource.readStringByLength(5));
           
            dataSink.write("789");
            dataSink.close();
            Assert.assertEquals("789", dataSource.readString());
        }
       
        con.close();
        container.stop();
    }   
View Full Code Here

        System.out.println("testStreamingNonPersistent");

        WebContainer container = new WebContainer(new MyServlet());
        container.start();
            
        HttpClientConnection con = new HttpClientConnection("localhost", container.getLocalPort());

        FutureResponseHandler respHdl = new FutureResponseHandler();
        HttpRequestHeader header = new HttpRequestHeader("POST", "http://localhost:" + container.getLocalPort() + "/test");
        header.setHeader("connection", "close");
        BodyDataSink dataSink = con.send(header, respHdl);
       
        dataSink.write("test");
       
        IHttpResponse response = respHdl.getResponse();
        Assert.assertEquals(200, response.getStatus());
       
        BodyDataSource dataSource = response.getBody();
        Assert.assertEquals("test", dataSource.readStringByLength(4));
       
        dataSink.write("12345");
        Assert.assertEquals("12345", dataSource.readStringByLength(5));
       
        dataSink.write("789");
        dataSink.close();
        Assert.assertEquals("789", dataSource.readString());       

               
        con.close();
        container.stop();
    }   
View Full Code Here

        System.out.println("testStreamingCompletionHandler");
       
        WebContainer container = new WebContainer(new MyServlet());
        container.start();
            
        HttpClientConnection con = new HttpClientConnection("localhost", container.getLocalPort());

       
        for (int i = 0; i < 100; i++) {
            final FutureResponseHandler respHdl = new FutureResponseHandler();
            final BodyDataSink dataSink = con.send(new HttpRequestHeader("POST", "http://localhost:" + container.getLocalPort() + "/test"), respHdl);
            dataSink.setFlushmode(FlushMode.ASYNC);
           
           
            IWriteCompletionHandler completionHandler1 = new IWriteCompletionHandler() {
               
                public void onWritten(int written) throws IOException {
                    try {
                        IHttpResponse response = respHdl.getResponse();
                        Assert.assertEquals(200, response.getStatus());
                       
                        final BodyDataSource dataSource = response.getBody();
                        Assert.assertEquals("test", dataSource.readStringByLength(4));
                       
   
                        IWriteCompletionHandler completionHandler2 = new IWriteCompletionHandler() {
                           
                            public void onWritten(int written) throws IOException {
                                Assert.assertEquals("12345", dataSource.readStringByLength(5));

                                IWriteCompletionHandler completionHandler3 = new IWriteCompletionHandler() {
                                   
                                    public void onWritten(int written) throws IOException {
                                        Assert.assertEquals("789", dataSource.readStringByLength(3));
                                        dataSink.close();
                                    }
                                   
                                    public void onException(IOException ioe) {
                                        ioe.printStackTrace();
                                    }
                                };
                               
                                // writing 3. junk and close
                                dataSink.write(new ByteBuffer[] { DataConverter.toByteBuffer("789", "US-ASCII") }, completionHandler3);
                            }
                           
                            public void onException(IOException ioe) {
                                ioe.printStackTrace();
                            }
                        };
                       
                       
                        // writing 2. junk
                        dataSink.write(new ByteBuffer[] { DataConverter.toByteBuffer("12345", "US-ASCII") }, completionHandler2);   
                       
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

                public void onException(IOException ioe) {
                    ioe.printStackTrace();
                }
            };
           
            // writing 1. junk
            dataSink.write(new ByteBuffer[] { DataConverter.toByteBuffer("test", "US-ASCII") }, completionHandler1);
           
                       
            IBodyCloseListener closeListener = new IBodyCloseListener() {
                public void onClose() throws IOException {
                    synchronized (dataSink) {
                        dataSink.notify();
                    }
                }
            };
           
            dataSink.addCloseListener(closeListener);
           
            try {
               synchronized (dataSink) {
                   if (dataSink.isOpen()) {
                       dataSink.wait();
                   }
               }
            } catch (InterruptedException ignore) {  }
           
            System.out.print(".");
       }
       
        System.out.print("finished");
        con.close();
        container.stop();
    }   
View Full Code Here

     
     
      IHttpRequestHandler reqHdl = new IHttpRequestHandler() {
       
          public void onRequest(IHttpExchange exchange) throws IOException, BadMessageException {
              HttpClientConnection con = new HttpClientConnection("localhost", oldServer.getLocalPort());
              IHttpResponse response = con.call(new GetRequest("http://localhost:" + oldServer.getLocalPort() + "/"));
             
              exchange.send(response);
          }
      };
     
    IServer server = new HttpServer(reqHdl);
    server.start();
 
   
    IBlockingConnection con = new BlockingConnection("localhost", server.getLocalPort());
     
    con.write("GET /test HTTP/1.1\r\n" +
          "Host: localhost\r\n" +
          "User-Agent: test\r\n" +
          "\r\n");
     
   
    String header = con.readStringByDelimiter("\r\n\r\n") + "\r\n";
    Assert.assertTrue(header.indexOf("200") != -1);
    Assert.assertTrue(header.indexOf("Connection: close") != -1);
    Assert.assertEquals("<html>this ...", con.readStringByLength(14));
   
    try {
      con.readByte();
      Assert.fail("ClosedChannelException expected");
    } catch (ClosedChannelException expected) { }
   
   
    oldServer.close();
View Full Code Here

   
    HttpServer server = new HttpServer(new ItWorksServerHandler());
    server.start();
 
    ConnectionLifeCycleHandler lifecycleHandler = new ConnectionLifeCycleHandler();
    HttpClientConnection con = new HttpClientConnection("localhost", server.getLocalPort(), lifecycleHandler);
   
    Assert.assertEquals(1, lifecycleHandler.getCountCalledConnect());
    Assert.assertEquals(0, lifecycleHandler.getCountCalledDisconnect());
   
    con.close();
    QAUtil.sleep(1000);
   
    Assert.assertEquals(1, lifecycleHandler.getCountCalledConnect());
    Assert.assertEquals(1, lifecycleHandler.getCountCalledDisconnect());
   
View Full Code Here

   
    server.start();
 


    HttpClientConnection con = new HttpClientConnection("localhost", server.getLocalPort());
    QAUtil.sleep(200);
   
    Assert.assertEquals(1, lifecycleHandler.getCountCalledConnect());
    Assert.assertEquals(0, lifecycleHandler.getCountCalledDisconnect());
   
    con.close();
    QAUtil.sleep(200);
   
    Assert.assertEquals(1, lifecycleHandler.getCountCalledConnect());
    Assert.assertEquals(1, lifecycleHandler.getCountCalledDisconnect());
   
View Full Code Here

  public void testSimple() throws Exception {
     
    IServer server = new HttpServer(new ContentEchoHandler());
    server.start();

    HttpClientConnection con = new HttpClientConnection("localhost", server.getLocalPort());
 
       
    FutureResponseHandler hdl = new FutureResponseHandler();
   
    BodyDataSink sink = con.send(new HttpRequestHeader("POST", "/", "text/plain; charset=iso-8859-1"), hdl);
    sink.flush();
   
    BlockingBodyDataSource source = hdl.getResponse().getBlockingBody();
   
    sink.write("test\r\n");
    Assert.assertEquals("test", source.readStringByDelimiter("\r\n"));

    sink.write("123\r\n");
    Assert.assertEquals("123", source.readStringByDelimiter("\r\n"));

        sink.write("456\r\n");
        Assert.assertEquals("456", source.readStringByDelimiter("\r\n"));

   
    con.close();
    server.close();
  }
View Full Code Here

     
        IServer server = new HttpServer(0, new ContentEchoHandler(), sslContext, true);
        server.start();

        INonBlockingConnection tcpCon = new NonBlockingConnection("localhost", server.getLocalPort(), sslContext, true);
        HttpClientConnection con = new HttpClientConnection(tcpCon);
   
           
        FutureResponseHandler hdl = new FutureResponseHandler();
       
        BodyDataSink sink = con.send(new HttpRequestHeader("POST", "/", "text/plain; charset=iso-8859-1"), hdl);
        sink.flush();
       
        BlockingBodyDataSource source = hdl.getResponse().getBlockingBody();
       
        sink.write("test\r\n");
        Assert.assertEquals("test", source.readStringByDelimiter("\r\n"));

        sink.write("123\r\n");
        Assert.assertEquals("123", source.readStringByDelimiter("\r\n"));

        sink.write("456\r\n");
        Assert.assertEquals("456", source.readStringByDelimiter("\r\n"));

       
        con.close();
        server.close();
    }
View Full Code Here

TOP

Related Classes of org.xlightweb.client.HttpClientConnection$MessageHandler

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.