@Override
public void handle(Request request, Response response) {
String fileKey = request.mandatoryParam("key");
+ UserSession.get().checkComponentPermission(UserRole.CODEVIEWER, fileKey);
int from = Math.max(request.mandatoryParamAsInt("from"), 1);
int to = (Integer) ObjectUtils.defaultIfNull(request.paramAsInt("to"), Integer.MAX_VALUE);
DbSession session = dbClient.openSession(false);
try {
ComponentDto componentDto = dbClient.componentDao().getByKey(session, fileKey);
- UserSession.get().checkComponentPermission(UserRole.CODEVIEWER, fileKey);
-
- long linesSize = sourceService.countLines(componentDto.uuid());
- int size = to - from;
- boolean disableHighlighting = size > 3000 && linesSize > 3000 ;
-
- List<String> linesHtml;
- if (!disableHighlighting) {
- linesHtml = sourceService.getLinesAsHtml(componentDto.uuid(), from, to);
- } else {
- linesHtml = sourceService.getLinesAsTxt(componentDto.uuid(), from, to);
- }
+ List<String> linesHtml = sourceService.getLinesAsHtml(componentDto.uuid(), from, to);
JsonWriter json = response.newJsonWriter().beginObject();
writeSource(linesHtml, from, json);
} finally {
session.close();
}
+
}
private void writeSource(List<String> lines, int from, JsonWriter json) {
import static com.google.common.collect.Lists.newArrayList;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.eq;
-import static org.mockito.Mockito.*;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public class ShowActionTest {
String fileKey = "src/Foo.java";
MockUserSession.set().addComponentPermission(UserRole.CODEVIEWER, "polop", fileKey);
when(componentDao.getByKey(session, fileKey)).thenReturn(file);
- when(sourceService.countLines(file.uuid())).thenReturn(6L);
when(sourceService.getLinesAsHtml(eq(file.uuid()), anyInt(), anyInt())).thenReturn(newArrayList(
"/*",
" * Header",
String fileKey = "src/Foo.java";
MockUserSession.set().addComponentPermission(UserRole.CODEVIEWER, "polop", fileKey);
when(componentDao.getByKey(session, fileKey)).thenReturn(file);
- when(sourceService.countLines(file.uuid())).thenReturn(6L);
when(sourceService.getLinesAsHtml(file.uuid(), 3, 5)).thenReturn(newArrayList(
" */",
"",
String fileKey = "src/Foo.java";
MockUserSession.set().addComponentPermission(UserRole.CODEVIEWER, "polop", fileKey);
when(componentDao.getByKey(session, fileKey)).thenReturn(file);
- when(sourceService.countLines(file.uuid())).thenReturn(6L);
when(sourceService.getLinesAsHtml(file.uuid(), 1, 5)).thenReturn(newArrayList(
" */",
"",
verify(sourceService).getLinesAsHtml(file.uuid(), 1, 5);
}
- @Test
- public void disable_highlighting_when_lines_greater_than_3000_without_from_and_to_params() throws Exception {
- String fileKey = "src/Foo.java";
- MockUserSession.set().addComponentPermission(UserRole.CODEVIEWER, "polop", fileKey);
- when(componentDao.getByKey(session, fileKey)).thenReturn(file);
- when(sourceService.countLines(file.uuid())).thenReturn(5000L);
- WsTester.TestRequest request = tester
- .newGetRequest("api/sources", "show")
- .setParam("key", fileKey);
- request.execute();
-
- verify(sourceService).getLinesAsTxt(eq(file.uuid()), anyInt(), anyInt());
- verify(sourceService, never()).getLinesAsHtml(eq(file.uuid()), anyInt(), anyInt());
- }
-
- @Test
- public void disable_highlighting_when_lines_greater_than_3000_with_from_and_to_params() throws Exception {
- String fileKey = "src/Foo.java";
- MockUserSession.set().addComponentPermission(UserRole.CODEVIEWER, "polop", fileKey);
- when(componentDao.getByKey(session, fileKey)).thenReturn(file);
- when(sourceService.countLines(file.uuid())).thenReturn(5000L);
- WsTester.TestRequest request = tester
- .newGetRequest("api/sources", "show")
- .setParam("key", fileKey)
- .setParam("from", "1000")
- .setParam("to", "5000");
- request.execute();
-
- verify(sourceService).getLinesAsTxt(eq(file.uuid()), anyInt(), anyInt());
- verify(sourceService, never()).getLinesAsHtml(eq(file.uuid()), anyInt(), anyInt());
- }
-
- @Test
- public void not_disable_highlighting_when_lines_smaller_than_3000_but_to_minus_to_greater_than_3000() throws Exception {
- String fileKey = "src/Foo.java";
- MockUserSession.set().addComponentPermission(UserRole.CODEVIEWER, "polop", fileKey);
- when(componentDao.getByKey(session, fileKey)).thenReturn(file);
- when(sourceService.countLines(file.uuid())).thenReturn(1000L);
- WsTester.TestRequest request = tester
- .newGetRequest("api/sources", "show")
- .setParam("key", fileKey)
- .setParam("from", "1000")
- .setParam("to", "5000");
- request.execute();
-
- verify(sourceService, never()).getLinesAsTxt(eq(file.uuid()), anyInt(), anyInt());
- verify(sourceService).getLinesAsHtml(eq(file.uuid()), anyInt(), anyInt());
- }
-
- @Test
- public void not_disable_highlighting_when_lines_greater_than_3000_but_to_minus_to_smaller_than_3000() throws Exception {
- String fileKey = "src/Foo.java";
- MockUserSession.set().addComponentPermission(UserRole.CODEVIEWER, "polop", fileKey);
- when(componentDao.getByKey(session, fileKey)).thenReturn(file);
- when(sourceService.countLines(file.uuid())).thenReturn(5000L);
- WsTester.TestRequest request = tester
- .newGetRequest("api/sources", "show")
- .setParam("key", fileKey)
- .setParam("from", "1")
- .setParam("to", "10");
- request.execute();
-
- verify(sourceService, never()).getLinesAsTxt(eq(file.uuid()), anyInt(), anyInt());
- verify(sourceService).getLinesAsHtml(eq(file.uuid()), anyInt(), anyInt());
- }
-
@Test(expected = ForbiddenException.class)
public void require_code_viewer() throws Exception {
String fileKey = "src/Foo.java";