ZanderXu commented on code in PR #4659:
URL: https://github.com/apache/hadoop/pull/4659#discussion_r939467240


##########
hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ipc/Server.java:
##########
@@ -1351,6 +1357,16 @@ private class ResponseParams {
 
     @Override
     public String toString() {
+      boolean isCallerContextEnabled = conf.getBoolean(

Review Comment:
   I don't think this is a good idea to get the value from conf every time. And 
`HADOOP_CALLER_CONTEXT_ENABLED_KEY` is used to control whether output 
callerContext into audit log. We plan use this key here?



##########
hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ipc/CallerContext.java:
##########
@@ -121,6 +122,51 @@ public String toString() {
     return str;
   }
 
+  /**
+   * Try to obtain the value corresponding to the key by parsing the content.
+   * @param content the full content to be parsed.
+   * @param key trying to obtain the value of the key.
+   * @return the value corresponding to the key.
+   */
+  @VisibleForTesting
+  public static String parseSpecialValue(String content, String key) {
+    int posn = content.indexOf(key);
+    if (posn != -1) {
+      posn += key.length();
+      int end = content.indexOf(",", posn);
+      return end == -1 ? content.substring(posn) : content.substring(posn, 
end);
+    }
+    return null;
+  }
+
+  /**
+   * Get client ip content from caller context.
+   * @param context The context here is obtained from outside.
+   * @return Filter the value carried by 'clientIp:' from the context.
+   *         If not, return null.
+   */
+  public static String getRealClientIp(String context) {
+    if (context != null && !context.equals("")) {

Review Comment:
   `!context.equals("")` -> `context.isContextValid()`



##########
hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ipc/CallerContext.java:
##########
@@ -121,6 +122,51 @@ public String toString() {
     return str;
   }
 
+  /**
+   * Try to obtain the value corresponding to the key by parsing the content.
+   * @param content the full content to be parsed.
+   * @param key trying to obtain the value of the key.
+   * @return the value corresponding to the key.
+   */
+  @VisibleForTesting
+  public static String parseSpecialValue(String content, String key) {
+    int posn = content.indexOf(key);
+    if (posn != -1) {
+      posn += key.length();
+      int end = content.indexOf(",", posn);
+      return end == -1 ? content.substring(posn) : content.substring(posn, 
end);
+    }
+    return null;
+  }
+
+  /**
+   * Get client ip content from caller context.
+   * @param context The context here is obtained from outside.
+   * @return Filter the value carried by 'clientIp:' from the context.
+   *         If not, return null.
+   */
+  public static String getRealClientIp(String context) {
+    if (context != null && !context.equals("")) {
+      String ipKey = CLIENT_IP_STR + Builder.KEY_VALUE_SEPARATOR;
+      return parseSpecialValue(context, ipKey);
+    }
+    return null;
+  }
+
+  /**
+   * Get client port content from caller context.
+   * @param context The context here is obtained from outside.
+   * @return Filter the value carried by 'clientPort:' from the context.
+   *         If not, return null.
+   */
+  public static String getRealClientPort(String context) {
+    if (context != null && !context.equals("")) {

Review Comment:
   !context.equals("") -> context.isContextValid()



-- 
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]

Reply via email to