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.

HashAction.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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.CharStreams;
  22. import com.google.common.io.Resources;
  23. import java.io.IOException;
  24. import java.io.OutputStreamWriter;
  25. import java.io.Reader;
  26. import java.nio.charset.StandardCharsets;
  27. import java.util.function.Consumer;
  28. import org.sonar.api.server.ws.Request;
  29. import org.sonar.api.server.ws.Response;
  30. import org.sonar.api.server.ws.WebService;
  31. import org.sonar.api.web.UserRole;
  32. import org.sonar.db.DbClient;
  33. import org.sonar.db.DbSession;
  34. import org.sonar.db.component.ComponentDto;
  35. import org.sonar.server.component.ComponentFinder;
  36. import org.sonar.server.user.UserSession;
  37. import static org.sonar.server.ws.KeyExamples.KEY_FILE_EXAMPLE_001;
  38. public class HashAction implements SourcesWsAction {
  39. private final DbClient dbClient;
  40. private final UserSession userSession;
  41. private final ComponentFinder componentFinder;
  42. public HashAction(DbClient dbClient, UserSession userSession, ComponentFinder componentFinder) {
  43. this.dbClient = dbClient;
  44. this.userSession = userSession;
  45. this.componentFinder = componentFinder;
  46. }
  47. @Override
  48. public void define(WebService.NewController controller) {
  49. WebService.NewAction action = controller.createAction("hash")
  50. .setDescription("Show line line hashes for a given file. Require See Source Code permission on file's project<br/>")
  51. .setSince("5.0")
  52. .setInternal(true)
  53. .setDeprecatedSince("7.7")
  54. .setResponseExample(Resources.getResource(getClass(), "example-hash.txt"))
  55. .setHandler(this);
  56. action
  57. .createParam("key")
  58. .setRequired(true)
  59. .setDescription("File key")
  60. .setExampleValue(KEY_FILE_EXAMPLE_001);
  61. }
  62. @Override
  63. public void handle(Request request, Response response) throws Exception {
  64. try (DbSession session = dbClient.openSession(false)) {
  65. String componentKey = request.mandatoryParam("key");
  66. ComponentDto component = componentFinder.getByKey(session, componentKey);
  67. userSession.checkComponentPermission(UserRole.USER, component);
  68. response.stream().setMediaType("text/plain");
  69. try (OutputStreamWriter writer = new OutputStreamWriter(response.stream().output(), StandardCharsets.UTF_8)) {
  70. HashConsumer hashFunction = new HashConsumer(writer, componentKey);
  71. dbClient.fileSourceDao().readLineHashesStream(session, component.uuid(), hashFunction);
  72. if (!hashFunction.hasData()) {
  73. response.noContent();
  74. }
  75. }
  76. }
  77. }
  78. private static class HashConsumer implements Consumer<Reader> {
  79. private final OutputStreamWriter writer;
  80. private final String componentKey;
  81. private boolean hasData = false;
  82. public HashConsumer(OutputStreamWriter writer, String componentKey) {
  83. this.writer = writer;
  84. this.componentKey = componentKey;
  85. }
  86. @Override
  87. public void accept(Reader input) {
  88. try {
  89. hasData = true;
  90. CharStreams.copy(input, writer);
  91. } catch (IOException e) {
  92. throw new IllegalStateException(String.format("Can't read line hashes of file '%s'", componentKey), e);
  93. }
  94. }
  95. public boolean hasData() {
  96. return hasData;
  97. }
  98. }
  99. }