Skip to content

Commit 4a78045

Browse files
committed
resolve conflicts
1 parent d786cdd commit 4a78045

File tree

1 file changed

+23
-62
lines changed

1 file changed

+23
-62
lines changed

core/src/main/java/org/apache/seata/core/rpc/netty/http/HttpDispatchHandler.java

Lines changed: 23 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,7 @@
3939
import org.apache.seata.core.rpc.netty.NettyServerConfig;
4040
import org.slf4j.Logger;
4141
import org.slf4j.LoggerFactory;
42-
43-
import java.io.IOException;
44-
4542
import org.apache.seata.common.rpc.http.HttpContext;
46-
import org.slf4j.Logger;
47-
import org.slf4j.LoggerFactory;
4843

4944
import java.lang.reflect.Method;
5045
import java.util.concurrent.ExecutorService;
@@ -76,45 +71,36 @@ public class HttpDispatchHandler extends SimpleChannelInboundHandler<HttpRequest
7671
}
7772

7873
@Override
79-
protected void channelRead0(ChannelHandlerContext ctx, HttpRequest httpRequest) throws Exception {
74+
protected void channelRead0(ChannelHandlerContext ctx, HttpRequest httpRequest) {
8075
boolean keepAlive = HttpUtil.isKeepAlive(httpRequest) && httpRequest.protocolVersion().isKeepAliveDefault();
8176
try {
8277
httpHandlerThreads.execute(() -> {
8378
try {
8479
processHttpRequest(ctx, httpRequest, keepAlive);
80+
} catch (IllegalArgumentException e) {
81+
LOGGER.error("Illegal argument exception: {}", e.getMessage(), e);
82+
sendErrorResponse(ctx, HttpResponseStatus.BAD_REQUEST, false);
8583
} catch (Exception e) {
86-
LOGGER.error("HTTP request processing error", e);
87-
sendResponse(ctx, null, keepAlive);
84+
LOGGER.error("Exception occurred while processing HTTP request: {}", e.getMessage(), e);
85+
sendErrorResponse(ctx, HttpResponseStatus.INTERNAL_SERVER_ERROR, false);
8886
}
8987
});
9088
} catch (RejectedExecutionException e) {
91-
sendUnavailable(ctx, keepAlive);
92-
LOGGER.warn("HTTP thread pool is full, return 503 status code", e);
89+
sendErrorResponse(ctx, HttpResponseStatus.SERVICE_UNAVAILABLE, false);
90+
LOGGER.error("HTTP thread pool is full: {}", e.getMessage(), e);
9391
}
9492
}
9593

96-
97-
/*
98-
catch (IllegalArgumentException e) {
99-
keepAlive = false;
100-
LOGGER.error("Illegal argument exception: {}", e.getMessage(), e);
101-
response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.BAD_REQUEST,
102-
Unpooled.wrappedBuffer(Unpooled.EMPTY_BUFFER));
103-
} catch (Exception e) {
104-
keepAlive = false;
105-
LOGGER.error("Exception occurred while processing HTTP request: {}", e.getMessage(), e);
106-
response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.INTERNAL_SERVER_ERROR,
107-
Unpooled.wrappedBuffer(Unpooled.EMPTY_BUFFER));
108-
}
109-
*/
11094
private void processHttpRequest(ChannelHandlerContext ctx, HttpRequest httpRequest, boolean keepAlive) throws Exception {
11195
QueryStringDecoder queryStringDecoder = new QueryStringDecoder(httpRequest.uri());
11296
String path = queryStringDecoder.path();
11397
HttpInvocation httpInvocation = ControllerManager.getHttpInvocation(path);
98+
11499
if (httpInvocation == null) {
115-
sendNotFound(ctx, keepAlive);
100+
sendErrorResponse(ctx, HttpResponseStatus.NOT_FOUND, keepAlive);
116101
return;
117102
}
103+
118104
HttpContext httpContext = new HttpContext(httpRequest, ctx, keepAlive);
119105
ObjectNode requestDataNode = OBJECT_MAPPER.createObjectNode();
120106
requestDataNode.putIfAbsent("param", ParameterParser.convertParamMap(queryStringDecoder.parameters()));
@@ -149,55 +135,30 @@ private void processHttpRequest(ChannelHandlerContext ctx, HttpRequest httpReque
149135
return;
150136
}
151137

152-
if (requestDataNode.get("channel") == null) {
153-
return;
154-
}
155-
156138
sendResponse(ctx, result, keepAlive);
157139
}
158140

159-
private void sendResponse(ChannelHandlerContext ctx, Object result, boolean keepAlive) {
160-
try {
161-
FullHttpResponse response;
162-
if (result != null) {
163-
byte[] body = OBJECT_MAPPER.writeValueAsBytes(result);
164-
response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK,
165-
Unpooled.wrappedBuffer(body));
166-
response.headers().set(HttpHeaderNames.CONTENT_LENGTH, body.length);
167-
} else {
168-
response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK,
169-
Unpooled.wrappedBuffer(Unpooled.EMPTY_BUFFER));
170-
}
171-
if (!keepAlive) {
172-
ctx.writeAndFlush(response).addListeners(ChannelFutureListener.CLOSE);
173-
} else {
174-
ctx.writeAndFlush(response);
175-
}
176-
} catch (JsonProcessingException e) {
177-
LOGGER.error("Failed to serialize response", e);
178-
FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.INTERNAL_SERVER_ERROR,
141+
private void sendResponse(ChannelHandlerContext ctx, Object result, boolean keepAlive) throws JsonProcessingException {
142+
FullHttpResponse response;
143+
if (result != null) {
144+
byte[] body = OBJECT_MAPPER.writeValueAsBytes(result);
145+
response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK,
146+
Unpooled.wrappedBuffer(body));
147+
response.headers().set(HttpHeaderNames.CONTENT_LENGTH, body.length);
148+
} else {
149+
response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK,
179150
Unpooled.wrappedBuffer(Unpooled.EMPTY_BUFFER));
180-
if (!keepAlive) {
181-
ctx.writeAndFlush(response).addListeners(ChannelFutureListener.CLOSE);
182-
} else {
183-
ctx.writeAndFlush(response);
184-
}
185151
}
186-
}
187-
188-
private void sendNotFound(ChannelHandlerContext ctx, boolean keepAlive) {
189-
FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.NOT_FOUND,
190-
Unpooled.wrappedBuffer(Unpooled.EMPTY_BUFFER));
191152
if (!keepAlive) {
192153
ctx.writeAndFlush(response).addListeners(ChannelFutureListener.CLOSE);
193154
} else {
194155
ctx.writeAndFlush(response);
195156
}
196157
}
197158

198-
private void sendUnavailable(ChannelHandlerContext ctx, boolean keepAlive) {
199-
FullHttpResponse response = new DefaultFullHttpResponse(
200-
HttpVersion.HTTP_1_1, HttpResponseStatus.SERVICE_UNAVAILABLE, Unpooled.wrappedBuffer(Unpooled.EMPTY_BUFFER));
159+
private void sendErrorResponse(ChannelHandlerContext ctx, HttpResponseStatus status, boolean keepAlive) {
160+
FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status,
161+
Unpooled.wrappedBuffer(Unpooled.EMPTY_BUFFER));
201162
if (!keepAlive) {
202163
ctx.writeAndFlush(response).addListeners(ChannelFutureListener.CLOSE);
203164
} else {

0 commit comments

Comments
 (0)