Skip to content

Commit 1a122f1

Browse files
author
Yaniv Inbar
committed
UrlEncodedContent.getContent and don't allow null data
http://codereview.appspot.com/5427043/
1 parent e8ae708 commit 1a122f1

File tree

4 files changed

+103
-10
lines changed

4 files changed

+103
-10
lines changed

google-http-client/src/main/java/com/google/api/client/http/BasicAuthentication.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
package com.google.api.client.http;
1616

17+
import com.google.common.base.Preconditions;
18+
1719
import java.io.IOException;
1820

1921
/**
@@ -34,8 +36,8 @@ public final class BasicAuthentication implements HttpRequestInitializer {
3436
private final String password;
3537

3638
public BasicAuthentication(String username, String password) {
37-
this.username = username;
38-
this.password = password;
39+
this.username = Preconditions.checkNotNull(username);
40+
this.password = Preconditions.checkNotNull(password);
3941
}
4042

4143
/**

google-http-client/src/main/java/com/google/api/client/http/HttpHeaders.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.google.api.client.util.GenericData;
2020
import com.google.api.client.util.Key;
2121
import com.google.api.client.util.Strings;
22+
import com.google.common.base.Preconditions;
2223

2324
import java.util.HashMap;
2425

@@ -583,8 +584,9 @@ public final void setAuthenticate(String authenticate) {
583584
* @since 1.2
584585
*/
585586
public final void setBasicAuthentication(String username, String password) {
586-
String encoded =
587-
Strings.fromBytesUtf8(Base64.encode(Strings.toBytesUtf8(username + ":" + password)));
587+
String userPass =
588+
Preconditions.checkNotNull(username) + ":" + Preconditions.checkNotNull(password);
589+
String encoded = Strings.fromBytesUtf8(Base64.encode(Strings.toBytesUtf8(userPass)));
588590
authorization = "Basic " + encoded;
589591
}
590592

google-http-client/src/main/java/com/google/api/client/http/UrlEncodedContent.java

Lines changed: 73 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@
2525
import java.io.OutputStream;
2626
import java.io.OutputStreamWriter;
2727
import java.io.Writer;
28+
import java.util.HashMap;
2829
import java.util.Map;
30+
import java.util.logging.Logger;
2931

3032
/**
3133
* Implements support for HTTP form content encoding serialization of type {@code
@@ -53,19 +55,39 @@ static void setContent(HttpRequest request, Object item) {
5355
*/
5456
public class UrlEncodedContent extends AbstractHttpContent {
5557

56-
/** Content type. Default value is {@link UrlEncodedParser#CONTENT_TYPE}. */
58+
private static final Logger LOGGER = Logger.getLogger(UrlEncodedContent.class.getName());
59+
60+
/**
61+
* Content type or {@code null} for none.
62+
*
63+
* <p>
64+
* Default value is {@link UrlEncodedParser#CONTENT_TYPE}.
65+
* </p>
66+
*/
5767
private String contentType = UrlEncodedParser.CONTENT_TYPE;
5868

59-
/** Key name/value data or {@code null} for none. */
69+
/** Key name/value data. */
6070
private Object data;
6171

6272
/**
63-
* @param data key name/value data or {@code null} for none
73+
* <p>
74+
* Upgrade warning: prior to version 1.7, the {@code data} parameter could be {@code null} but now
75+
* {@code null} is not allowed and instead a new instance of an implementation of {@link Map} will
76+
* be created. In version 1.8 a {@link NullPointerException} will be thrown instead.
77+
* </p>
78+
*
79+
* @param data key name/value data
6480
*/
6581
public UrlEncodedContent(Object data) {
66-
this.data = data;
82+
setData(data);
6783
}
6884

85+
/**
86+
* {@inheritDoc}
87+
* <p>
88+
* Upgrade warning: overriding this method is no longer supported, and will be made final in 1.8.
89+
* </p>
90+
*/
6991
public String getType() {
7092
return contentType;
7193
}
@@ -94,7 +116,11 @@ public void writeTo(OutputStream out) throws IOException {
94116
* Sets the content type or {@code null} for none.
95117
*
96118
* <p>
97-
* Defaults to {@link UrlEncodedParser#CONTENT_TYPE}.
119+
* Default value is {@link UrlEncodedParser#CONTENT_TYPE}.
120+
* </p>
121+
* <p>
122+
* Overriding is only supported for the purpose of calling the super implementation and changing
123+
* the return type, but nothing else.
98124
* </p>
99125
*
100126
* @since 1.5
@@ -107,22 +133,63 @@ public UrlEncodedContent setType(String type) {
107133
/**
108134
* Returns the key name/value data or {@code null} for none.
109135
*
136+
* <p>
137+
* Upgrade warning: prior to version 1.7 this could return {@code null} but now it always returns
138+
* a {@code non-null} value. Also overriding this method is no longer supported, and will be made
139+
* final in 1.8.
140+
* </p>
141+
*
110142
* @since 1.5
111143
*/
112144
public Object getData() {
113145
return data;
114146
}
115147

116148
/**
117-
* Sets the key name/value data or {@code null} for none.
149+
* Sets the key name/value data.
150+
*
151+
* <p>
152+
* Overriding is only supported for the purpose of calling the super implementation and changing
153+
* the return type, but nothing else.
154+
* </p>
155+
* <p>
156+
* Upgrade warning: prior to version 1.7, the {@code data} parameter could be {@code null} but now
157+
* {@code null} is not allowed and instead a new instance of an implementation of {@link Map} will
158+
* be created. In version 1.8 a {@link NullPointerException} will be thrown instead.
159+
* </p>
118160
*
119161
* @since 1.5
120162
*/
121163
public UrlEncodedContent setData(Object data) {
164+
if (data == null) {
165+
LOGGER.warning("UrlEncodedContent.setData(null) no longer supported");
166+
data = new HashMap<String, Object>();
167+
}
122168
this.data = data;
123169
return this;
124170
}
125171

172+
/**
173+
* Returns the URL-encoded content of the given HTTP request, or if none return and set as content
174+
* a new instance of {@link UrlEncodedContent} (whose {@link #getData()} is an implementation of
175+
* {@link Map}).
176+
*
177+
* @param request HTTP request
178+
* @return URL-encoded content
179+
* @throws ClassCastException if the HTTP request has a content defined that is not
180+
* {@link UrlEncodedContent}
181+
* @since 1.7
182+
*/
183+
public static UrlEncodedContent getContent(HttpRequest request) {
184+
HttpContent content = request.getContent();
185+
if (content != null) {
186+
return (UrlEncodedContent) content;
187+
}
188+
UrlEncodedContent result = new UrlEncodedContent(new HashMap<String, Object>());
189+
request.setContent(result);
190+
return result;
191+
}
192+
126193
private static boolean appendParam(boolean first, Writer writer, String name, Object value)
127194
throws IOException {
128195
// ignore nulls

google-http-client/src/test/java/com/google/api/client/http/UrlEncodedContentTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,17 @@
1414

1515
package com.google.api.client.http;
1616

17+
import com.google.api.client.testing.http.HttpTesting;
18+
import com.google.api.client.testing.http.MockHttpTransport;
1719
import com.google.api.client.util.ArrayMap;
1820

1921
import junit.framework.TestCase;
2022

2123
import java.io.ByteArrayOutputStream;
2224
import java.io.IOException;
2325
import java.util.Arrays;
26+
import java.util.HashMap;
27+
import java.util.Map;
2428

2529
/**
2630
* Tests {@link UrlEncodedContent}.
@@ -51,4 +55,22 @@ static String toString(Object data) throws IOException {
5155
content.writeTo(out);
5256
return out.toString();
5357
}
58+
59+
public void testGetContent() throws IOException {
60+
HttpRequest request =
61+
new MockHttpTransport().createRequestFactory().buildGetRequest(
62+
HttpTesting.SIMPLE_GENERIC_URL);
63+
UrlEncodedContent content = UrlEncodedContent.getContent(request);
64+
assertNotNull(content);
65+
assertTrue(content.getData() instanceof Map);
66+
assertEquals(content, UrlEncodedContent.getContent(request));
67+
}
68+
69+
public void testGetData() {
70+
UrlEncodedContent content = new UrlEncodedContent(null);
71+
assertTrue(content.getData() instanceof Map);
72+
Map<String, Object> map = new HashMap<String, Object>();
73+
content = new UrlEncodedContent(map);
74+
assertEquals(map, content.getData());
75+
}
5476
}

0 commit comments

Comments
 (0)