Skip to content

Commit

Permalink
代码优化 (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
scx567888 authored Sep 17, 2024
1 parent fd9cf1f commit ad37787
Show file tree
Hide file tree
Showing 79 changed files with 595 additions and 214 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@
<version>${helidon.version}</version>
</dependency>

<dependency>
<groupId>io.helidon.webclient</groupId>
<artifactId>helidon-webclient-websocket</artifactId>
<version>${helidon.version}</version>
</dependency>

<!-- vert.x 核心包 -->
<dependency>
<groupId>io.vertx</groupId>
Expand Down
5 changes: 5 additions & 0 deletions scx-http-helidon/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@
<artifactId>helidon-webclient</artifactId>
</dependency>

<dependency>
<groupId>io.helidon.webclient</groupId>
<artifactId>helidon-webclient-websocket</artifactId>
</dependency>

<!-- ****************** 以下为测试依赖 ****************** -->

<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package cool.scx.http.helidon;

import cool.scx.http.ScxClientWebSocket;
import cool.scx.http.ScxServerWebSocket;
import io.helidon.common.buffers.BufferData;
import io.helidon.http.Headers;
import io.helidon.http.HttpPrologue;
import io.helidon.websocket.WsListener;
import io.helidon.websocket.WsSession;
import io.helidon.websocket.WsUpgradeException;

import java.util.Optional;
import java.util.function.Consumer;

//todo
public class HelidonClientWebSocket implements ScxClientWebSocket, WsListener {

@Override
public ScxServerWebSocket onTextMessage(Consumer<String> textMessageHandler) {
return null;
}

@Override
public ScxServerWebSocket onBinaryMessage(Consumer<byte[]> binaryMessageHandler) {
return null;
}

@Override
public ScxServerWebSocket onPing(Consumer<byte[]> pingHandler) {
return null;
}

@Override
public ScxServerWebSocket onPong(Consumer<byte[]> pongHandler) {
return null;
}

@Override
public ScxServerWebSocket onClose(Consumer<Integer> closeHandler) {
return null;
}

@Override
public ScxServerWebSocket onError(Consumer<Throwable> errorHandler) {
return null;
}

@Override
public ScxServerWebSocket send(String textMessage, boolean var2) {
return null;
}

@Override
public ScxServerWebSocket send(byte[] binaryMessage, boolean var2) {
return null;
}

@Override
public ScxServerWebSocket ping(byte[] data) {
return null;
}

@Override
public ScxServerWebSocket pong(byte[] data) {
return null;
}

@Override
public ScxServerWebSocket close(int var1, String var2) {
return null;
}

//*************** 方法 *************

@Override
public void onMessage(WsSession session, String text, boolean last) {
WsListener.super.onMessage(session, text, last);
}

@Override
public void onMessage(WsSession session, BufferData buffer, boolean last) {
WsListener.super.onMessage(session, buffer, last);
}

@Override
public void onPing(WsSession session, BufferData buffer) {
WsListener.super.onPing(session, buffer);
}

@Override
public void onPong(WsSession session, BufferData buffer) {
WsListener.super.onPong(session, buffer);
}

@Override
public void onClose(WsSession session, int status, String reason) {
WsListener.super.onClose(session, status, reason);
}

@Override
public void onError(WsSession session, Throwable t) {
WsListener.super.onError(session, t);
}

@Override
public void onOpen(WsSession session) {
WsListener.super.onOpen(session);
}

@Override
public Optional<Headers> onHttpUpgrade(HttpPrologue prologue, Headers headers) throws WsUpgradeException {
return WsListener.super.onHttpUpgrade(prologue, headers);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
import java.util.Arrays;
import java.util.function.Supplier;

/**
* HelidonHelper
*/
class HelidonHelper {

public static WsRouting.Builder createHelidonWebSocketRouting(HelidonHttpServer server) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

import java.io.InputStream;

/**
* HelidonHttpBody
*/
class HelidonHttpBody implements ScxHttpBody {

private final ReadableEntity content;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
package cool.scx.http.helidon;

import cool.scx.http.*;
import cool.scx.http.uri.ScxURI;
import io.helidon.http.HeaderNames;
import io.helidon.http.Method;
import io.helidon.webclient.api.WebClient;
import io.helidon.webclient.websocket.WsClient;

import java.io.IOException;
import java.net.URI;

//todo
public class HelidonHttpClient implements ScxHttpClient {

private final WebClient webClient;
private final WsClient wsClient;

public HelidonHttpClient(ScxHttpClientOptions options) {
this.webClient = WebClient.builder().build();
this.wsClient = WsClient.builder().build();
}

@Override
Expand All @@ -34,8 +38,10 @@ public ScxHttpClientResponse request(ScxHttpClientRequest request) throws IOExce
}

@Override
public ScxClientWebSocket webSocket(URI uri) {
throw new UnsupportedOperationException();
public ScxClientWebSocket webSocket(ScxURI uri) {
var clientWebSocket = new HelidonClientWebSocket();
wsClient.connect(uri.toString(), clientWebSocket);
return clientWebSocket;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
import io.helidon.http.Headers;

import java.util.*;
import java.util.stream.Collectors;

/**
* HelidonHttpHeaders
*/
class HelidonHttpHeaders<T extends Headers> implements ScxHttpHeaders {

protected final T headers;
Expand All @@ -15,6 +19,16 @@ public HelidonHttpHeaders(T headers) {
this.headers = headers;
}

@Override
public long size() {
return headers.size();
}

@Override
public Set<ScxHttpHeaderName> names() {
return headers.stream().map(c -> ScxHttpHeaderName.of(c.name())).collect(Collectors.toSet());
}

@Override
public String get(ScxHttpHeaderName headerName) {
var all = getAll(headerName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

import java.util.function.Supplier;

/**
* HelidonHttpRouting
*/
class HelidonHttpRouting implements HttpRouting, HttpRouting.Builder {

private final HelidonHttpServer server;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,37 @@
import cool.scx.http.ScxHttpServerOptions;
import cool.scx.http.ScxHttpServerRequest;
import cool.scx.http.ScxServerWebSocket;
import io.helidon.common.tls.Tls;
import io.helidon.webserver.WebServer;

import java.util.function.Consumer;

import static cool.scx.http.helidon.HelidonHelper.createHelidonWebSocketRouting;

/**
* HelidonHttpServer
*/
public class HelidonHttpServer implements ScxHttpServer {

private final WebServer webServer;
Consumer<ScxHttpServerRequest> requestHandler;
Consumer<ScxServerWebSocket> webSocketHandler;
Consumer<Throwable> exceptionHandler;
Consumer<Throwable> errorHandler;

public HelidonHttpServer(ScxHttpServerOptions options) {
var httpRouting = new HelidonHttpRouting(this);
var webSocketRouting = createHelidonWebSocketRouting(this);
this.webServer = WebServer.builder()
var builder = WebServer.builder()
.addRouting(httpRouting)
.addRouting(webSocketRouting)
.port(options.getPort())
.build();
.port(options.getPort());
if (options.getTLS() != null) {
builder.tls((Tls) options.getTLS());
}
this.webServer = builder.build();
this.requestHandler = null;
this.webSocketHandler = null;
this.errorHandler = null;
}

@Override
Expand All @@ -42,8 +50,8 @@ public ScxHttpServer webSocketHandler(Consumer<ScxServerWebSocket> handler) {
}

@Override
public ScxHttpServer exceptionHandler(Consumer<Throwable> handler) {
this.exceptionHandler = handler;
public ScxHttpServer errorHandler(Consumer<Throwable> handler) {
this.errorHandler = handler;
return this;
}

Expand All @@ -57,4 +65,9 @@ public void stop() {
this.webServer.stop();
}

@Override
public int port() {
return this.webServer.port();
}

}
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package cool.scx.http.helidon;

import cool.scx.http.*;
import cool.scx.http.uri.URI;
import cool.scx.http.uri.ScxURI;
import io.helidon.webserver.ConnectionContext;
import io.helidon.webserver.http.RoutingRequest;
import io.helidon.webserver.http.RoutingResponse;

/**
* HelidonHttpServerRequest
*/
class HelidonHttpServerRequest implements ScxHttpServerRequest {

private final ScxHttpMethod method;
private final URI uri;
private final ScxURI uri;
private final HttpVersion version;
private final ScxHttpHeaders headers;
private final ScxHttpBody body;
Expand All @@ -20,7 +23,7 @@ class HelidonHttpServerRequest implements ScxHttpServerRequest {
public HelidonHttpServerRequest(ConnectionContext ctx, RoutingRequest request, RoutingResponse response) {
var p = request.prologue();
this.method = ScxHttpMethod.of(p.method().text());
this.uri = URI.of().path(p.uriPath().path()).query(new HelidonURIQuery(p.query())).fragment(p.fragment().hasValue() ? p.fragment().value() : null);
this.uri = ScxURI.of().path(p.uriPath().path()).query(new HelidonURIQuery(p.query())).fragment(p.fragment().hasValue() ? p.fragment().value() : null);
this.version = HttpVersion.of(p.rawProtocol());
this.headers = new HelidonHttpHeaders<>(request.headers());
this.body = new HelidonHttpBody(request.content());
Expand All @@ -35,7 +38,7 @@ public ScxHttpMethod method() {
}

@Override
public URI uri() {
public ScxURI uri() {
return uri;
}

Expand Down
Loading

0 comments on commit ad37787

Please sign in to comment.