42
42
*
43
43
* <p>Implementation is not thread-safe.
44
44
*
45
- * @since 1.0
46
45
* @author Yaniv Inbar
46
+ * @since 1.0
47
47
*/
48
48
public class UrlEncodedContent extends AbstractHttpContent {
49
49
50
50
/** Key name/value data. */
51
51
private Object data ;
52
52
53
- /** @param data key name/value data */
53
+ /** Use URI Path encoder flag. False by default (use legacy and deprecated escapeUri) */
54
+ private boolean uriPathEncodingFlag ;
55
+
56
+ /**
57
+ * Initialize the UrlEncodedContent with the legacy and deprecated escapeUri encoder
58
+ *
59
+ * @param data key name/value data
60
+ */
54
61
public UrlEncodedContent (Object data ) {
55
62
super (UrlEncodedParser .MEDIA_TYPE );
56
63
setData (data );
64
+ this .uriPathEncodingFlag = false ;
57
65
}
58
66
67
+ /**
68
+ * Initialize the UrlEncodedContent with or without the legacy and deprecated escapeUri encoder
69
+ *
70
+ * @param data key name/value data
71
+ * @param useUriPathEncoding escapes the string value so it can be safely included in URI path
72
+ * segments. For details on escaping URIs, see <a
73
+ * href="http://tools.ietf.org/html/rfc3986#section-2.4">RFC 3986 - section 2.4</a>
74
+ */
75
+ public UrlEncodedContent (Object data , boolean useUriPathEncoding ) {
76
+ super (UrlEncodedParser .MEDIA_TYPE );
77
+ setData (data );
78
+ this .uriPathEncodingFlag = useUriPathEncoding ;
79
+ }
80
+
81
+ @ Override
59
82
public void writeTo (OutputStream out ) throws IOException {
60
83
Writer writer = new BufferedWriter (new OutputStreamWriter (out , getCharset ()));
61
84
boolean first = true ;
@@ -66,10 +89,10 @@ public void writeTo(OutputStream out) throws IOException {
66
89
Class <? extends Object > valueClass = value .getClass ();
67
90
if (value instanceof Iterable <?> || valueClass .isArray ()) {
68
91
for (Object repeatedValue : Types .iterableOf (value )) {
69
- first = appendParam (first , writer , name , repeatedValue );
92
+ first = appendParam (first , writer , name , repeatedValue , this . uriPathEncodingFlag );
70
93
}
71
94
} else {
72
- first = appendParam (first , writer , name , value );
95
+ first = appendParam (first , writer , name , value , this . uriPathEncodingFlag );
73
96
}
74
97
}
75
98
}
@@ -125,7 +148,8 @@ public static UrlEncodedContent getContent(HttpRequest request) {
125
148
return result ;
126
149
}
127
150
128
- private static boolean appendParam (boolean first , Writer writer , String name , Object value )
151
+ private static boolean appendParam (
152
+ boolean first , Writer writer , String name , Object value , boolean uriPathEncodingFlag )
129
153
throws IOException {
130
154
// ignore nulls
131
155
if (value == null || Data .isNull (value )) {
@@ -139,8 +163,13 @@ private static boolean appendParam(boolean first, Writer writer, String name, Ob
139
163
}
140
164
writer .write (name );
141
165
String stringValue =
142
- CharEscapers .escapeUri (
143
- value instanceof Enum <?> ? FieldInfo .of ((Enum <?>) value ).getName () : value .toString ());
166
+ value instanceof Enum <?> ? FieldInfo .of ((Enum <?>) value ).getName () : value .toString ();
167
+
168
+ if (uriPathEncodingFlag ) {
169
+ stringValue = CharEscapers .escapeUriPath (stringValue );
170
+ } else {
171
+ stringValue = CharEscapers .escapeUri (stringValue );
172
+ }
144
173
if (stringValue .length () != 0 ) {
145
174
writer .write ("=" );
146
175
writer .write (stringValue );
0 commit comments