Package org.webbitserver

Examples of org.webbitserver.HttpHandler


import java.util.concurrent.ExecutionException;

public class WebbitServer {
  public static void main(String[] args) throws ExecutionException, InterruptedException {
    final RedisClient redis = RedisClient.connect("localhost", 6379).get();
    WebServers.createWebServer(8080).add(new HttpHandler() {
      public void handleHttpRequest(final HttpRequest request, final HttpResponse response, final HttpControl control) throws Exception {
        System.out.println(request.uri());
        redis.get(request.uri()).onSuccess(new Block<BulkReply>() {
          public void apply(final BulkReply bulkReply) {
            control.execute(new Runnable() {
View Full Code Here


    public void nextHandler(HttpRequest request, HttpResponse response, HttpControl control) {
        this.defaultRequest = request;
        this.webbitHttpResponse = response;
        this.defaultControl = control;
        if (handlerIterator.hasNext()) {
            HttpHandler handler = handlerIterator.next();
            try {
                handler.handleHttpRequest(request, response, control);
            } catch (Throwable e) {
                response.error(e);
            }
        } else {
            response.status(404).end();
View Full Code Here

                } catch (UnsupportedEncodingException e) {
                    throw new RuntimeException();
                }
            }
        });
        webServer.add(new HttpHandler() {
            @Override
            public void handleHttpRequest(HttpRequest request, HttpResponse response, HttpControl control) throws Exception {
                request.data(TemplateEngine.TEMPLATE_CONTEXT, "THE CONTEXT");
                control.nextHandler();
            }
View Full Code Here

        webServer.stop().get();
    }

    @Test
    public void exposesBodyInRequest() throws IOException, InterruptedException, ExecutionException {
        webServer.add(new HttpHandler() {
            @Override
            public void handleHttpRequest(HttpRequest request, HttpResponse response, HttpControl control) throws Exception {
                response.content("Body = {" + request.body() + "}").end();
            }
        }).start().get();
View Full Code Here

        assertEquals("Body = {hello\n world}", result);
    }

    @Test
    public void exposesPostBodyAsParameters() throws IOException, InterruptedException, ExecutionException {
        webServer.add(new HttpHandler() {
            @Override
            public void handleHttpRequest(HttpRequest request, HttpResponse response, HttpControl control) throws Exception {
                response.content("a=" + request.postParam("a") + ", b=" + request.postParam("b")).end();
            }
        }).start().get();
View Full Code Here

    @Test
    public void stopsServerCleanlyAlsoWhenClientsAreConnected() throws Exception {
        final CountDownLatch stopper = new CountDownLatch(1);
        server = new NettyWebServer(Executors.newSingleThreadScheduledExecutor(), 9080).start().get();
        server.add(new HttpHandler() {
            @Override
            public void handleHttpRequest(HttpRequest request, HttpResponse response, HttpControl control) throws Exception {
                server.stop().get();
                stopper.countDown();
            }
View Full Code Here

        assertEquals("a=hello world, b=foo", result);
    }

    @Test
    public void exposesPostParamKeys() throws IOException, InterruptedException, ExecutionException {
        webServer.add(new HttpHandler() {
            @Override
            public void handleHttpRequest(HttpRequest request, HttpResponse response, HttpControl control) throws Exception {
                ArrayList<String> keysList = new ArrayList<String>(request.postParamKeys());
                Collections.sort(keysList);
View Full Code Here

        assertEquals("keys=[a, b, c]", result);
    }

    @Test
    public void exposesPostBodyAsBytes() throws IOException, ExecutionException, InterruptedException {
        webServer.add(new HttpHandler() {
            @Override
            public void handleHttpRequest(HttpRequest request, HttpResponse response, HttpControl control) throws Exception {
                response.content(Arrays.toString(request.bodyAsBytes())).end();
            }
        }).start().get();
View Full Code Here

        webServer.uncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
            @Override
            public void uncaughtException(Thread t, Throwable e) {
            }
        });
        webServer.add(new HttpHandler() {
            @Override
            public void handleHttpRequest(HttpRequest request, HttpResponse response, HttpControl control) throws Exception {
                response.error(new RuntimeException("Should never get here"));
            }
        }).start().get();
View Full Code Here

    }

    @Test
    public void max_content_length_can_be_increased() throws IOException, ExecutionException, InterruptedException {
        webServer.maxContentLength(65537);
        webServer.add(new HttpHandler() {
            @Override
            public void handleHttpRequest(HttpRequest request, HttpResponse response, HttpControl control) throws Exception {
                response.content("length:" + request.bodyAsBytes().length).end();
            }
        }).start().get();
View Full Code Here

TOP

Related Classes of org.webbitserver.HttpHandler

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.