Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

FormDataAction.java 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2018 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.edition.ws;
  21. import org.sonar.api.platform.Server;
  22. import org.sonar.api.server.ws.Request;
  23. import org.sonar.api.server.ws.Response;
  24. import org.sonar.api.server.ws.WebService;
  25. import org.sonar.db.DbClient;
  26. import org.sonar.db.DbSession;
  27. import org.sonar.server.user.UserSession;
  28. import org.sonarqube.ws.Editions.FormDataResponse;
  29. import static org.sonar.server.ws.WsUtils.writeProtobuf;
  30. public class FormDataAction implements EditionsWsAction {
  31. private final UserSession userSession;
  32. private final Server server;
  33. private final DbClient dbClient;
  34. public FormDataAction(UserSession userSession, Server server, DbClient dbClient) {
  35. this.userSession = userSession;
  36. this.server = server;
  37. this.dbClient = dbClient;
  38. }
  39. @Override
  40. public void define(WebService.NewController controller) {
  41. controller.createAction("form_data")
  42. .setSince("6.7")
  43. .setPost(false)
  44. .setDescription("Provide data to prefill license request forms: the server ID and the total number of lines of code.")
  45. .setResponseExample(getClass().getResource("example-edition-form_data.json"))
  46. .setInternal(true)
  47. .setHandler(this);
  48. }
  49. @Override
  50. public void handle(Request request, Response response) throws Exception {
  51. userSession
  52. .checkLoggedIn()
  53. .checkIsSystemAdministrator();
  54. FormDataResponse responsePayload = FormDataResponse.newBuilder()
  55. .setNcloc(computeNcloc())
  56. .setServerId(server.getId())
  57. .build();
  58. writeProtobuf(responsePayload, request, response);
  59. }
  60. private long computeNcloc() {
  61. try (DbSession dbSession = dbClient.openSession(false)) {
  62. return dbClient.liveMeasureDao().sumNclocOfBiggestLongLivingBranch(dbSession);
  63. }
  64. }
  65. }