Skip to content

Commit

Permalink
fix: Use IOUtils toStream and toString (#10974) (#10978)
Browse files Browse the repository at this point in the history
* fix: Use IOUtils toStream and toString

Use IOUtils toString and toInputStream
to not change any characters in the file content.

fixes #10893

Co-authored-by: caalador <mikael.grankvist@vaadin.com>
  • Loading branch information
vaadin-bot and caalador committed May 12, 2021
1 parent 0387912 commit 028ba87
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,8 @@ private static String getNonNull(String... valueArray) {
}

/**
* Read a stream and copy the content in a String.
* Read a stream and copy the content into a String using system line
* separators for all 'carriage return' characters.
*
* @param inputStream
* the input stream
Expand Down Expand Up @@ -515,12 +516,13 @@ private static String getHostString(VaadinRequest request) {
return host;
}

private static InputStream getStatsFromClassPath(VaadinService service) {
private static InputStream getStatsFromClassPath(VaadinService service)
throws IOException {
Stats statistics = service.getContext().getAttribute(Stats.class);

if (statistics != null) {
return new ByteArrayInputStream(
statistics.statsJson.getBytes(StandardCharsets.UTF_8));
return IOUtils.toInputStream(statistics.statsJson,
StandardCharsets.UTF_8);
}

String stats = service.getDeploymentConfiguration()
Expand All @@ -545,10 +547,11 @@ private static InputStream getStatsFromClassPath(VaadinService service) {
"Cannot get the 'stats.json' from the classpath '{}'",
stats);
} else {
statistics = new Stats(streamToString(stream), null);
statistics = new Stats(
IOUtils.toString(stream, StandardCharsets.UTF_8), null);
service.getContext().setAttribute(statistics);
stream = new ByteArrayInputStream(
statistics.statsJson.getBytes(StandardCharsets.UTF_8));
stream = IOUtils.toInputStream(statistics.statsJson,
StandardCharsets.UTF_8);
}
return stream;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
import com.vaadin.flow.server.VaadinService;
import com.vaadin.tests.util.MockDeploymentConfiguration;

import elemental.json.Json;
import elemental.json.JsonException;
import static com.vaadin.flow.server.Constants.STATISTICS_JSON_DEFAULT;
import static com.vaadin.flow.server.Constants.VAADIN_SERVLET_RESOURCES;
import static com.vaadin.flow.server.InitParameters.SERVLET_PARAMETER_STATISTICS_JSON;
Expand Down Expand Up @@ -320,6 +322,39 @@ public void getStatsAssetsByChunkName_getStatsFromClassPath_populatesStatsCache(
service.getContext().getAttribute(CACHE_KEY));
}

@Test // #10893
public void newLineCharacterInJsonStats_readStreamIsJsonParsable()
throws IOException, ServiceException {
VaadinService service = setupStatsAssetMocks(
"specialCharacterValue.json");

assertNull("Stats cache should not be present",
service.getContext().getAttribute(CACHE_KEY));

// Load file from classpath and populate cache
String statsContent = FrontendUtils.getStatsContent(service);

try {
// Json parsing should not throw for loaded stats.
Json.parse(statsContent);
} catch (JsonException jsonException) {
Assert.fail("Json loaded from class path was not parsable json");
}

assertNotNull("Stats cache should be created",
service.getContext().getAttribute(CACHE_KEY));

// Load cached stats.
statsContent = FrontendUtils.getStatsContent(service);

try {
// Json parsing should not throw for cached stats.
Json.parse(statsContent);
} catch (JsonException jsonException) {
Assert.fail("Json loaded from cache was not parsable json");
}
}

@Test
public void clearCachedStatsContent_clearsCache()
throws IOException, ServiceException {
Expand Down
3 changes: 3 additions & 0 deletions flow-server/src/test/resources/specialCharacterValue.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"value": "function(e){return r(function(){return!!o[e]()||\"​…᠎\"!=\"​…᠎\"[e]()||o[e].name!==e;});};}",
}

0 comments on commit 028ba87

Please sign in to comment.