Skip to content

Commit 6c6302d

Browse files
committed
Added a workaround to allow ClientHttpHandler to run on JVMs >= 12, albeit without PATCH support (see https://bugs.openjdk.java.net/browse/JDK-8210522)
1 parent 48c62bb commit 6c6302d

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.googlecode.utterlyidle;
2+
3+
public class Java {
4+
5+
private Java() {}
6+
7+
public static int majorVersion() {
8+
return Integer.parseInt(System.getProperty("java.version").split("[.-]")[0]);
9+
}
10+
11+
}

src/com/googlecode/utterlyidle/handlers/ClientHttpHandler.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import com.googlecode.totallylazy.time.Dates;
1818
import com.googlecode.utterlyidle.ClientConfiguration;
1919
import com.googlecode.utterlyidle.Entity;
20+
import com.googlecode.utterlyidle.Java;
2021
import com.googlecode.utterlyidle.MediaType;
2122
import com.googlecode.utterlyidle.Request;
2223
import com.googlecode.utterlyidle.Response;
@@ -41,6 +42,7 @@
4142
import java.util.Date;
4243

4344
import static com.googlecode.totallylazy.Closeables.using;
45+
import static com.googlecode.totallylazy.Debug.debugging;
4446
import static com.googlecode.totallylazy.Maps.pairs;
4547
import static com.googlecode.totallylazy.Option.option;
4648
import static com.googlecode.totallylazy.Pair.pair;
@@ -91,7 +93,13 @@ public static void allowHttpMethods(String... newMethods) {
9193
String[] combined = sequence(existingMethods).join(sequence(newMethods)).unique().toArray(String.class);
9294
methods.set(null, combined);
9395
} catch (Exception e) {
94-
throw new RuntimeException(e);
96+
if (Java.majorVersion() >= 12) {
97+
if (debugging()) {
98+
System.err.println("Failed to modify HttpURLConnection to allow PATCH method - this will not work on JVMs >= 12");
99+
}
100+
} else {
101+
throw new RuntimeException(e);
102+
}
95103
}
96104
}
97105

test/com/googlecode/utterlyidle/handlers/ClientHttpHandlerTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import com.googlecode.utterlyidle.HttpHandler;
1616
import com.googlecode.utterlyidle.HttpHeaders;
1717
import com.googlecode.utterlyidle.HttpMessage;
18+
import com.googlecode.utterlyidle.Java;
1819
import com.googlecode.utterlyidle.Protocol;
1920
import com.googlecode.utterlyidle.Request;
2021
import com.googlecode.utterlyidle.Response;
@@ -45,6 +46,7 @@
4546
import static com.googlecode.totallylazy.functions.Functions.modify;
4647
import static com.googlecode.totallylazy.io.Uri.uri;
4748
import static com.googlecode.totallylazy.matchers.NumberMatcher.greaterThan;
49+
import static com.googlecode.totallylazy.matchers.NumberMatcher.lessThanOrEqualTo;
4850
import static com.googlecode.utterlyidle.ApplicationBuilder.application;
4951
import static com.googlecode.utterlyidle.ClientConfiguration.Builder.clientConfiguration;
5052
import static com.googlecode.utterlyidle.Entities.inputStreamOf;
@@ -64,6 +66,7 @@
6466
import static org.hamcrest.CoreMatchers.is;
6567
import static org.hamcrest.MatcherAssert.assertThat;
6668
import static org.junit.Assert.fail;
69+
import static org.junit.Assume.assumeThat;
6770

6871
public class ClientHttpHandlerTest {
6972
@Test(expected = UnsupportedOperationException.class)
@@ -79,6 +82,8 @@ public void doesNotThrowNullPointerExceptionWhenNoSchema() throws Exception {
7982

8083
@Test
8184
public void supportsPatch() throws Exception {
85+
assumeThat(Java.majorVersion(), lessThanOrEqualTo(11));
86+
8287
Server server = application().addAnnotated(RestTest.PatchContent.class).start();
8388
Response response = new ClientHttpHandler().handle(patch(server.uri().mergePath("path/bar")).entity("input"));
8489

@@ -89,6 +94,8 @@ public void supportsPatch() throws Exception {
8994

9095
@Test
9196
public void supportsPatchOverHttps() throws Exception {
97+
assumeThat(Java.majorVersion(), lessThanOrEqualTo(11));
98+
9299
try (InputStream resource = SecureStringTest.class.getResourceAsStream("localhost.jks");
93100
SecureString password = secureString('p', 'a', 's', 's', 'w', 'o', 'r', 'd')) {
94101
SSLContext context = sslContext(keyStore(password, resource), password);

0 commit comments

Comments
 (0)