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.

RawAction.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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.io.IOException;
  23. import java.io.OutputStream;
  24. import java.nio.charset.StandardCharsets;
  25. import java.util.Optional;
  26. import org.sonar.api.server.ws.Request;
  27. import org.sonar.api.server.ws.Response;
  28. import org.sonar.api.server.ws.WebService;
  29. import org.sonar.api.web.UserRole;
  30. import org.sonar.db.DbClient;
  31. import org.sonar.db.DbSession;
  32. import org.sonar.db.component.ComponentDto;
  33. import org.sonar.server.component.ComponentFinder;
  34. import org.sonar.server.source.SourceService;
  35. import org.sonar.server.user.UserSession;
  36. import static org.sonar.server.ws.KeyExamples.KEY_BRANCH_EXAMPLE_001;
  37. import static org.sonar.server.ws.KeyExamples.KEY_PULL_REQUEST_EXAMPLE_001;
  38. public class RawAction implements SourcesWsAction {
  39. private static final String PARAM_KEY = "key";
  40. private static final String PARAM_BRANCH = "branch";
  41. private static final String PARAM_PULL_REQUEST = "pullRequest";
  42. private final DbClient dbClient;
  43. private final SourceService sourceService;
  44. private final UserSession userSession;
  45. private final ComponentFinder componentFinder;
  46. public RawAction(DbClient dbClient, SourceService sourceService, UserSession userSession, ComponentFinder componentFinder) {
  47. this.dbClient = dbClient;
  48. this.sourceService = sourceService;
  49. this.userSession = userSession;
  50. this.componentFinder = componentFinder;
  51. }
  52. @Override
  53. public void define(WebService.NewController controller) {
  54. WebService.NewAction action = controller.createAction("raw")
  55. .setDescription("Get source code as raw text. Require 'See Source Code' permission on file")
  56. .setSince("5.0")
  57. .setResponseExample(Resources.getResource(getClass(), "example-raw.txt"))
  58. .setHandler(this);
  59. action
  60. .createParam(PARAM_KEY)
  61. .setRequired(true)
  62. .setDescription("File key")
  63. .setExampleValue("my_project:src/foo/Bar.php");
  64. action
  65. .createParam(PARAM_BRANCH)
  66. .setDescription("Branch key")
  67. .setInternal(true)
  68. .setExampleValue(KEY_BRANCH_EXAMPLE_001);
  69. action
  70. .createParam(PARAM_PULL_REQUEST)
  71. .setDescription("Pull request id")
  72. .setInternal(true)
  73. .setExampleValue(KEY_PULL_REQUEST_EXAMPLE_001);
  74. }
  75. @Override
  76. public void handle(Request request, Response response) {
  77. String fileKey = request.mandatoryParam(PARAM_KEY);
  78. String branch = request.param(PARAM_BRANCH);
  79. String pullRequest = request.param(PARAM_PULL_REQUEST);
  80. try (DbSession dbSession = dbClient.openSession(false)) {
  81. ComponentDto file = componentFinder.getByKeyAndOptionalBranchOrPullRequest(dbSession, fileKey, branch, pullRequest);
  82. userSession.checkComponentPermission(UserRole.CODEVIEWER, file);
  83. Optional<Iterable<String>> lines = sourceService.getLinesAsRawText(dbSession, file.uuid(), 1, Integer.MAX_VALUE);
  84. response.stream().setMediaType("text/plain");
  85. if (lines.isPresent()) {
  86. OutputStream output = response.stream().output();
  87. for (String line : lines.get()) {
  88. output.write(line.getBytes(StandardCharsets.UTF_8));
  89. output.write("\n".getBytes(StandardCharsets.UTF_8));
  90. }
  91. }
  92. } catch (IOException e) {
  93. throw new IllegalStateException("Fail to write raw source of file " + fileKey, e);
  94. }
  95. }
  96. }