funky-eyes commented on code in PR #7346: URL: https://github.com/apache/incubator-seata/pull/7346#discussion_r2125220422
########## core/src/main/java/org/apache/seata/core/rpc/netty/http/HttpDispatchHandler.java: ########## @@ -0,0 +1,114 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.seata.core.rpc.netty.http; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import io.netty.buffer.Unpooled; +import io.netty.channel.ChannelFutureListener; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.SimpleChannelInboundHandler; +import io.netty.handler.codec.http.DefaultFullHttpResponse; +import io.netty.handler.codec.http.FullHttpResponse; +import io.netty.handler.codec.http.HttpHeaderNames; +import io.netty.handler.codec.http.HttpMethod; +import io.netty.handler.codec.http.HttpRequest; +import io.netty.handler.codec.http.HttpResponseStatus; +import io.netty.handler.codec.http.HttpUtil; +import io.netty.handler.codec.http.HttpVersion; +import io.netty.handler.codec.http.QueryStringDecoder; +import io.netty.handler.codec.http.multipart.Attribute; +import io.netty.handler.codec.http.multipart.HttpPostRequestDecoder; +import io.netty.handler.codec.http.multipart.InterfaceHttpData; +import java.lang.reflect.Method; + +public class HttpDispatchHandler extends SimpleChannelInboundHandler<HttpRequest> { + + private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); + + @Override + protected void channelRead0(ChannelHandlerContext ctx, HttpRequest httpRequest) throws Exception { + final boolean keepAlive = HttpUtil.isKeepAlive(httpRequest) + && httpRequest.protocolVersion().isKeepAliveDefault(); + QueryStringDecoder queryStringDecoder = new QueryStringDecoder(httpRequest.uri()); + String path = queryStringDecoder.path(); + + HttpInvocation httpInvocation = ControllerManager.getHttpInvocation(path); + if (httpInvocation == null) { + sendNotFound(ctx, keepAlive); + return; + } + + ObjectNode requestDataNode = OBJECT_MAPPER.createObjectNode(); + requestDataNode.putIfAbsent("param", ParameterParser.convertParamMap(queryStringDecoder.parameters())); + requestDataNode.putPOJO("channel", ctx.channel()); + + if (httpRequest.method() == HttpMethod.POST) { + HttpPostRequestDecoder httpPostRequestDecoder = null; + try { + httpPostRequestDecoder = new HttpPostRequestDecoder(httpRequest); + ObjectNode bodyDataNode = OBJECT_MAPPER.createObjectNode(); + for (InterfaceHttpData interfaceHttpData : httpPostRequestDecoder.getBodyHttpDatas()) { + if (interfaceHttpData.getHttpDataType() != InterfaceHttpData.HttpDataType.Attribute) { + continue; + } + Attribute attribute = (Attribute) interfaceHttpData; + bodyDataNode.put(attribute.getName(), attribute.getValue()); Review Comment: We can treat this as an additional task and handle it in a separate PR. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
