Package org.xlightweb

Examples of org.xlightweb.HttpResponse


  private static final class BlockingReadHandler implements IHttpRequestHandler {
   
    public void onRequest(IHttpExchange exchange) throws IOException {

      String body = exchange.getRequest().getBlockingBody().readString();
      exchange.send(new HttpResponse(200, "text/plain", body));
     
    }
View Full Code Here


  public void testSimple() throws Exception {
     
        IHttpRequestHandler reqHdl = new IHttpRequestHandler() {
           
            public void onRequest(IHttpExchange exchange) throws IOException, BadMessageException {
                HttpResponse resp = new HttpResponse(200, "text/plain", "test");
                resp.setHeader("Cache-Control", "public, max-age=1000");
                exchange.send(resp);
            }
        };
       
        HttpServer server = new HttpServer(reqHdl);
        server.start();
       
        HttpClient httpClient = new HttpClient();
        httpClient.setCacheMaxSizeKB(100);
       
        IHttpResponse resp = httpClient.call(new GetRequest("http://localhost:" + server.getLocalPort() + "/"));
        Assert.assertEquals(200, resp.getStatus());
        Assert.assertEquals("test", resp.getBody().readString());
        Assert.assertNull(resp.getHeader(CacheHandler.XHEADER_NAME));
       
        QAUtil.sleep(1000);
       
        resp = httpClient.call(new GetRequest("http://localhost:" + server.getLocalPort() + "/"));
        Assert.assertEquals(200, resp.getStatus());
        Assert.assertTrue(resp.getHeader(CacheHandler.XHEADER_NAME).startsWith("HIT"));
        Assert.assertEquals("test", resp.getBody().readString());
       
       
        Assert.assertEquals(1, httpClient.getNumCacheHit());
        Assert.assertEquals(1, httpClient.getNumCacheMiss());
       
View Full Code Here

    public void testRevalidate() throws Exception {
       
        IHttpRequestHandler reqHdl = new IHttpRequestHandler() {
           
            public void onRequest(IHttpExchange exchange) throws IOException, BadMessageException {
                HttpResponse resp = new HttpResponse(200, "text/plain", "test");
                resp.setHeader("Cache-Control", "public, max-age=1000, must-revalidate");
                exchange.send(resp);
            }
        };
       
        HttpServer server = new HttpServer(reqHdl);
        server.start();
       
        HttpClient httpClient = new HttpClient();
        httpClient.setCacheMaxSizeKB(100);
       
        IHttpResponse resp = httpClient.call(new GetRequest("http://localhost:" + server.getLocalPort() + "/"));
        Assert.assertEquals(200, resp.getStatus());
        Assert.assertEquals("test", resp.getBody().readString());
        Assert.assertNull(resp.getHeader(CacheHandler.XHEADER_NAME));
       
        QAUtil.sleep(1000);
       
        resp = httpClient.call(new GetRequest("http://localhost:" + server.getLocalPort() + "/"));
        Assert.assertEquals(200, resp.getStatus());
        Assert.assertNull(resp.getHeader(CacheHandler.XHEADER_NAME));
        Assert.assertEquals("test", resp.getBody().readString());
       
       
        Assert.assertEquals(0, httpClient.getNumCacheHit());
        Assert.assertEquals(2, httpClient.getNumCacheMiss());
       
View Full Code Here

    public void testMissingAge() throws Exception {
       
        IHttpRequestHandler reqHdl = new IHttpRequestHandler() {
           
            public void onRequest(IHttpExchange exchange) throws IOException, BadMessageException {
                HttpResponse resp = new HttpResponse(200, "text/plain", "test");
                resp.setHeader("Cache-Control", "public");
                exchange.send(resp);
            }
        };
       
        HttpServer server = new HttpServer(reqHdl);
        server.start();
       
        HttpClient httpClient = new HttpClient();
        httpClient.setCacheMaxSizeKB(100);
       
        IHttpResponse resp = httpClient.call(new GetRequest("http://localhost:" + server.getLocalPort() + "/"));
        Assert.assertEquals(200, resp.getStatus());
        Assert.assertEquals("test", resp.getBody().readString());
        Assert.assertNull(resp.getHeader(CacheHandler.XHEADER_NAME));
       
        QAUtil.sleep(1000);
       
        resp = httpClient.call(new GetRequest("http://localhost:" + server.getLocalPort() + "/"));
        Assert.assertEquals(200, resp.getStatus());
        Assert.assertNull(resp.getHeader(CacheHandler.XHEADER_NAME));
        Assert.assertEquals("test", resp.getBody().readString());
       
       
        Assert.assertEquals(0, httpClient.getNumCacheHit());
        Assert.assertEquals(2, httpClient.getNumCacheMiss());
       
View Full Code Here

      if (request.getHeader("sleep-time") != null) {
        int sleepTime = Integer.parseInt(request.getHeader("sleep-time"));
        QAUtil.sleep(sleepTime);
      }
      
      exchange.send(new HttpResponse(200, "text/plain", "OK"));
    }
View Full Code Here

  private static final class ServerHandler3 implements IHttpRequestHandler {
   
    @InvokeOn(InvokeOn.MESSAGE_RECEIVED)
    public void onRequest(IHttpExchange exchange) throws IOException {

      exchange.send(new HttpResponse(200, "text/plain", "OK"));
    }
View Full Code Here

            public void onRequest(IHttpExchange exchange) throws IOException, BadMessageException {
               
                IHttpRequest request = exchange.getRequest();
                String ifNoneMatch = request.getHeader("If-None-Match");
                if ((ifNoneMatch != null) && (ifNoneMatch.equals("\"23\""))) {
                    exchange.send(new HttpResponse(304));
                    return;
                }
               
                HttpResponse resp = new HttpResponse(200, "text/plain", "test");
                resp.setHeader("ETag", "\"23\"");
                exchange.send(resp);
            }
        };
       
       
        HttpServer server = new HttpServer(reqHdl);
        server.start();
       
        HttpClient httpClient = new HttpClient();
        httpClient.setCacheMaxSizeKB(100);
       
        IHttpRequest request = new GetRequest("http://localhost:" + server.getLocalPort() + "/");
        request.setHeader("If-None-Match", "\"23\"");
        IHttpResponse resp = httpClient.call(request);
        Assert.assertEquals(304, resp.getStatus());
        Assert.assertNull(resp.getHeader(CacheHandler.XHEADER_NAME));
       
        QAUtil.sleep(1000);
       
        Assert.assertEquals(0, httpClient.getNumCacheHit());
        Assert.assertEquals(0, httpClient.getNumCacheMiss());
View Full Code Here

            public void onRequest(IHttpExchange exchange) throws IOException, BadMessageException {
               
                IHttpRequest request = exchange.getRequest();
                String ifModifiedSince = request.getHeader("If-Modified-Since");
                if ((ifModifiedSince != null) && (ifModifiedSince.equals(date))) {
                    exchange.send(new HttpResponse(304));
                    return;
                }
               
                HttpResponse resp = new HttpResponse(200, "text/plain", "test");
                resp.setHeader("Last-Modified", date);
                exchange.send(resp);
            }
        };
       
       
        HttpServer server = new HttpServer(reqHdl);
        server.start();
       
        HttpClient httpClient = new HttpClient();
        httpClient.setCacheMaxSizeKB(100);
       
        IHttpResponse resp = httpClient.call(new GetRequest("http://localhost:" + server.getLocalPort() + "/"));
        Assert.assertEquals(200, resp.getStatus());
        Assert.assertEquals("test", resp.getBody().readString());
        Assert.assertNull(resp.getHeader(CacheHandler.XHEADER_NAME));
        String lastModified = resp.getHeader("Last-Modified");
       
        QAUtil.sleep(1000);
       
        IHttpRequest request = new GetRequest("http://localhost:" + server.getLocalPort() + "/");
        request.setHeader("If-Modified-Since", lastModified);
        resp = httpClient.call(request);
        Assert.assertEquals(304, resp.getStatus());
        Assert.assertNull(resp.getHeader(CacheHandler.XHEADER_NAME));
       
        QAUtil.sleep(1000);
       
        Assert.assertEquals(0, httpClient.getNumCacheHit());
        Assert.assertEquals(1, httpClient.getNumCacheMiss());
View Full Code Here

  @Override
  protected void onProtocolException(Exception ex) {
    if (ex instanceof BadMessageException) {
      setPersistent(false);
      try {
        send(new HttpResponse(400, "text/html", generateErrorMessageHtml(400, ex.getMessage(), getId())));
      } catch (IOException ioe) {
        if (LOG.isLoggable(Level.FINE)) {
          LOG.fine("could not send error message " + 400 + " reason " + ioe.toString());
        }
        destroy();
View Full Code Here

        if ((upgrade != null) && upgrade.equalsIgnoreCase("TLS/1.0")) {

          if (getUnderlyingTcpConnection().isSecuredModeActivateable()) {
            suspendReceiving();
           
            HttpResponse response = new HttpResponse(101);
            response.setHeader("Connection", "Upgrade");
            response.setHeader("Upgrade", "TLS/1.0, HTTP/1.1");
            writeMessage(response, false);
           
            getUnderlyingTcpConnection().activateSecuredMode();
         
            resumeReceiving();
           
          } else {
            send(new HttpResponse(400, "text/html", generateErrorMessageHtml(400, "upgrade TLS is not supported", getId())));
            return;
          }
         
          return;
        }
      }


      // handle 100 continue header
      if (isAutconfirmExpect100ContinueHeader) {
        String expectHeader = request.getHeader("Expect");
        if ((expectHeader != null) && expectHeader.equalsIgnoreCase("100-Continue")) {
          writeMessage(new HttpResponse(100), false);
        }
      }


View Full Code Here

TOP

Related Classes of org.xlightweb.HttpResponse

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.