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.6KB

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