Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

RequestWithPayload.java 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 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.io.File;
  22. import java.util.LinkedHashMap;
  23. import java.util.Map;
  24. import java.util.Optional;
  25. import java.util.function.Function;
  26. import okhttp3.Request;
  27. import okhttp3.RequestBody;
  28. /**
  29. * @since 5.3
  30. */
  31. public abstract class RequestWithPayload<T extends RequestWithPayload<T>> extends BaseRequest<RequestWithPayload<T>> {
  32. private String body;
  33. private String contentType = null;
  34. private final Map<String, Part> parts = new LinkedHashMap<>();
  35. protected RequestWithPayload(String path) {
  36. super(path);
  37. }
  38. public T setBody(String body) {
  39. this.body = body;
  40. return (T) this;
  41. }
  42. public String getBody() {
  43. return body;
  44. }
  45. public boolean hasBody() {
  46. return this.body != null;
  47. }
  48. public T setContentType(String contentType) {
  49. this.contentType = contentType;
  50. return (T) this;
  51. }
  52. public Optional<String> getContentType() {
  53. return Optional.ofNullable(contentType);
  54. }
  55. public T setPart(String name, Part part) {
  56. this.parts.put(name, part);
  57. return (T) this;
  58. }
  59. abstract Function<Request.Builder, Request.Builder> addVerbToBuilder(RequestBody body);
  60. public Map<String, Part> getParts() {
  61. return parts;
  62. }
  63. public static class Part {
  64. private final String mediaType;
  65. private final File file;
  66. public Part(String mediaType, File file) {
  67. this.mediaType = mediaType;
  68. this.file = file;
  69. }
  70. public String getMediaType() {
  71. return mediaType;
  72. }
  73. public File getFile() {
  74. return file;
  75. }
  76. }
  77. }