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

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