From e61cccf08206d8d6f559d223834db0648ad6ed49 Mon Sep 17 00:00:00 2001 From: Guillaume Blaquiere Date: Fri, 28 Aug 2020 11:02:06 +0200 Subject: [PATCH 1/6] Add the possibility to use UriPath encoding for the URL content instead of the deprecated urlEncoder --- .../api/client/http/UrlEncodedContent.java | 33 +++++++++++++++---- .../client/http/UrlEncodedContentTest.java | 27 +++++++++++---- 2 files changed, 47 insertions(+), 13 deletions(-) diff --git a/google-http-client/src/main/java/com/google/api/client/http/UrlEncodedContent.java b/google-http-client/src/main/java/com/google/api/client/http/UrlEncodedContent.java index eb6428b18..71377aaf0 100644 --- a/google-http-client/src/main/java/com/google/api/client/http/UrlEncodedContent.java +++ b/google-http-client/src/main/java/com/google/api/client/http/UrlEncodedContent.java @@ -42,18 +42,33 @@ * *

Implementation is not thread-safe. * - * @since 1.0 * @author Yaniv Inbar + * @since 1.0 */ public class UrlEncodedContent extends AbstractHttpContent { /** Key name/value data. */ private Object data; + /** Use URI Path encoder flag. False by default (use legacy and deprecated escapeUri) */ + private Boolean useEscapeUriPathEncoding; + /** @param data key name/value data */ public UrlEncodedContent(Object data) { super(UrlEncodedParser.MEDIA_TYPE); setData(data); + this.useEscapeUriPathEncoding = false; + } + + /** + * @param data key name/value data + * @param useEscapeUriPathEncoding replace the legacy (and deprecated) URI encoder, by the + * EncodedPathUri encoder + */ + public UrlEncodedContent(Object data, Boolean useEscapeUriPathEncoding) { + super(UrlEncodedParser.MEDIA_TYPE); + setData(data); + this.useEscapeUriPathEncoding = useEscapeUriPathEncoding; } public void writeTo(OutputStream out) throws IOException { @@ -66,10 +81,10 @@ public void writeTo(OutputStream out) throws IOException { Class valueClass = value.getClass(); if (value instanceof Iterable || valueClass.isArray()) { for (Object repeatedValue : Types.iterableOf(value)) { - first = appendParam(first, writer, name, repeatedValue); + first = appendParam(first, writer, name, repeatedValue, this.useEscapeUriPathEncoding); } } else { - first = appendParam(first, writer, name, value); + first = appendParam(first, writer, name, value, this.useEscapeUriPathEncoding); } } } @@ -125,7 +140,8 @@ public static UrlEncodedContent getContent(HttpRequest request) { return result; } - private static boolean appendParam(boolean first, Writer writer, String name, Object value) + private static boolean appendParam( + boolean first, Writer writer, String name, Object value, boolean useEscapePathEncoding) throws IOException { // ignore nulls if (value == null || Data.isNull(value)) { @@ -139,8 +155,13 @@ private static boolean appendParam(boolean first, Writer writer, String name, Ob } writer.write(name); String stringValue = - CharEscapers.escapeUri( - value instanceof Enum ? FieldInfo.of((Enum) value).getName() : value.toString()); + value instanceof Enum ? FieldInfo.of((Enum) value).getName() : value.toString(); + + if (useEscapePathEncoding) { + stringValue = CharEscapers.escapeUriPath(stringValue); + } else { + stringValue = CharEscapers.escapeUri(stringValue); + } if (stringValue.length() != 0) { writer.write("="); writer.write(stringValue); diff --git a/google-http-client/src/test/java/com/google/api/client/http/UrlEncodedContentTest.java b/google-http-client/src/test/java/com/google/api/client/http/UrlEncodedContentTest.java index f0e0768a9..059d9e2c9 100644 --- a/google-http-client/src/test/java/com/google/api/client/http/UrlEncodedContentTest.java +++ b/google-http-client/src/test/java/com/google/api/client/http/UrlEncodedContentTest.java @@ -33,19 +33,32 @@ public class UrlEncodedContentTest extends TestCase { public void testWriteTo() throws IOException { - subtestWriteTo("a=x", ArrayMap.of("a", "x")); - subtestWriteTo("noval", ArrayMap.of("noval", "")); - subtestWriteTo("multi=a&multi=b&multi=c", ArrayMap.of("multi", Arrays.asList("a", "b", "c"))); - subtestWriteTo("multi=a&multi=b&multi=c", ArrayMap.of("multi", new String[] {"a", "b", "c"})); + subtestWriteTo("a=x", ArrayMap.of("a", "x"), false); + subtestWriteTo("noval", ArrayMap.of("noval", ""), false); + subtestWriteTo( + "multi=a&multi=b&multi=c", ArrayMap.of("multi", Arrays.asList("a", "b", "c")), false); + subtestWriteTo( + "multi=a&multi=b&multi=c", ArrayMap.of("multi", new String[] {"a", "b", "c"}), false); // https://github.com/googleapis/google-http-java-client/issues/202 final Map params = new LinkedHashMap(); params.put("username", "un"); params.put("password", "password123;{}"); - subtestWriteTo("username=un&password=password123%3B%7B%7D", params); + subtestWriteTo("username=un&password=password123%3B%7B%7D", params, false); + subtestWriteTo("additionkey=add%2Btion", ArrayMap.of("additionkey", "add+tion"), false); + + subtestWriteTo("a=x", ArrayMap.of("a", "x"), true); + subtestWriteTo("noval", ArrayMap.of("noval", ""), true); + subtestWriteTo( + "multi=a&multi=b&multi=c", ArrayMap.of("multi", Arrays.asList("a", "b", "c")), true); + subtestWriteTo( + "multi=a&multi=b&multi=c", ArrayMap.of("multi", new String[] {"a", "b", "c"}), true); + subtestWriteTo("username=un&password=password123;%7B%7D", params, true); + subtestWriteTo("additionkey=add+tion", ArrayMap.of("additionkey", "add+tion"), true); } - private void subtestWriteTo(String expected, Object data) throws IOException { - UrlEncodedContent content = new UrlEncodedContent(data); + private void subtestWriteTo(String expected, Object data, boolean useEscapeUriPathEncoding) + throws IOException { + UrlEncodedContent content = new UrlEncodedContent(data, useEscapeUriPathEncoding); ByteArrayOutputStream out = new ByteArrayOutputStream(); content.writeTo(out); assertEquals(expected, out.toString()); From 6b50a0430687c657848dbce7ee546930445314b8 Mon Sep 17 00:00:00 2001 From: Guillaume Blaquiere Date: Sat, 29 Aug 2020 20:24:57 +0200 Subject: [PATCH 2/6] Use boolean primitive type. Improve variable naming Improve documentation more user oriented. --- .../api/client/http/UrlEncodedContent.java | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/google-http-client/src/main/java/com/google/api/client/http/UrlEncodedContent.java b/google-http-client/src/main/java/com/google/api/client/http/UrlEncodedContent.java index 71377aaf0..e254e60a6 100644 --- a/google-http-client/src/main/java/com/google/api/client/http/UrlEncodedContent.java +++ b/google-http-client/src/main/java/com/google/api/client/http/UrlEncodedContent.java @@ -51,24 +51,25 @@ public class UrlEncodedContent extends AbstractHttpContent { private Object data; /** Use URI Path encoder flag. False by default (use legacy and deprecated escapeUri) */ - private Boolean useEscapeUriPathEncoding; + private boolean uriPathEncodingFlag; /** @param data key name/value data */ public UrlEncodedContent(Object data) { super(UrlEncodedParser.MEDIA_TYPE); setData(data); - this.useEscapeUriPathEncoding = false; + this.uriPathEncodingFlag = false; } /** * @param data key name/value data - * @param useEscapeUriPathEncoding replace the legacy (and deprecated) URI encoder, by the - * EncodedPathUri encoder + * @param useUriPathEncoding Escapes the string value so it can be safely included in URI path segments. For details on + * * escaping URIs, see RFC 3986 - section + * * 2.4 */ - public UrlEncodedContent(Object data, Boolean useEscapeUriPathEncoding) { + public UrlEncodedContent(Object data, Boolean useUriPathEncoding) { super(UrlEncodedParser.MEDIA_TYPE); setData(data); - this.useEscapeUriPathEncoding = useEscapeUriPathEncoding; + this.uriPathEncodingFlag = useUriPathEncoding; } public void writeTo(OutputStream out) throws IOException { @@ -81,10 +82,10 @@ public void writeTo(OutputStream out) throws IOException { Class valueClass = value.getClass(); if (value instanceof Iterable || valueClass.isArray()) { for (Object repeatedValue : Types.iterableOf(value)) { - first = appendParam(first, writer, name, repeatedValue, this.useEscapeUriPathEncoding); + first = appendParam(first, writer, name, repeatedValue, this.uriPathEncodingFlag); } } else { - first = appendParam(first, writer, name, value, this.useEscapeUriPathEncoding); + first = appendParam(first, writer, name, value, this.uriPathEncodingFlag); } } } @@ -141,7 +142,7 @@ public static UrlEncodedContent getContent(HttpRequest request) { } private static boolean appendParam( - boolean first, Writer writer, String name, Object value, boolean useEscapePathEncoding) + boolean first, Writer writer, String name, Object value, boolean uriPathEncodingFlag) throws IOException { // ignore nulls if (value == null || Data.isNull(value)) { @@ -157,7 +158,7 @@ private static boolean appendParam( String stringValue = value instanceof Enum ? FieldInfo.of((Enum) value).getName() : value.toString(); - if (useEscapePathEncoding) { + if (uriPathEncodingFlag) { stringValue = CharEscapers.escapeUriPath(stringValue); } else { stringValue = CharEscapers.escapeUri(stringValue); From 681381d7afce5d8fc61c61503645dcf7889afc01 Mon Sep 17 00:00:00 2001 From: Guillaume Blaquiere Date: Mon, 12 Oct 2020 16:45:46 +0200 Subject: [PATCH 3/6] Fix documentation issues --- .../google/api/client/http/UrlEncodedContent.java | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/google-http-client/src/main/java/com/google/api/client/http/UrlEncodedContent.java b/google-http-client/src/main/java/com/google/api/client/http/UrlEncodedContent.java index e254e60a6..d6d9d2a51 100644 --- a/google-http-client/src/main/java/com/google/api/client/http/UrlEncodedContent.java +++ b/google-http-client/src/main/java/com/google/api/client/http/UrlEncodedContent.java @@ -53,7 +53,10 @@ public class UrlEncodedContent extends AbstractHttpContent { /** Use URI Path encoder flag. False by default (use legacy and deprecated escapeUri) */ private boolean uriPathEncodingFlag; - /** @param data key name/value data */ + /** + * Initialize the UrlEncodedContent with the legacy and deprecated escapeUri encoder + * @param data key name/value data + * */ public UrlEncodedContent(Object data) { super(UrlEncodedParser.MEDIA_TYPE); setData(data); @@ -61,10 +64,11 @@ public UrlEncodedContent(Object data) { } /** + * Initialize the UrlEncodedContent with our without the legacy and deprecated escapeUri encoder * @param data key name/value data - * @param useUriPathEncoding Escapes the string value so it can be safely included in URI path segments. For details on - * * escaping URIs, see RFC 3986 - section - * * 2.4 + * @param useUriPathEncoding Escapes the string value so it can be safely included in URI path segments. + * For details on escaping URIs, see RFC 3986 - + * section 2.4 */ public UrlEncodedContent(Object data, Boolean useUriPathEncoding) { super(UrlEncodedParser.MEDIA_TYPE); @@ -72,6 +76,7 @@ public UrlEncodedContent(Object data, Boolean useUriPathEncoding) { this.uriPathEncodingFlag = useUriPathEncoding; } + @Override public void writeTo(OutputStream out) throws IOException { Writer writer = new BufferedWriter(new OutputStreamWriter(out, getCharset())); boolean first = true; From a8d4e8734b096d658b1aff38460e8ed01cc48ed8 Mon Sep 17 00:00:00 2001 From: Guillaume Blaquiere Date: Mon, 12 Oct 2020 20:19:58 +0200 Subject: [PATCH 4/6] Fix documentation typos --- .../java/com/google/api/client/http/UrlEncodedContent.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/google-http-client/src/main/java/com/google/api/client/http/UrlEncodedContent.java b/google-http-client/src/main/java/com/google/api/client/http/UrlEncodedContent.java index d6d9d2a51..ab1a00ac9 100644 --- a/google-http-client/src/main/java/com/google/api/client/http/UrlEncodedContent.java +++ b/google-http-client/src/main/java/com/google/api/client/http/UrlEncodedContent.java @@ -64,13 +64,13 @@ public UrlEncodedContent(Object data) { } /** - * Initialize the UrlEncodedContent with our without the legacy and deprecated escapeUri encoder + * Initialize the UrlEncodedContent with or without the legacy and deprecated escapeUri encoder * @param data key name/value data - * @param useUriPathEncoding Escapes the string value so it can be safely included in URI path segments. + * @param useUriPathEncoding escapes the string value so it can be safely included in URI path segments. * For details on escaping URIs, see RFC 3986 - * section 2.4 */ - public UrlEncodedContent(Object data, Boolean useUriPathEncoding) { + public UrlEncodedContent(Object data, boolean useUriPathEncoding) { super(UrlEncodedParser.MEDIA_TYPE); setData(data); this.uriPathEncodingFlag = useUriPathEncoding; From b528d57ad13e313841cee1b61c89397877a2f1e7 Mon Sep 17 00:00:00 2001 From: Guillaume Blaquiere Date: Mon, 12 Oct 2020 20:19:58 +0200 Subject: [PATCH 5/6] fix: documentation typos. --- .../java/com/google/api/client/http/UrlEncodedContent.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/google-http-client/src/main/java/com/google/api/client/http/UrlEncodedContent.java b/google-http-client/src/main/java/com/google/api/client/http/UrlEncodedContent.java index d6d9d2a51..ab1a00ac9 100644 --- a/google-http-client/src/main/java/com/google/api/client/http/UrlEncodedContent.java +++ b/google-http-client/src/main/java/com/google/api/client/http/UrlEncodedContent.java @@ -64,13 +64,13 @@ public UrlEncodedContent(Object data) { } /** - * Initialize the UrlEncodedContent with our without the legacy and deprecated escapeUri encoder + * Initialize the UrlEncodedContent with or without the legacy and deprecated escapeUri encoder * @param data key name/value data - * @param useUriPathEncoding Escapes the string value so it can be safely included in URI path segments. + * @param useUriPathEncoding escapes the string value so it can be safely included in URI path segments. * For details on escaping URIs, see RFC 3986 - * section 2.4 */ - public UrlEncodedContent(Object data, Boolean useUriPathEncoding) { + public UrlEncodedContent(Object data, boolean useUriPathEncoding) { super(UrlEncodedParser.MEDIA_TYPE); setData(data); this.uriPathEncodingFlag = useUriPathEncoding; From 92ac9ab20b0d2e445c734bdd2fec530321473378 Mon Sep 17 00:00:00 2001 From: Jeff Ching Date: Tue, 13 Oct 2020 09:13:59 -0700 Subject: [PATCH 6/6] chore: fix lint --- .../google/api/client/http/UrlEncodedContent.java | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/google-http-client/src/main/java/com/google/api/client/http/UrlEncodedContent.java b/google-http-client/src/main/java/com/google/api/client/http/UrlEncodedContent.java index ab1a00ac9..ea08b1e18 100644 --- a/google-http-client/src/main/java/com/google/api/client/http/UrlEncodedContent.java +++ b/google-http-client/src/main/java/com/google/api/client/http/UrlEncodedContent.java @@ -54,9 +54,10 @@ public class UrlEncodedContent extends AbstractHttpContent { private boolean uriPathEncodingFlag; /** - * Initialize the UrlEncodedContent with the legacy and deprecated escapeUri encoder - * @param data key name/value data - * */ + * Initialize the UrlEncodedContent with the legacy and deprecated escapeUri encoder + * + * @param data key name/value data + */ public UrlEncodedContent(Object data) { super(UrlEncodedParser.MEDIA_TYPE); setData(data); @@ -65,10 +66,11 @@ public UrlEncodedContent(Object data) { /** * Initialize the UrlEncodedContent with or without the legacy and deprecated escapeUri encoder + * * @param data key name/value data - * @param useUriPathEncoding escapes the string value so it can be safely included in URI path segments. - * For details on escaping URIs, see RFC 3986 - - * section 2.4 + * @param useUriPathEncoding escapes the string value so it can be safely included in URI path + * segments. For details on escaping URIs, see RFC 3986 - section 2.4 */ public UrlEncodedContent(Object data, boolean useUriPathEncoding) { super(UrlEncodedParser.MEDIA_TYPE);