4242 *
4343 * <p>Implementation is not thread-safe.
4444 *
45- * @since 1.0
4645 * @author Yaniv Inbar
46+ * @since 1.0
4747 */
4848public class UrlEncodedContent extends AbstractHttpContent {
4949
5050 /** Key name/value data. */
5151 private Object data ;
5252
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+ */
5461 public UrlEncodedContent (Object data ) {
5562 super (UrlEncodedParser .MEDIA_TYPE );
5663 setData (data );
64+ this .uriPathEncodingFlag = false ;
5765 }
5866
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
5982 public void writeTo (OutputStream out ) throws IOException {
6083 Writer writer = new BufferedWriter (new OutputStreamWriter (out , getCharset ()));
6184 boolean first = true ;
@@ -66,10 +89,10 @@ public void writeTo(OutputStream out) throws IOException {
6689 Class <? extends Object > valueClass = value .getClass ();
6790 if (value instanceof Iterable <?> || valueClass .isArray ()) {
6891 for (Object repeatedValue : Types .iterableOf (value )) {
69- first = appendParam (first , writer , name , repeatedValue );
92+ first = appendParam (first , writer , name , repeatedValue , this . uriPathEncodingFlag );
7093 }
7194 } else {
72- first = appendParam (first , writer , name , value );
95+ first = appendParam (first , writer , name , value , this . uriPathEncodingFlag );
7396 }
7497 }
7598 }
@@ -125,7 +148,8 @@ public static UrlEncodedContent getContent(HttpRequest request) {
125148 return result ;
126149 }
127150
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 )
129153 throws IOException {
130154 // ignore nulls
131155 if (value == null || Data .isNull (value )) {
@@ -139,8 +163,13 @@ private static boolean appendParam(boolean first, Writer writer, String name, Ob
139163 }
140164 writer .write (name );
141165 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+ }
144173 if (stringValue .length () != 0 ) {
145174 writer .write ("=" );
146175 writer .write (stringValue );
0 commit comments