Skip to content

Commit 626f17d

Browse files
committed
Add resource bundle adapter.
1 parent 8943687 commit 626f17d

File tree

8 files changed

+154
-8
lines changed

8 files changed

+154
-8
lines changed

README.md

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
[![Maven Central](https://img.shields.io/maven-central/v/org.httprpc/httprpc-server.svg)](https://repo1.maven.org/maven2/org/httprpc/httprpc-server/)
33

44
# Introduction
5-
HTTP-RPC is an open-source framework for creating and consuming RESTful and REST-like web services in Java. It is extremely lightweight and requires only a Java runtime environment and a servlet container. The entire framework is less than 90KB in size, making it an ideal choice for applications where a minimal footprint is desired.
5+
HTTP-RPC is an open-source framework for creating and consuming RESTful and REST-like web services in Java. It is extremely lightweight and requires only a Java runtime environment and a servlet container. The entire framework is less than 100KB in size, making it an ideal choice for applications where a minimal footprint is desired.
66

77
This guide introduces the HTTP-RPC framework and provides an overview of its key features.
88

@@ -33,6 +33,7 @@ Classes provided by the HTTP-RPC framework include:
3333
* [BeanAdapter](#beanadapter) - map adapter for Java beans
3434
* [ResultSetAdapter and Parameters](#resultsetadapter-and-parameters) - iterable adapter for JDBC result sets/applies named parameter values to prepared statements
3535
* [ElementAdapter](#elementadapter) - map adapter for XML elements
36+
* [ResourceBundleAdapter](#resourcebundleadapter) - map adapter for resource bundles
3637
* [StreamAdapter](#streamadapter) - iterable adapter for streams
3738
* [Collections](#collections) - utility methods for working with collections
3839

@@ -852,6 +853,40 @@ System.out.println(credit.get("amount").toString());
852853
System.out.println(credit.get("date").toString());
853854
```
854855

856+
## ResourceBundleAdapter
857+
The `ResourceBundleAdapter` class provides access to the contents of a resource bundle via the `Map` interface. It can be used to localize the contents of a template document, for example:
858+
859+
```html
860+
<table>
861+
<!-- {{?headings}} -->
862+
<tr>
863+
<td>{{name}}</td>
864+
<td>{{description}}</td>
865+
<td>{{quantity}}</td>
866+
</tr>
867+
<!-- {{/headings}} -->
868+
869+
<!-- {{#items}} -->
870+
<tr>
871+
<td>{{name}}</td>
872+
<td>{{description}}</td>
873+
<td>{{quantity}}</td>
874+
</tr>
875+
<!-- {{/items}} -->
876+
</table>
877+
```
878+
879+
```java
880+
TemplateEncoder templateEncoder = new TemplateEncoder(getClass().getResource("list.html"));
881+
882+
ResourceBundle resourceBundle = ResourceBundle.getBundle(getClass().getPackage().getName() + ".headings");
883+
884+
templateEncoder.write(mapOf(
885+
entry("headings", new ResourceBundleAdapter(resourceBundle)),
886+
entry("items", items)
887+
), System.out);
888+
```
889+
855890
## StreamAdapter
856891
The `StreamAdapter` class provides access to the contents of a stream via the `Iterable` interface. For example, it can be used to serialize the result of a stream operation without needing to first collect the results, which could be expensive if the stream is large:
857892

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
15+
package org.httprpc.util;
16+
17+
import java.util.AbstractMap;
18+
import java.util.Map;
19+
import java.util.MissingResourceException;
20+
import java.util.ResourceBundle;
21+
import java.util.Set;
22+
23+
/**
24+
* {@link Map} adapter for resource bundles.
25+
*/
26+
public class ResourceBundleAdapter extends AbstractMap<String, String> {
27+
private ResourceBundle resourceBundle;
28+
29+
public ResourceBundleAdapter(ResourceBundle resourceBundle) {
30+
if (resourceBundle == null) {
31+
throw new IllegalArgumentException();
32+
}
33+
34+
this.resourceBundle = resourceBundle;
35+
}
36+
37+
@Override
38+
public String get(Object key) {
39+
if (key == null) {
40+
throw new IllegalArgumentException();
41+
}
42+
43+
try {
44+
return resourceBundle.getString(key.toString());
45+
} catch (MissingResourceException exception) {
46+
return null;
47+
}
48+
}
49+
50+
@Override
51+
public Set<Entry<String, String>> entrySet() {
52+
throw new UnsupportedOperationException();
53+
}
54+
}

httprpc-client/src/main/java/org/httprpc/xml/ElementAdapter.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ public ElementAdapter(Element element) {
6565

6666
@Override
6767
public Object get(Object key) {
68+
if (key == null) {
69+
throw new IllegalArgumentException();
70+
}
71+
6872
String name = key.toString();
6973

7074
Object value;
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
15+
package org.httprpc.util;
16+
17+
import org.junit.jupiter.api.Test;
18+
19+
import java.util.ResourceBundle;
20+
21+
import static org.junit.jupiter.api.Assertions.assertEquals;
22+
import static org.junit.jupiter.api.Assertions.assertNull;
23+
24+
public class ResourceBundleAdapterTest {
25+
@Test
26+
public void testResourceBundleAdapter() {
27+
ResourceBundle resourceBundle = ResourceBundle.getBundle(getClass().getPackage().getName() + ".test");
28+
29+
ResourceBundleAdapter resourceBundleAdapter = new ResourceBundleAdapter(resourceBundle);
30+
31+
assertEquals(resourceBundle.getString("a"), resourceBundleAdapter.get("a"));
32+
assertEquals(resourceBundle.getString("b"), resourceBundleAdapter.get("b"));
33+
assertEquals(resourceBundle.getString("c"), resourceBundleAdapter.get("c"));
34+
35+
assertNull(resourceBundleAdapter.get("d"));
36+
}
37+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
a = A
2+
b = B
3+
c = C

httprpc-test/src/main/java/org/httprpc/test/PetService.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.httprpc.io.TemplateEncoder;
2424
import org.httprpc.sql.Parameters;
2525
import org.httprpc.sql.ResultSetAdapter;
26+
import org.httprpc.util.ResourceBundleAdapter;
2627

2728
import javax.naming.Context;
2829
import javax.naming.InitialContext;
@@ -36,6 +37,7 @@
3637
import java.sql.SQLException;
3738
import java.sql.Statement;
3839
import java.util.Date;
40+
import java.util.ResourceBundle;
3941
import java.util.stream.Stream;
4042

4143
import static org.httprpc.util.Collections.entry;
@@ -95,7 +97,12 @@ public void getPets(String owner, String format) throws SQLException, IOExceptio
9597

9698
TemplateEncoder templateEncoder = new TemplateEncoder(getClass().getResource("pets.html"));
9799

98-
templateEncoder.write(resultSetAdapter, getResponse().getOutputStream());
100+
ResourceBundle resourceBundle = ResourceBundle.getBundle(getClass().getPackage().getName() + ".pets", getRequest().getLocale());
101+
102+
templateEncoder.write(mapOf(
103+
entry("headings", new ResourceBundleAdapter(resourceBundle)),
104+
entry("data", resultSetAdapter)
105+
), getResponse().getOutputStream());
99106
} else {
100107
throw new IllegalArgumentException();
101108
}

httprpc-test/src/main/resources/org/httprpc/test/pets.html

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,23 @@
55
<body>
66

77
<table>
8+
<!-- {{?headings}} -->
89
<tr>
9-
<td>Name</td>
10-
<td>Species</td>
11-
<td>Sex</td>
12-
<td>Birth</td>
10+
<td>{{name}}</td>
11+
<td>{{species}}</td>
12+
<td>{{sex}}</td>
13+
<td>{{birth}}</td>
1314
</tr>
15+
<!-- {{/headings}} -->
1416

15-
<!-- {{#.}} -->
17+
<!-- {{#data}} -->
1618
<tr>
1719
<td>{{name}}</td>
1820
<td>{{species}}</td>
1921
<td>{{sex}}</td>
2022
<td>{{birth}}</td>
2123
</tr>
22-
<!-- {{/.}} -->
24+
<!-- {{/data}} -->
2325
</table>
2426

2527
</body>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
name = Name
2+
species = Species
3+
sex = Sex
4+
birth = Birth

0 commit comments

Comments
 (0)