You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2022/11/12 21:05:26 UTC

[commons-net] 03/03: Remove reflection from SSLSocketUtils

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

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-net.git

commit 5b3b12d78601c5476081f31887610d6d0e0cbe2a
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Nov 12 16:05:18 2022 -0500

    Remove reflection from SSLSocketUtils
---
 src/changes/changes.xml                            |  3 +++
 .../apache/commons/net/util/SSLSocketUtils.java    | 25 +++++++---------------
 2 files changed, 11 insertions(+), 17 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 2c299be0..f59ce292 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -89,6 +89,9 @@ The <action> type attribute can be add,update,fix,remove.
       <action type="fix" dev="ggregory" due-to="Arturo Bernal, Gary Gregory">
         Simplify conditions avoiding extra operations #88.
       </action>
+      <action type="fix" dev="ggregory" due-to="Gary Gregory">
+        Remove reflection from SSLSocketUtils.
+      </action>
       <!-- ADD -->
       <action type="add" dev="ggregory" due-to="Gary Gregory">
         [FTP] Add FTPClient.mdtmInstant(String).
diff --git a/src/main/java/org/apache/commons/net/util/SSLSocketUtils.java b/src/main/java/org/apache/commons/net/util/SSLSocketUtils.java
index 3261c08c..5485a8b7 100644
--- a/src/main/java/org/apache/commons/net/util/SSLSocketUtils.java
+++ b/src/main/java/org/apache/commons/net/util/SSLSocketUtils.java
@@ -18,9 +18,7 @@
 
 package org.apache.commons.net.util;
 
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
-
+import javax.net.ssl.SSLParameters;
 import javax.net.ssl.SSLSocket;
 
 /**
@@ -29,26 +27,19 @@ import javax.net.ssl.SSLSocket;
  * @since 3.4
  */
 public class SSLSocketUtils {
+
     /**
      * Enable the HTTPS endpoint identification algorithm on an SSLSocket.
      *
      * @param socket the SSL socket
-     * @return {@code true} on success (this is only supported on Java 1.7+)
+     * @return {@code true} on success
      */
     public static boolean enableEndpointNameVerification(final SSLSocket socket) {
-        try {
-            final Class<?> cls = Class.forName("javax.net.ssl.SSLParameters");
-            final Method setEndpointIdentificationAlgorithm = cls.getDeclaredMethod("setEndpointIdentificationAlgorithm", String.class);
-            final Method getSSLParameters = SSLSocket.class.getDeclaredMethod("getSSLParameters");
-            final Method setSSLParameters = SSLSocket.class.getDeclaredMethod("setSSLParameters", cls);
-            final Object sslParams = getSSLParameters.invoke(socket);
-            if (sslParams != null) {
-                setEndpointIdentificationAlgorithm.invoke(sslParams, "HTTPS");
-                setSSLParameters.invoke(socket, sslParams);
-                return true;
-            }
-        } catch (final SecurityException | ClassNotFoundException | NoSuchMethodException | IllegalArgumentException | IllegalAccessException
-                | InvocationTargetException e) { // Ignored
+        final SSLParameters sslParameters = socket.getSSLParameters();
+        if (sslParameters != null) {
+            sslParameters.setEndpointIdentificationAlgorithm("HTTPS");
+            socket.setSSLParameters(sslParameters);
+            return true;
         }
         return false;
     }