Skip to content

Commit 21455cd

Browse files
committed
Added virtual host example
1 parent 95c4d44 commit 21455cd

3 files changed

Lines changed: 72 additions & 0 deletions

File tree

stubbornjava-common/src/main/java/com/stubbornjava/common/undertow/Exchange.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,10 @@ public static interface PathParamImpl extends PathParams {};
3636
public static PathParamImpl pathParams() {
3737
return PATHPARAMS;
3838
}
39+
40+
public static interface UrlImpl extends Urls {};
41+
private static final UrlImpl URLS = new UrlImpl(){};
42+
public static UrlImpl urls() {
43+
return URLS;
44+
}
3945
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.stubbornjava.common.undertow;
2+
import io.undertow.server.HttpServerExchange;
3+
import okhttp3.HttpUrl;
4+
5+
public interface Urls {
6+
7+
// {{start:currentUrl}}
8+
default HttpUrl currentUrl(HttpServerExchange exchange) {
9+
HttpUrl.Builder urlBuilder = HttpUrl.parse(exchange.getRequestURL()).newBuilder();
10+
11+
if (!"".equals(exchange.getQueryString())) {
12+
urlBuilder = urlBuilder.encodedQuery(exchange.getQueryString());
13+
}
14+
return urlBuilder.build();
15+
}
16+
// {{end:currentUrl}}
17+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.stubbornjava.examples.undertow.redirects;
2+
3+
import com.stubbornjava.common.undertow.Exchange;
4+
import com.stubbornjava.common.undertow.SimpleServer;
5+
import com.stubbornjava.examples.undertow.routing.RoutingHandlers;
6+
7+
import io.undertow.Handlers;
8+
import io.undertow.server.HttpHandler;
9+
import io.undertow.server.HttpServerExchange;
10+
import io.undertow.server.RoutingHandler;
11+
import io.undertow.server.handlers.NameVirtualHostHandler;
12+
import okhttp3.HttpUrl;
13+
14+
public class VirtualHostServer {
15+
16+
// {{start:routes}}
17+
// Simple hello world
18+
public static void hello(HttpServerExchange exchange) {
19+
Exchange.body().sendText(exchange, "Hello World");
20+
}
21+
22+
/*
23+
* Using our utility to get the current url redirect
24+
* to the given host keeping paths and query strings.
25+
*/
26+
public static HttpHandler redirectToHost(String host) {
27+
return exchange -> {
28+
HttpUrl url = Exchange.urls().currentUrl(exchange);
29+
Exchange.redirect().permanent(exchange, url.newBuilder().host(host).build().toString());
30+
};
31+
}
32+
33+
private static final HttpHandler ROUTES = new RoutingHandler()
34+
.get("/hello", RedirectServer::hello)
35+
.setFallbackHandler(RoutingHandlers::notFoundHandler)
36+
;
37+
// {{end:routes}}
38+
39+
40+
// {{start:server}}
41+
public static void main(String[] args) {
42+
NameVirtualHostHandler handler = Handlers.virtualHost()
43+
.addHost("localhost", VirtualHostServer.redirectToHost("www.localhost"))
44+
.addHost("www.localhost", ROUTES);
45+
SimpleServer server = SimpleServer.simpleServer(handler);
46+
server.start();
47+
}
48+
// {{end:server}}
49+
}

0 commit comments

Comments
 (0)