Skip to content

Commit

Permalink
Support Vert.x 3
Browse files Browse the repository at this point in the history
Fixes #9
  • Loading branch information
flowersinthesand committed May 26, 2018
1 parent 9a7b2a4 commit 481d142
Show file tree
Hide file tree
Showing 10 changed files with 528 additions and 2 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Asity is a lightweight abstraction layer for web frameworks, which is designed t
```java
Action<ServerHttpExchange> httpAction = (ServerHttpExchange http) -> {
// Request properties
System.out.println(http.method() + " " + http.uri());
System.out.println(http.method() + " " + http.uri());
http.headerNames().stream().forEach(name -> System.out.println(name + ": " + http.header(name)));

// Sets 200 OK response status
Expand Down Expand Up @@ -49,6 +49,7 @@ Action<ServerHttpExchange> httpAction = (ServerHttpExchange http) -> {
Action<ServerWebSocket> wsAction = (ServerWebSocket ws) -> {
// Handshake request properties
System.out.println(HttpMethod.GET + " " + ws.uri());
ws.headerNames().stream().forEach(name -> System.out.println(name + ": " + ws.header(name)));

// When a text frame is arrived, sends it back
ws.ontext((String data) -> ws.send(data));
Expand All @@ -74,7 +75,7 @@ Action<ServerWebSocket> wsAction = (ServerWebSocket ws) -> {
* Java WebSocket API 1
* Netty 4
* Spring WebFlux 5
* Vert.x 2
* Vert.x 2 and 3

For details of how to set up a bridge module, check tests of each bridge module.

Expand Down
67 changes: 67 additions & 0 deletions bridge-vertx3/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 2018 the original author or authors.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.cettia.asity</groupId>
<artifactId>asity-parent</artifactId>
<version>2.0.0-Alpha2-SNAPSHOT</version>
</parent>
<artifactId>asity-bridge-vertx3</artifactId>
<name>Asity/Bridge/Vert.x 3</name>
<dependencies>
<dependency>
<groupId>io.cettia.asity</groupId>
<artifactId>asity-action</artifactId>
</dependency>
<dependency>
<groupId>io.cettia.asity</groupId>
<artifactId>asity-http</artifactId>
</dependency>
<dependency>
<groupId>io.cettia.asity</groupId>
<artifactId>asity-websocket</artifactId>
</dependency>
<dependency>
<groupId>io.cettia.asity</groupId>
<artifactId>asity-test</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-core</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-codec-http</artifactId>
<version>4.1.24.Final</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright 2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.cettia.asity.bridge.vertx3;

import io.cettia.asity.action.Action;
import io.cettia.asity.action.Actions;
import io.cettia.asity.action.ConcurrentActions;
import io.cettia.asity.http.ServerHttpExchange;
import io.vertx.core.Handler;
import io.vertx.core.http.HttpServerRequest;

/**
* <code>Handler&lt;HttpServerRequest&gt;</code> to provide {@link VertxServerHttpExchange}.
* <p/>
* <pre>
* AsityRequestHandler requestHandler = new AsityRequestHandler().onhttp(http -&gt; {});
* httpServer.requestHandler(request -> {
* if (request.path().equals("/cettia")) {
* requestHandler.handle(request);
* }
* });
* </pre>
* Or
* <p/>
* <pre>
* AsityRequestHandler requestHandler = new AsityRequestHandler().onhttp(http -&gt; {});
* Router router = Router.router(vertx);
* router.route("/cettia").handler(rc -&gt; requestHandler.handle(rc.request()));
* </pre>
*
* @author Donghwan Kim
*/
public class AsityRequestHandler implements Handler<HttpServerRequest> {

private Actions<ServerHttpExchange> httpActions = new ConcurrentActions<>();

@Override
public void handle(HttpServerRequest request) {
httpActions.fire(new VertxServerHttpExchange(request));
}

/**
* Registers an action to be called when {@link ServerHttpExchange} is available.
*/
public AsityRequestHandler onhttp(Action<ServerHttpExchange> action) {
httpActions.add(action);
return this;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright 2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.cettia.asity.bridge.vertx3;

import io.cettia.asity.action.Action;
import io.cettia.asity.action.Actions;
import io.cettia.asity.action.ConcurrentActions;
import io.cettia.asity.websocket.ServerWebSocket;
import io.vertx.core.Handler;

/**
* <code>Handler&lt;io.vertx.core.http.ServerWebSocket&gt;</code> to provide {@link VertxServerWebSocket}.
* <p/>
* <pre>
* AsityWebSocketHandler websocketHandler = new AsityWebSocketHandler().onhttp(http -&gt; {});
* httpServer.websocketHandler(socket -> {
* if (socket.path().equals("/cettia")) {
* websocketHandler.handle(socket);
* }
* });
* </pre>
*
* @author Donghwan Kim
*/
public class AsityWebSocketHandler implements Handler<io.vertx.core.http.ServerWebSocket> {

private Actions<ServerWebSocket> wsActions = new ConcurrentActions<>();

@Override
public void handle(io.vertx.core.http.ServerWebSocket ws) {
wsActions.fire(new VertxServerWebSocket(ws));
}

/**
* Registers an action to be called when {@link ServerWebSocket} is available.
*/
public AsityWebSocketHandler onwebsocket(Action<ServerWebSocket> action) {
wsActions.add(action);
return this;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* Copyright 2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.cettia.asity.bridge.vertx3;

import io.cettia.asity.action.Action;
import io.cettia.asity.http.AbstractServerHttpExchange;
import io.cettia.asity.http.HttpMethod;
import io.cettia.asity.http.HttpStatus;
import io.cettia.asity.http.ServerHttpExchange;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.HttpServerRequest;
import io.vertx.core.http.HttpServerResponse;

import java.nio.ByteBuffer;
import java.util.List;
import java.util.Set;

/**
* {@link ServerHttpExchange} for Vert.x 3.
*
* @author Donghwan Kim
*/
public class VertxServerHttpExchange extends AbstractServerHttpExchange {

private final HttpServerRequest request;
private final HttpServerResponse response;

public VertxServerHttpExchange(HttpServerRequest request) {
this.request = request;
this.response = request.response();
request.exceptionHandler(errorActions::fire);
response.exceptionHandler(errorActions::fire).closeHandler(closeActions::fire).setChunked(true);
}

@Override
public String uri() {
return request.uri();
}

@Override
public HttpMethod method() {
return HttpMethod.valueOf(request.rawMethod());
}

@Override
public Set<String> headerNames() {
return request.headers().names();
}

@Override
public List<String> headers(String name) {
return request.headers().getAll(name);
}

@Override
protected void doRead(final Action<ByteBuffer> chunkAction) {
request.handler(chunk -> chunkAction.on(chunk.getByteBuf().nioBuffer())).endHandler(endActions::fire);
}

@Override
protected void doSetStatus(HttpStatus status) {
response.setStatusCode(status.code()).setStatusMessage(status.reason());
}

@Override
protected void doSetHeader(String name, String value) {
response.putHeader(name, value);
}

@Override
protected void doWrite(ByteBuffer byteBuffer) {
response.write(Buffer.buffer().setBytes(0, byteBuffer));
}

@Override
protected void doEnd() {
response.end();
}

/**
* {@link HttpServerRequest} is available.
*/
@Override
public <T> T unwrap(Class<T> clazz) {
return HttpServerRequest.class.isAssignableFrom(clazz) ? clazz.cast(request) : null;
}

}
Loading

0 comments on commit 481d142

Please sign in to comment.