Remove major and minor code smells in dr-node
[dmaap/datarouter.git] / datarouter-node / src / main / java / org / onap / dmaap / datarouter / node / PathUtil.java
1 /*-\r
2  * ============LICENSE_START=======================================================\r
3  *  Copyright (C) 2019 Nordix Foundation.\r
4  * ================================================================================\r
5  * Licensed under the Apache License, Version 2.0 (the "License");\r
6  * you may not use this file except in compliance with the License.\r
7  * You may obtain a copy of the License at\r
8  *\r
9  *      http://www.apache.org/licenses/LICENSE-2.0\r
10  *\r
11  * Unless required by applicable law or agreed to in writing, software\r
12  * distributed under the License is distributed on an "AS IS" BASIS,\r
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
14  * See the License for the specific language governing permissions and\r
15  * limitations under the License.\r
16  *\r
17  * SPDX-License-Identifier: Apache-2.0\r
18  * ============LICENSE_END=========================================================\r
19  */\r
20 \r
21 package org.onap.dmaap.datarouter.node;\r
22 \r
23 /**\r
24  * FORTIFY SCAN FIXES\r
25  * <p>This Utility is used for Fortify fixes. It Validates the path url formed from\r
26  * the string passed in the request parameters.</p>\r
27  */\r
28 class PathUtil {\r
29 \r
30     private PathUtil() {\r
31         throw new IllegalStateException("Utility Class");\r
32     }\r
33 \r
34     /**\r
35      * This method takes String as the parameter and return the filtered path string.\r
36      *\r
37      * @param aString String to clean\r
38      * @return A cleaned String\r
39      */\r
40     static String cleanString(String aString) {\r
41         if (aString == null) {\r
42             return null;\r
43         }\r
44         StringBuilder cleanString = new StringBuilder();\r
45         for (int i = 0; i < aString.length(); ++i) {\r
46             cleanString.append(cleanChar(aString.charAt(i)));\r
47         }\r
48         return cleanString.toString();\r
49     }\r
50 \r
51     /**\r
52      * This method filters the valid special characters in path string.\r
53      *\r
54      * @param aChar The char to be cleaned\r
55      * @return The cleaned char\r
56      */\r
57     private static char cleanChar(char aChar) {\r
58         // 0 - 9\r
59         for (int i = 48; i < 58; ++i) {\r
60             if (aChar == i) {\r
61                 return (char) i;\r
62             }\r
63         }\r
64         // 'A' - 'Z'\r
65         for (int i = 65; i < 91; ++i) {\r
66             if (aChar == i) {\r
67                 return (char) i;\r
68             }\r
69         }\r
70         // 'a' - 'z'\r
71         for (int i = 97; i < 123; ++i) {\r
72             if (aChar == i) {\r
73                 return (char) i;\r
74             }\r
75         }\r
76         return getValidCharacter(aChar);\r
77     }\r
78 \r
79     private static char getValidCharacter(char aChar) {\r
80         // other valid characters\r
81         switch (aChar) {\r
82             case '/':\r
83                 return '/';\r
84             case '.':\r
85                 return '.';\r
86             case '-':\r
87                 return '-';\r
88             case ':':\r
89                 return ':';\r
90             case '?':\r
91                 return '?';\r
92             case '&':\r
93                 return '&';\r
94             case '=':\r
95                 return '=';\r
96             case '#':\r
97                 return '#';\r
98             case '_':\r
99                 return '_';\r
100             case ' ':\r
101                 return ' ';\r
102             default:\r
103                 return '%';\r
104         }\r
105     }\r
106 }\r