Skip to content

Commit 12c742b

Browse files
authoredDec 4, 2024··
fix: NPE if response entity is null (#2043)
* fix npe * fix npe * fix npe * fix npe
1 parent 3453864 commit 12c742b

File tree

4 files changed

+88
-3
lines changed

4 files changed

+88
-3
lines changed
 

‎google-http-client-apache-v5/src/main/java/com/google/api/client/http/apache/v5/Apache5HttpResponse.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ public int getStatusCode() {
4646

4747
@Override
4848
public InputStream getContent() throws IOException {
49-
return new Apache5ResponseContent(entity.getContent(), response);
49+
InputStream content = entity == null ? null : entity.getContent();
50+
return new Apache5ResponseContent(content, response);
5051
}
5152

5253
@Override

‎google-http-client-apache-v5/src/main/java/com/google/api/client/http/apache/v5/Apache5ResponseContent.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,12 @@ public synchronized void reset() throws IOException {
5959

6060
@Override
6161
public void close() throws IOException {
62-
wrappedStream.close();
63-
response.close();
62+
if (wrappedStream != null) {
63+
wrappedStream.close();
64+
}
65+
if (response != null) {
66+
response.close();
67+
}
6468
}
6569

6670
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
15+
package com.google.api.client.http.apache.v5;
16+
17+
import static org.junit.Assert.assertNotNull;
18+
19+
import java.io.InputStream;
20+
import org.apache.hc.client5.http.classic.methods.HttpPost;
21+
import org.apache.hc.client5.http.classic.methods.HttpUriRequestBase;
22+
import org.junit.Test;
23+
24+
public class Apache5HttpResponseTest {
25+
@Test
26+
public void testNullContent() throws Exception {
27+
HttpUriRequestBase base = new HttpPost("http://www.google.com");
28+
MockClassicHttpResponse mockResponse = new MockClassicHttpResponse();
29+
mockResponse.setEntity(null);
30+
Apache5HttpResponse response = new Apache5HttpResponse(base, mockResponse);
31+
32+
InputStream content = response.getContent();
33+
34+
assertNotNull(content);
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
15+
package com.google.api.client.http.apache.v5;
16+
17+
import java.io.IOException;
18+
import java.io.InputStream;
19+
import org.junit.Test;
20+
21+
public class Apache5ResponseContentTest {
22+
@Test
23+
public void testNullResponseContent_doesNotThrowExceptionOnClose() throws Exception {
24+
Apache5ResponseContent response =
25+
new Apache5ResponseContent(
26+
new InputStream() {
27+
@Override
28+
public int read() throws IOException {
29+
return 0;
30+
}
31+
},
32+
null);
33+
34+
response.close();
35+
}
36+
37+
@Test
38+
public void testNullWrappedContent_doesNotThrowExceptionOnClose() throws Exception {
39+
MockClassicHttpResponse mockResponse = new MockClassicHttpResponse();
40+
Apache5ResponseContent response = new Apache5ResponseContent(null, mockResponse);
41+
42+
response.close();
43+
}
44+
}

0 commit comments

Comments
 (0)
Please sign in to comment.