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.

LocalWsConnector.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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.ByteArrayInputStream;
  22. import java.io.InputStream;
  23. import java.io.InputStreamReader;
  24. import java.io.Reader;
  25. import java.util.HashMap;
  26. import java.util.List;
  27. import java.util.Map;
  28. import java.util.Optional;
  29. import java.util.stream.Collectors;
  30. import org.sonar.api.server.ws.LocalConnector;
  31. import static java.nio.charset.StandardCharsets.UTF_8;
  32. import static java.util.function.Function.identity;
  33. class LocalWsConnector implements WsConnector {
  34. private final LocalConnector localConnector;
  35. LocalWsConnector(LocalConnector localConnector) {
  36. this.localConnector = localConnector;
  37. }
  38. LocalConnector localConnector() {
  39. return localConnector;
  40. }
  41. @Override
  42. public String baseUrl() {
  43. return "/";
  44. }
  45. @Override
  46. public WsResponse call(WsRequest wsRequest) {
  47. DefaultLocalRequest localRequest = new DefaultLocalRequest(wsRequest);
  48. LocalConnector.LocalResponse localResponse = localConnector.call(localRequest);
  49. return new ByteArrayResponse(wsRequest.getPath(), localResponse);
  50. }
  51. private static class DefaultLocalRequest implements LocalConnector.LocalRequest {
  52. private final WsRequest wsRequest;
  53. public DefaultLocalRequest(WsRequest wsRequest) {
  54. this.wsRequest = wsRequest;
  55. }
  56. @Override
  57. public String getPath() {
  58. return wsRequest.getPath();
  59. }
  60. @Override
  61. public String getMediaType() {
  62. return wsRequest.getMediaType();
  63. }
  64. @Override
  65. public String getMethod() {
  66. return wsRequest.getMethod().name();
  67. }
  68. @Override
  69. public boolean hasParam(String key) {
  70. return !wsRequest.getParameters().getValues(key).isEmpty();
  71. }
  72. @Override
  73. public String getParam(String key) {
  74. return wsRequest.getParameters().getValue(key);
  75. }
  76. @Override
  77. public List<String> getMultiParam(String key) {
  78. return wsRequest.getParameters().getValues(key);
  79. }
  80. @Override
  81. public Optional<String> getHeader(String name) {
  82. return wsRequest.getHeaders().getValue(name);
  83. }
  84. @Override
  85. public Map<String, String[]> getParameterMap() {
  86. return wsRequest.getParameters()
  87. .getKeys()
  88. .stream()
  89. .collect(Collectors.toMap(
  90. identity(),
  91. k -> wsRequest.getParameters().getValues(k).toArray(new String[0])));
  92. }
  93. }
  94. private static class ByteArrayResponse extends BaseResponse {
  95. private final String path;
  96. private final byte[] bytes;
  97. private final String contentType;
  98. private final int code;
  99. ByteArrayResponse(String path, LocalConnector.LocalResponse localResponse) {
  100. this.path = path;
  101. this.bytes = localResponse.getBytes();
  102. this.contentType = localResponse.getMediaType();
  103. this.code = localResponse.getStatus();
  104. }
  105. @Override
  106. public String requestUrl() {
  107. return path;
  108. }
  109. @Override
  110. public int code() {
  111. return code;
  112. }
  113. @Override
  114. public String contentType() {
  115. return contentType;
  116. }
  117. @Override
  118. public InputStream contentStream() {
  119. return new ByteArrayInputStream(bytes);
  120. }
  121. @Override
  122. public Reader contentReader() {
  123. return new InputStreamReader(new ByteArrayInputStream(bytes), UTF_8);
  124. }
  125. @Override
  126. public String content() {
  127. return new String(bytes, UTF_8);
  128. }
  129. /**
  130. * Not implemented yet
  131. */
  132. @Override
  133. public Optional<String> header(String name) {
  134. return Optional.empty();
  135. }
  136. @Override
  137. public Map<String, List<String>> headers() {
  138. return new HashMap<>();
  139. }
  140. }
  141. }