+
+ /**
+ * Utility class that validates the path url formed from
+ * the string passed in the request parameters.
+ */
+ static class PathUtil {
+
+ private PathUtil() {
+ throw new IllegalStateException("Utility Class");
+ }
+
+ /**
+ * This method takes String as the parameter and return the filtered path string.
+ *
+ * @param string String to clean
+ * @return A cleaned String
+ */
+ static String cleanString(String string) {
+ if (string == null) {
+ return null;
+ }
+ StringBuilder cleanString = new StringBuilder();
+ for (int i = 0; i < string.length(); ++i) {
+ cleanString.append(cleanChar(string.charAt(i)));
+ }
+ return cleanString.toString();
+ }
+
+ /**
+ * This method filters the valid special characters in path string.
+ *
+ * @param character The char to be cleaned
+ * @return The cleaned char
+ */
+ private static char cleanChar(char character) {
+ // 0 - 9
+ for (int i = 48; i < 58; ++i) {
+ if (character == i) {
+ return (char) i;
+ }
+ }
+ // 'A' - 'Z'
+ for (int i = 65; i < 91; ++i) {
+ if (character == i) {
+ return (char) i;
+ }
+ }
+ // 'a' - 'z'
+ for (int i = 97; i < 123; ++i) {
+ if (character == i) {
+ return (char) i;
+ }
+ }
+ return getValidCharacter(character);
+ }
+
+ private static char getValidCharacter(char character) {
+ // other valid characters
+ switch (character) {
+ case '/':
+ return '/';
+ case '.':
+ return '.';
+ case '-':
+ return '-';
+ case ':':
+ return ':';
+ case '?':
+ return '?';
+ case '&':
+ return '&';
+ case '=':
+ return '=';
+ case '#':
+ return '#';
+ case '_':
+ return '_';
+ case ' ':
+ return ' ';
+ default:
+ return '%';
+ }
+ }
+ }