Package com.vtence.molecule.lib

Examples of com.vtence.molecule.lib.Middleware


public class FilterMap extends AbstractMiddleware {

    private final Map<Matcher<? super Request>, Middleware> filters = new LinkedHashMap<Matcher<? super Request>, Middleware>();

    public void handle(Request request, Response response) throws Exception {
        Middleware filter = filterMappedTo(request);
        filter.connectTo(successor);
        filter.handle(request, response);
    }
View Full Code Here


        filter.connectTo(successor);
        filter.handle(request, response);
    }

    private Middleware filterMappedTo(Request request) {
        Middleware bestMatch = new PassThrough();
        for (Matcher<? super Request> requestMatcher : filters.keySet()) {
            if (requestMatcher.matches(request)) bestMatch = filters.get(requestMatcher);
        }
        return bestMatch;
    }
View Full Code Here

public class CustomMiddlewareExample {

    public void run(WebServer server) throws IOException {

        // An example of performing some work before handling control to the next or application
        Middleware getFirefox = new AbstractMiddleware() {
            public void handle(Request request, Response response) throws Exception {
                // Tell IE users to get Firefox
                String userAgent = request.header("User-Agent");
                if (userAgent != null && userAgent.contains("MSIE")) {
                    response.redirectTo("http://www.mozilla.org");
                } else {
                    // Hand over control to next application in the stack
                    forward(request, response);
                }
            }
        };

        // An example of performing additional work after getting control back
        // (there's already a middleware for that, btw)
        Middleware contentLengthHeader = new AbstractMiddleware() {
            public void handle(Request request, Response response) throws Exception {
                forward(request, response);
                // Set content length header on the response
                response.contentLength(response.size());
            }
View Full Code Here

        users.put("admin", "admin");
    }

    public void run(WebServer server) throws IOException {
        // An an authentication filter that checks against a map of authorized users
        Middleware authenticate = new AbstractMiddleware() {
            public void handle(Request request, Response response) throws Exception {
                String user = request.parameter("username");
                String password = request.parameter("password");

                String token = users.get(user);
View Full Code Here

TOP

Related Classes of com.vtence.molecule.lib.Middleware

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.