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

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