Skip to content

Commit

Permalink
Eliminate use of deprecated URL constructors; drop support for adapti…
Browse files Browse the repository at this point in the history
…ng/coercing URL values.
  • Loading branch information
gk-brown committed Sep 12, 2024
1 parent 90cdee2 commit 76821dc
Show file tree
Hide file tree
Showing 21 changed files with 75 additions and 62 deletions.
2 changes: 1 addition & 1 deletion .idea/dataSources.local.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,6 @@ Additionally, instances of the following types are automatically converted to th
* `java.time.TemporalAccessor`
* `java.time.TemporalAmount`
* `java.util.UUID`
* `java.net.URL`

All other values are assumed to be beans and are serialized as objects.

Expand Down Expand Up @@ -396,7 +395,7 @@ The following code demonstrates how `WebServiceProxy` might be used to access th

```java
// GET /math/sum?a=2&b=4
var webServiceProxy = new WebServiceProxy("GET", new URL(baseURL, "math/sum"));
var webServiceProxy = new WebServiceProxy("GET", baseURL, "math/sum");

webServiceProxy.setArguments(mapOf(
entry("a", 4),
Expand All @@ -408,7 +407,7 @@ System.out.println(webServiceProxy.invoke()); // 6.0

```java
// GET /math/sum?values=1&values=2&values=3
var webServiceProxy = new WebServiceProxy("GET", new URL(baseURL, "math/sum"));
var webServiceProxy = new WebServiceProxy("GET", baseURL, "math/sum");

webServiceProxy.setArguments(mapOf(
entry("values", listOf(1, 2, 3))
Expand Down
27 changes: 22 additions & 5 deletions kilo-client/src/main/java/org/httprpc/kilo/WebServiceProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import java.lang.reflect.Proxy;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -257,7 +259,9 @@ public Object invoke(Object proxy, Method method, Object[] arguments) throws Thr
}
}

var webServiceProxy = new WebServiceProxy(requestMethod.value().toUpperCase(), new URL(baseURL, pathBuilder.toString()));
var url = baseURL.toURI().resolve(pathBuilder.toString()).toURL();

var webServiceProxy = new WebServiceProxy(requestMethod.value().toUpperCase(), url);

if (initializer != null) {
initializer.accept(webServiceProxy);
Expand Down Expand Up @@ -398,7 +402,13 @@ public WebServiceProxy(String method, URL url) {
* If a URL cannot be constructed from the base URL and path.
*/
public WebServiceProxy(String method, URL baseURL, String path, Object... arguments) throws MalformedURLException {
this(method, new URL(baseURL, String.format(path, arguments)));
this(method, baseURL);

try {
url = url.toURI().resolve(String.format(path, arguments)).toURL();
} catch (URISyntaxException exception) {
throw new MalformedURLException();
}
}

/**
Expand Down Expand Up @@ -719,7 +729,14 @@ public void encodeRequest(OutputStream outputStream) throws IOException {
var query = encodeQuery();

if (!query.isEmpty()) {
url = new URL(this.url.getProtocol(), this.url.getHost(), this.url.getPort(), String.format("%s?%s", this.url.getFile(), query));
URI uri;
try {
uri = new URI(String.format("%s?%s", url, query));
} catch (URISyntaxException exception) {
throw new MalformedURLException();
}

url = uri.toURL();
}

if (body != null) {
Expand Down Expand Up @@ -1036,8 +1053,8 @@ public static <T> T of(Class<T> type, URL baseURL, Consumer<WebServiceProxy> ini

if (servicePath != null) {
try {
baseURL = new URL(baseURL, String.format("%s/", servicePath.value()));
} catch (MalformedURLException exception) {
baseURL = baseURL.toURI().resolve(String.format("%s/", servicePath.value())).toURL();
} catch (URISyntaxException | MalformedURLException exception) {
throw new UnsupportedOperationException(exception);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Proxy;
import java.lang.reflect.Type;
import java.net.MalformedURLException;
import java.net.URL;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
Expand Down Expand Up @@ -602,7 +600,6 @@ public Entry<String, Object> next() {
* <li>{@link TemporalAccessor}</li>
* <li>{@link TemporalAmount}</li>
* <li>{@link UUID}</li>
* <li>{@link URL}</li>
* </ul>
*
* <p>If the value is an array, it is wrapped in a {@link List} that will
Expand Down Expand Up @@ -640,8 +637,7 @@ public static Object adapt(Object value) {
|| value instanceof Date
|| value instanceof TemporalAccessor
|| value instanceof TemporalAmount
|| value instanceof UUID
|| value instanceof URL) {
|| value instanceof UUID) {
return value;
} else if (value.getClass().isArray()) {
return new ArrayAdapter(value);
Expand Down Expand Up @@ -680,7 +676,6 @@ public static Object adapt(Object value) {
* <li>{@link Duration}</li>
* <li>{@link Period}</li>
* <li>{@link UUID}</li>
* <li>{@link URL}</li>
* </ul>
*
* <p>If the target type is an array, the provided value must be an array
Expand Down Expand Up @@ -968,12 +963,6 @@ private static Object toRawType(Object value, Class<?> type) {
return Period.parse(value.toString());
} else if (type == UUID.class) {
return UUID.fromString(value.toString());
} else if (type == URL.class) {
try {
return new URL(value.toString());
} catch (MalformedURLException exception) {
throw new IllegalArgumentException(exception);
}
} else if (type.isArray()) {
if (value.getClass().isArray()) {
return toArray(new ArrayAdapter(value), type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.text.NumberFormat;
import java.time.Instant;
Expand Down Expand Up @@ -787,7 +789,12 @@ private void write(Object root, Writer writer, Locale locale, TimeZone timeZone,
}
case INCLUDE -> {
if (root != null) {
var url = new URL(this.url, marker);
URL url;
try {
url = this.url.toURI().resolve(marker).toURL();
} catch (URISyntaxException exception) {
throw new MalformedURLException();
}

try (var inputStream = url.openStream()) {
write(dictionary, writer, locale, timeZone, new PagedReader(new InputStreamReader(inputStream)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@

import java.lang.reflect.ParameterizedType;
import java.math.BigInteger;
import java.net.MalformedURLException;
import java.net.URL;
import java.time.DayOfWeek;
import java.time.Duration;
import java.time.Instant;
Expand Down Expand Up @@ -112,7 +110,7 @@ default int getY() {
}

@Test
public void testBeanAdapter() throws MalformedURLException {
public void testBeanAdapter() {
var now = LocalDate.now();

var map = mapOf(
Expand All @@ -130,7 +128,6 @@ public void testBeanAdapter() throws MalformedURLException {
entry("duration", Duration.parse("PT2H30M")),
entry("period", Period.parse("P3Y2M")),
entry("UUID", UUID.randomUUID()),
entry("URL", new URL("http://localhost:8080")),
entry("nestedBean", mapOf(
entry("flag", true),
entry("character", 'y')
Expand Down Expand Up @@ -289,11 +286,6 @@ public void testUUIDCoercion() {
assertEquals(uuid, BeanAdapter.coerce(uuid.toString(), UUID.class));
}

@Test
public void testURLCoercion() throws MalformedURLException {
assertEquals(new URL("http://localhost:8080"), BeanAdapter.coerce("http://localhost:8080", URL.class));
}

@Test
public void testListCoercion() {
assertEquals(listOf(
Expand Down Expand Up @@ -572,7 +564,7 @@ public void testGetProperties() {
assertEquals(LocalDateTime.class, properties.get("localDateTime"));
assertEquals(Duration.class, properties.get("duration"));
assertEquals(Period.class, properties.get("period"));
assertEquals(URL.class, properties.get("URL"));
assertEquals(UUID.class, properties.get("UUID"));

assertEquals(TestInterface.NestedInterface.class, properties.get("nestedBean"));

Expand Down
11 changes: 0 additions & 11 deletions kilo-client/src/test/java/org/httprpc/kilo/beans/TestBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import org.httprpc.kilo.Required;

import java.math.BigInteger;
import java.net.URL;
import java.time.DayOfWeek;
import java.time.Duration;
import java.time.Instant;
Expand Down Expand Up @@ -72,7 +71,6 @@ public void setCharacter(Character character) {
private Duration duration = null;
private Period period = null;
private UUID uuid = null;
private URL url = null;

private NestedInterface nestedBean = null;

Expand Down Expand Up @@ -216,15 +214,6 @@ public void setUUID(UUID uuid) {
this.uuid = uuid;
}

@Override
public URL getURL() {
return url;
}

public void setURL(URL url) {
this.url = url;
}

@Override
public NestedInterface getNestedBean() {
return nestedBean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import org.httprpc.kilo.Required;

import java.math.BigInteger;
import java.net.URL;
import java.time.DayOfWeek;
import java.time.Duration;
import java.time.Instant;
Expand Down Expand Up @@ -62,7 +61,6 @@ interface NestedInterface {
Duration getDuration();
Period getPeriod();
UUID getUUID();
URL getURL();

NestedInterface getNestedBean();
void setNestedBean(NestedInterface nestedBean);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import java.io.IOException;
import java.io.StringWriter;
import java.net.URL;
import java.net.URI;
import java.time.DayOfWeek;
import java.time.Duration;
import java.time.Instant;
Expand Down Expand Up @@ -162,7 +162,7 @@ public void testUUID() throws IOException {

@Test
public void testURL() throws IOException {
assertEquals("\"http://localhost:8080\"", encode(new URL("http://localhost:8080")));
assertEquals("\"http://localhost:8080\"", encode(URI.create("http://localhost:8080").toURL()));
}

@Test
Expand Down
11 changes: 10 additions & 1 deletion kilo-server/src/main/java/org/httprpc/kilo/WebService.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
import java.lang.reflect.Parameter;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandler;
Expand Down Expand Up @@ -912,7 +914,14 @@ protected void service(HttpServletRequest request, HttpServletResponse response)
argumentMap.put(name, values);
}

values.add(new URL("part", null, -1, submittedFileName, new PartURLStreamHandler(part)));
URI uri;
try {
uri = new URI(String.format("part:/%s", submittedFileName));
} catch (URISyntaxException exception) {
throw new RuntimeException(exception);
}

values.add(URL.of(uri, new PartURLStreamHandler(part)));
}
}

Expand Down
10 changes: 8 additions & 2 deletions kilo-test/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,24 @@ compileTestJava {
}

compileKotlin {
kotlinOptions {
jvmTarget = 17
}

compilerOptions {
freeCompilerArgs = [
'-jvm-target', '17',
'-java-parameters'
]
}
}

compileTestKotlin {
kotlinOptions {
jvmTarget = 17
}

compilerOptions {
freeCompilerArgs = [
'-jvm-target', '17',
'-java-parameters'
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
package org.httprpc.kilo.test;

import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;

public abstract class AbstractTest {
protected URL baseURL;

protected AbstractTest() {
try {
baseURL = new URL("http://localhost:8080/kilo-test/");
baseURL = URI.create("http://localhost:8080/kilo-test/").toURL();
} catch (MalformedURLException exception) {
throw new RuntimeException(exception);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;
import java.net.URL;
import java.util.Iterator;
import java.util.Map;
Expand Down Expand Up @@ -62,7 +63,7 @@ public Map<String, Object> next() {
}

public static void main(String[] args) throws IOException {
var baseURL = new URL("http://localhost:8080/kilo-test/bulk-upload/");
var baseURL = URI.create("http://localhost:8080/kilo-test/bulk-upload/").toURL();

var t0 = System.currentTimeMillis();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@
import org.httprpc.kilo.WebServiceProxy;

import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.util.List;

public class EmployeesTest {
public static void main(String[] args) throws IOException {
var baseURL = new URL("http://localhost:8080/kilo-test/");
var baseURL = URI.create("http://localhost:8080/kilo-test/").toURL();

logTiming(baseURL, "employees");
logTiming(baseURL, "employees/stream");
Expand Down
Loading

0 comments on commit 76821dc

Please sign in to comment.