Skip to content

Commit 31be3af

Browse files
committed
Simplify MongoDB and MySQL examples.
1 parent f16c1d7 commit 31be3af

File tree

7 files changed

+22
-46
lines changed

7 files changed

+22
-46
lines changed

httprpc-test/src/main/java/org/httprpc/test/mongodb/RestaurantService.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,23 +74,27 @@ public Iterator<Document> iterator() {
7474
}
7575
};
7676

77-
if (format == null) {
77+
if (format == null || format.equals("json")) {
78+
getResponse().setContentType("application/json");
79+
7880
JSONEncoder jsonEncoder = new JSONEncoder();
7981

8082
jsonEncoder.writeValue(cursorAdapter, getResponse().getOutputStream());
8183
} else if (format.equals("csv")) {
84+
getResponse().setContentType("text/csv");
85+
8286
CSVEncoder csvEncoder = new CSVEncoder(Arrays.asList("name", "address.building", "address.street", "borough", "cuisine"));
8387

8488
csvEncoder.writeValues(cursorAdapter, getResponse().getOutputStream());
85-
} else {
86-
String name = String.format("%s.%s", getRequest().getServletPath().substring(1), format);
89+
} else if (format.equals("html")){
90+
getResponse().setContentType("text/html;charset=UTF-8");
8791

88-
getResponse().setContentType(String.format("%s;charset=UTF-8", getServletContext().getMimeType(name)));
92+
TemplateEncoder templateEncoder = new TemplateEncoder(getClass().getResource("restaurants.html"));
8993

90-
TemplateEncoder templateEncoder = new TemplateEncoder(getClass().getResource(name));
91-
92-
templateEncoder.setBaseName(getClass().getName());
94+
templateEncoder.setBaseName(getClass().getPackage().getName() + ".restaurants");
9395
templateEncoder.writeValue(cursorAdapter, getResponse().getOutputStream(), getRequest().getLocale());
96+
} else {
97+
throw new UnsupportedOperationException();
9498
}
9599
} finally {
96100
getResponse().flushBuffer();

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,23 +84,27 @@ public void getPets(String owner, String format) throws SQLException, IOExceptio
8484
try (ResultSet resultSet = statement.executeQuery()) {
8585
ResultSetAdapter resultSetAdapter = new ResultSetAdapter(resultSet);
8686

87-
if (format == null) {
87+
if (format == null || format.equals("json")) {
88+
getResponse().setContentType("application/json");
89+
8890
JSONEncoder jsonEncoder = new JSONEncoder();
8991

9092
jsonEncoder.writeValue(resultSetAdapter, getResponse().getOutputStream());
9193
} else if (format.equals("csv")) {
94+
getResponse().setContentType("text/csv");
95+
9296
CSVEncoder csvEncoder = new CSVEncoder(Arrays.asList("name", "species", "sex", "birth"));
9397

9498
csvEncoder.writeValues(resultSetAdapter, getResponse().getOutputStream());
95-
} else {
96-
String name = String.format("%s.%s", getRequest().getServletPath().substring(1), format);
99+
} else if (format.equals("html")) {
100+
getResponse().setContentType("text/html;charset=UTF-8");
97101

98-
getResponse().setContentType(String.format("%s;charset=UTF-8", getServletContext().getMimeType(name)));
102+
TemplateEncoder templateEncoder = new TemplateEncoder(getClass().getResource("pets.html"));
99103

100-
TemplateEncoder templateEncoder = new TemplateEncoder(getClass().getResource(name));
101-
102-
templateEncoder.setBaseName(getClass().getName());
104+
templateEncoder.setBaseName(getClass().getPackage().getName() + ".pets");
103105
templateEncoder.writeValue(resultSetAdapter, getResponse().getOutputStream(), getRequest().getLocale());
106+
} else {
107+
throw new UnsupportedOperationException();
104108
}
105109
}
106110
}

httprpc-test/src/main/resources/org/httprpc/test/mongodb/RestaurantService.properties renamed to httprpc-test/src/main/resources/org/httprpc/test/mongodb/restaurants.properties

File renamed without changes.

httprpc-test/src/main/resources/org/httprpc/test/mongodb/restaurants.xml

Lines changed: 0 additions & 18 deletions
This file was deleted.

httprpc-test/src/main/resources/org/httprpc/test/mysql/PetService.properties renamed to httprpc-test/src/main/resources/org/httprpc/test/mysql/pets.properties

File renamed without changes.

httprpc-test/src/main/resources/org/httprpc/test/mysql/pets.xml

Lines changed: 0 additions & 12 deletions
This file was deleted.

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,6 @@
155155
<a href="${pageContext.request.contextPath}/restaurants?zipCode=10462">Restaurants</a><br/>
156156
<a href="${pageContext.request.contextPath}/restaurants?zipCode=10462&format=csv">Restaurants (CSV)</a><br/>
157157
<a href="${pageContext.request.contextPath}/restaurants?zipCode=10462&format=html">Restaurants (HTML)</a><br/>
158-
<a href="${pageContext.request.contextPath}/restaurants?zipCode=10462&format=xml">Restaurants (XML)</a><br/>
159158

160159
<h2>MySQL</h2>
161160

@@ -165,7 +164,6 @@
165164
<a href="${pageContext.request.contextPath}/pets?owner=Gwen">Pets</a><br/>
166165
<a href="${pageContext.request.contextPath}/pets?owner=Gwen&format=csv">Pets (CSV)</a><br/>
167166
<a href="${pageContext.request.contextPath}/pets?owner=Gwen&format=html">Pets (HTML)</a><br/>
168-
<a href="${pageContext.request.contextPath}/pets?owner=Gwen&format=xml">Pets (XML)</a><br/>
169167
<br/>
170168
<a href="${pageContext.request.contextPath}/pets/average-age">Average Age</a><br/>
171169

0 commit comments

Comments
 (0)