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.

Request.java 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  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.sonar.api.server.ws;
  21. import com.google.common.base.Splitter;
  22. import com.google.common.collect.ImmutableMap;
  23. import java.io.BufferedReader;
  24. import java.io.InputStream;
  25. import java.util.ArrayList;
  26. import java.util.Date;
  27. import java.util.List;
  28. import java.util.Map;
  29. import java.util.Optional;
  30. import java.util.function.BiFunction;
  31. import java.util.function.Consumer;
  32. import java.util.function.Supplier;
  33. import javax.annotation.CheckForNull;
  34. import javax.annotation.Nullable;
  35. import org.apache.commons.lang.StringUtils;
  36. import org.sonar.api.utils.DateUtils;
  37. import static com.google.common.base.Preconditions.checkArgument;
  38. import static java.lang.String.format;
  39. import static java.util.Objects.requireNonNull;
  40. import static org.sonar.api.utils.DateUtils.parseDateQuietly;
  41. import static org.sonar.api.utils.DateUtils.parseDateTimeQuietly;
  42. /**
  43. * @since 4.2
  44. */
  45. public abstract class Request {
  46. protected static final String MSG_PARAMETER_MISSING = "The '%s' parameter is missing";
  47. /**
  48. * Returns the name of the HTTP method with which this request was made. Possible
  49. * values are GET and POST. Others are not supported.
  50. */
  51. public abstract String method();
  52. public BufferedReader getReader() {
  53. throw new UnsupportedOperationException("getReader not supported");
  54. }
  55. /**
  56. * Returns the requested MIME type, or {@code "application/octet-stream"} if not specified.
  57. */
  58. public abstract String getMediaType();
  59. /**
  60. * Return true of the parameter is set in the request.
  61. * Does NOT take into account the deprecated key of a parameter.
  62. */
  63. public abstract boolean hasParam(String key);
  64. /**
  65. * Returns a non-null value. To be used when parameter is required or has a default value.
  66. *
  67. * @throws java.lang.IllegalArgumentException is value is null or blank
  68. */
  69. public String mandatoryParam(String key) {
  70. String value = param(key);
  71. checkArgument(value != null && !value.isEmpty(), format(MSG_PARAMETER_MISSING, key));
  72. return value;
  73. }
  74. /**
  75. * Returns a boolean value. To be used when parameter is required or has a default value.
  76. *
  77. * @throws java.lang.IllegalArgumentException is value is null or blank
  78. */
  79. public boolean mandatoryParamAsBoolean(String key) {
  80. String s = mandatoryParam(key);
  81. return parseBoolean(key, s);
  82. }
  83. /**
  84. * Returns an int value. To be used when parameter is required or has a default value.
  85. *
  86. * @throws java.lang.IllegalArgumentException is value is null or blank
  87. */
  88. public int mandatoryParamAsInt(String key) {
  89. String s = mandatoryParam(key);
  90. return parseInt(key, s);
  91. }
  92. /**
  93. * Returns a long value. To be used when parameter is required or has a default value.
  94. *
  95. * @throws java.lang.IllegalArgumentException is value is null or blank
  96. */
  97. public long mandatoryParamAsLong(String key) {
  98. String s = mandatoryParam(key);
  99. return parseLong(key, s);
  100. }
  101. public <E extends Enum<E>> E mandatoryParamAsEnum(String key, Class<E> enumClass) {
  102. return Enum.valueOf(enumClass, mandatoryParam(key));
  103. }
  104. public List<String> mandatoryParamAsStrings(String key) {
  105. List<String> values = paramAsStrings(key);
  106. checkArgument(values != null, format(MSG_PARAMETER_MISSING, key));
  107. return values;
  108. }
  109. public List<String> mandatoryMultiParam(String key) {
  110. List<String> values = multiParam(key);
  111. checkArgument(!values.isEmpty(), MSG_PARAMETER_MISSING, key);
  112. return values;
  113. }
  114. @CheckForNull
  115. public abstract List<String> paramAsStrings(String key);
  116. public abstract Map<String, String[]> getParams();
  117. @CheckForNull
  118. public abstract String param(String key);
  119. public abstract List<String> multiParam(String key);
  120. @CheckForNull
  121. public abstract InputStream paramAsInputStream(String key);
  122. @CheckForNull
  123. public abstract Part paramAsPart(String key);
  124. public Part mandatoryParamAsPart(String key) {
  125. Part part = paramAsPart(key);
  126. checkArgument(part != null, MSG_PARAMETER_MISSING, key);
  127. return part;
  128. }
  129. /**
  130. * @deprecated to be dropped in 4.4. Default values are declared in ws metadata
  131. */
  132. @CheckForNull
  133. @Deprecated
  134. public String param(String key, @CheckForNull String defaultValue) {
  135. return StringUtils.defaultString(param(key), defaultValue);
  136. }
  137. /**
  138. * @deprecated to be dropped in 4.4. Default values must be declared in {@link org.sonar.api.server.ws.WebService} then
  139. * this method can be replaced by {@link #mandatoryParamAsBoolean(String)}.
  140. */
  141. @Deprecated
  142. public boolean paramAsBoolean(String key, boolean defaultValue) {
  143. String value = param(key);
  144. return value == null ? defaultValue : parseBoolean(key, value);
  145. }
  146. /**
  147. * @deprecated to be dropped in 4.4. Default values must be declared in {@link org.sonar.api.server.ws.WebService} then
  148. * this method can be replaced by {@link #mandatoryParamAsInt(String)}.
  149. */
  150. @Deprecated
  151. public int paramAsInt(String key, int defaultValue) {
  152. String s = param(key);
  153. return s == null ? defaultValue : parseInt(key, s);
  154. }
  155. /**
  156. * @deprecated to be dropped in 4.4. Default values must be declared in {@link org.sonar.api.server.ws.WebService} then
  157. * this method can be replaced by {@link #mandatoryParamAsLong(String)}.
  158. */
  159. @Deprecated
  160. public long paramAsLong(String key, long defaultValue) {
  161. String s = param(key);
  162. return s == null ? defaultValue : parseLong(key, s);
  163. }
  164. @CheckForNull
  165. public Boolean paramAsBoolean(String key) {
  166. String value = param(key);
  167. return value == null ? null : parseBoolean(key, value);
  168. }
  169. @CheckForNull
  170. public Integer paramAsInt(String key) {
  171. String s = param(key);
  172. return s == null ? null : parseInt(key, s);
  173. }
  174. @CheckForNull
  175. public Long paramAsLong(String key) {
  176. String s = param(key);
  177. return s == null ? null : parseLong(key, s);
  178. }
  179. @CheckForNull
  180. public <E extends Enum<E>> E paramAsEnum(String key, Class<E> enumClass) {
  181. String s = param(key);
  182. return s == null ? null : Enum.valueOf(enumClass, s);
  183. }
  184. @CheckForNull
  185. public <E extends Enum<E>> List<E> paramAsEnums(String key, Class<E> enumClass) {
  186. String value = param(key);
  187. if (value == null) {
  188. return null;
  189. }
  190. Iterable<String> values = Splitter.on(',').omitEmptyStrings().trimResults().split(value);
  191. List<E> result = new ArrayList<>();
  192. for (String s : values) {
  193. result.add(Enum.valueOf(enumClass, s));
  194. }
  195. return result;
  196. }
  197. @CheckForNull
  198. public Date paramAsDateTime(String key) {
  199. String stringDate = param(key);
  200. if (stringDate == null) {
  201. return null;
  202. }
  203. Date date = parseDateTimeQuietly(stringDate);
  204. if (date != null) {
  205. return date;
  206. }
  207. date = parseDateQuietly(stringDate);
  208. checkArgument(date != null, "'%s' cannot be parsed as either a date or date+time", stringDate);
  209. return date;
  210. }
  211. @CheckForNull
  212. public Date paramAsDate(String key) {
  213. String s = param(key);
  214. if (s == null) {
  215. return null;
  216. }
  217. try {
  218. return DateUtils.parseDate(s);
  219. } catch (RuntimeException notDateException) {
  220. throw new IllegalArgumentException(notDateException);
  221. }
  222. }
  223. private static boolean parseBoolean(String key, String value) {
  224. if ("true".equals(value) || "yes".equals(value)) {
  225. return true;
  226. }
  227. if ("false".equals(value) || "no".equals(value)) {
  228. return false;
  229. }
  230. throw new IllegalArgumentException(format("Property %s is not a boolean value: %s", key, value));
  231. }
  232. private static int parseInt(String key, String value) {
  233. try {
  234. return Integer.parseInt(value);
  235. } catch (NumberFormatException expection) {
  236. throw new IllegalArgumentException(format("The '%s' parameter cannot be parsed as an integer value: %s", key, value));
  237. }
  238. }
  239. private static long parseLong(String key, String value) {
  240. try {
  241. return Long.parseLong(value);
  242. } catch (NumberFormatException exception) {
  243. throw new IllegalArgumentException(format("The '%s' parameter cannot be parsed as a long value: %s", key, value));
  244. }
  245. }
  246. public <T> Param<T> getParam(String key, BiFunction<Request, String, T> retrieveAndValidate) {
  247. String param = this.param(key);
  248. if (param != null) {
  249. return GenericParam.present(retrieveAndValidate.apply(this, key));
  250. }
  251. return AbsentParam.absent();
  252. }
  253. public StringParam getParam(String key, Consumer<String> validate) {
  254. String value = this.param(key);
  255. if (value != null) {
  256. validate.accept(value);
  257. return StringParamImpl.present(value);
  258. }
  259. return AbsentStringParam.absent();
  260. }
  261. public StringParam getParam(String key) {
  262. String value = this.param(key);
  263. if (value != null) {
  264. return StringParamImpl.present(value);
  265. }
  266. return AbsentStringParam.absent();
  267. }
  268. /**
  269. * Optional value of the HTTP header with specified name.
  270. * If present, the result can have an empty string value ({@code ""}).
  271. *
  272. * @since 6.6
  273. */
  274. public abstract Optional<String> header(String name);
  275. public Map<String, String> getHeaders() {
  276. return ImmutableMap.of();
  277. }
  278. /**
  279. * Allows a web service to call another web service.
  280. * @see LocalConnector
  281. * @since 5.5
  282. */
  283. public abstract LocalConnector localConnector();
  284. /**
  285. * Return path of the request
  286. * @since 6.0
  287. */
  288. public abstract String getPath();
  289. /**
  290. * @since 6.0
  291. */
  292. public interface Part {
  293. InputStream getInputStream();
  294. String getFileName();
  295. }
  296. /**
  297. * Represents a Request parameter, provides information whether is was specified or not (check {@link #isPresent()})
  298. * and utility method to nicely handles cases where the parameter is not present.
  299. */
  300. public interface Param<T> {
  301. boolean isPresent();
  302. /**
  303. * @return the value of the parameter
  304. *
  305. * @throws IllegalStateException if param is not present.
  306. */
  307. @CheckForNull
  308. T getValue();
  309. @CheckForNull
  310. T or(Supplier<T> defaultValueSupplier);
  311. }
  312. /**
  313. * Implementation of {@link Param} where the param is not present.
  314. */
  315. private enum AbsentParam implements Param<Object> {
  316. INSTANCE;
  317. @SuppressWarnings("unchecked")
  318. protected static <T> Param<T> absent() {
  319. return (Param<T>) INSTANCE;
  320. }
  321. /**
  322. * Always returns true.
  323. */
  324. @Override
  325. public boolean isPresent() {
  326. return false;
  327. }
  328. /**
  329. * Always throws a {@link IllegalStateException}.
  330. */
  331. @Override
  332. public Object getValue() {
  333. throw createGetValueISE();
  334. }
  335. /**
  336. * Always returns the value supplied by {@code defaultValueSupplier}.
  337. */
  338. @Override
  339. @CheckForNull
  340. public Object or(Supplier<Object> defaultValueSupplier) {
  341. return checkDefaultValueSupplier(defaultValueSupplier).get();
  342. }
  343. }
  344. /**
  345. * Implementation of {@link Param} where the param is present.
  346. */
  347. private static final class GenericParam<T> implements Param<T> {
  348. private final T value;
  349. private GenericParam(T value) {
  350. this.value = value;
  351. }
  352. static <T> Param<T> present(T value) {
  353. return new GenericParam<>(value);
  354. }
  355. /**
  356. * Always returns true.
  357. */
  358. @Override
  359. public boolean isPresent() {
  360. return true;
  361. }
  362. /**
  363. * @return the value of the parameter
  364. *
  365. * @throws IllegalStateException if param is not present.
  366. */
  367. @Override
  368. @CheckForNull
  369. public T getValue() {
  370. return value;
  371. }
  372. /**
  373. * Always returns value of the parameter.
  374. *
  375. * @throws NullPointerException As per the inherited contract, {@code defaultValueSupplier} can't be null
  376. */
  377. @Override
  378. @CheckForNull
  379. public T or(Supplier<T> defaultValueSupplier) {
  380. checkDefaultValueSupplier(defaultValueSupplier);
  381. return value;
  382. }
  383. }
  384. /**
  385. * Extends {@link Param} with convenience methods specific to the type {@link String}.
  386. */
  387. public interface StringParam extends Param<String> {
  388. /**
  389. * Returns a {@link StringParam} object which methods {@link #getValue()} and {@link #or(Supplier)} will
  390. * return {@code null} rather than an empty String when the param is present and its value is an empty String.
  391. */
  392. StringParam emptyAsNull();
  393. }
  394. /**
  395. * Implementation of {@link StringParam} where the param is not present.
  396. */
  397. private enum AbsentStringParam implements StringParam {
  398. INSTANCE;
  399. protected static StringParam absent() {
  400. return INSTANCE;
  401. }
  402. /**
  403. * Always returns false.
  404. */
  405. @Override
  406. public boolean isPresent() {
  407. return false;
  408. }
  409. /**
  410. * Always throws a {@link IllegalStateException}.
  411. */
  412. @Override
  413. public String getValue() {
  414. throw createGetValueISE();
  415. }
  416. /**
  417. * Always returns the value supplied by {@code defaultValueSupplier}.
  418. */
  419. @Override
  420. public String or(Supplier<String> defaultValueSupplier) {
  421. return checkDefaultValueSupplier(defaultValueSupplier).get();
  422. }
  423. /**
  424. * Returns itself.
  425. */
  426. @Override
  427. public StringParam emptyAsNull() {
  428. return this;
  429. }
  430. }
  431. /**
  432. * Implementation of {@link StringParam} where the param is present.
  433. */
  434. private static final class StringParamImpl implements StringParam {
  435. @CheckForNull
  436. private final String value;
  437. private final boolean emptyAsNull;
  438. private StringParamImpl(@Nullable String value, boolean emptyAsNull) {
  439. this.value = value;
  440. this.emptyAsNull = emptyAsNull;
  441. }
  442. static StringParam present(String value) {
  443. return new StringParamImpl(value, false);
  444. }
  445. @Override
  446. public boolean isPresent() {
  447. return true;
  448. }
  449. @Override
  450. public String getValue() {
  451. if (emptyAsNull && value != null && value.isEmpty()) {
  452. return null;
  453. }
  454. return value;
  455. }
  456. @Override
  457. @CheckForNull
  458. public String or(Supplier<String> defaultValueSupplier) {
  459. checkDefaultValueSupplier(defaultValueSupplier);
  460. if (emptyAsNull && value != null && value.isEmpty()) {
  461. return null;
  462. }
  463. return value;
  464. }
  465. @Override
  466. public StringParam emptyAsNull() {
  467. if (emptyAsNull || (value != null && !value.isEmpty())) {
  468. return this;
  469. }
  470. return new StringParamImpl(value, true);
  471. }
  472. }
  473. private static <T> Supplier<T> checkDefaultValueSupplier(Supplier<T> defaultValueSupplier) {
  474. return requireNonNull(defaultValueSupplier, "default value supplier can't be null");
  475. }
  476. private static IllegalStateException createGetValueISE() {
  477. return new IllegalStateException("Param has no value. Use isPresent() before calling getValue()");
  478. }
  479. }