-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5442d1b
commit c539f90
Showing
3 changed files
with
94 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
presto-main/src/main/java/com/facebook/presto/util/SanitizeMessage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.facebook.presto.util; | ||
|
||
public class SanitizeMessage | ||
{ | ||
private SanitizeMessage() {} | ||
|
||
public static String getSanitizeMessage(String message) | ||
{ | ||
if (message == null) { | ||
return ""; | ||
} | ||
StringBuilder stringBuilder = new StringBuilder(message.length()); | ||
|
||
// Traverse each character in the string and replace only when necessary | ||
for (int i = 0; i < message.length(); i++) { | ||
char ch = message.charAt(i); | ||
switch (ch) { | ||
case '&': | ||
stringBuilder.append("&"); | ||
break; | ||
case '<': | ||
stringBuilder.append("<"); | ||
break; | ||
case '>': | ||
stringBuilder.append(">"); | ||
break; | ||
case '"': | ||
stringBuilder.append("""); | ||
break; | ||
case '\'': | ||
stringBuilder.append("'"); | ||
break; | ||
case '/': | ||
stringBuilder.append("/"); | ||
break; | ||
default: | ||
stringBuilder.append(ch); | ||
break; | ||
} | ||
} | ||
return stringBuilder.toString(); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
presto-main/src/test/java/com/facebook/presto/util/TestSanitizeMessage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.facebook.presto.util; | ||
|
||
import org.testng.annotations.Test; | ||
|
||
import static org.testng.Assert.assertEquals; | ||
|
||
public class TestSanitizeMessage | ||
{ | ||
@Test | ||
public void testGetSanitizeMessage() | ||
{ | ||
String message = "Server&> error/ to \" process < task result request ' "; | ||
assertEquals(SanitizeMessage.getSanitizeMessage(message), "Server&> error/ to " process < task result request ' "); | ||
} | ||
|
||
@Test | ||
public void testGetSanitizeMessageNullCheck() | ||
{ | ||
String message = null; | ||
assertEquals(SanitizeMessage.getSanitizeMessage(message), ""); | ||
} | ||
} |