Package org.xlightweb

Source Code of org.xlightweb.ContextTest

/*
*  Copyright (c) xlightweb.org, 2008 - 2009. All rights reserved.
*
*  This library is free software; you can redistribute it and/or
*  modify it under the terms of the GNU Lesser General Public
*  License as published by the Free Software Foundation; either
*  version 2.1 of the License, or (at your option) any later version.
*
*  This library is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
*  Lesser General Public License for more details.
*
*  You should have received a copy of the GNU Lesser General Public
*  License along with this library; if not, write to the Free Software
*  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*
* Please refer to the LGPL license at: http://www.gnu.org/copyleft/lesser.txt
* The latest copy of this software may be found on http://www.xlightweb.org/
*/
package org.xlightweb;


import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jboss.resteasy.util.GetRestful;
import org.junit.Assert;

import org.junit.Test;
import org.mortbay.jetty.servlet.ServletHolder;
import org.xlightweb.GetRequest;
import org.xlightweb.HttpResponse;
import org.xlightweb.IHttpConnectHandler;
import org.xlightweb.IHttpConnection;
import org.xlightweb.IHttpDisconnectHandler;
import org.xlightweb.IHttpExchange;
import org.xlightweb.IHttpRequest;
import org.xlightweb.IHttpRequestHandler;
import org.xlightweb.IHttpResponse;
import org.xlightweb.IHttpResponseHandler;
import org.xlightweb.InvokeOn;
import org.xlightweb.client.HttpClient;
import org.xlightweb.server.HttpServer;
import org.xsocket.Execution;
import org.xsocket.ILifeCycle;
import org.xsocket.connection.BlockingConnection;
import org.xsocket.connection.IBlockingConnection;
import org.xsocket.connection.IServer;
import org.xsocket.connection.Server;
import org.xsocket.connection.ConnectionUtils;



/**
*
* @author grro@xlightweb.org
*/
public final class ContextTest {
 
 
  @Test
  public void testSimple() throws Exception {
   
    System.setProperty(IHttpExchange.SHOW_DETAILED_ERROR_KEY, "true");
   
   
    String basePath = this.getClass().getResource("").getFile();
    File testFile = new File(basePath + File.separator + "werewrewrewrewr.bin");
    FileWriter fw = new FileWriter(testFile);
    fw.write("ewrdfsdfsfsfdsfds");
    fw.close();
   
    // start xSocket
    Context ctx = new Context("");
    ctx.addHandler("/site/*", new FileServiceRequestHandler(basePath, true));
    ctx.addHandler("/rpc/*", new MyBusinessHandler());
   
    Server server = new HttpServer(ctx);
    server.start();
   
   
    HttpClient httpClient = new HttpClient();
   
    IHttpResponse response = httpClient.call(new GetRequest("http://localhost:" + server.getLocalPort() + "/site/" + testFile.getName()));
    Assert.assertEquals(200, response.getStatus());
    Assert.assertEquals("application/octet-stream", response.getContentType());
    Assert.assertTrue(response.hasBody());
   
    response = httpClient.call(new GetRequest("http://localhost:" + server.getLocalPort() + "/rpc/getCustomers"));
    Assert.assertEquals(200, response.getStatus());
    Assert.assertTrue(response.getContentType().startsWith("text/plain"));
    Assert.assertTrue(response.hasBody());
   
    httpClient.close();
    server.close();
   
   
    testFile.delete();
  }
 
 
  @Test
  public void testAnnotated() throws Exception {
   
    System.setProperty(IHttpExchange.SHOW_DETAILED_ERROR_KEY, "true");
   
   
    String basePath = this.getClass().getResource("").getFile();
    File testFile = new File(basePath + File.separator + "werewrewre.bin");
    FileWriter fw = new FileWriter(testFile);
    fw.write("ewrdfsdfsfsfdffdfdsfds");
    fw.close();
   
    // start xSocket
    Context ctx = new Context("");
    ctx.addHandler("/site/*", new FileServiceRequestHandler(basePath, true));
    ctx.addHandler(new AnnotatedHandler());
   
    Server server = new HttpServer(ctx);
    server.start();
   
   
    HttpClient httpClient = new HttpClient();
   
    IHttpResponse response = httpClient.call(new GetRequest("http://localhost:" + server.getLocalPort() + "/site/" + testFile.getName()));
    Assert.assertEquals(200, response.getStatus());
    Assert.assertEquals("application/octet-stream", response.getContentType());
    Assert.assertTrue(response.hasBody());
   
    response = httpClient.call(new GetRequest("http://localhost:" + server.getLocalPort() + "/rpc/getCustomers"));
    Assert.assertEquals(200, response.getStatus());
    Assert.assertTrue(response.getContentType().startsWith("text/plain"));
    Assert.assertTrue(response.hasBody());
   
    httpClient.close();
    server.close();
   
   
    testFile.delete();
  }
 
 
 
  @Test
    public void testAnnotated2() throws Exception {
     
      int port = 0;

      Context rootCtx = new Context("");
      rootCtx.addHandler(new PersonRequestHandler());
      rootCtx.addHandler(new AccountRequestHandler());

      IServer server = new HttpServer(port, rootCtx);
      server.start();

      HttpClient httpClient = new HttpClient()
     
      IHttpResponse response = httpClient.call(new GetRequest("http://localhost:" + server.getLocalPort()+ "/Person/1"));
      Assert.assertEquals(200, response.getStatus());
      Assert.assertEquals("person", response.getBlockingBody().readString());
     
      response = httpClient.call(new GetRequest("http://localhost:" + server.getLocalPort()+ "/Account/1"));
        Assert.assertEquals(200, response.getStatus());
        Assert.assertEquals("account", response.getBlockingBody().readString());
     
      httpClient.close();
      server.close();
    }
 

  @Mapping("/Person/*")
  private static final class PersonRequestHandler implements IHttpRequestHandler {
     
      public void onRequest(IHttpExchange exchange) throws IOException, BadMessageException {
          exchange.send(new HttpResponse(200, "text/plain", "person"));
      }     
  }

 
    @Mapping("/Account/*")
    private static final class AccountRequestHandler implements IHttpRequestHandler {
       
        public void onRequest(IHttpExchange exchange) throws IOException, BadMessageException {
            exchange.send(new HttpResponse(200, "text/plain", "account"));
        }      
    }
 
 
 
 
  @Test
  public void testReplace() throws Exception {
   
    // start xSocket
    Context ctx = new Context("");
    ctx.addHandler("/*", new ServerErrorRequestHandler());
    Assert.assertEquals(1, ctx.size());

    ctx.addHandler("/badRequest", new BadRequestHandler());
    Assert.assertEquals(2, ctx.size());
   
   
    ctx.addHandler("/*", new GoodRequestHandler());
    Assert.assertEquals(2, ctx.size());
 

   
    Server server = new HttpServer(ctx);
    server.start();
   
   
    HttpClient httpClient = new HttpClient();
   
    IHttpResponse response = httpClient.call(new GetRequest("http://localhost:" + server.getLocalPort() + "/test"));
    Assert.assertEquals(200, response.getStatus());
   
    response = httpClient.call(new GetRequest("http://localhost:" + server.getLocalPort() + "/badRequest"));
    Assert.assertEquals(400, response.getStatus());

    httpClient.close();
    server.close();
  }

 
 
  private static final class ServerErrorRequestHandler implements IHttpRequestHandler {
   
    public void onRequest(IHttpExchange exchange) throws IOException, BadMessageException {
      exchange.send(new HttpResponse(500));
    }
  }
 

  private static final class GoodRequestHandler implements IHttpRequestHandler {
   
    public void onRequest(IHttpExchange exchange) throws IOException, BadMessageException {
      exchange.send(new HttpResponse(200));
    }
  }
 
 
  private static final class BadRequestHandler implements IHttpRequestHandler {
   
    public void onRequest(IHttpExchange exchange) throws IOException, BadMessageException {
      exchange.send(new HttpResponse(400));
    }
  }
 
 
  @Test
  public void testCompare() throws Exception {

   
    org.mortbay.jetty.Server jettyServer = new org.mortbay.jetty.Server(0);
   
    org.mortbay.jetty.servlet.Context rootCtx = new org.mortbay.jetty.servlet.Context(jettyServer, "/ctx1");
    ServletHolder servletHolder = new ServletHolder(new TestServlet());
    rootCtx.addServlet(servletHolder, "/test/*");
    jettyServer.start();
   
   
    Context ctx = new Context("/ctx1");
    ctx.addHandler("/test/*", new TestRequestHandler());
    HttpServer server = new HttpServer(ctx);
    server.start();
   
   
    HttpClient httpClient = new HttpClient();
   
    IHttpResponse response = httpClient.call(new GetRequest("http://localhost:" + jettyServer.getConnectors()[0].getLocalPort() + "/ctx1/test/you"));
    if (response.getStatus() != 200) {
      System.out.println("got " + response.getStatus() + " instead of 200");
      Assert.fail();
    }
   
    String body = response.getBlockingBody().readString();
    if (body.indexOf("contextPath=/ctx1") == -1) {
      System.out.println("contextPath=/ctx1 expected");
      Assert.fail();
    }
   
    if (body.indexOf("contextPath=/ctx1") == -1) {
      System.out.println("servletPath=/test expected");
      Assert.fail();
    }

   
    response = httpClient.call(new GetRequest("http://localhost:" + server.getLocalPort() + "/ctx1/test/you"));
    if (response.getStatus() != 200) {
      System.out.println("got " + response.getStatus());
      Assert.fail();
    }
   
    body = response.getBlockingBody().readString();
    Assert.assertTrue(body.indexOf("contextPath=/ctx1") != -1);
    Assert.assertTrue(body.indexOf("servletPath=/test") != -1);

   
    httpClient.close();
    jettyServer.stop();
    server.close();
  }
 
 
 
 
  @Test
  public void testInvokeOnMessage() throws Exception {
   
    System.setProperty(IHttpExchange.SHOW_DETAILED_ERROR_KEY, "true");
   
    OnMessageReceivedHandler hdl = new OnMessageReceivedHandler();

    // start xSocket
    Context ctx = new Context("");
    ctx.addHandler("/ctx1/*", hdl);
    Server server = new HttpServer(ctx);
    ConnectionUtils.start(server);
   
   

    IBlockingConnection con = new BlockingConnection("localhost", server.getLocalPort());

    con.write("POST /ctx1/test/test2 HTTP/1.1\r\n" +
          "Host: localhost\r\n" +
          "User-Agent: me\r\n" +
          "Content-Length: 10\r\n" +
          "\r\n" +
          "12345");

   
    QAUtil.sleep(300);
    Assert.assertNull(hdl.getOnRequestThreadname());
   
    con.write("67890");
    QAUtil.sleep(200);
    Assert.assertNotNull(hdl.getOnRequestThreadname());
   
    String header = con.readStringByDelimiter("\r\n\r\n") + "\r\n";
    int contentLength = QAUtil.readContentLength(header);
     
    String body = con.readStringByLength(contentLength);
    Assert.assertEquals("OK", body);
   
    con.close();
    server.close();
  }
 
 
  @Test
  public void testOnRequestMultithreaded() throws Exception {

    MultithreadedHandler hdl = new MultithreadedHandler();

    // start xSocket
    Context ctx = new Context("");
    ctx.addHandler("/ctx1/*", hdl);
    Server server = new HttpServer(ctx);
    ConnectionUtils.start(server);
   
   
    GetRequest req = new GetRequest("http://localhost:" + server.getLocalPort() + "/ctx1/test/test2");
    req.setHeader("Host", "localhost");
    req.setHeader("User-Agent", "me");
   
   
    IBlockingConnection con = new BlockingConnection("localhost", server.getLocalPort());

    con.write(req.toString());
   
    String header = con.readStringByDelimiter("\r\n\r\n") + "\r\n";
    int contentLength = QAUtil.readContentLength(header);
     
    String body = con.readStringByLength(contentLength);
    Assert.assertEquals("OK", body);
   
    System.out.println(hdl.getOnRequestThreadname());
    Assert.assertTrue(hdl.getOnRequestThreadname().startsWith("xServerPool"));

    con.close();
    server.close();
  }
 
 
  @Test
  public void testLifeCycle() throws Exception {

    OnMessageReceivedHandler hdl = new OnMessageReceivedHandler();

    // start xSocket
    Context ctx = new Context("");
    ctx.addHandler("/ctx1/*", hdl);
    Server server = new HttpServer(ctx);
    ConnectionUtils.start(server);
   
    QAUtil.sleep(200);
    Assert.assertEquals(1, hdl.getCountOnInitCalled());
    Assert.assertEquals(0, hdl.getCountOnDestroyCalled());
     
    server.close();
    QAUtil.sleep(200);
    Assert.assertEquals(1, hdl.getCountOnInitCalled());
    Assert.assertEquals(1, hdl.getCountOnDestroyCalled());

  }

 
  @Test
  public void testOnRequestNonthreaded() throws Exception {

    NonthreadedHandler hdl = new NonthreadedHandler();

    // start xSocket
    Context ctx = new Context("");
    ctx.addHandler("/ctx1/*", hdl);
    Server server = new HttpServer(ctx);
    server.start();
   
   
    GetRequest req = new GetRequest("http://localhost:" + server.getLocalPort() + "/ctx1/test/test2");
    req.setHeader("Host", "localhost");
    req.setHeader("User-Agent", "me");

   
    IBlockingConnection con = new BlockingConnection("localhost", server.getLocalPort());

    con.write(req.toString());
   
    String header = con.readStringByDelimiter("\r\n\r\n") + "\r\n";
    int contentLength = QAUtil.readContentLength(header);
     
    String body = con.readStringByLength(contentLength);
    Assert.assertEquals("OK", body);

    con.close();
    server.close();
  }

 

  @Test
  public void testCompareParametersRootContext() throws Exception {

    // start jetty server
    WebContainer servletEngine = new WebContainer(new CompareParametersServlet(), "/ctx1");
    servletEngine.start();


    // start xSocket
    Context ctx = new Context("");
    ctx.addHandler("/ctx1/*", new CompareParametersHandler());
    Server server = new HttpServer(ctx);
    ConnectionUtils.start(server);
   
   
   
    GetRequest reqXSocket = new GetRequest("http://localhost:" + server.getLocalPort() + "/ctx1/test/test2");
    reqXSocket.setHeader("Host", "localhost");
    reqXSocket.setHeader("User-Agent", "me");

    GetRequest reqJetty = new GetRequest("http://localhost:" + servletEngine.getLocalPort() + "/ctx1/test/test2");
    reqJetty.setHeader("Host", "localhost");
    reqJetty.setHeader("User-Agent", "me");

   
    IBlockingConnection conXSocket = new BlockingConnection("localhost", server.getLocalPort());
    IBlockingConnection conJetty = new BlockingConnection("localhost", servletEngine.getLocalPort());

    conXSocket.write(reqXSocket.toString());
   
    String header = conXSocket.readStringByDelimiter("\r\n\r\n");
    int contentLength = QAUtil.readContentLength(header);
     
    String bodyXSocket = conXSocket.readStringByLength(contentLength);
   
   
    conJetty.write(reqJetty.toString());
   
    String headerJetty = conJetty.readStringByDelimiter("\r\n\r\n");
    int contentLengthJetty = QAUtil.readContentLength(headerJetty);
     
    String bodyJetty = conJetty.readStringByLength(contentLengthJetty);
   
    Assert.assertEquals(bodyXSocket, bodyJetty);
   

    conXSocket.close();
    conJetty.close();
    servletEngine.stop();
    server.close();
  }

 
  @Test
  public void testCompareParametersSubContext() throws Exception {

    // start jetty server
    WebContainer servletEngine = new WebContainer(new CompareParametersServlet(), "/ctx1", "/ctx0");
    servletEngine.start();


    // start xSocket
    Context ctx = new Context("/ctx0");
    ctx.addHandler("/ctx1/*", new CompareParametersHandler());
    Server server = new HttpServer(ctx);
    ConnectionUtils.start(server);
   
   
   
   
    GetRequest reqXSocket = new GetRequest("http://localhost:" + server.getLocalPort() + "/ctx0/ctx1/test/test2");
    reqXSocket.setHeader("Host", "localhost");
    reqXSocket.setHeader("User-Agent", "me");

   
    GetRequest reqJetty = new GetRequest("http://localhost:" + servletEngine.getLocalPort() + "/ctx0/ctx1/test/test2");
    reqJetty.setHeader("Host", "localhost");
    reqJetty.setHeader("User-Agent", "me");

   
    IBlockingConnection conXSocket = new BlockingConnection("localhost", server.getLocalPort());
    IBlockingConnection conJetty = new BlockingConnection("localhost", servletEngine.getLocalPort());

    conXSocket.write(reqXSocket.toString());
   
    String header = conXSocket.readStringByDelimiter("\r\n\r\n");
    int contentLength = QAUtil.readContentLength(header);
     
    String bodyXSocket = conXSocket.readStringByLength(contentLength);
   
   
    conJetty.write(reqJetty.toString());
   
    String headerJetty = conJetty.readStringByDelimiter("\r\n\r\n");
    int contentLengthJetty = QAUtil.readContentLength(headerJetty);
     
    String bodyJetty = conJetty.readStringByLength(contentLengthJetty);
   
    Assert.assertEquals(bodyXSocket, bodyJetty);

    conJetty.close();
    conXSocket.close();
    servletEngine.stop();
    server.close();
  }

 
 
  @Test
  public void testCompareParametersNestedContext() throws Exception {

    // start jetty server
    org.mortbay.jetty.Server jettyServer = new org.mortbay.jetty.Server(0);
   
    org.mortbay.jetty.servlet.Context subCtx = new org.mortbay.jetty.servlet.Context(jettyServer, "/ctx0/ctx1");
   
    ServletHolder servletHolder = new ServletHolder(new CompareParametersServlet());
    subCtx.addServlet(servletHolder, "/test/*");
    jettyServer.start();
   

   
    // start xSocket
    Context ctxRoot = new Context("/ctx0");
    Context ctxSub = new Context(ctxRoot, "/ctx1");
   
    ctxRoot.addHandler("/ctx1/uuu/*", new DoNothingHandler());
   
    ctxSub.addHandler("/test/*", new CompareParametersHandler());
    Server server = new HttpServer(ctxRoot);
    ConnectionUtils.start(server);
   
   

    GetRequest reqXSocket = new GetRequest("http://localhost:" + server.getLocalPort() + "/ctx0/ctx1/test/test2");
    reqXSocket.setHeader("Host", "localhost");
    reqXSocket.setHeader("User-Agent", "me");

   
    GetRequest reqJetty = new GetRequest("http://localhost:" + jettyServer.getConnectors()[0].getLocalPort() + "/ctx0/ctx1/test/test2");
    reqJetty.setHeader("Host", "localhost");
    reqJetty.setHeader("User-Agent", "me");

   
    IBlockingConnection conXSocket = new BlockingConnection("localhost", server.getLocalPort());
    IBlockingConnection conJetty = new BlockingConnection("localhost", jettyServer.getConnectors()[0].getLocalPort());

    conXSocket.write(reqXSocket.toString());
   
    String header = conXSocket.readStringByDelimiter("\r\n\r\n");
    int contentLength = QAUtil.readContentLength(header);
     
    String bodyXSocket = conXSocket.readStringByLength(contentLength);
   
   
    conJetty.write(reqJetty.toString());
   
    String headerJetty = conJetty.readStringByDelimiter("\r\n\r\n");
    int contentLengthJetty = QAUtil.readContentLength(headerJetty);
     
    String bodyJetty = conJetty.readStringByLength(contentLengthJetty);
   
    Assert.assertEquals(bodyXSocket, bodyJetty);

    conJetty.close();
    conXSocket.close();
   
    jettyServer.stop();
    server.close();
  }
 
 
 

  @Test
  public void testMultithreaded() throws Exception {

    MHandler hdl = new MHandler();

    // start xSocket
    Context ctx = new Context("");
    ctx.addHandler("/ctx1/*", hdl);
    HttpServer server = new HttpServer(ctx);
    server.addConnectionHandler(hdl);
    server.start();
   
   
    GetRequest req = new GetRequest("http://localhost:" + server.getLocalPort() + "/ctx1/test/test2");
    req.setHeader("Host", "localhost");
    req.setHeader("User-Agent", "me");

   
    IBlockingConnection con = new BlockingConnection("localhost", server.getLocalPort());

    con.write(req.toString());

    String header = con.readStringByDelimiter("\r\n\r\n") + "\r\n";
    int contentLength = QAUtil.readContentLength(header);
    String body = con.readStringByLength(contentLength);
   
    Assert.assertTrue(header.indexOf("200") != -1);
    Assert.assertEquals("OK", body);
   
   
    Assert.assertNotNull(hdl.getRequest());
    Assert.assertTrue(hdl.getOnRequestThreadname().startsWith("xServerPool"));
   
    Assert.assertEquals(1, hdl.getCountOnConnect());
    Assert.assertTrue(hdl.getOnConnectThreadname().startsWith("xServerPool"));
   
    con.close();
    QAUtil.sleep(200);

    Assert.assertEquals(1, hdl.getCountOnDisconnect());
    Assert.assertTrue(hdl.getOnConnectThreadname().startsWith("xServerPool"));
   
   
    server.close();
  }
 
 
 
  @Test
  public void testRequestTimeout() throws Exception {

    MHandler hdl = new MHandler();

    // start xSocket
    Context ctx = new Context("");
    ctx.addHandler("/ctx1/*", hdl);
    HttpServer server = new HttpServer(ctx);
   
    server.setRequestTimeoutMillis(1000);
   
    server.start();
   
   
   
    IBlockingConnection con = new BlockingConnection("localhost", server.getLocalPort());



    QAUtil.sleep(1500);

    Assert.assertEquals(1, hdl.getCountOnRequestTimeout());
    if (!hdl.getOnRequestTimeoutThreadname().startsWith("xServerPool")) {
      System.out.println("handler executed by " + hdl.getOnRequestTimeoutThreadname() + " not by xServerPool...");
      Assert.fail("handler executed by " + hdl.getOnRequestTimeoutThreadname() + " not by xServerPool...");
    }
   
    con.close();
    server.close();
  }
 

 
   
 
 
 
 
  private static final class CompareParametersHandler implements IHttpRequestHandler {

    public void onRequest(IHttpExchange exchange) throws IOException {

      IHttpRequest request = exchange.getRequest();
     
      StringBuilder sb = new StringBuilder();
      sb.append("requestUri=" + request.getRequestURI() + "\r\n");
      sb.append("queryString=" + request.getQueryString() + "\r\n");
      sb.append("path=" + request.getRequestHandlerPath() + "\r\n");
      sb.append("contextPath=" + request.getContextPath() + "\r\n");


      List<String> paramNames = sort(request.getParameterNameSet());
      for (String key : paramNames) {
        String value = request.getParameter(key);
        sb.append("[param] " + key  + "=" + value + "\r\n");
      }


      exchange.send(new HttpResponse(200, "text/plain", sb.toString()));
    }
  }


  private static List<String> sort(Set<String> s) {
    List<String> l = new ArrayList<String>();
    l.addAll(s);
    Collections.sort(l);
    return l;
  }
 

  private static final class CompareParametersServlet extends HttpServlet {


    private static final long serialVersionUID = -2043092996345779862L;

    @SuppressWarnings("unchecked")
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
      resp.setContentType("text/plain");
      PrintWriter writer = resp.getWriter();
      writer.write("requestUri=" + req.getRequestURI() + "\r\n");
      writer.write("queryString=" + req.getQueryString() + "\r\n");
      writer.write("path=" + req.getServletPath() + "\r\n");
      writer.write("contextPath=" + req.getContextPath() + "\r\n");
     
      Map<String, String> params = new HashMap<String, String>();
      params.putAll(req.getParameterMap());

      for (String key : sort(params.keySet())) {
        String value = req.getParameter(key);
        String txt = "[param] " + key  + "=" + value + "\r\n";
        writer.write(txt);
      }



      writer.close();
    }
  }
 
 
  private static final class MultithreadedHandler implements IHttpRequestHandler {

    private String onRequestThreadname = null;
    private String onRequestTimeoutThreadname = null;
     
   
    public void onRequest(IHttpExchange exchange) throws IOException {
      onRequestThreadname = Thread.currentThread().getName();
     
      exchange.send(new HttpResponse(200, "text/plain", "OK"));
    }
   
    public boolean onRequestTimeout(IHttpConnection connection) throws IOException {
      onRequestTimeoutThreadname = Thread.currentThread().getName();
      return true;
    }
   
    public String getOnRequestThreadname() {
      return onRequestThreadname;
    }
   
    public String getOnRequestTimeoutThreadname() {
      return onRequestTimeoutThreadname;
    }
  }
 

  private static final class NonthreadedHandler implements IHttpRequestHandler, IHttpRequestTimeoutHandler {

    private String onRequestThreadname = null;
    private String onRequestTimeoutThreadname = null;
   
   
    @Execution(Execution.NONTHREADED)
    public void onRequest(IHttpExchange exchange) throws IOException {
      onRequestThreadname = Thread.currentThread().getName();

      exchange.send(new HttpResponse(200, "text/plain", "OK"));
    }
   
   
    @Execution(Execution.NONTHREADED)
    public boolean onRequestTimeout(IHttpConnection connection) throws IOException {
      onRequestTimeoutThreadname = Thread.currentThread().getName();
      return true;
    }
   
    public String getOnRequestThreadname() {
      return onRequestThreadname;
    }
   
    public String getOnRequestTimeoutThreadname() {
      return onRequestTimeoutThreadname;
    }
  }
 
 
  private static final class OnMessageReceivedHandler implements IHttpRequestHandler, ILifeCycle {

    private String onRequestThreadname = null;
    private String onRequestTimeoutThreadname = null;
   
    private int countOnInitCalled = 0;
    private int countOnDestroyCalled = 0;
   
   
    public void onInit() {
      countOnInitCalled++;
    }
   
    public void onDestroy() throws IOException {
      countOnDestroyCalled++;
    }
   
    @InvokeOn(InvokeOn.MESSAGE_RECEIVED)
    public void onRequest(IHttpExchange exchange) throws IOException {
      onRequestThreadname = Thread.currentThread().getName();

      exchange.send(new HttpResponse(200, "text/plain", "OK"));
    }
   
    @Execution(Execution.NONTHREADED)
    public boolean onRequestTimeout(IHttpConnection connection) throws IOException {
      onRequestTimeoutThreadname = Thread.currentThread().getName();
      return true;
    }
   
    public String getOnRequestThreadname() {
      return onRequestThreadname;
    }
   
    public String getOnRequestTimeoutThreadname() {
      return onRequestTimeoutThreadname;
    }
   
    public int getCountOnInitCalled() {
      return countOnInitCalled;
    }
   
    public int getCountOnDestroyCalled() {
      return countOnDestroyCalled;
    }
  }
 

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

      exchange.send(new HttpResponse(200, "text/plain", "OK"));
    }
  }
 
 
  private static final class MHandler implements IHttpRequestHandler, IHttpRequestTimeoutHandler, IHttpConnectHandler, IHttpDisconnectHandler {
   
   
    private int countOnConnect = 0;
    private String onConnectThreadname = null;

    private int countOnDisconnect = 0;
    private String onDisconnectThreadname = null;
   
    private IHttpRequest httpRequest = null;
    private String onRequestThreadname = null;
   
    private int countOnRequestTimeout = 0;
    private String onRequestTimeoutThreadname = null;

   
    public boolean onConnect(IHttpConnection httpConnection) throws IOException {
      countOnConnect++;
      onConnectThreadname = Thread.currentThread().getName();
     
      return true;
    }

   
    public boolean onDisconnect(IHttpConnection httpConnection) throws IOException {
      countOnDisconnect++;
      onDisconnectThreadname = Thread.currentThread().getName();

      return true;
    }
   
    @InvokeOn(InvokeOn.MESSAGE_RECEIVED)
    public void onRequest(IHttpExchange exchange) throws IOException {
      httpRequest = exchange.getRequest();
      onRequestThreadname = Thread.currentThread().getName();
     
      exchange.send(new HttpResponse(200, "text/plain", "OK"));
    }

    public boolean onRequestTimeout(IHttpConnection connection) throws IOException {
      countOnRequestTimeout++;
      onRequestTimeoutThreadname = Thread.currentThread().getName();
     
      return true;
    }
   
   
    int getCountOnConnect() {
      return countOnConnect;
    }
   
    int getCountOnDisconnect() {
      return countOnDisconnect;
    }
   
    int getCountOnRequestTimeout() {
      return countOnRequestTimeout;
    }
   
    IHttpRequest getRequest() {
      return httpRequest;
    }
   
    String getOnConnectThreadname() {
      return onConnectThreadname;
    }
   
    String getOnDisconnectThreadname() {
      return onDisconnectThreadname;
    }
   
    String getOnRequestThreadname() {
      return onRequestThreadname;
    }
   
    String getOnRequestTimeoutThreadname() {
      return onRequestTimeoutThreadname;
    }

  }

 
  @Execution(Execution.NONTHREADED)
  private static final class NHandler implements IHttpRequestHandler, IHttpRequestTimeoutHandler, IHttpConnectHandler, IHttpDisconnectHandler {
   
   
    private int countOnConnect = 0;
    private String onConnectThreadname = null;

    private int countOnDisconnect = 0;
    private String onDisconnectThreadname = null;
   
    private IHttpRequest httpRequest = null;
    private String onRequestThreadname = null;
   
    private int countOnRequestTimeout = 0;
    private String onRequestTimeoutThreadname = null;

   
    public boolean onConnect(IHttpConnection httpConnection) throws IOException {
      countOnConnect++;
      onConnectThreadname = Thread.currentThread().getName();
     
      return true;
    }

   
    public boolean onDisconnect(IHttpConnection httpConnection) throws IOException {
      countOnDisconnect++;
      onDisconnectThreadname = Thread.currentThread().getName();

      return true;
    }
   
    @InvokeOn(InvokeOn.MESSAGE_RECEIVED)
    public void onRequest(IHttpExchange exchange) throws IOException {
      httpRequest = exchange.getRequest();
      onRequestThreadname = Thread.currentThread().getName();
     
      exchange.send(new HttpResponse(200, "text/plain", "OK"));
    }

    public boolean onRequestTimeout(IHttpConnection connection) throws IOException {
      countOnRequestTimeout++;
      onRequestTimeoutThreadname = Thread.currentThread().getName();
     
      return true;
    }
   
   
    int getCountOnConnect() {
      return countOnConnect;
    }
   
    int getCountOnDisconnect() {
      return countOnDisconnect;
    }
   
    int getCountOnRequestTimeout() {
      return countOnRequestTimeout;
    }
   
    IHttpRequest getRequest() {
      return httpRequest;
    }
   
    String getOnConnectThreadname() {
      return onConnectThreadname;
    }
   
    String getOnDisconnectThreadname() {
      return onDisconnectThreadname;
    }
   
    String getOnRequestThreadname() {
      return onRequestThreadname;
    }
   
    String getOnRequestTimeoutThreadname() {
      return onRequestTimeoutThreadname;
    }
  }
 
 
  @Execution(Execution.NONTHREADED)
  private static final class NFilter implements IHttpRequestHandler, IHttpRequestTimeoutHandler, IHttpConnectHandler, IHttpDisconnectHandler {
   
   
    private int countOnConnect = 0;
    private String onConnectThreadname = null;

    private int countOnDisconnect = 0;
    private String onDisconnectThreadname = null;
   
    private IHttpRequest httpRequest = null;
    private String onRequestThreadname = null;
   
    private int countOnRequestTimeout = 0;
    private String onRequestTimeoutThreadname = null;

   
    public boolean onConnect(IHttpConnection httpConnection) throws IOException {
      countOnConnect++;
      onConnectThreadname = Thread.currentThread().getName();
     
      return true;
    }

   
    public boolean onDisconnect(IHttpConnection httpConnection) throws IOException {
      countOnDisconnect++;
      onDisconnectThreadname = Thread.currentThread().getName();

      return true;
    }
   
    public void onRequest(final IHttpExchange exchange) throws IOException {
      httpRequest = exchange.getRequest();
      onRequestThreadname = Thread.currentThread().getName();
     
     
      IHttpResponseHandler respHdl = new IHttpResponseHandler() {
       
        public void onResponse(IHttpResponse response) throws IOException {
          exchange.send(response);
        }
       
        public void onException(IOException ioe) {
         
        }
      };
     
      exchange.forward(exchange.getRequest(), respHdl);
    }

    public boolean onRequestTimeout(IHttpConnection connection) throws IOException {
      countOnRequestTimeout++;
      onRequestTimeoutThreadname = Thread.currentThread().getName();
     
      return true;
    }
   
   
    int getCountOnConnect() {
      return countOnConnect;
    }
   
    int getCountOnDisconnect() {
      return countOnDisconnect;
    }
   
    int getCountOnRequestTimeout() {
      return countOnRequestTimeout;
    }
   
    IHttpRequest getRequest() {
      return httpRequest;
    }
   
    String getOnConnectThreadname() {
      return onConnectThreadname;
    }
   
    String getOnDisconnectThreadname() {
      return onDisconnectThreadname;
    }
   
    String getOnRequestThreadname() {
      return onRequestThreadname;
    }
   
    String getOnRequestTimeoutThreadname() {
      return onRequestTimeoutThreadname;
    }
  }
 
 
  private static final class MyBusinessHandler implements IHttpRequestHandler {
   
    public void onRequest(IHttpExchange exchange) throws IOException, BadMessageException {
      exchange.send(new HttpResponse(200, "text/plain", "[12, 23, 456, 67]"));
    }
  }
 
 
  @Mapping("/rpc/*")
  public static final class AnnotatedHandler implements IHttpRequestHandler {
    public void onRequest(IHttpExchange exchange) throws IOException, BadMessageException {
      exchange.send(new HttpResponse(200, "text/plain", "[12, 23, 456, 67]"));
    }
  }
 

 
  private static final class TestServlet extends HttpServlet {
   
    private static final long serialVersionUID = -1757998262121404672L;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

      String ctxPath = req.getContextPath();
      String srvPath = req.getServletPath();
     
      resp.getWriter().println("contextPath=" + ctxPath);
      resp.getWriter().println("servletPath=" + srvPath);
    }
   
  }

 
 
  private static final class TestRequestHandler implements IHttpRequestHandler {
   

    public void onRequest(IHttpExchange exchange) throws IOException, BadMessageException {

      IHttpRequest req = exchange.getRequest();
      String ctxPath = req.getContextPath();
      String srvPath = req.getRequestHandlerPath();
     
      StringBuilder sb = new StringBuilder();
      sb.append("contextPath=" + ctxPath + "\r\n");
      sb.append("servletPath=" + srvPath + "\r\n");
     
      exchange.send(new HttpResponse(sb.toString()));
    }
   
  }
}
TOP

Related Classes of org.xlightweb.ContextTest

TOP
Copyright © 2018 www.massapi.com. 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.