a403441019b8bd3e9db895734275848d503d150c
[dmaap/datarouter.git] / datarouter-node / src / main / java / org / onap / dmaap / datarouter / node / PathUtil.java
1 /**\r
2  * -\r
3  * ============LICENSE_START=======================================================\r
4  * Copyright (C) 2019 Nordix Foundation.\r
5  * ================================================================================\r
6  * Licensed under the Apache License, Version 2.0 (the "License");\r
7  * you may not use this file except in compliance with the License.\r
8  * You may obtain a copy of the License at\r
9  * <p>\r
10  * http://www.apache.org/licenses/LICENSE-2.0\r
11  * <p>\r
12  * Unless required by applicable law or agreed to in writing, software\r
13  * distributed under the License is distributed on an "AS IS" BASIS,\r
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
15  * See the License for the specific language governing permissions and\r
16  * limitations under the License.\r
17  * <p>\r
18  * SPDX-License-Identifier: Apache-2.0\r
19  * ============LICENSE_END=========================================================\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  */\r
29 class PathUtil {\r
30 \r
31     /**\r
32      * This method takes String as the parameter and return the filtered path string.\r
33      * @param aString String to clean\r
34      * @return A cleaned String\r
35      */\r
36     static String cleanString(String aString) {\r
37         if (aString == null) return null;\r
38         String cleanString = "";\r
39         for (int i = 0; i < aString.length(); ++i) {\r
40             cleanString += cleanChar(aString.charAt(i));\r
41         }\r
42         return cleanString;\r
43     }\r
44 \r
45     /**\r
46      * This method filters the valid special characters in path string.\r
47      * @param aChar The char to be cleaned\r
48      * @return The cleaned char\r
49      */\r
50     private static char cleanChar(char aChar) {\r
51         // 0 - 9\r
52         for (int i = 48; i < 58; ++i) {\r
53             if (aChar == i) return (char) i;\r
54         }\r
55         // 'A' - 'Z'\r
56         for (int i = 65; i < 91; ++i) {\r
57             if (aChar == i) return (char) i;\r
58         }\r
59         // 'a' - 'z'\r
60         for (int i = 97; i < 123; ++i) {\r
61             if (aChar == i) return (char) i;\r
62         }\r
63         // other valid characters\r
64         switch (aChar) {\r
65             case '/':\r
66                 return '/';\r
67             case '.':\r
68                 return '.';\r
69             case '-':\r
70                 return '-';\r
71             case ':':\r
72                 return ':';\r
73             case '?':\r
74                 return '?';\r
75             case '&':\r
76                 return '&';\r
77             case '=':\r
78                 return '=';\r
79             case '#':\r
80                 return '#';\r
81             case '_':\r
82                 return '_';\r
83             case ' ':\r
84                 return ' ';\r
85         }\r
86         return '%';\r
87     }\r
88 }\r