Skip to content

Commit

Permalink
Introduce Result
Browse files Browse the repository at this point in the history
  • Loading branch information
Pil0tXia committed Apr 5, 2024
1 parent 195cb9d commit dbc32a0
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,43 +18,56 @@
package org.apache.eventmesh.runtime.admin.handler;

import org.apache.eventmesh.common.enums.HttpMethod;
import org.apache.eventmesh.runtime.admin.response.Result;
import org.apache.eventmesh.runtime.constants.EventMeshConstants;
import org.apache.eventmesh.runtime.util.HttpResponseUtils;

import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.DefaultHttpHeaders;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpHeaderValues;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpResponse;
import io.netty.handler.codec.http.HttpResponseStatus;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONWriter;

import lombok.Data;

@Data
public abstract class AbstractHttpHandler implements HttpHandler {

protected void writeText(ChannelHandlerContext ctx, String result) {
HttpHeaders responseHeaders = new DefaultHttpHeaders();
responseHeaders.add(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.TEXT_HTML);
responseHeaders.add(EventMeshConstants.HANDLER_ORIGIN, "*");
write(ctx, HttpResponseUtils.buildHttpResponse(result, ctx, responseHeaders, HttpResponseStatus.OK));
protected void writeText(ChannelHandlerContext ctx, String text) {
HttpHeaders responseHeaders = HttpResponseUtils.buildDefaultHttpHeaders(HttpHeaderValues.TEXT_HTML);
write(ctx, HttpResponseUtils.buildHttpResponse(text, ctx, responseHeaders, HttpResponseStatus.OK));
}

protected void writeJson(ChannelHandlerContext ctx, String result) {
HttpHeaders responseHeaders = new DefaultHttpHeaders();
responseHeaders.add(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.APPLICATION_JSON);
responseHeaders.add(EventMeshConstants.HANDLER_ORIGIN, "*");
write(ctx, HttpResponseUtils.buildHttpResponse(result, ctx, responseHeaders, HttpResponseStatus.OK));
protected void writeJson(ChannelHandlerContext ctx, String json) {
HttpHeaders responseHeaders = HttpResponseUtils.buildDefaultHttpHeaders(HttpHeaderValues.APPLICATION_JSON);
write(ctx, HttpResponseUtils.buildHttpResponse(json, ctx, responseHeaders, HttpResponseStatus.OK));
}

protected void writeUnauthorized(ChannelHandlerContext ctx, String result) {
HttpHeaders responseHeaders = new DefaultHttpHeaders();
responseHeaders.add(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.APPLICATION_JSON);
responseHeaders.add(EventMeshConstants.HANDLER_ORIGIN, "*");
write(ctx, HttpResponseUtils.buildHttpResponse(result, ctx, responseHeaders, HttpResponseStatus.UNAUTHORIZED));
protected void writeSuccess(ChannelHandlerContext ctx, Object data) {
HttpHeaders responseHeaders = HttpResponseUtils.buildDefaultHttpHeaders(HttpHeaderValues.APPLICATION_JSON);
Result<Object> result = Result.success(data);
String json = JSON.toJSONString(result, JSONWriter.Feature.WriteNulls);
write(ctx, HttpResponseUtils.buildHttpResponse(json, ctx, responseHeaders, HttpResponseStatus.OK));
}

protected void writeBadRequest(ChannelHandlerContext ctx, String message) {
HttpHeaders responseHeaders = HttpResponseUtils.buildDefaultHttpHeaders(HttpHeaderValues.APPLICATION_JSON);
Result<String> result = new Result<>(message);
String json = JSON.toJSONString(result, JSONWriter.Feature.WriteNulls);
write(ctx, HttpResponseUtils.buildHttpResponse(json, ctx, responseHeaders, HttpResponseStatus.BAD_REQUEST));
}

protected void writeUnauthorized(ChannelHandlerContext ctx, String message) {
HttpHeaders responseHeaders = HttpResponseUtils.buildDefaultHttpHeaders(HttpHeaderValues.APPLICATION_JSON);
Result<String> result = new Result<>(message);
String json = JSON.toJSONString(result, JSONWriter.Feature.WriteNulls);
write(ctx, HttpResponseUtils.buildHttpResponse(json, ctx, responseHeaders, HttpResponseStatus.UNAUTHORIZED));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,6 @@ protected void post(HttpRequest httpRequest, ChannelHandlerContext ctx) throws E
.getInstance()
.resolveFormat(JsonFormat.CONTENT_TYPE)).deserialize(rawRequest);
admin.publish(event);
writeJson(ctx, "");
writeText(ctx, "");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ protected void delete(HttpRequest httpRequest, ChannelHandlerContext ctx) throws
}
}
}
writeJson(ctx, "");
writeText(ctx, "");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ protected void delete(HttpRequest httpRequest, ChannelHandlerContext ctx) throws
clientList.removeIf(client -> Objects.equals(client.getUrl(), url));
}
// Set the response headers and send a 200 status code empty response
writeJson(ctx, "");
writeText(ctx, "");
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ protected void delete(HttpRequest httpRequest, ChannelHandlerContext ctx) throws
}

// Set the response headers and send a 200 status code empty response
writeJson(ctx, "");
writeText(ctx, "");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ protected void post(HttpRequest httpRequest, ChannelHandlerContext ctx) throws E
CreateTopicRequest createTopicRequest = JsonUtils.mapToObject(body, CreateTopicRequest.class);
String topicName = Objects.requireNonNull(createTopicRequest).getName();
admin.createTopic(topicName);
writeJson(ctx, "");
writeText(ctx, "");
}

@Override
Expand All @@ -103,6 +103,6 @@ protected void delete(HttpRequest httpRequest, ChannelHandlerContext ctx) throws
DeleteTopicRequest deleteTopicRequest = JsonUtils.mapToObject(body, DeleteTopicRequest.class);
String topicName = Objects.requireNonNull(deleteTopicRequest).getName();
admin.deleteTopic(topicName);
writeJson(ctx, "");
writeText(ctx, "");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.apache.eventmesh.common.config.Config;
import org.apache.eventmesh.common.config.ConfigField;
import org.apache.eventmesh.runtime.admin.handler.AbstractHttpHandler;
import org.apache.eventmesh.runtime.admin.response.Result;
import org.apache.eventmesh.runtime.admin.response.v2.GetConfigurationResponse;
import org.apache.eventmesh.runtime.common.EventMeshHttpHandler;
import org.apache.eventmesh.runtime.configuration.EventMeshGrpcConfiguration;
Expand Down Expand Up @@ -97,7 +98,7 @@ protected void get(HttpRequest httpRequest, ChannelHandlerContext ctx) {
filters = new Filter[] {new IPAddressToStringFilter()};
} else {
log.warn("Invalid format param: {}", format);
writeJson(ctx, "Invalid format param: " + format);
writeBadRequest(ctx, "Invalid format param: " + format);
return;
}

Expand All @@ -106,8 +107,8 @@ protected void get(HttpRequest httpRequest, ChannelHandlerContext ctx) {
eventMeshHTTPConfiguration,
eventMeshGrpcConfiguration
);
String result = JSON.toJSONString(getConfigurationResponse, filters);
writeJson(ctx, result);
String json = JSON.toJSONString(Result.success(getConfigurationResponse), filters);
writeJson(ctx, json);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,35 @@

package org.apache.eventmesh.runtime.admin.response;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
* A RESTful response DTO.
*/

@Data
public class Error {
@NoArgsConstructor
@AllArgsConstructor
public class Result<T> {

private String message;
private T data;

private String stackTrace;
private String message;

@JsonCreator
public Error(@JsonProperty("message") String message, @JsonProperty("stackTrace") String stackTrace) {
super();
public Result(String message) {
this.message = message;
this.stackTrace = stackTrace;
}

/**
* The request is valid and the result is wrapped in {@link Result}.
*/
public static <T> Result<T> success() {
return new Result<>("Success");
}

public static <T> Result<T> success(T data) {
return new Result<>(data, "Success");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,18 @@ public static Map<String, String> queryStringToMap(String queryString) {
return result;
}

/**
* Get the value of a query parameter in URI query string.
*/
public static String getQueryParam(HttpRequest httpRequest, String key, String defaultValue) {
List<String> values = new QueryStringDecoder(httpRequest.uri()).parameters().get(key);
return values != null ? values.get(0) : defaultValue;
}

public static String getPostParam(HttpRequest httpRequest, String key) throws IOException {
/**
* Get the value of a query parameter in body.
*/
public static String getBodyParam(HttpRequest httpRequest, String key) throws IOException {
HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(httpRequest);
Attribute attribute = (Attribute) decoder.getBodyHttpData(key);
return attribute.getValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.eventmesh.runtime.util;

import org.apache.eventmesh.common.Constants;
import org.apache.eventmesh.runtime.constants.EventMeshConstants;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
Expand Down Expand Up @@ -66,6 +67,13 @@ public static HttpResponse buildHttpResponse(String body, ChannelHandlerContext
return new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status, createByteBuf(ctx, body), responseHeaders, responseHeaders);
}

public static HttpHeaders buildDefaultHttpHeaders(AsciiString contentType) {
HttpHeaders responseHeaders = new DefaultHttpHeaders();
responseHeaders.add(HttpHeaderNames.CONTENT_TYPE, contentType);
responseHeaders.add(EventMeshConstants.HANDLER_ORIGIN, "*");
return responseHeaders;
}

private static ByteBuf createByteBuf(ChannelHandlerContext ctx, String body) {
byte[] bytes = body.getBytes(Constants.DEFAULT_CHARSET);
ByteBuf byteBuf = ctx.alloc().buffer(bytes.length);
Expand Down

0 comments on commit dbc32a0

Please sign in to comment.