Skip to content

Commit af9afd4

Browse files
committed
Add support for URLs to BeanAdapter, etc.
1 parent f69cc5e commit af9afd4

File tree

7 files changed

+38
-15
lines changed

7 files changed

+38
-15
lines changed

README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -285,16 +285,17 @@ Methods are grouped by resource path. Parameters and return values are encoded a
285285
* `Long` or `long`: "long"
286286
* `Float` or `float`: "float"
287287
* `Double` or `double`: "double"
288-
* Any other type that extends `Number`: "number"
289-
* Any type that implements `CharSequence`: "string"
290-
* Any `Enum` type: "enum"
291-
* Any type that extends `java.util.Date`: "date"
288+
* Any other `Number`: "number"
289+
* `CharSequence`: "string"
290+
* `Enum`: "enum"
291+
* `java.util.Date`: "date"
292292
* `java.util.time.LocalDate`: "date-local"
293293
* `java.util.time.LocalTime`: "time-local"
294294
* `java.util.time.LocalDateTime`: "datetime-local"
295+
* `java.net.URL`: "file" for parameters, "url" for return values
295296
* `java.lang.Iterable`, `java.util.Collection`, or `java.util.List`: "[<em>element type</em>]"
296297
* `java.util.Map`: "[<em>key type</em>: <em>value type</em>]"
297-
* Any other type: "{property1: <em>property 1 type</em>, property2: <em>property 2 type</em>, ...}"
298+
* Any other type: "{property1: <em>property1 type</em>, property2: <em>property2 type</em>, ...}"
298299

299300
For example, a description of the math service might look like this:
300301

@@ -584,6 +585,7 @@ If a property value is `null` or an instance of one of the following types, it i
584585
* `java.util.time.LocalDate`
585586
* `java.util.time.LocalTime`
586587
* `java.util.time.LocalDateTime`
588+
* `java.net.URL`
587589

588590
If a property returns an instance of `Iterable` or `Map`, the value is wrapped in an adapter of the same type that automatically adapts its sub-elements. Otherwise, the value is assumed to be a bean and is wrapped in a `BeanAdapter`.
589591

httprpc/src/main/java/org/httprpc/WebService.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -677,10 +677,6 @@ private void describeService(HttpServletRequest request, HttpServletResponse res
677677
for (Map.Entry<Class<?>, String> entry : structures.entrySet()) {
678678
Class<?> type = entry.getKey();
679679

680-
if (type == URL.class) {
681-
continue;
682-
}
683-
684680
String name = type.getSimpleName();
685681

686682
xmlStreamWriter.writeStartElement("h3");

httprpc/src/main/java/org/httprpc/beans/BeanAdapter.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.lang.reflect.Proxy;
2222
import java.lang.reflect.Type;
2323
import java.lang.reflect.WildcardType;
24+
import java.net.URL;
2425
import java.time.LocalDate;
2526
import java.time.LocalDateTime;
2627
import java.time.LocalTime;
@@ -307,6 +308,7 @@ public Entry<String, Object> next() {
307308
* <li>{@link LocalDate}</li>
308309
* <li>{@link LocalTime}</li>
309310
* <li>{@link LocalDateTime}</li>
311+
* <li>{@link URL}</li>
310312
* </ul>
311313
*
312314
* If the value is an instance of {@link Iterable}, it is wrapped in an
@@ -338,7 +340,8 @@ private static Object adapt(Object value, HashMap<Class<?>, HashMap<String, Meth
338340
|| value instanceof Date
339341
|| value instanceof LocalDate
340342
|| value instanceof LocalTime
341-
|| value instanceof LocalDateTime) {
343+
|| value instanceof LocalDateTime
344+
|| value instanceof URL) {
342345
return value;
343346
} else if (value instanceof Iterable<?>) {
344347
return new IterableAdapter((Iterable<?>)value, accessorCache);
@@ -397,13 +400,14 @@ public static <V> V valueAt(Map<String, ?> root, String path) {
397400
* <li>{@link Long} or <tt>long</tt>: "long"</li>
398401
* <li>{@link Float} or <tt>float</tt>: "float"</li>
399402
* <li>{@link Double} or <tt>double</tt>: "double"</li>
400-
* <li>Any other type that extends {@link Number}: "number"</li>
401-
* <li>Any type that implements {@link CharSequence}: "string"</li>
402-
* <li>Any {@link Enum} type: "enum"</li>
403-
* <li>Any type that extends {@link Date}: "date"</li>
403+
* <li>Any other {@link Number}: "number"</li>
404+
* <li>{@link CharSequence}: "string"</li>
405+
* <li>{@link Enum}: "enum"</li>
406+
* <li>{@link Date}: "date"</li>
404407
* <li>{@link LocalDate}: "date-local"</li>
405408
* <li>{@link LocalTime}: "time-local"</li>
406409
* <li>{@link LocalDateTime}: "datetime-local"</li>
410+
* <li>{@link URL}: "url"</li>
407411
* <li>{@link Iterable}, {@link Collection}, or {@link List}: "[<i>element type</i>]"</li>
408412
* <li>{@link Map}: "[<i>key type</i>: <i>value type</i>]"</li>
409413
* <li>Any other type: "{property1: <i>property 1 type</i>, property2: <i>property 2 type</i>, ...}"</li>
@@ -477,6 +481,8 @@ private static String describe(Class<?> type, Map<Class<?>, String> structures)
477481
return "time-local";
478482
} else if (type == LocalDateTime.class) {
479483
return "datetime-local";
484+
} else if (type == URL.class) {
485+
return "url";
480486
} else {
481487
if (!structures.containsKey(type)) {
482488
structures.put(type, null);

httprpc/src/main/java/org/httprpc/io/JSONEncoder.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.io.OutputStream;
1919
import java.io.OutputStreamWriter;
2020
import java.io.Writer;
21+
import java.net.URL;
2122
import java.nio.charset.Charset;
2223
import java.time.LocalDate;
2324
import java.time.LocalDateTime;
@@ -122,6 +123,8 @@ private void encode(Object value, Writer writer) throws IOException {
122123
encode(((Date)value).getTime(), writer);
123124
} else if (value instanceof LocalDate || value instanceof LocalTime || value instanceof LocalDateTime) {
124125
encode(value.toString(), writer);
126+
} else if (value instanceof URL) {
127+
encode(value.toString(), writer);
125128
} else if (value instanceof Iterable<?>) {
126129
writer.write("[");
127130

httprpc/src/test/java/org/httprpc/beans/BeanAdapterTest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import org.junit.Test;
2020

2121
import java.math.BigInteger;
22+
import java.net.MalformedURLException;
23+
import java.net.URL;
2224
import java.time.DayOfWeek;
2325
import java.time.LocalDate;
2426
import java.time.LocalDateTime;
@@ -29,7 +31,7 @@
2931

3032
public class BeanAdapterTest extends AbstractTest {
3133
@Test
32-
public void testBeanAdapter() {
34+
public void testBeanAdapter() throws MalformedURLException {
3335
Map<String, ?> expected = mapOf(
3436
entry("i", 1),
3537
entry("long", 2L),
@@ -41,6 +43,7 @@ public void testBeanAdapter() {
4143
entry("localDate", LocalDate.parse("2018-06-28")),
4244
entry("localTime", LocalTime.parse("10:45")),
4345
entry("localDateTime", LocalDateTime.parse("2018-06-28T10:45")),
46+
entry("URL", new URL("http://localhost:8080")),
4447
entry("list", listOf(2L, 4.0, mapOf(
4548
entry("flag", true)
4649
))),
@@ -88,6 +91,7 @@ public void testDescribe() {
8891

8992
Assert.assertEquals(structures.get(TestBean.class),
9093
"{\n" +
94+
" URL: url,\n" +
9195
" bigInteger: number,\n" +
9296
" date: date,\n" +
9397
" dayOfWeek: enum,\n" +

httprpc/src/test/java/org/httprpc/beans/TestBean.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import static org.httprpc.AbstractTest.*;
1818

1919
import java.math.BigInteger;
20+
import java.net.MalformedURLException;
21+
import java.net.URL;
2022
import java.time.DayOfWeek;
2123
import java.time.LocalDate;
2224
import java.time.LocalDateTime;
@@ -79,6 +81,10 @@ public LocalDateTime getLocalDateTime() {
7981
return LocalDateTime.parse("2018-06-28T10:45");
8082
}
8183

84+
public URL getURL() throws MalformedURLException {
85+
return new URL("http://localhost:8080");
86+
}
87+
8288
public List<?> getList() {
8389
return listOf(2L, 4.0, mapOf(entry("flag", true)));
8490
}

httprpc/src/test/java/org/httprpc/io/JSONEncoderTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import java.io.IOException;
1818
import java.io.StringWriter;
19+
import java.net.URL;
1920
import java.time.DayOfWeek;
2021
import java.util.Date;
2122
import java.util.List;
@@ -53,6 +54,11 @@ public void testDate() throws IOException {
5354
Assert.assertEquals("0", encode(new Date(0)));
5455
}
5556

57+
@Test
58+
public void testURL() throws IOException {
59+
Assert.assertEquals("\"http://localhost:8080\"", encode(new URL("http://localhost:8080")));
60+
}
61+
5662
@Test
5763
public void testEnum() throws IOException {
5864
Assert.assertEquals("3", encode(DayOfWeek.THURSDAY));

0 commit comments

Comments
 (0)