選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

OrganizationsWsSupport.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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.organization.ws;
  21. import javax.annotation.CheckForNull;
  22. import org.sonar.api.server.ws.Request;
  23. import org.sonar.api.server.ws.WebService;
  24. import org.sonar.db.organization.OrganizationDto;
  25. import org.sonar.server.organization.OrganizationValidation;
  26. import org.sonarqube.ws.Organizations.Organization;
  27. import static org.sonar.core.util.Protobuf.setNullable;
  28. import static org.sonar.server.organization.OrganizationValidation.DESCRIPTION_MAX_LENGTH;
  29. import static org.sonar.server.organization.OrganizationValidation.NAME_MAX_LENGTH;
  30. import static org.sonar.server.organization.OrganizationValidation.NAME_MIN_LENGTH;
  31. import static org.sonar.server.organization.OrganizationValidation.URL_MAX_LENGTH;
  32. /**
  33. * Factorizes code and constants between Organization WS's actions.
  34. */
  35. public class OrganizationsWsSupport {
  36. static final String PARAM_ORGANIZATION = "organization";
  37. static final String PARAM_KEY = "key";
  38. static final String PARAM_NAME = "name";
  39. static final String PARAM_DESCRIPTION = "description";
  40. static final String PARAM_URL = "url";
  41. static final String PARAM_AVATAR_URL = "avatar";
  42. static final String PARAM_LOGIN = "login";
  43. private final OrganizationValidation organizationValidation;
  44. public OrganizationsWsSupport(OrganizationValidation organizationValidation) {
  45. this.organizationValidation = organizationValidation;
  46. }
  47. String getAndCheckMandatoryName(Request request) {
  48. String name = request.mandatoryParam(PARAM_NAME);
  49. organizationValidation.checkName(name);
  50. return name;
  51. }
  52. @CheckForNull
  53. String getAndCheckName(Request request) {
  54. String name = request.param(PARAM_NAME);
  55. if (name != null) {
  56. organizationValidation.checkName(name);
  57. }
  58. return name;
  59. }
  60. @CheckForNull
  61. String getAndCheckAvatar(Request request) {
  62. return organizationValidation.checkAvatar(request.param(PARAM_AVATAR_URL));
  63. }
  64. @CheckForNull
  65. String getAndCheckUrl(Request request) {
  66. return organizationValidation.checkUrl(request.param(PARAM_URL));
  67. }
  68. @CheckForNull
  69. String getAndCheckDescription(Request request) {
  70. return organizationValidation.checkDescription(request.param(PARAM_DESCRIPTION));
  71. }
  72. void addOrganizationDetailsParams(WebService.NewAction action, boolean isNameRequired) {
  73. action.createParam(PARAM_NAME)
  74. .setRequired(isNameRequired)
  75. .setMinimumLength(NAME_MIN_LENGTH)
  76. .setMaximumLength(NAME_MAX_LENGTH)
  77. .setDescription("Name of the organization")
  78. .setExampleValue("Foo Company");
  79. action.createParam(PARAM_DESCRIPTION)
  80. .setRequired(false)
  81. .setMaximumLength(DESCRIPTION_MAX_LENGTH)
  82. .setDescription("Description of the organization.<br/> It must be less than 256 chars long.")
  83. .setExampleValue("The Foo company produces quality software for Bar.");
  84. action.createParam(PARAM_URL)
  85. .setRequired(false)
  86. .setMaximumLength(URL_MAX_LENGTH)
  87. .setDescription("URL of the organization.<br/> It must be less than 256 chars long.")
  88. .setExampleValue("https://www.foo.com");
  89. action.createParam(PARAM_AVATAR_URL)
  90. .setRequired(false)
  91. .setMaximumLength(URL_MAX_LENGTH)
  92. .setDescription("URL of the organization avatar.<br/> It must be less than 256 chars long.")
  93. .setExampleValue("https://www.foo.com/foo.png");
  94. }
  95. Organization.Builder toOrganization(OrganizationDto dto) {
  96. Organization.Builder builder = Organization.newBuilder();
  97. builder
  98. .setName(dto.getName())
  99. .setKey(dto.getKey())
  100. .setGuarded(dto.isGuarded());
  101. setNullable(dto.getDescription(), builder::setDescription);
  102. setNullable(dto.getUrl(), builder::setUrl);
  103. setNullable(dto.getAvatarUrl(), builder::setAvatar);
  104. return builder;
  105. }
  106. }