From fe456d9661947b1e0633a59b98a12d1ce8d4a152 Mon Sep 17 00:00:00 2001 From: kexianjun Date: Mon, 10 Dec 2018 13:13:22 +0800 Subject: [PATCH] make telnet config work again (#2925) --- .../org/apache/dubbo/common/Constants.java | 1 + .../telnet/support/TelnetHandlerAdapter.java | 39 +++++++++++++++---- 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/Constants.java b/dubbo-common/src/main/java/org/apache/dubbo/common/Constants.java index 18ad19f5246..9265287a114 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/Constants.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/Constants.java @@ -665,6 +665,7 @@ public class Constants { public static final String REQUEST_TAG_KEY = "request.tag"; + public static final String TELNET = "telnet"; /* * private Constants(){ } */ diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/telnet/support/TelnetHandlerAdapter.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/telnet/support/TelnetHandlerAdapter.java index 580238b0338..a5ceda82f88 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/telnet/support/TelnetHandlerAdapter.java +++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/telnet/support/TelnetHandlerAdapter.java @@ -17,7 +17,9 @@ package org.apache.dubbo.remoting.telnet.support; import org.apache.dubbo.common.Constants; +import org.apache.dubbo.common.URL; import org.apache.dubbo.common.extension.ExtensionLoader; +import org.apache.dubbo.common.utils.StringUtils; import org.apache.dubbo.remoting.Channel; import org.apache.dubbo.remoting.RemotingException; import org.apache.dubbo.remoting.telnet.TelnetHandler; @@ -49,14 +51,20 @@ public String telnet(Channel channel, String message) throws RemotingException { } if (command.length() > 0) { if (extensionLoader.hasExtension(command)) { - try { - String result = extensionLoader.getExtension(command).telnet(channel, message); - if (result == null) { - return null; + if (commandEnabled(channel.getUrl(), command)) { + try { + String result = extensionLoader.getExtension(command).telnet(channel, message); + if (result == null) { + return null; + } + buf.append(result); + } catch (Throwable t) { + buf.append(t.getMessage()); } - buf.append(result); - } catch (Throwable t) { - buf.append(t.getMessage()); + } else { + buf.append("Command: "); + buf.append(command); + buf.append(" disabled"); } } else { buf.append("Unsupported command: "); @@ -72,4 +80,21 @@ public String telnet(Channel channel, String message) throws RemotingException { return buf.toString(); } + private boolean commandEnabled(URL url, String command) { + boolean commandEnable = false; + String supportCommands = url.getParameter(Constants.TELNET); + if (StringUtils.isEmpty(supportCommands)) { + commandEnable = true; + } else { + String[] commands = Constants.COMMA_SPLIT_PATTERN.split(supportCommands); + for (String c : commands) { + if (command.equals(c)) { + commandEnable = true; + break; + } + } + } + return commandEnable; + } + }