From fb98d511ede15527e43deb07c34392f431fdc73e Mon Sep 17 00:00:00 2001
From: ldetmer <1771267+ldetmer@users.noreply.github.com>
Date: Wed, 4 Dec 2024 13:17:36 -0500
Subject: [PATCH 1/4] fix npe

---
 .../http/apache/v5/Apache5HttpResponse.java   |  4 +-
 .../apache/v5/Apache5HttpResponseTest.java    | 48 +++++++++++++++++++
 2 files changed, 51 insertions(+), 1 deletion(-)
 create mode 100644 google-http-client-apache-v5/src/test/java/com/google/api/client/http/apache/v5/Apache5HttpResponseTest.java

diff --git a/google-http-client-apache-v5/src/main/java/com/google/api/client/http/apache/v5/Apache5HttpResponse.java b/google-http-client-apache-v5/src/main/java/com/google/api/client/http/apache/v5/Apache5HttpResponse.java
index 1574c8c89..f38f9bed3 100644
--- a/google-http-client-apache-v5/src/main/java/com/google/api/client/http/apache/v5/Apache5HttpResponse.java
+++ b/google-http-client-apache-v5/src/main/java/com/google/api/client/http/apache/v5/Apache5HttpResponse.java
@@ -46,7 +46,9 @@ public int getStatusCode() {
 
   @Override
   public InputStream getContent() throws IOException {
-    return new Apache5ResponseContent(entity.getContent(), response);
+    HttpEntity entity = response.getEntity();
+    InputStream content = entity == null ? null : entity.getContent();
+    return new Apache5ResponseContent(content, response);
   }
 
   @Override
diff --git a/google-http-client-apache-v5/src/test/java/com/google/api/client/http/apache/v5/Apache5HttpResponseTest.java b/google-http-client-apache-v5/src/test/java/com/google/api/client/http/apache/v5/Apache5HttpResponseTest.java
new file mode 100644
index 000000000..3395e9d64
--- /dev/null
+++ b/google-http-client-apache-v5/src/test/java/com/google/api/client/http/apache/v5/Apache5HttpResponseTest.java
@@ -0,0 +1,48 @@
+/*
+ * Copyright 2019 Google LLC
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
+ * in compliance with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the License
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
+ * or implied. See the License for the specific language governing permissions and limitations under
+ * the License.
+ */
+
+package com.google.api.client.http.apache.v5;
+
+import com.google.api.client.http.*;
+import org.apache.hc.client5.http.classic.methods.*;
+import org.apache.hc.core5.http.*;
+import org.apache.hc.core5.http.io.entity.*;
+import org.apache.hc.core5.http.protocol.*;
+import org.junit.*;
+
+import java.io.*;
+import java.nio.charset.*;
+import java.util.*;
+import java.util.concurrent.atomic.*;
+
+import static org.junit.Assert.*;
+
+public class Apache5HttpResponseTest {
+    @Test
+    public void testNullContent() throws Exception {
+        HttpUriRequestBase base = new HttpPost("http://www.google.com");
+        MockClassicHttpResponse mockResponse = new MockClassicHttpResponse();
+        mockResponse.setEntity(null);
+        Apache5HttpResponse response =
+                new Apache5HttpResponse(
+                        base,
+                        mockResponse);
+
+        InputStream content =
+                response.getContent();
+
+        assertNotNull(content);
+    }
+
+}
\ No newline at end of file

From 416369b7c3492f1e089894db5c3ec7699d600a36 Mon Sep 17 00:00:00 2001
From: ldetmer <1771267+ldetmer@users.noreply.github.com>
Date: Wed, 4 Dec 2024 13:38:17 -0500
Subject: [PATCH 2/4] fix npe

---
 .../http/apache/v5/Apache5HttpResponse.java   |  1 -
 .../apache/v5/Apache5ResponseContent.java     |  8 ++-
 .../apache/v5/Apache5HttpResponseTest.java    |  1 -
 .../apache/v5/Apache5ResponseContentTest.java | 50 +++++++++++++++++++
 4 files changed, 56 insertions(+), 4 deletions(-)
 create mode 100644 google-http-client-apache-v5/src/test/java/com/google/api/client/http/apache/v5/Apache5ResponseContentTest.java

diff --git a/google-http-client-apache-v5/src/main/java/com/google/api/client/http/apache/v5/Apache5HttpResponse.java b/google-http-client-apache-v5/src/main/java/com/google/api/client/http/apache/v5/Apache5HttpResponse.java
index f38f9bed3..ae3c2ffb6 100644
--- a/google-http-client-apache-v5/src/main/java/com/google/api/client/http/apache/v5/Apache5HttpResponse.java
+++ b/google-http-client-apache-v5/src/main/java/com/google/api/client/http/apache/v5/Apache5HttpResponse.java
@@ -46,7 +46,6 @@ public int getStatusCode() {
 
   @Override
   public InputStream getContent() throws IOException {
-    HttpEntity entity = response.getEntity();
     InputStream content = entity == null ? null : entity.getContent();
     return new Apache5ResponseContent(content, response);
   }
diff --git a/google-http-client-apache-v5/src/main/java/com/google/api/client/http/apache/v5/Apache5ResponseContent.java b/google-http-client-apache-v5/src/main/java/com/google/api/client/http/apache/v5/Apache5ResponseContent.java
index c2d3091df..dfb6da8a4 100644
--- a/google-http-client-apache-v5/src/main/java/com/google/api/client/http/apache/v5/Apache5ResponseContent.java
+++ b/google-http-client-apache-v5/src/main/java/com/google/api/client/http/apache/v5/Apache5ResponseContent.java
@@ -59,8 +59,12 @@ public synchronized void reset() throws IOException {
 
   @Override
   public void close() throws IOException {
-    wrappedStream.close();
-    response.close();
+    if (wrappedStream != null) {
+      wrappedStream.close();
+    }
+    if (response != null) {
+      response.close();
+    }
   }
 
   @Override
diff --git a/google-http-client-apache-v5/src/test/java/com/google/api/client/http/apache/v5/Apache5HttpResponseTest.java b/google-http-client-apache-v5/src/test/java/com/google/api/client/http/apache/v5/Apache5HttpResponseTest.java
index 3395e9d64..9385ea1fa 100644
--- a/google-http-client-apache-v5/src/test/java/com/google/api/client/http/apache/v5/Apache5HttpResponseTest.java
+++ b/google-http-client-apache-v5/src/test/java/com/google/api/client/http/apache/v5/Apache5HttpResponseTest.java
@@ -44,5 +44,4 @@ public void testNullContent() throws Exception {
 
         assertNotNull(content);
     }
-
 }
\ No newline at end of file
diff --git a/google-http-client-apache-v5/src/test/java/com/google/api/client/http/apache/v5/Apache5ResponseContentTest.java b/google-http-client-apache-v5/src/test/java/com/google/api/client/http/apache/v5/Apache5ResponseContentTest.java
new file mode 100644
index 000000000..65f7598fd
--- /dev/null
+++ b/google-http-client-apache-v5/src/test/java/com/google/api/client/http/apache/v5/Apache5ResponseContentTest.java
@@ -0,0 +1,50 @@
+/*
+ * Copyright 2019 Google LLC
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
+ * in compliance with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the License
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
+ * or implied. See the License for the specific language governing permissions and limitations under
+ * the License.
+ */
+
+package com.google.api.client.http.apache.v5;
+
+import org.apache.hc.client5.http.classic.methods.*;
+import org.junit.*;
+
+import java.io.*;
+
+import static org.junit.Assert.*;
+
+public class Apache5ResponseContentTest {
+    @Test
+    public void testNullResponseContent_doesNotThrowExceptionOnClose() throws Exception {
+        Apache5ResponseContent response =
+                new Apache5ResponseContent(
+                        new InputStream() {
+                            @Override
+                            public int read() throws IOException {
+                                return 0;
+                            }
+                        },
+                        null);
+
+        response.close();
+    }
+
+    @Test
+    public void testNullWrappedContent_doesNotThrowExceptionOnClose() throws Exception {
+        MockClassicHttpResponse mockResponse = new MockClassicHttpResponse();
+        Apache5ResponseContent response =
+                new Apache5ResponseContent(
+                        null,
+                        mockResponse);
+
+        response.close();
+    }
+}
\ No newline at end of file

From 6036be3f1fb4756630a4cba35c94e70b929294fa Mon Sep 17 00:00:00 2001
From: ldetmer <1771267+ldetmer@users.noreply.github.com>
Date: Wed, 4 Dec 2024 13:42:04 -0500
Subject: [PATCH 3/4] fix npe

---
 .../apache/v5/Apache5HttpResponseTest.java    | 21 +++++++------------
 .../apache/v5/Apache5ResponseContentTest.java | 13 +++++-------
 2 files changed, 12 insertions(+), 22 deletions(-)

diff --git a/google-http-client-apache-v5/src/test/java/com/google/api/client/http/apache/v5/Apache5HttpResponseTest.java b/google-http-client-apache-v5/src/test/java/com/google/api/client/http/apache/v5/Apache5HttpResponseTest.java
index 9385ea1fa..694b3f37e 100644
--- a/google-http-client-apache-v5/src/test/java/com/google/api/client/http/apache/v5/Apache5HttpResponseTest.java
+++ b/google-http-client-apache-v5/src/test/java/com/google/api/client/http/apache/v5/Apache5HttpResponseTest.java
@@ -1,5 +1,5 @@
 /*
- * Copyright 2019 Google LLC
+ * Copyright 2024 Google LLC
  *
  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
  * in compliance with the License. You may obtain a copy of the License at
@@ -14,19 +14,12 @@
 
 package com.google.api.client.http.apache.v5;
 
-import com.google.api.client.http.*;
-import org.apache.hc.client5.http.classic.methods.*;
-import org.apache.hc.core5.http.*;
-import org.apache.hc.core5.http.io.entity.*;
-import org.apache.hc.core5.http.protocol.*;
-import org.junit.*;
+import org.apache.hc.client5.http.classic.methods.HttpPost;
+import org.apache.hc.client5.http.classic.methods.HttpUriRequestBase;
+import org.junit.Test;
+import java.io.InputStream;
 
-import java.io.*;
-import java.nio.charset.*;
-import java.util.*;
-import java.util.concurrent.atomic.*;
-
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertNotNull;
 
 public class Apache5HttpResponseTest {
     @Test
@@ -44,4 +37,4 @@ public void testNullContent() throws Exception {
 
         assertNotNull(content);
     }
-}
\ No newline at end of file
+}
diff --git a/google-http-client-apache-v5/src/test/java/com/google/api/client/http/apache/v5/Apache5ResponseContentTest.java b/google-http-client-apache-v5/src/test/java/com/google/api/client/http/apache/v5/Apache5ResponseContentTest.java
index 65f7598fd..1c4e4ce61 100644
--- a/google-http-client-apache-v5/src/test/java/com/google/api/client/http/apache/v5/Apache5ResponseContentTest.java
+++ b/google-http-client-apache-v5/src/test/java/com/google/api/client/http/apache/v5/Apache5ResponseContentTest.java
@@ -1,5 +1,5 @@
 /*
- * Copyright 2019 Google LLC
+ * Copyright 2024 Google LLC
  *
  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
  * in compliance with the License. You may obtain a copy of the License at
@@ -14,12 +14,9 @@
 
 package com.google.api.client.http.apache.v5;
 
-import org.apache.hc.client5.http.classic.methods.*;
-import org.junit.*;
-
-import java.io.*;
-
-import static org.junit.Assert.*;
+import java.io.IOException;
+import java.io.InputStream;
+import org.junit.Test;
 
 public class Apache5ResponseContentTest {
     @Test
@@ -47,4 +44,4 @@ public void testNullWrappedContent_doesNotThrowExceptionOnClose() throws Excepti
 
         response.close();
     }
-}
\ No newline at end of file
+}

From cd968697c9b7a855d6bf910f90ed709699437798 Mon Sep 17 00:00:00 2001
From: ldetmer <1771267+ldetmer@users.noreply.github.com>
Date: Wed, 4 Dec 2024 13:45:48 -0500
Subject: [PATCH 4/4] fix npe

---
 .../apache/v5/Apache5HttpResponseTest.java    | 28 ++++++-------
 .../apache/v5/Apache5ResponseContentTest.java | 41 +++++++++----------
 2 files changed, 31 insertions(+), 38 deletions(-)

diff --git a/google-http-client-apache-v5/src/test/java/com/google/api/client/http/apache/v5/Apache5HttpResponseTest.java b/google-http-client-apache-v5/src/test/java/com/google/api/client/http/apache/v5/Apache5HttpResponseTest.java
index 694b3f37e..d2712b356 100644
--- a/google-http-client-apache-v5/src/test/java/com/google/api/client/http/apache/v5/Apache5HttpResponseTest.java
+++ b/google-http-client-apache-v5/src/test/java/com/google/api/client/http/apache/v5/Apache5HttpResponseTest.java
@@ -14,27 +14,23 @@
 
 package com.google.api.client.http.apache.v5;
 
+import static org.junit.Assert.assertNotNull;
+
+import java.io.InputStream;
 import org.apache.hc.client5.http.classic.methods.HttpPost;
 import org.apache.hc.client5.http.classic.methods.HttpUriRequestBase;
 import org.junit.Test;
-import java.io.InputStream;
-
-import static org.junit.Assert.assertNotNull;
 
 public class Apache5HttpResponseTest {
-    @Test
-    public void testNullContent() throws Exception {
-        HttpUriRequestBase base = new HttpPost("http://www.google.com");
-        MockClassicHttpResponse mockResponse = new MockClassicHttpResponse();
-        mockResponse.setEntity(null);
-        Apache5HttpResponse response =
-                new Apache5HttpResponse(
-                        base,
-                        mockResponse);
+  @Test
+  public void testNullContent() throws Exception {
+    HttpUriRequestBase base = new HttpPost("http://www.google.com");
+    MockClassicHttpResponse mockResponse = new MockClassicHttpResponse();
+    mockResponse.setEntity(null);
+    Apache5HttpResponse response = new Apache5HttpResponse(base, mockResponse);
 
-        InputStream content =
-                response.getContent();
+    InputStream content = response.getContent();
 
-        assertNotNull(content);
-    }
+    assertNotNull(content);
+  }
 }
diff --git a/google-http-client-apache-v5/src/test/java/com/google/api/client/http/apache/v5/Apache5ResponseContentTest.java b/google-http-client-apache-v5/src/test/java/com/google/api/client/http/apache/v5/Apache5ResponseContentTest.java
index 1c4e4ce61..ddbda0dd5 100644
--- a/google-http-client-apache-v5/src/test/java/com/google/api/client/http/apache/v5/Apache5ResponseContentTest.java
+++ b/google-http-client-apache-v5/src/test/java/com/google/api/client/http/apache/v5/Apache5ResponseContentTest.java
@@ -19,29 +19,26 @@
 import org.junit.Test;
 
 public class Apache5ResponseContentTest {
-    @Test
-    public void testNullResponseContent_doesNotThrowExceptionOnClose() throws Exception {
-        Apache5ResponseContent response =
-                new Apache5ResponseContent(
-                        new InputStream() {
-                            @Override
-                            public int read() throws IOException {
-                                return 0;
-                            }
-                        },
-                        null);
+  @Test
+  public void testNullResponseContent_doesNotThrowExceptionOnClose() throws Exception {
+    Apache5ResponseContent response =
+        new Apache5ResponseContent(
+            new InputStream() {
+              @Override
+              public int read() throws IOException {
+                return 0;
+              }
+            },
+            null);
 
-        response.close();
-    }
+    response.close();
+  }
 
-    @Test
-    public void testNullWrappedContent_doesNotThrowExceptionOnClose() throws Exception {
-        MockClassicHttpResponse mockResponse = new MockClassicHttpResponse();
-        Apache5ResponseContent response =
-                new Apache5ResponseContent(
-                        null,
-                        mockResponse);
+  @Test
+  public void testNullWrappedContent_doesNotThrowExceptionOnClose() throws Exception {
+    MockClassicHttpResponse mockResponse = new MockClassicHttpResponse();
+    Apache5ResponseContent response = new Apache5ResponseContent(null, mockResponse);
 
-        response.close();
-    }
+    response.close();
+  }
 }