You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

IndexAction.java 3.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.server.source.ws;
  21. import com.google.common.io.Resources;
  22. import java.util.Optional;
  23. import org.sonar.api.server.ws.Request;
  24. import org.sonar.api.server.ws.Response;
  25. import org.sonar.api.server.ws.WebService;
  26. import org.sonar.api.utils.text.JsonWriter;
  27. import org.sonar.api.web.UserRole;
  28. import org.sonar.db.DbClient;
  29. import org.sonar.db.DbSession;
  30. import org.sonar.db.component.ComponentDto;
  31. import org.sonar.server.component.ComponentFinder;
  32. import org.sonar.server.source.SourceService;
  33. import org.sonar.server.user.UserSession;
  34. public class IndexAction implements SourcesWsAction {
  35. private final DbClient dbClient;
  36. private final SourceService sourceService;
  37. private final UserSession userSession;
  38. private final ComponentFinder componentFinder;
  39. public IndexAction(DbClient dbClient, SourceService sourceService, UserSession userSession, ComponentFinder componentFinder) {
  40. this.dbClient = dbClient;
  41. this.sourceService = sourceService;
  42. this.userSession = userSession;
  43. this.componentFinder = componentFinder;
  44. }
  45. @Override
  46. public void define(WebService.NewController controller) {
  47. WebService.NewAction action = controller.createAction("index")
  48. .setDescription("Get source code as line number / text pairs. Require See Source Code permission on file")
  49. .setSince("5.0")
  50. .setResponseExample(Resources.getResource(getClass(), "example-index.json"))
  51. .setInternal(true)
  52. .setHandler(this);
  53. action
  54. .createParam("resource")
  55. .setRequired(true)
  56. .setDescription("File key")
  57. .setExampleValue("my_project:/src/foo/Bar.php");
  58. action
  59. .createParam("from")
  60. .setDefaultValue(1)
  61. .setDescription("First line");
  62. action
  63. .createParam("to")
  64. .setDescription("Last line (excluded). If not specified, all lines are returned until end of file");
  65. }
  66. @Override
  67. public void handle(Request request, Response response) {
  68. String fileKey = request.mandatoryParam("resource");
  69. int from = request.mandatoryParamAsInt("from");
  70. Integer to = request.paramAsInt("to");
  71. try (DbSession session = dbClient.openSession(false)) {
  72. ComponentDto component = componentFinder.getByKey(session, fileKey);
  73. userSession.checkComponentPermission(UserRole.CODEVIEWER, component);
  74. Optional<Iterable<String>> lines = sourceService.getLinesAsRawText(session, component.uuid(), from, to == null ? Integer.MAX_VALUE : (to - 1));
  75. try (JsonWriter json = response.newJsonWriter()) {
  76. json.beginArray().beginObject();
  77. if (lines.isPresent()) {
  78. int lineCounter = from;
  79. for (String line : lines.get()) {
  80. json.prop(String.valueOf(lineCounter), line);
  81. lineCounter++;
  82. }
  83. }
  84. json.endObject().endArray();
  85. }
  86. }
  87. }
  88. }