From fc8a153c0c2f57daef9abbb66f5854a787fe268d Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Lesaint?= Date: Tue, 13 Oct 2015 10:10:30 +0200 Subject: [PATCH] SONAR-6375 fix HTML content display in source of huge files --- .../sonar/server/source/SourceService.java | 27 ++++++++++++++----- .../server/source/SourceServiceTest.java | 10 +++++-- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/source/SourceService.java b/server/sonar-server/src/main/java/org/sonar/server/source/SourceService.java index 56587c5cedb..47b3c20b695 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/source/SourceService.java +++ b/server/sonar-server/src/main/java/org/sonar/server/source/SourceService.java @@ -20,7 +20,15 @@ package org.sonar.server.source; +import com.google.common.base.Function; import com.google.common.base.Splitter; +import com.google.common.collect.Iterables; +import java.util.Collections; +import java.util.List; +import javax.annotation.CheckForNull; +import javax.annotation.Nullable; +import org.apache.commons.lang.StringEscapeUtils; +import org.apache.commons.lang.StringUtils; import org.sonar.api.ServerComponent; import org.sonar.api.measures.CoreMetrics; import org.sonar.api.web.UserRole; @@ -33,12 +41,6 @@ import org.sonar.core.source.db.SnapshotSourceDao; import org.sonar.server.db.DbClient; import org.sonar.server.user.UserSession; -import javax.annotation.CheckForNull; -import javax.annotation.Nullable; - -import java.util.Collections; -import java.util.List; - import static com.google.common.collect.Lists.newArrayList; public class SourceService implements ServerComponent { @@ -126,9 +128,20 @@ public class SourceService implements ServerComponent { private static List getLinesAsTxt(@Nullable String source) { if (source != null) { - return newArrayList(Splitter.onPattern("\r?\n|\r").split(source)); + return newArrayList(Iterables.transform(Splitter.onPattern("\r?\n|\r").split(source), ToEscapedHTML.INSTANCE)); } return Collections.emptyList(); } + private enum ToEscapedHTML implements Function { + INSTANCE; + + @Override + public String apply(@Nullable String input) { + if (StringUtils.isEmpty(input)) { + return input; + } + return StringEscapeUtils.escapeHtml(input); + } + } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/source/SourceServiceTest.java b/server/sonar-server/src/test/java/org/sonar/server/source/SourceServiceTest.java index 5320c7d9f2e..e0507dda8a2 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/source/SourceServiceTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/source/SourceServiceTest.java @@ -167,14 +167,20 @@ public class SourceServiceTest { } @Test - public void return_txt_when_source_is_too_big() throws Exception { + public void return_txt_when_source_is_too_big_and_html_is_escaped() throws Exception { MockUserSession.set().addComponentPermission(UserRole.CODEVIEWER, PROJECT_KEY, COMPONENT_KEY); - when(snapshotSourceDao.selectSnapshotSourceByComponentKey(COMPONENT_KEY, session)).thenReturn(Strings.repeat("a\n", 70000)); + when(snapshotSourceDao.selectSnapshotSourceByComponentKey(COMPONENT_KEY, session)) + .thenReturn( + "some html \n" + + "if (10 < 15 && true == false) {\n" + + Strings.repeat("a\n", 70000)); List lines = service.getLinesAsHtml(COMPONENT_KEY); assertThat(lines).isNotEmpty(); assertThat(lines.size()).isGreaterThan(3000); + assertThat(lines.get(0)).isEqualTo("some html <input/>"); + assertThat(lines.get(1)).isEqualTo("if (10 < 15 && true == false) {"); verifyZeroInteractions(sourceDecorator); verifyZeroInteractions(deprecatedSourceDecorator); -- 2.39.5