Skip to content

Commit f4d18fe

Browse files
committed
Add ResourceBundleAdapter.
1 parent 262d3f7 commit f4d18fe

File tree

9 files changed

+92
-75
lines changed

9 files changed

+92
-75
lines changed

.idea/inspectionProfiles/Project_Default.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 1 addition & 67 deletions
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

@@ -785,72 +785,6 @@ Once applied, the statement can be executed:
785785
ResultSetAdapter resultSetAdapter = new ResultSetAdapter(statement.executeQuery());
786786
```
787787

788-
A complete example that uses both classes is shown below. It is based on the "pet" table from the MySQL "menagerie" sample database:
789-
790-
```sql
791-
CREATE TABLE pet (
792-
name VARCHAR(20),
793-
owner VARCHAR(20),
794-
species VARCHAR(20),
795-
sex CHAR(1),
796-
birth DATE,
797-
death DATE
798-
);
799-
```
800-
801-
The following service method queries this table to retrieve a list of all pets belonging to a given owner:
802-
803-
```java
804-
@RequestMethod("GET")
805-
public void getPets(String owner, String format) throws SQLException, IOException {
806-
Parameters parameters = Parameters.parse("SELECT name, species, sex, birth FROM pet WHERE owner = :owner");
807-
808-
try (Connection connection = dataSource.getConnection();
809-
PreparedStatement statement = connection.prepareStatement(parameters.getSQL())) {
810-
parameters.apply(statement, mapOf(
811-
entry("owner", owner)
812-
));
813-
814-
try (ResultSetAdapter resultSetAdapter = new ResultSetAdapter(statement.executeQuery())) {
815-
JSONEncoder jsonEncoder = new JSONEncoder();
816-
817-
jsonEncoder.write(resultSetAdapter, getResponse().getOutputStream());
818-
}
819-
}
820-
}
821-
```
822-
823-
For example, given this request:
824-
825-
```
826-
GET /pets?owner=Gwen
827-
```
828-
829-
The service would return something like this:
830-
831-
```json
832-
[
833-
{
834-
"name": "Claws",
835-
"species": "cat",
836-
"sex": "m",
837-
"birth": 763880400000
838-
},
839-
{
840-
"name": "Chirpy",
841-
"species": "bird",
842-
"sex": "f",
843-
"birth": 905486400000
844-
},
845-
{
846-
"name": "Whistler",
847-
"species": "bird",
848-
"sex": null,
849-
"birth": 881643600000
850-
}
851-
]
852-
```
853-
854788
## ElementAdapter
855789
TODO
856790

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
subprojects {
1616
group = 'org.httprpc'
17-
version = '7.3.1'
17+
version = '7.4'
1818

1919
apply plugin: 'java-library'
2020
apply plugin: 'maven-publish'
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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.ResourceBundle;
20+
import java.util.Set;
21+
22+
/**
23+
* {@link Map} adapter for resource bundles.
24+
*/
25+
public class ResourceBundleAdapter extends AbstractMap<String, Object> {
26+
private ResourceBundle resourceBundle;
27+
28+
public ResourceBundleAdapter(ResourceBundle resourceBundle) {
29+
if (resourceBundle == null) {
30+
throw new IllegalArgumentException();
31+
}
32+
33+
this.resourceBundle = resourceBundle;
34+
}
35+
36+
@Override
37+
public Object get(Object key) {
38+
if (key == null) {
39+
throw new IllegalArgumentException();
40+
}
41+
42+
return resourceBundle.getObject(key.toString());
43+
}
44+
45+
@Override
46+
public Set<Entry<String, Object>> entrySet() {
47+
throw new UnsupportedOperationException();
48+
}
49+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
public class ResourceBundleAdapterTest {
20+
@Test
21+
public void testResourceBundle() {
22+
// TODO
23+
}
24+
}

httprpc-client/src/test/resources/org/httprpc/util/test.properties

Whitespace-only changes.

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

Lines changed: 6 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;
@@ -37,6 +38,7 @@
3738
import java.sql.Statement;
3839
import java.util.Arrays;
3940
import java.util.Date;
41+
import java.util.ResourceBundle;
4042
import java.util.stream.Stream;
4143

4244
import static org.httprpc.util.Collections.entry;
@@ -97,7 +99,10 @@ public void getPets(String owner, String format) throws SQLException, IOExceptio
9799

98100
TemplateEncoder templateEncoder = new TemplateEncoder(getClass().getResource("pets.html"));
99101

100-
templateEncoder.write(resultSetAdapter, getResponse().getOutputStream());
102+
templateEncoder.write(mapOf(
103+
entry("resources", new ResourceBundleAdapter(ResourceBundle.getBundle(getClass().getPackage().getName() + ".pets"))),
104+
entry("pets", resultSetAdapter)
105+
), getResponse().getOutputStream());
101106
} else {
102107
throw new IllegalArgumentException();
103108
}

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+
<!-- {{?resources}} -->
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+
<!-- {{/resources}} -->
1416

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

2527
</body>

httprpc-test/src/main/webapp/index.jsp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,8 @@
186186
<br/>
187187
<a href="${pageContext.request.contextPath}/system-info">System Info</a><br/>
188188

189+
<hr/>
190+
189191
<a href="${pageContext.request.contextPath}/pets?api">Pets (API)</a><br/>
190192
<br/>
191193
<a href="${pageContext.request.contextPath}/pets?owner=Gwen">Pets (JSON)</a><br/>

0 commit comments

Comments
 (0)