This is an automated email from the ASF dual-hosted git repository.

albumenj pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/dubbo.git


The following commit(s) were added to refs/heads/master by this push:
     new 3963ba8  Extract common logic for HttpProcessHandler to optimize it 
(#7918)
3963ba8 is described below

commit 3963ba88ec6b5d4cf3cdaf267963f2d1800d5c32
Author: 灼华 <[email protected]>
AuthorDate: Mon May 31 10:21:53 2021 +0800

    Extract common logic for HttpProcessHandler to optimize it (#7918)
---
 .../qos/server/handler/HttpProcessHandler.java     | 28 ++++++++++------------
 1 file changed, 12 insertions(+), 16 deletions(-)

diff --git 
a/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/server/handler/HttpProcessHandler.java
 
b/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/server/handler/HttpProcessHandler.java
index 408e185..5f0ad93 100644
--- 
a/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/server/handler/HttpProcessHandler.java
+++ 
b/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/server/handler/HttpProcessHandler.java
@@ -56,48 +56,44 @@ public class HttpProcessHandler extends 
SimpleChannelInboundHandler<HttpRequest>
     protected void channelRead0(ChannelHandlerContext ctx, HttpRequest msg) 
throws Exception {
         CommandContext commandContext = HttpCommandDecoder.decode(msg);
         // return 404 when fail to construct command context
+        FullHttpResponse response;
         if (commandContext == null) {
             log.warn("can not found commandContext url: " + msg.getUri());
-            FullHttpResponse response = http404();
-            
ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
+            response = http404();
         } else {
             commandContext.setRemote(ctx.channel());
             try {
                 String result = commandExecutor.execute(commandContext);
-                FullHttpResponse response = http200(result);
-                
ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
+                response = http200(result);
             } catch (NoSuchCommandException ex) {
                 log.error("can not find commandContext: " + commandContext, 
ex);
-                FullHttpResponse response = http404();
-                
ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
+                response = http404();
             } catch (Exception qosEx) {
                 log.error("execute commandContext: " + commandContext + " got 
exception", qosEx);
-                FullHttpResponse response = http500(qosEx.getMessage());
-                
ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
+                response = http500(qosEx.getMessage());
             }
         }
+        ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
     }
 
     private static final FullHttpResponse http200(String result) {
         FullHttpResponse response = new 
DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK,
                 Unpooled.wrappedBuffer(result.getBytes()));
-        HttpHeaders httpHeaders = response.headers();
-        httpHeaders.set(HttpHeaders.Names.CONTENT_TYPE, "text/plain");
-        httpHeaders.set(HttpHeaders.Names.CONTENT_LENGTH, 
response.content().readableBytes());
-        return response;
+        return fillFullHttpResponse(response);
     }
 
     private static final FullHttpResponse http404() {
         FullHttpResponse response = new 
DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.NOT_FOUND);
-        HttpHeaders httpHeaders = response.headers();
-        httpHeaders.set(HttpHeaders.Names.CONTENT_TYPE, "text/plain");
-        httpHeaders.set(HttpHeaders.Names.CONTENT_LENGTH, 
response.content().readableBytes());
-        return response;
+        return fillFullHttpResponse(response);
     }
 
     private static final FullHttpResponse http500(String errorMessage) {
         FullHttpResponse response = new 
DefaultFullHttpResponse(HttpVersion.HTTP_1_1, 
HttpResponseStatus.INTERNAL_SERVER_ERROR
                 , Unpooled.wrappedBuffer(errorMessage.getBytes()));
+        return fillFullHttpResponse(response);
+    }
+
+    private static FullHttpResponse fillFullHttpResponse(FullHttpResponse 
response) {
         HttpHeaders httpHeaders = response.headers();
         httpHeaders.set(HttpHeaders.Names.CONTENT_TYPE, "text/plain");
         httpHeaders.set(HttpHeaders.Names.CONTENT_LENGTH, 
response.content().readableBytes());

Reply via email to