fuweng11 commented on code in PR #9012:
URL: https://github.com/apache/inlong/pull/9012#discussion_r1349656672


##########
inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/resource/sink/starrocks/StarRocksJdbcUtils.java:
##########
@@ -44,25 +45,50 @@ public class StarRocksJdbcUtils {
     private static final Logger LOGGER = 
LoggerFactory.getLogger(StarRocksJdbcUtils.class);
 
     /**
-     * Get starRocks connection from starRocks url and user
+     * Get a StarRocks JDBC connection using the provided URL, username, and 
password.
+     *
+     * @param url      The StarRocks JDBC URL.
+     * @param user     The username for authentication.
+     * @param password The password for authentication.
+     * @return A {@link Connection} representing the StarRocks database 
connection.
+     * @throws Exception If an error occurs during connection establishment.
      */
     public static Connection getConnection(String url, String user, String 
password) throws Exception {
         if (StringUtils.isBlank(url) || 
!url.startsWith(STAR_ROCKS_JDBC_PREFIX)) {
-            throw new Exception("starRocks server url should start with " + 
STAR_ROCKS_JDBC_PREFIX);
+            throw new Exception("StarRocks server URL should start with " + 
STAR_ROCKS_JDBC_PREFIX);
+        }
+
+        // Verify that the URL format is valid
+        if (!isValidUrlFormat(url)) {
+            throw new Exception("Invalid URL format");
         }
+
         Connection conn;
         try {
             Class.forName(STAR_ROCKS_DRIVER_CLASS);
             conn = DriverManager.getConnection(url, user, password);
-            LOGGER.info("get star rocks connection success, url={}", url);
+            LOGGER.info("Successfully obtained StarRocks connection, URL: {}", 
url);
             return conn;
         } catch (Exception e) {
-            String errMsg = "get star rocks connection error, please check 
starRocks jdbc url, username or password";
+            String errMsg =
+                    "Failed to get StarRocks connection, please check 
StarRocks JDBC URL, username, or password.";
             LOGGER.error(errMsg, e);
             throw new Exception(errMsg + ", error: " + e.getMessage());
         }
     }
 
+    /**
+     * Validates the format of the given JDBC URL.
+     *
+     * @param url The JDBC URL to validate.
+     * @return {@code true} if the URL format is valid, {@code false} 
otherwise.
+     */
+    private static boolean isValidUrlFormat(String url) {
+        // Modify this regular expression to match your URL format
+        String urlPattern = 
"^jdbc:mysql://(localhost|\\d{1,3}(\\.\\d{1,3}){3})(:\\d{1,5})/.*$";
+        return Pattern.matches(urlPattern, url);

Review Comment:
   Can we verify directly in the request? like 
   ```
      @ApiModelProperty("JDBC URL of the ClickHouse server")
       @Pattern(regexp = "^((?!\\s).)*$", message = "not supports blank in url")
       private String jdbcUrl;
   ```



##########
inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/resource/sink/starrocks/StarRocksJdbcUtils.java:
##########
@@ -44,25 +45,50 @@ public class StarRocksJdbcUtils {
     private static final Logger LOGGER = 
LoggerFactory.getLogger(StarRocksJdbcUtils.class);
 
     /**
-     * Get starRocks connection from starRocks url and user
+     * Get a StarRocks JDBC connection using the provided URL, username, and 
password.
+     *
+     * @param url      The StarRocks JDBC URL.
+     * @param user     The username for authentication.
+     * @param password The password for authentication.
+     * @return A {@link Connection} representing the StarRocks database 
connection.
+     * @throws Exception If an error occurs during connection establishment.
      */
     public static Connection getConnection(String url, String user, String 
password) throws Exception {
         if (StringUtils.isBlank(url) || 
!url.startsWith(STAR_ROCKS_JDBC_PREFIX)) {
-            throw new Exception("starRocks server url should start with " + 
STAR_ROCKS_JDBC_PREFIX);
+            throw new Exception("StarRocks server URL should start with " + 
STAR_ROCKS_JDBC_PREFIX);
+        }
+
+        // Verify that the URL format is valid
+        if (!isValidUrlFormat(url)) {
+            throw new Exception("Invalid URL format");
         }
+
         Connection conn;
         try {
             Class.forName(STAR_ROCKS_DRIVER_CLASS);
             conn = DriverManager.getConnection(url, user, password);
-            LOGGER.info("get star rocks connection success, url={}", url);
+            LOGGER.info("Successfully obtained StarRocks connection, URL: {}", 
url);
             return conn;
         } catch (Exception e) {
-            String errMsg = "get star rocks connection error, please check 
starRocks jdbc url, username or password";
+            String errMsg =
+                    "Failed to get StarRocks connection, please check 
StarRocks JDBC URL, username, or password.";
             LOGGER.error(errMsg, e);
             throw new Exception(errMsg + ", error: " + e.getMessage());
         }
     }
 
+    /**
+     * Validates the format of the given JDBC URL.
+     *
+     * @param url The JDBC URL to validate.
+     * @return {@code true} if the URL format is valid, {@code false} 
otherwise.
+     */
+    private static boolean isValidUrlFormat(String url) {
+        // Modify this regular expression to match your URL format
+        String urlPattern = 
"^jdbc:mysql://(localhost|\\d{1,3}(\\.\\d{1,3}){3})(:\\d{1,5})/.*$";
+        return Pattern.matches(urlPattern, url);

Review Comment:
   Can we verify directly in the request? like 
   ```
      @ApiModelProperty("JDBC URL of the ClickHouse server")
       @Pattern(regexp = "^((?!\\s).)*$", message = "not supports blank in url")
       private String jdbcUrl;
   ```



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

Reply via email to