Package com.vtence.molecule

Examples of com.vtence.molecule.Application


    }

    @Test public void
    leavesResponseUnchangedWhenCacheValidatorsAreMissing() throws Exception {
        conditional.connectTo(new Application() {
            public void handle(Request request, Response response) throws Exception {
                response.body("response content");
            }
        });
View Full Code Here


        response.assertBody("response content");
    }

    @Test public void
    ignoresCacheValidatorsIfResponseNotOK() throws Exception {
        conditional.connectTo(new Application() {
            public void handle(Request request, Response response) throws Exception {
                response.status(CREATED).set("ETag", "12345678");
            }
        });
View Full Code Here

        response.assertStatus(CREATED);
    }

    @Test public void
    supportsHeadRequests() throws Exception {
        conditional.connectTo(new Application() {
            public void handle(Request request, Response response) throws Exception {
                response.set("ETag", "12345678");
            }
        });
View Full Code Here

        response.assertStatus(NOT_MODIFIED);
    }

    @Test public void
    ignoresNonGetOrHeadRequests() throws Exception {
        conditional.connectTo(new Application() {
            public void handle(Request request, Response response) throws Exception {
                response.set("ETag", "12345678");
            }
        });
View Full Code Here

    }

    @Test public void
    sendsNotModifiedWhenEntityHasNotBeenModifiedSinceLastServed() throws Exception {
        final String lastModification = httpDate(now().toDate());
        conditional.connectTo(new Application() {
            public void handle(Request request, Response response) throws Exception {
                response.set("Last-Modified", lastModification);
            }
        });
View Full Code Here

    }

    @Test public void
    leavesResponseUnchangedWhenEntityHasNotBeenModifiedButETagIndicatesItIsNotCurrent() throws Exception {
        final String lastModification = httpDate(aDate().toDate());
        conditional.connectTo(new Application() {
            public void handle(Request request, Response response) throws Exception {
                response.set("ETag", "12345678").set("Last-Modified", lastModification);
            }
        });
View Full Code Here

        response.assertStatus(OK);
    }

    @Test public void
    leavesResponseUnchangedWhenEntityWasModifiedButETagIndicatesItIsCurrent() throws Exception {
        conditional.connectTo(new Application() {
            public void handle(Request request, Response response) throws Exception {
                response.set("ETag", "12345678")
                        .set("Last-Modified", httpDate(now().toDate()));
            }
        });
View Full Code Here

public class RoutingExample {

    public void run(WebServer server) throws IOException {
        server.start(new DynamicRoutes() {{

            map("/").to(new Application() {
                public void handle(Request request, Response response) throws Exception {
                    response.body("Welcome!");
                }
            });

            post("/login").to(new Application() {
                public void handle(Request request, Response response) throws Exception {
                    String username = request.parameter("username");
                    response.redirectTo("/users/" + username);
                }
            });

            get("/hello/:username").to(new Application() {
                public void handle(Request request, Response response) throws Exception {
                    response.contentType("text/html");
                    response.body(
                            "<html>" +
                            "<body>" +
View Full Code Here

        final Template greeting = templates.named("greeting");

        // Apply a common layout to all rendered pages
        server.filter("/", Layout.html(layout))
              .start(new DynamicRoutes() {{
                  get("/hello").to(new Application() {
                      public void handle(final Request request, final Response response) throws Exception {
                          response.contentType("text/html; charset=utf-8");
                          String name = request.parameter("name") != null ? request.parameter("name") : "World";
                          // Mustache can use any object or a Map as a rendering context
                          response.body(greeting.render(new User(name)));
View Full Code Here

        final Map<Integer, Album> albums = new TreeMap<Integer, Album>();
        final Sequence sequence = new Sequence();

        server.start((new DynamicRoutes() {{
            get("/albums").to(new Application() {
                public void handle(Request request, Response response) throws Exception {
                    TextBody body = new TextBody();
                    for (int id : albums.keySet()) {
                        Album album = albums.get(id);
                        body.append(String.format("%d: %s\n", id, album.info()));
                    }
                    if (body.text().isEmpty()) {
                        body.append("Your music library is empty");
                    }
                    response.body(body);
                }
            });

            post("/albums").to(new Application() {
                public void handle(Request request, Response response) throws Exception {
                    int id = sequence.next();
                    Album album = new Album(request.parameter("title"), request.parameter("artist"));
                    albums.put(id, album);
                    response.statusCode(201);
                    response.body(album.info());
                }
            });

            get("/albums/:id").to(new Application() {
                public void handle(Request request, Response response) throws Exception {
                    int id = Integer.parseInt(request.parameter("id"));
                    if (albums.containsKey(id)) {
                        Album album = albums.get(id);
                        response.body(album.info());
                    } else {
                        response.statusCode(404);
                        response.body("No such album");
                    }
                }
            });

            // Access with either a PUT or a POST with _method=PUT
            put("/albums/:id").to(new Application() {
                public void handle(Request request, Response response) throws Exception {
                    int id = Integer.parseInt(request.parameter("id"));
                    Album album = albums.get(id);
                    if (album != null) {
                        String title = request.parameter("title");
                        if (title != null) album.title = title;
                        String artist = request.parameter("artist");
                        if (artist != null) album.artist = artist;
                        response.body(album.info());
                    } else {
                        response.statusCode(404);
                        response.body("No such album");
                    }
                }
            });

            // Access with either a DELETE or a POST with _method=DELETE
            delete("/albums/:id").to(new Application() {
                public void handle(Request request, Response response) throws Exception {
                    int id = Integer.parseInt(request.parameter("id"));
                    Album album = albums.remove(id);
                    if (album != null) {
                        response.body(album.info());
View Full Code Here

TOP

Related Classes of com.vtence.molecule.Application

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.