Remove major and minor code smells in dr-node
[dmaap/datarouter.git] / datarouter-node / src / main / java / org / onap / dmaap / datarouter / node / PathUtil.java
index a403441..16f8033 100644 (file)
@@ -1,65 +1,82 @@
-/**\r
- * -\r
+/*-\r
  * ============LICENSE_START=======================================================\r
  * ============LICENSE_START=======================================================\r
- * Copyright (C) 2019 Nordix Foundation.\r
+ *  Copyright (C) 2019 Nordix Foundation.\r
  * ================================================================================\r
  * Licensed under the Apache License, Version 2.0 (the "License");\r
  * you may not use this file except in compliance with the License.\r
  * You may obtain a copy of the License at\r
  * ================================================================================\r
  * Licensed under the Apache License, Version 2.0 (the "License");\r
  * you may not use this file except in compliance with the License.\r
  * You may obtain a copy of the License at\r
- * <p>\r
- * http://www.apache.org/licenses/LICENSE-2.0\r
- * <p>\r
+ *\r
+ *      http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
  * Unless required by applicable law or agreed to in writing, software\r
  * distributed under the License is distributed on an "AS IS" BASIS,\r
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
  * See the License for the specific language governing permissions and\r
  * limitations under the License.\r
  * Unless required by applicable law or agreed to in writing, software\r
  * distributed under the License is distributed on an "AS IS" BASIS,\r
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
  * See the License for the specific language governing permissions and\r
  * limitations under the License.\r
- * <p>\r
+ *\r
  * SPDX-License-Identifier: Apache-2.0\r
  * ============LICENSE_END=========================================================\r
  */\r
  * SPDX-License-Identifier: Apache-2.0\r
  * ============LICENSE_END=========================================================\r
  */\r
+\r
 package org.onap.dmaap.datarouter.node;\r
 \r
 /**\r
  * FORTIFY SCAN FIXES\r
  * <p>This Utility is used for Fortify fixes. It Validates the path url formed from\r
 package org.onap.dmaap.datarouter.node;\r
 \r
 /**\r
  * FORTIFY SCAN FIXES\r
  * <p>This Utility is used for Fortify fixes. It Validates the path url formed from\r
- *  the string passed in the request parameters.</p>\r
- *\r
+ * the string passed in the request parameters.</p>\r
  */\r
 class PathUtil {\r
 \r
  */\r
 class PathUtil {\r
 \r
+    private PathUtil() {\r
+        throw new IllegalStateException("Utility Class");\r
+    }\r
+\r
     /**\r
      * This method takes String as the parameter and return the filtered path string.\r
     /**\r
      * This method takes String as the parameter and return the filtered path string.\r
+     *\r
      * @param aString String to clean\r
      * @return A cleaned String\r
      */\r
     static String cleanString(String aString) {\r
      * @param aString String to clean\r
      * @return A cleaned String\r
      */\r
     static String cleanString(String aString) {\r
-        if (aString == null) return null;\r
-        String cleanString = "";\r
+        if (aString == null) {\r
+            return null;\r
+        }\r
+        StringBuilder cleanString = new StringBuilder();\r
         for (int i = 0; i < aString.length(); ++i) {\r
         for (int i = 0; i < aString.length(); ++i) {\r
-            cleanString += cleanChar(aString.charAt(i));\r
+            cleanString.append(cleanChar(aString.charAt(i)));\r
         }\r
         }\r
-        return cleanString;\r
+        return cleanString.toString();\r
     }\r
 \r
     /**\r
      * This method filters the valid special characters in path string.\r
     }\r
 \r
     /**\r
      * This method filters the valid special characters in path string.\r
+     *\r
      * @param aChar The char to be cleaned\r
      * @return The cleaned char\r
      */\r
     private static char cleanChar(char aChar) {\r
         // 0 - 9\r
         for (int i = 48; i < 58; ++i) {\r
      * @param aChar The char to be cleaned\r
      * @return The cleaned char\r
      */\r
     private static char cleanChar(char aChar) {\r
         // 0 - 9\r
         for (int i = 48; i < 58; ++i) {\r
-            if (aChar == i) return (char) i;\r
+            if (aChar == i) {\r
+                return (char) i;\r
+            }\r
         }\r
         // 'A' - 'Z'\r
         for (int i = 65; i < 91; ++i) {\r
         }\r
         // 'A' - 'Z'\r
         for (int i = 65; i < 91; ++i) {\r
-            if (aChar == i) return (char) i;\r
+            if (aChar == i) {\r
+                return (char) i;\r
+            }\r
         }\r
         // 'a' - 'z'\r
         for (int i = 97; i < 123; ++i) {\r
         }\r
         // 'a' - 'z'\r
         for (int i = 97; i < 123; ++i) {\r
-            if (aChar == i) return (char) i;\r
+            if (aChar == i) {\r
+                return (char) i;\r
+            }\r
         }\r
         }\r
+        return getValidCharacter(aChar);\r
+    }\r
+\r
+    private static char getValidCharacter(char aChar) {\r
         // other valid characters\r
         switch (aChar) {\r
             case '/':\r
         // other valid characters\r
         switch (aChar) {\r
             case '/':\r
@@ -82,7 +99,8 @@ class PathUtil {
                 return '_';\r
             case ' ':\r
                 return ' ';\r
                 return '_';\r
             case ' ':\r
                 return ' ';\r
+            default:\r
+                return '%';\r
         }\r
         }\r
-        return '%';\r
     }\r
 }\r
     }\r
 }\r