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.

BaseRequest.java 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 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.sonarqube.ws.client;
  21. import java.util.ArrayList;
  22. import java.util.Collection;
  23. import java.util.HashMap;
  24. import java.util.LinkedHashMap;
  25. import java.util.List;
  26. import java.util.Map;
  27. import java.util.Objects;
  28. import java.util.Optional;
  29. import java.util.OptionalInt;
  30. import java.util.Set;
  31. import java.util.function.Function;
  32. import java.util.stream.Collectors;
  33. import javax.annotation.CheckForNull;
  34. import javax.annotation.Nullable;
  35. import org.sonarqube.ws.MediaTypes;
  36. import static java.util.Collections.emptyList;
  37. import static java.util.Collections.unmodifiableSet;
  38. import static java.util.Objects.requireNonNull;
  39. import static org.sonarqube.ws.WsUtils.checkArgument;
  40. import static org.sonarqube.ws.WsUtils.isNullOrEmpty;
  41. abstract class BaseRequest<SELF extends BaseRequest> implements WsRequest {
  42. private final String path;
  43. private String mediaType = MediaTypes.JSON;
  44. private final DefaultParameters parameters = new DefaultParameters();
  45. private final DefaultHeaders headers = new DefaultHeaders();
  46. private OptionalInt timeOutInMs = OptionalInt.empty();
  47. BaseRequest(String path) {
  48. this.path = path;
  49. }
  50. @Override
  51. public String getPath() {
  52. return path;
  53. }
  54. @Override
  55. public String getMediaType() {
  56. return mediaType;
  57. }
  58. @Override
  59. public OptionalInt getTimeOutInMs() {
  60. return timeOutInMs;
  61. }
  62. public SELF setTimeOutInMs(int timeOutInMs) {
  63. this.timeOutInMs = OptionalInt.of(timeOutInMs);
  64. return (SELF) this;
  65. }
  66. /**
  67. * Expected media type of response. Default is {@link MediaTypes#JSON}.
  68. */
  69. @SuppressWarnings("unchecked")
  70. public SELF setMediaType(String s) {
  71. requireNonNull(s, "media type of response cannot be null");
  72. this.mediaType = s;
  73. return (SELF) this;
  74. }
  75. public SELF setParam(String key, @Nullable String value) {
  76. return setSingleValueParam(key, value);
  77. }
  78. public SELF setParam(String key, @Nullable Integer value) {
  79. return setSingleValueParam(key, value);
  80. }
  81. public SELF setParam(String key, @Nullable Long value) {
  82. return setSingleValueParam(key, value);
  83. }
  84. public SELF setParam(String key, @Nullable Float value) {
  85. return setSingleValueParam(key, value);
  86. }
  87. public SELF setParam(String key, @Nullable Boolean value) {
  88. return setSingleValueParam(key, value);
  89. }
  90. @SuppressWarnings("unchecked")
  91. private SELF setSingleValueParam(String key, @Nullable Object value) {
  92. checkArgument(!isNullOrEmpty(key), "a WS parameter key cannot be null");
  93. if (value == null) {
  94. return (SELF) this;
  95. }
  96. parameters.setValue(key, value.toString());
  97. return (SELF) this;
  98. }
  99. @SuppressWarnings("unchecked")
  100. public SELF setParam(String key, @Nullable Collection<? extends Object> values) {
  101. checkArgument(!isNullOrEmpty(key), "a WS parameter key cannot be null");
  102. if (values == null || values.isEmpty()) {
  103. return (SELF) this;
  104. }
  105. parameters.setValues(key, values.stream()
  106. .filter(Objects::nonNull)
  107. .map(Object::toString)
  108. .collect(Collectors.toList()));
  109. return (SELF) this;
  110. }
  111. @Override
  112. public Map<String, String> getParams() {
  113. return parameters.keyValues.keySet().stream()
  114. .collect(Collectors.toMap(
  115. Function.identity(),
  116. key -> parameters.keyValues.get(key).get(0),
  117. (v1, v2) -> {
  118. throw new IllegalStateException(String.format("Duplicate key '%s' in request", v1));
  119. },
  120. LinkedHashMap::new));
  121. }
  122. @Override
  123. public Parameters getParameters() {
  124. return parameters;
  125. }
  126. @Override
  127. public Headers getHeaders() {
  128. return headers;
  129. }
  130. @SuppressWarnings("unchecked")
  131. public SELF setHeader(String name, @Nullable String value) {
  132. requireNonNull(name, "Header name can't be null");
  133. headers.setValue(name, value);
  134. return (SELF) this;
  135. }
  136. private static class DefaultParameters implements Parameters {
  137. // preserve insertion order
  138. private final Map<String, List<String>> keyValues = new LinkedHashMap<>();
  139. @Override
  140. @CheckForNull
  141. public String getValue(String key) {
  142. return keyValues.containsKey(key) ? keyValues.get(key).get(0) : null;
  143. }
  144. @Override
  145. public List<String> getValues(String key) {
  146. return keyValues.containsKey(key) ? keyValues.get(key) : emptyList();
  147. }
  148. @Override
  149. public Set<String> getKeys() {
  150. return keyValues.keySet();
  151. }
  152. private DefaultParameters setValue(String key, String value) {
  153. checkArgument(!isNullOrEmpty(key));
  154. checkArgument(value != null);
  155. keyValues.computeIfAbsent(key, k -> new ArrayList<>()).add(value);
  156. return this;
  157. }
  158. private DefaultParameters setValues(String key, Collection<String> values) {
  159. checkArgument(!isNullOrEmpty(key));
  160. checkArgument(values != null && !values.isEmpty());
  161. keyValues.computeIfAbsent(key, k -> new ArrayList<>()).addAll(values.stream().map(Object::toString).filter(Objects::nonNull).collect(Collectors.toList()));
  162. return this;
  163. }
  164. }
  165. private static class DefaultHeaders implements Headers {
  166. private final Map<String, String> keyValues = new HashMap<>();
  167. @Override
  168. public Optional<String> getValue(String name) {
  169. return Optional.ofNullable(keyValues.get(name));
  170. }
  171. private DefaultHeaders setValue(String name, @Nullable String value) {
  172. checkArgument(!isNullOrEmpty(name));
  173. if (value == null) {
  174. keyValues.remove(name);
  175. } else {
  176. keyValues.put(name, value);
  177. }
  178. return this;
  179. }
  180. @Override
  181. public Set<String> getNames() {
  182. return unmodifiableSet(keyValues.keySet());
  183. }
  184. }
  185. }