Skip to content

Commit a807df2

Browse files
committed
Most of the logic of this class has moved, contains only sanitize URL logic
1 parent d03faf3 commit a807df2

File tree

2 files changed

+282
-218
lines changed

2 files changed

+282
-218
lines changed

core/src/main/java/com/microsoft/applicationinsights/internal/util/Sanitizer.java

Lines changed: 73 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,33 @@
2222
package com.microsoft.applicationinsights.internal.util;
2323

2424
import java.net.URI;
25+
import java.text.StringCharacterIterator;
2526
import java.util.HashMap;
2627
import java.util.Map;
2728
import java.util.UUID;
2829

2930
import com.google.common.base.Strings;
31+
import org.apache.http.annotation.Obsolete;
3032

3133
/**
3234
* Created by gupele on 1/7/2015.
35+
*
36+
* Most of the methods of this class are now obsolete except URL methods which will
37+
* be moved soon.
3338
*/
3439
public final class Sanitizer {
3540
public final static int MAX_MAP_NAME_LENGTH = 150;
36-
public final static int MAX_VALUE_LENGTH = 1024;
41+
42+
// Schema V2 allows max length to be 8192
43+
public final static int MAX_VALUE_LENGTH = 8192;
44+
3745
public final static int MAX_NAME_LENGTH = 1024;
3846
public final static int MAX_MESSAGE_LENGTH = 32768;
3947
public final static int MAX_URL_LENGTH = 2048;
4048

4149
private final static String INVALID_NAME_CHARACTERS = "[^0-9a-zA-Z-._()\\/ ]";
4250

51+
@Obsolete
4352
public static void sanitizeProperties(Map<String, String> map) {
4453
if (map != null) {
4554
HashMap<String, String> tempMap = new HashMap<String, String>(map.size());
@@ -55,6 +64,7 @@ public static void sanitizeProperties(Map<String, String> map) {
5564
}
5665
}
5766

67+
@Obsolete
5868
public static void sanitizeMeasurements(Map<String, Double> map) {
5969
if (map != null) {
6070
HashMap<String, Double> tempMap = new HashMap<String, Double>(map.size());
@@ -90,18 +100,24 @@ public static URI sanitizeUri(String urlAsString) {
90100
return null;
91101
}
92102

103+
@Obsolete
93104
public static String sanitizeValue(String value) {
94-
return trimAndTruncate(value, MAX_VALUE_LENGTH);
105+
String truncatedString = trimAndTruncate(value, MAX_VALUE_LENGTH);
106+
String sanitizedString = sanitizeStringForJSON(truncatedString, MAX_VALUE_LENGTH);
107+
return sanitizedString;
95108
}
96109

110+
@Obsolete
97111
public static String sanitizeName(String name) {
98112
return trimAndTruncate(name, MAX_NAME_LENGTH);
99113
}
100114

115+
@Obsolete
101116
public static String sanitizeMessage(String message) {
102117
return trimAndTruncate(message, MAX_MESSAGE_LENGTH);
103118
}
104119

120+
@Obsolete
105121
public static boolean isUUID(String possibleUUID) {
106122
try {
107123
UUID.fromString(possibleUUID);
@@ -126,15 +142,16 @@ public static URI safeStringToUri(String url) {
126142
return result;
127143
}
128144

145+
@Obsolete
129146
private static <V> String sanitizeKey(String key, Map<String, V> map) {
130147
String sanitizedKey = trimAndTruncate(key, MAX_MAP_NAME_LENGTH);
131-
132-
sanitizedKey = sanitizedKey.replaceAll(INVALID_NAME_CHARACTERS, "");
148+
sanitizedKey = sanitizeStringForJSON(sanitizedKey, MAX_MAP_NAME_LENGTH);
133149
sanitizedKey = MakeKeyNonEmpty(sanitizedKey);
134150
sanitizedKey = MakeKeyUnique(sanitizedKey, map);
135151
return sanitizedKey;
136152
}
137153

154+
@Obsolete
138155
private static String trimAndTruncate(String value, int maxLength) {
139156
if (value == null) {
140157
return value;
@@ -148,10 +165,12 @@ private static String trimAndTruncate(String value, int maxLength) {
148165
return sanitized;
149166
}
150167

168+
@Obsolete
151169
private static String MakeKeyNonEmpty(String key) {
152170
return Strings.isNullOrEmpty(key) ? "(required property name is empty)" : key;
153171
}
154172

173+
@Obsolete
155174
private static <V> String MakeKeyUnique(String key, Map<String, V> map)
156175
{
157176
if (map.containsKey(key)) {
@@ -168,7 +187,57 @@ private static <V> String MakeKeyUnique(String key, Map<String, V> map)
168187
return key;
169188
}
170189

190+
@Obsolete
171191
private static String truncate(String value, int maxLength) {
172192
return value.length() > maxLength ? value.substring(0, maxLength) : value;
173193
}
194+
195+
@Obsolete
196+
private static String sanitizeStringForJSON(String text, int maxLength) {
197+
198+
final StringBuilder result = new StringBuilder();
199+
StringCharacterIterator iterator = new StringCharacterIterator(text);
200+
for (char curr = iterator.current(); curr != iterator.DONE && result.length() < maxLength - 2; curr = iterator.next()) {
201+
if( curr == '\"' ){
202+
result.append("\\\"");
203+
}
204+
else if (curr == '\'') {
205+
result.append("\\\'");
206+
}
207+
else if(curr == '\\'){
208+
result.append("\\\\");
209+
}
210+
else if(curr == '/'){
211+
result.append("\\/");
212+
}
213+
else if(curr == '\b'){
214+
result.append("\\b");
215+
}
216+
else if(curr == '\f'){
217+
result.append("\\f");
218+
}
219+
else if(curr == '\n'){
220+
result.append("\\n");
221+
}
222+
else if(curr == '\r'){
223+
result.append("\\r");
224+
}
225+
else if(curr == '\t'){
226+
result.append("\\t");
227+
}
228+
else if (!Character.isISOControl(curr)){
229+
result.append(curr);
230+
}
231+
else {
232+
if (result.length() + 7 < maxLength) { // needs 7 more character space to be appended
233+
result.append("\\u");
234+
result.append((String.format( "%04x", Integer.valueOf(curr))));
235+
}
236+
else {
237+
break;
238+
}
239+
}
240+
}
241+
return result.toString();
242+
}
174243
}

0 commit comments

Comments
 (0)