Skip to content

Commit 355474a

Browse files
committed
Disconnecting HttpURLConnection and Socket instances
Signed-off-by: David Matějček <david.matejcek@omnifish.ee>
1 parent ad05fb5 commit 355474a

File tree

33 files changed

+758
-755
lines changed

33 files changed

+758
-755
lines changed

appserver/itest-tools/src/main/java/org/glassfish/main/itest/tools/FormAuthHttpClient.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -107,16 +107,18 @@ public HttpURLConnection get(String relativePath) throws IOException {
107107

108108
private HttpCookie createSession(String relativePath) throws IOException {
109109
final HttpURLConnection connection = openConnection(baseUrl.getPort(), baseUrl.getPath() + "/" + relativePath);
110-
connection.setRequestMethod("GET");
111-
assertThat(connection.getResponseCode(), equalTo(200));
112-
try (InputStream is = connection.getInputStream()) {
113-
final String text = new String(is.readAllBytes(), UTF_8);
114-
assertThat(text,
115-
stringContainsInOrder("<title>Login Page</title>", "Please login", "j_username", "j_password"));
116-
final HttpCookie sessionId = cookieManager.getCookieStore().getCookies().stream()
117-
.filter(c -> c.getName().equals("JSESSIONID")).findFirst().get();
118-
is.readAllBytes();
119-
return sessionId;
110+
try {
111+
connection.setRequestMethod("GET");
112+
assertThat(connection.getResponseCode(), equalTo(200));
113+
try (InputStream is = connection.getInputStream()) {
114+
final String text = new String(is.readAllBytes(), UTF_8);
115+
assertThat(text,
116+
stringContainsInOrder("<title>Login Page</title>", "Please login", "j_username", "j_password"));
117+
final HttpCookie sessionId = cookieManager.getCookieStore().getCookies().stream()
118+
.filter(c -> c.getName().equals("JSESSIONID")).findFirst().get();
119+
is.readAllBytes();
120+
return sessionId;
121+
}
120122
} finally {
121123
connection.disconnect();
122124
}

appserver/tests/admin/tests/src/test/java/org/glassfish/main/admin/test/ConnectionUtils.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.io.IOException;
2020
import java.io.InputStreamReader;
2121
import java.io.StringWriter;
22+
import java.net.HttpURLConnection;
2223
import java.net.URL;
2324
import java.net.URLConnection;
2425

@@ -34,8 +35,12 @@ public class ConnectionUtils {
3435
* @return The string returned from that URL, or empty string if there was a problem contacting the URL
3536
*/
3637
public static String getURL(String urlstr) {
37-
URLConnection urlc = openConnection(urlstr);
38-
return getContent(urlc);
38+
HttpURLConnection urlc = openConnection(urlstr);
39+
try {
40+
return getContent(urlc);
41+
} finally {
42+
urlc.disconnect();
43+
}
3944
}
4045

4146
/**
@@ -45,8 +50,8 @@ public static String getURL(String urlstr) {
4550
* @return The string returned from that connection, or empty string if there was a problem contacting the URL
4651
*/
4752
public static String getContent(URLConnection connection) {
48-
try (
49-
BufferedReader ir = new BufferedReader(new InputStreamReader(connection.getInputStream(), ISO_8859_1)); StringWriter ow = new StringWriter()) {
53+
try (BufferedReader ir = new BufferedReader(new InputStreamReader(connection.getInputStream(), ISO_8859_1));
54+
StringWriter ow = new StringWriter()) {
5055
String line;
5156
while ((line = ir.readLine()) != null) {
5257
ow.write(line);
@@ -58,13 +63,11 @@ public static String getContent(URLConnection connection) {
5863
}
5964
}
6065

61-
public static URLConnection openConnection(String url) {
66+
public static HttpURLConnection openConnection(String url) {
6267
try {
63-
return new URL(url).openConnection();
68+
return (HttpURLConnection) new URL(url).openConnection();
6469
} catch (IOException e) {
6570
throw new IllegalArgumentException(e);
6671
}
6772
}
68-
69-
7073
}

appserver/tests/admin/tests/src/test/java/org/glassfish/main/admin/test/webapp/HttpServerNameITest.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2024 Contributors to the Eclipse Foundation
2+
* Copyright (c) 2024, 2025 Contributors to the Eclipse Foundation
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -55,12 +55,14 @@ public class HttpServerNameITest {
5555
@ParameterizedTest(name = "[{index}] testServerName: {0}")
5656
public void testHostName(String description, String serverName, String expectedUrlPrefix) throws Exception {
5757
assertThat(ASADMIN.exec("set", SERVER_NAME_PROPERTY + "=" + serverName), asadminOK());
58-
5958
final HttpURLConnection conn = GlassFishTestEnvironment.openConnection(HTTP_PORT, "/" + TEST_APP_NAME);
60-
conn.setInstanceFollowRedirects(false);
61-
62-
assertThat(conn.getHeaderField("Location"), stringContainsInOrder(expectedUrlPrefix));
63-
assertThat(getContent(conn), stringContainsInOrder("This document has moved <a href=\"" + expectedUrlPrefix));
59+
try {
60+
conn.setInstanceFollowRedirects(false);
61+
assertThat(conn.getHeaderField("Location"), stringContainsInOrder(expectedUrlPrefix));
62+
assertThat(getContent(conn), stringContainsInOrder("This document has moved <a href=\"" + expectedUrlPrefix));
63+
} finally {
64+
conn.disconnect();
65+
}
6466
}
6567

6668
@BeforeAll

appserver/tests/application/src/test/java/org/glassfish/main/test/app/concurrency/executor/ManagedExecutorDefinitionWebTest.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,12 @@ static void undeploy() {
6767
@Test
6868
void testCopyCompletableFutureTwice() throws Exception {
6969
HttpURLConnection connection = GlassFishTestEnvironment.openConnection(8080, "/");
70-
connection.setRequestMethod("GET");
71-
assertEquals(200, connection.getResponseCode());
70+
try {
71+
connection.setRequestMethod("GET");
72+
assertEquals(200, connection.getResponseCode());
73+
} finally {
74+
connection.disconnect();
75+
}
7276
}
7377

7478

appserver/tests/application/src/test/java/org/glassfish/main/test/app/concurrency/executor/ManagedExecutorTasksSubmittedDuringDeploymentTest.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,14 @@ static void undeploy() {
6565
@Test
6666
void testSubmittedTaskExecuted() throws Exception {
6767
HttpURLConnection connection = GlassFishTestEnvironment.openConnection(8080, "/");
68-
connection.setRequestMethod("GET");
69-
assertEquals(200, connection.getResponseCode());
70-
assertEquals("true", connection.getHeaderField(ManagedExecutorTasksSubmittedDuringDeploymentServlet.HEADER_EXECUTED));
68+
try {
69+
connection.setRequestMethod("GET");
70+
assertEquals(200, connection.getResponseCode());
71+
assertEquals("true",
72+
connection.getHeaderField(ManagedExecutorTasksSubmittedDuringDeploymentServlet.HEADER_EXECUTED));
73+
} finally {
74+
connection.disconnect();
75+
}
7176
}
7277

7378

appserver/tests/application/src/test/java/org/glassfish/main/test/app/ejb/AccessEJBWebTest.java

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2023, 2024 Contributors to the Eclipse Foundation.
2+
* Copyright (c) 2023, 2025 Contributors to the Eclipse Foundation.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -34,6 +34,7 @@
3434
import org.junit.jupiter.api.Test;
3535

3636
import static java.lang.System.Logger.Level.INFO;
37+
import static org.glassfish.main.itest.tools.GlassFishTestEnvironment.openConnection;
3738
import static org.hamcrest.MatcherAssert.assertThat;
3839
import static org.junit.jupiter.api.Assertions.assertAll;
3940
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -64,49 +65,61 @@ static void undeploy() {
6465
@Test
6566
void testAccessLocalEJBByCDI() throws Exception {
6667
String message = RandomGenerator.generateRandomString();
67-
HttpURLConnection connection = GlassFishTestEnvironment
68-
.openConnection(8080, "/local_ejb_cdi?message=" + message);
69-
connection.setRequestMethod("GET");
70-
assertAll(
68+
HttpURLConnection connection = openConnection(8080, "/local_ejb_cdi?message=" + message);
69+
try {
70+
connection.setRequestMethod("GET");
71+
assertAll(
7172
() -> assertEquals(200, connection.getResponseCode()),
7273
() -> assertEquals(message, HttpParser.readResponseInputStream(connection).trim())
73-
);
74+
);
75+
} finally {
76+
connection.disconnect();
77+
}
7478
}
7579

7680
@Test
7781
void testAccessLocalEJBByJNDI() throws Exception {
7882
String message = RandomGenerator.generateRandomString();
79-
HttpURLConnection connection = GlassFishTestEnvironment
80-
.openConnection(8080, "/local_ejb_jndi?message=" + message);
81-
connection.setRequestMethod("GET");
82-
assertAll(
83+
HttpURLConnection connection = openConnection(8080, "/local_ejb_jndi?message=" + message);
84+
try {
85+
connection.setRequestMethod("GET");
86+
assertAll(
8387
() -> assertEquals(200, connection.getResponseCode()),
8488
() -> assertEquals(message, HttpParser.readResponseInputStream(connection).trim())
85-
);
89+
);
90+
} finally {
91+
connection.disconnect();
92+
}
8693
}
8794

8895
@Test
8996
void testAccessRemoteEJBByCDI() throws Exception {
9097
String message = RandomGenerator.generateRandomString();
91-
HttpURLConnection connection = GlassFishTestEnvironment
92-
.openConnection(8080, "/remote_ejb_cdi?message=" + message);
93-
connection.setRequestMethod("GET");
94-
assertAll(
98+
HttpURLConnection connection = openConnection(8080, "/remote_ejb_cdi?message=" + message);
99+
try {
100+
connection.setRequestMethod("GET");
101+
assertAll(
95102
() -> assertEquals(200, connection.getResponseCode()),
96103
() -> assertEquals(message, HttpParser.readResponseInputStream(connection).trim())
97-
);
104+
);
105+
} finally {
106+
connection.disconnect();
107+
}
98108
}
99109

100110
@Test
101111
void testAccessRemoteEJBByJNDI() throws Exception {
102112
String message = RandomGenerator.generateRandomString();
103-
HttpURLConnection connection = GlassFishTestEnvironment
104-
.openConnection(8080, "/remote_ejb_jndi?message=" + message);
105-
connection.setRequestMethod("GET");
106-
assertAll(
113+
HttpURLConnection connection = openConnection(8080, "/remote_ejb_jndi?message=" + message);
114+
try {
115+
connection.setRequestMethod("GET");
116+
assertAll(
107117
() -> assertEquals(200, connection.getResponseCode()),
108118
() -> assertEquals(message, HttpParser.readResponseInputStream(connection).trim())
109-
);
119+
);
120+
} finally {
121+
connection.disconnect();
122+
}
110123
}
111124

112125
private static File createDeployment() {

appserver/tests/application/src/test/java/org/glassfish/main/test/app/ejblinksyntax/EjbDependsOnByFileNameTest.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,18 @@ public static void undeploy() {
8282
@Test
8383
public void testEjbLinkSyntax() throws Exception{
8484
HttpURLConnection connection = openConnection(8080, "/ejblinksyntax/inspector");
85-
connection.setRequestMethod("GET");
86-
assertAll(
87-
() -> assertEquals(200, connection.getResponseCode()),
88-
() -> assertEquals(
89-
"Cat of color: brown (file-name#) and creature with name: Z",
90-
readResponseInputStream(connection)
91-
)
92-
);
85+
try {
86+
connection.setRequestMethod("GET");
87+
assertAll(
88+
() -> assertEquals(200, connection.getResponseCode()),
89+
() -> assertEquals(
90+
"Cat of color: brown (file-name#) and creature with name: Z",
91+
readResponseInputStream(connection)
92+
)
93+
);
94+
} finally {
95+
connection.disconnect();
96+
}
9397
}
9498

9599
private static File createDeployment() {

appserver/tests/application/src/test/java/org/glassfish/main/test/app/web/jsp/JspReloadGeneratedServletIfUpdatedTest.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2023, 2024 Contributors to the Eclipse Foundation
2+
* Copyright (c) 2023, 2025 Contributors to the Eclipse Foundation
33
* Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved.
44
*
55
* This program and the accompanying materials are made available under the
@@ -93,12 +93,16 @@ public void doTest() throws Exception {
9393
private void doHttpGet(String expectedText) throws Exception {
9494
HttpURLConnection connection = GlassFishTestEnvironment.openConnection(8080,
9595
"/reload/" + JSP_FILE_NAME + ".jsp");
96-
connection.setRequestMethod("GET");
97-
String line = HttpParser.readResponseInputStream(connection).trim();
98-
assertAll(
99-
() -> assertEquals(200, connection.getResponseCode(), "Wrong response code."),
100-
() -> assertEquals(expectedText, line, "Wrong response body.")
101-
);
96+
try {
97+
connection.setRequestMethod("GET");
98+
String line = HttpParser.readResponseInputStream(connection).trim();
99+
assertAll(
100+
() -> assertEquals(200, connection.getResponseCode(), "Wrong response code."),
101+
() -> assertEquals(expectedText, line, "Wrong response body.")
102+
);
103+
} finally {
104+
connection.disconnect();
105+
}
102106
}
103107

104108

Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2023 Contributors to the Eclipse Foundation.
2+
* Copyright (c) 2023, 2025 Contributors to the Eclipse Foundation.
33
* Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved.
44
*
55
* This program and the accompanying materials are made available under the
@@ -18,56 +18,47 @@
1818
package org.glassfish.tests.applicationDispatcher;
1919

2020
import java.io.BufferedReader;
21-
import java.io.IOException;
2221
import java.io.InputStreamReader;
22+
import java.net.HttpURLConnection;
2323
import java.net.URL;
24-
import java.net.URLConnection;
2524

26-
import org.junit.jupiter.api.Assertions;
27-
import org.junit.jupiter.api.BeforeAll;
2825
import org.junit.jupiter.api.Test;
2926

27+
import static org.junit.jupiter.api.Assertions.assertEquals;
28+
3029
public class WebTest {
3130

32-
private static int count = 0;
33-
private static int EXPECTED_COUNT = 1;
31+
private static final int EXPECTED_COUNT = 1;
3432

3533
private String contextPath = "test";
3634

37-
@BeforeAll
38-
public static void setup() throws IOException {
39-
}
40-
4135
@Test
4236
public void testWeb() throws Exception {
4337
goGet("localhost", 8080, "ApplicationDispatcher", contextPath+"/ServletTest");
4438
}
4539

46-
private static void goGet(String host, int port,
47-
String result, String contextPath) throws Exception {
48-
try {
49-
URL servlet = new URL("http://localhost:8080/test");
50-
URLConnection yc = servlet.openConnection();
51-
BufferedReader in = new BufferedReader(new InputStreamReader(
52-
yc.getInputStream()));
40+
41+
private static void goGet(String host, int port, String result, String contextPath) throws Exception {
42+
URL servlet = new URL("http://localhost:8080/test");
43+
HttpURLConnection connection = (HttpURLConnection) servlet.openConnection();
44+
try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
5345
String line = null;
54-
int index, lineNum=0;
46+
int index, lineNum = 0;
47+
int count = 0;
5548
while ((line = in.readLine()) != null) {
5649
index = line.indexOf("::");
57-
System.out.println(lineNum+": "+line);
50+
System.out.println(lineNum + ": " + line);
5851
if (index != -1) {
59-
String status = line.substring(index+2);
60-
if (status.equalsIgnoreCase("PASS")){
52+
String status = line.substring(index + 2);
53+
if (status.equalsIgnoreCase("PASS")) {
6154
count++;
6255
}
6356
}
6457
lineNum++;
6558
}
66-
Assertions.assertTrue(count==EXPECTED_COUNT);
67-
} catch(Exception e) {
68-
e.printStackTrace();
69-
throw e;
59+
assertEquals(EXPECTED_COUNT, count);
60+
} finally {
61+
connection.disconnect();
7062
}
71-
}
72-
63+
}
7364
}

0 commit comments

Comments
 (0)