-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
10 changed files
with
528 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
63 changes: 63 additions & 0 deletions
63
bridge-vertx3/src/main/java/io/cettia/asity/bridge/vertx3/AsityRequestHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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<HttpServerRequest></code> to provide {@link VertxServerHttpExchange}. | ||
* <p/> | ||
* <pre> | ||
* AsityRequestHandler requestHandler = new AsityRequestHandler().onhttp(http -> {}); | ||
* httpServer.requestHandler(request -> { | ||
* if (request.path().equals("/cettia")) { | ||
* requestHandler.handle(request); | ||
* } | ||
* }); | ||
* </pre> | ||
* Or | ||
* <p/> | ||
* <pre> | ||
* AsityRequestHandler requestHandler = new AsityRequestHandler().onhttp(http -> {}); | ||
* Router router = Router.router(vertx); | ||
* router.route("/cettia").handler(rc -> 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; | ||
} | ||
|
||
} |
55 changes: 55 additions & 0 deletions
55
bridge-vertx3/src/main/java/io/cettia/asity/bridge/vertx3/AsityWebSocketHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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<io.vertx.core.http.ServerWebSocket></code> to provide {@link VertxServerWebSocket}. | ||
* <p/> | ||
* <pre> | ||
* AsityWebSocketHandler websocketHandler = new AsityWebSocketHandler().onhttp(http -> {}); | ||
* 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; | ||
} | ||
|
||
} |
101 changes: 101 additions & 0 deletions
101
bridge-vertx3/src/main/java/io/cettia/asity/bridge/vertx3/VertxServerHttpExchange.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
Oops, something went wrong.