Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* Copyright 2011-2014 the original author or authors.
*/
package com.jetdrone.vertx.yoke;
import com.jetdrone.vertx.yoke.core.impl.GroovyRequestWrapper;
import com.jetdrone.vertx.yoke.middleware.YokeRequest;
import com.jetdrone.vertx.yoke.store.SessionStore;
import groovy.lang.Closure;
import org.jetbrains.annotations.NotNull;
import org.vertx.groovy.platform.Container;
import org.vertx.groovy.platform.Verticle;
import org.vertx.java.core.Handler;
import org.vertx.groovy.core.Vertx;
import org.vertx.groovy.core.http.HttpServer;
import org.vertx.java.core.json.JsonObject;
import java.util.Collections;
import java.util.List;
import java.util.Map;
/**
* Yoke is a chain executor of middleware for Vert.x 2.x.
* The goal of this library is not to provide a web application framework but
* the backbone that helps the creation of web applications.
*
* Yoke works in a similar way to Connect middleware. Users start by declaring
* which middleware components want to use and then start an http server either
* managed by Yoke or provided by the user (say when you need https).
*
* Yoke has no extra dependencies than Vert.x itself so it is self contained.
*/
public class GYoke {
private final Yoke jYoke;
private final org.vertx.java.core.Vertx vertx;
private final Vertx gVertx;
private final Container container;
/**
* Creates a Yoke instance.
*
* This constructor should be called from a verticle and pass a valid Vertx instance. This instance will be shared
* with all registered middleware. The reason behind this is to allow middleware to use Vertx features such as file
* system and timers.
*
*
* public class MyVerticle extends Verticle {
* public void start() {
* def yoke = new Yoke(this)
* ...
* }
* }
*
* @param verticle the main verticle
*/
public GYoke(Verticle verticle) {
this(verticle.getVertx(), verticle.getContainer());
}
/**
* Creates a Yoke instance.
* This constructor should be called from a verticle and pass a valid Vertx
* instance. This instance will be shared with all registered middleware.
* The reason behind this is to allow middleware to use Vertx features such
* as file system and timers.
*
* @param vertx The Vertx instance
*/
public GYoke(Vertx vertx, Container container) {
this.gVertx = vertx;
this.vertx = vertx.toJavaVertx();
this.container = container;
jYoke = new Yoke(this.vertx, null, new GroovyRequestWrapper());
}
public GYoke store(SessionStore store) {
jYoke.store(store);
return this;
}
/**
* Adds a Middleware to the chain. If the middleware is an Error Handler Middleware then it is
* treated differently and only the last error handler is kept.
*
* You might want to add a middleware that is only supposed to run on a specific route (path prefix).
* In this case if the request path does not match the prefix the middleware is skipped automatically.
*
* @param route The route prefix for the middleware
* @param closure The closure add to the chain
*/
public GYoke use(String route, final Closure closure) {
final int params = closure.getMaximumNumberOfParameters();
jYoke.use(route, new Middleware() {
@Override
public void handle(@NotNull YokeRequest request, @NotNull Handler