-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(datahub-client): support utf8 encoding #4878
Merged
shirshanka
merged 8 commits into
datahub-project:master
from
MugdhaHardikar-GSLab:utf8-client
May 20, 2022
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
23902f6
fix(datahub-client): support utf8 encoding
MugdhaHardikar-GSLab e810a32
Merge branch 'datahub-project:master' into utf8-client
MugdhaHardikar-GSLab b2130cc
Update RestEmitter.java
jjoyce0510 d549aa5
fix(datahub-client): add test, javadoc, fix error handling
MugdhaHardikar-GSLab 41c0266
fix(datahub-client): lint fix
MugdhaHardikar-GSLab ddad174
refactor(datahub-client): move utf8 encoding code to eventFormatter
MugdhaHardikar-GSLab 02c3238
removing unneeded code
shirshanka 574bb00
Merge branch 'datahub-project:master' into utf8-client
MugdhaHardikar-GSLab File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
149 changes: 149 additions & 0 deletions
149
metadata-integration/java/datahub-client/src/main/java/datahub/event/StringEscapeUtils.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,149 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you 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 datahub.event; | ||
|
||
import java.io.IOException; | ||
import java.io.StringWriter; | ||
import java.io.Writer; | ||
import java.util.Locale; | ||
|
||
public class StringEscapeUtils { | ||
|
||
private StringEscapeUtils() { | ||
|
||
} | ||
|
||
/** | ||
* Worker method for the {@link #escapeJavaScript(String)} method. | ||
* | ||
* @param out write to receieve the escaped string | ||
* @param str String to escape values in, may be null | ||
* @param escapeSingleQuote escapes single quotes if <code>true</code> | ||
* @param escapeForwardSlash TODO | ||
* @throws IOException if an IOException occurs | ||
*/ | ||
private static void escapeJavaStyleString(Writer out, String str, boolean escapeSingleQuote, | ||
boolean escapeForwardSlash) throws IOException { | ||
if (out == null) { | ||
throw new IllegalArgumentException("The Writer must not be null"); | ||
} else if (str != null) { | ||
int sz = str.length(); | ||
|
||
for (int i = 0; i < sz; ++i) { | ||
char ch = str.charAt(i); | ||
if (ch > 4095) { | ||
out.write("\\u" + hex(ch)); | ||
} else if (ch > 255) { | ||
out.write("\\u0" + hex(ch)); | ||
} else if (ch > 127) { | ||
out.write("\\u00" + hex(ch)); | ||
} else if (ch < ' ') { | ||
switch (ch) { | ||
case '\b': | ||
out.write(92); | ||
out.write(98); | ||
break; | ||
case '\t': | ||
out.write(92); | ||
out.write(116); | ||
break; | ||
case '\n': | ||
out.write(92); | ||
out.write(110); | ||
break; | ||
case '\u000b': | ||
|
||
case '\f': | ||
out.write(92); | ||
out.write(102); | ||
break; | ||
case '\r': | ||
out.write(92); | ||
out.write(114); | ||
break; | ||
default: | ||
if (ch > 15) { | ||
out.write("\\u00" + hex(ch)); | ||
} else { | ||
out.write("\\u000" + hex(ch)); | ||
} | ||
break; | ||
} | ||
|
||
} else { | ||
out.write(ch); | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Returns an upper case hexadecimal <code>String</code> for the given | ||
* character. | ||
* | ||
* @param ch The character to convert. | ||
* @return An upper case hexadecimal <code>String</code> | ||
*/ | ||
private static String hex(char ch) { | ||
return Integer.toHexString(ch).toUpperCase(Locale.ENGLISH); | ||
} | ||
|
||
/** | ||
* Worker method for the {@link #escapeJavaScript(String)} method. | ||
* | ||
* @param str String to escape values in, may be null | ||
* @param escapeSingleQuotes escapes single quotes if <code>true</code> | ||
* @param escapeForwardSlash TODO | ||
* @return the escaped string | ||
*/ | ||
private static String escapeJavaStyleString(String str, boolean escapeSingleQuotes, boolean escapeForwardSlash) throws IOException { | ||
if (str == null) { | ||
return null; | ||
} else { | ||
StringWriter writer = new StringWriter(str.length() * 2); | ||
escapeJavaStyleString(writer, str, escapeSingleQuotes, escapeForwardSlash); | ||
return writer.toString(); | ||
|
||
} | ||
} | ||
|
||
/** | ||
* Escapes the characters in a <code>String</code> using Java String rules. | ||
* <p> | ||
* Deals correctly with quotes and control-chars (tab, backslash, cr, ff, etc.) | ||
* <p> | ||
* So a tab becomes the characters <code>'\\'</code> and <code>'t'</code>. | ||
* <p> | ||
* The only difference between Java strings and JavaScript strings | ||
* is that in JavaScript, a single quote must be escaped. | ||
* <p> | ||
* Example: | ||
* <pre> | ||
* input string: He didn't say, "Stop!" | ||
* output string: He didn't say, \"Stop!\" | ||
* </pre> | ||
* | ||
* @param str String to escape values in, may be null | ||
* @return String with escaped values, <code>null</code> if null string input | ||
*/ | ||
public static String escapeJava(String str) throws IOException { | ||
return escapeJavaStyleString(str, false, false); | ||
} | ||
} |
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why are we abandoning the previous member variable
eventFormatter
(classEventFormatter
) and replacing with a hard-codedStringEscapeUtils.convert
static method? Can we instead improve the existing EventFormatter implementation to add this functionality? Then the unit test can be moved to the existingEventFormatterTest
class as well.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missed this, yeah it makes sense to continue routing through the eventFormatter and to just modify the convert method rather than pass it directly to the util.