com.jetdrone.vertx.yoke.middleware.Vhost Maven / Gradle / Ivy
/**
* Copyright 2011-2014 the original author or authors.
*/
package com.jetdrone.vertx.yoke.middleware;
import com.jetdrone.vertx.yoke.Middleware;
import org.jetbrains.annotations.NotNull;
import org.vertx.java.core.Handler;
import org.vertx.java.core.http.HttpServerRequest;
import java.util.regex.Pattern;
/**
* # Vhost
*
* Setup vhost for the given *hostname* and *server*.
*/
public class Vhost extends Middleware {
private final Handler handler;
private final Pattern regex;
/**
* Create a new Vhost middleware. This middleware will verify the request hostname and if it matches it will send
* the request to the registered handler, otherwise will continue inside the middleware chain.
*
*
* new Yoke(...)
* .use(new Vhost("*.jetdrone.com", existingHttpServerObject))
*
*
* @param hostname
* @param handler
*/
public Vhost(@NotNull final String hostname, @NotNull final Handler handler) {
this.handler = handler;
this.regex = Pattern.compile("^" + hostname.replaceAll("\\.", "\\\\.").replaceAll("[*]", "(.*?)") + "$", Pattern.CASE_INSENSITIVE);
}
@Override
public void handle(@NotNull final YokeRequest request, @NotNull final Handler