Created
August 22, 2018 09:29
-
-
Save thisismydesign/7e0e1fccc3afbf2193e44ee66cacf996 to your computer and use it in GitHub Desktop.
EscapeUtil
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
package com.thisismydesign; | |
import org.apache.commons.codec.binary.Hex; | |
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
public final class EscapeUtil { | |
private final static Pattern pipe = Pattern.compile("\\|"); | |
private final static Pattern snortSpecial = Pattern.compile("[\";\\\\]"); | |
private final static Pattern nonASCII = Pattern.compile("[^\\p{InBasic_Latin}]"); | |
private final static Pattern specialOrWhiteSpaceExceptSpace = Pattern.compile("[\\p{C}\\p{Z}&&[^ ]]"); | |
// Order of things is important, all successful escapes will create pipes (|) | |
// so the actual ones we should escape in the beginning | |
private final static Pattern[] patterns = new Pattern[]{pipe, snortSpecial, nonASCII, | |
specialOrWhiteSpaceExceptSpace}; | |
private EscapeUtil(){} | |
public static String escapeAll(String string) { | |
String escaped = string; | |
for (Pattern pattern : patterns) { | |
escaped = escape(escaped, pattern); | |
} | |
return mergePipes(escaped); | |
} | |
private static String escape(String string, Pattern pattern) { | |
Matcher matcher = pattern.matcher(string); | |
StringBuffer sb = new StringBuffer(); | |
while (matcher.find()) { | |
matcher.appendReplacement(sb, getReplacement(matcher.group(0))); | |
} | |
matcher.appendTail(sb); | |
return sb.toString(); | |
} | |
private static String getReplacement(String string) { | |
return betweenPipes(toHex(string)); | |
} | |
private static String toHex(String string) { | |
return Hex.encodeHexString(string.getBytes()); | |
} | |
private static String betweenPipes(String string) { | |
return "|" + string + "|"; | |
} | |
private static String mergePipes(String string) { | |
return string.replaceAll("\\|\\|", " "); | |
} | |
} |
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
package com.thisismydesign; | |
import org.apache.commons.lang3.StringEscapeUtils; | |
import org.junit.Ignore; | |
import org.junit.Test; | |
import static org.junit.Assert.*; | |
public class EscapeUtilTest { | |
@Test | |
public void escapeAll_withPipe() throws Exception { | |
assertEquals("|7c|", EscapeUtil.escapeAll("|")); | |
assertEquals("hello|7c|there", EscapeUtil.escapeAll("hello|there")); | |
assertEquals("hello|7c 7c|there", EscapeUtil.escapeAll("hello||there")); | |
assertEquals("hello|7c|there|7c|you", EscapeUtil.escapeAll("hello|there|you")); | |
} | |
@Test | |
public void escapeAll_withSpecialSnortChars() throws Exception { | |
assertEquals("hi|22|there", EscapeUtil.escapeAll("hi\"there")); | |
assertEquals("hi|3b|there", EscapeUtil.escapeAll("hi;there")); | |
assertEquals("hi|5c|there", EscapeUtil.escapeAll("hi\\there")); | |
} | |
@Test | |
public void escapeAll_withNotPrintable() throws Exception { | |
assertEquals("|00|", EscapeUtil.escapeAll(Character.toString((char) 0))); | |
assertEquals("|00|hi", EscapeUtil.escapeAll(StringEscapeUtils.unescapeJava("\\u0000") + "hi")); | |
assertEquals("hi|00|there", EscapeUtil.escapeAll("hi" + StringEscapeUtils.unescapeJava("\\u0000") + "there")); | |
} | |
@Test | |
@Ignore | |
public void escapeAll_withUnicodeReplacementChar() throws Exception { | |
assertEquals("hi|efbfbd|there", EscapeUtil.escapeAll("hi" + StringEscapeUtils.unescapeJava("\\ufffd") + "there")); | |
assertEquals("hi|efbfbd|there", EscapeUtil.escapeAll("hi�there")); | |
} | |
@Test | |
public void escapeAll_withWhitespace() throws Exception { | |
assertEquals("hi there", EscapeUtil.escapeAll("hi there")); | |
assertEquals("hi|09|there", EscapeUtil.escapeAll("hi\tthere")); | |
assertEquals("hi|0a|there", EscapeUtil.escapeAll("hi\nthere")); | |
assertEquals("hi|0a|there", EscapeUtil.escapeAll("hi" + StringEscapeUtils.unescapeJava("\\u000a") + "there")); | |
} | |
@Test | |
@Ignore | |
public void escapeAll_withNonASCII() throws Exception { | |
assertEquals("hi|c3a1|there", EscapeUtil.escapeAll("hiáthere")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment