Skip to content
This repository was archived by the owner on Dec 4, 2023. It is now read-only.

Commit ad8eb63

Browse files
authored
Merge pull request #137 from matthiasblaesing/utf8-stream
Read Activity Data directly from InputStream
2 parents fdcb731 + 7d5ee88 commit ad8eb63

File tree

2 files changed

+4
-34
lines changed
  • Generator/generator-botbuilder-java/generators/app/templates
  • samples/servlet-echo/src/main/java/com/microsoft/bot/sample/servlet

2 files changed

+4
-34
lines changed

Generator/generator-botbuilder-java/generators/app/templates/app.java

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -82,25 +82,9 @@ public void handle(HttpExchange httpExchange) throws IOException {
8282
}
8383
}
8484

85-
private String getRequestBody(HttpExchange httpExchange) throws IOException {
86-
StringBuilder buffer = new StringBuilder();
87-
InputStream stream = httpExchange.getRequestBody();
88-
int rByte;
89-
while ((rByte = stream.read()) != -1) {
90-
buffer.append((char)rByte);
91-
}
92-
stream.close();
93-
if (buffer.length() > 0) {
94-
return URLDecoder.decode(buffer.toString(), "UTF-8");
95-
}
96-
return "";
97-
}
98-
9985
private Activity getActivity(HttpExchange httpExchange) {
100-
try {
101-
String body = getRequestBody(httpExchange);
102-
LOGGER.log(Level.INFO, body);
103-
return objectMapper.readValue(body, Activity.class);
86+
try (InputStream is = httpExchange.getRequestBody()) {
87+
return objectMapper.readValue(is, Activity.class);
10488
} catch (Exception ex) {
10589
LOGGER.log(Level.WARNING, "Failed to get activity", ex);
10690
return null;

samples/servlet-echo/src/main/java/com/microsoft/bot/sample/servlet/ControllerBase.java

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -82,22 +82,8 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
8282

8383
// Creates an Activity object from the request
8484
private Activity getActivity(HttpServletRequest request) throws IOException {
85-
String body = getRequestBody(request);
86-
return objectMapper.readValue(body, Activity.class);
87-
}
88-
89-
private String getRequestBody(HttpServletRequest request) throws IOException {
90-
StringBuilder buffer = new StringBuilder();
91-
try (InputStream stream = request.getInputStream()) {
92-
int rByte;
93-
while ((rByte = stream.read()) != -1) {
94-
buffer.append((char) rByte);
95-
}
96-
stream.close();
97-
if (buffer.length() > 0) {
98-
return buffer.toString();
99-
}
100-
return "";
85+
try(InputStream is = request.getInputStream()) {
86+
return objectMapper.readValue(is, Activity.class);
10187
}
10288
}
10389
}

0 commit comments

Comments
 (0)